-
Notifications
You must be signed in to change notification settings - Fork 21
Feature InventoryCleaner module #229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
IceTank
wants to merge
9
commits into
lambda-client:1.21.11
Choose a base branch
from
IceTank:feature/module/easy-trash
base: 1.21.11
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+176
−1
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
900e4ab
Add easy trash prototype
IceTank 5dec1d4
Improved EasyTrash
IceTank c128fd1
Improved EasyTrash
IceTank 2bc9bb8
Lint
IceTank 70c9add
Add InventoryEvent$SlotAction$Click Event
IceTank 9c6358e
Add named module initialization parameters
IceTank 47111d8
Delegate item drop speed to InventoryManager
IceTank 90f69cb
refactor(EasyTrash): Renamed module to InventoryCleaner.kt and add mo…
IceTank 5bbf1e4
Fix pr code comments
IceTank File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
src/main/kotlin/com/lambda/module/modules/player/InventoryCleaner.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| /* | ||
| * Copyright 2025 Lambda | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package com.lambda.module.modules.player | ||
|
|
||
| import com.lambda.context.SafeContext | ||
| import com.lambda.event.events.InventoryEvent | ||
| import com.lambda.event.events.TickEvent | ||
| import com.lambda.event.listener.SafeListener.Companion.listen | ||
| import com.lambda.interaction.managers.inventory.InventoryRequest.Companion.inventoryRequest | ||
| import com.lambda.interaction.material.StackSelection | ||
| import com.lambda.interaction.material.container.containers.InventoryContainer | ||
| import com.lambda.module.Module | ||
| import com.lambda.module.tag.ModuleTag | ||
| import net.minecraft.entity.EntityType | ||
| import net.minecraft.entity.ItemEntity | ||
| import net.minecraft.item.Item | ||
| import net.minecraft.item.Items | ||
| import net.minecraft.registry.Registries.ITEM | ||
| import net.minecraft.screen.GenericContainerScreenHandler | ||
| import net.minecraft.screen.ScreenHandler | ||
| import net.minecraft.screen.slot.SlotActionType | ||
| import net.minecraft.world.GameMode | ||
|
|
||
| @Suppress("unused") | ||
| object InventoryCleaner : Module( | ||
| name = "InventoryCleaner", | ||
| description = """ | ||
| Automatically drops unwanted items when the player inventory is full. | ||
| """.trimIndent(), | ||
| tag = ModuleTag.PLAYER | ||
| ) { | ||
| private val itemsCanTrash by setting("Items Can Trash", setOf<Item>(Items.NETHERRACK, Items.COBBLESTONE), ITEM.toSet(), description = "A list of items that the module can trash when trying to make space in the inventory.") | ||
|
|
||
| private val dropOnContainerClick by setting("Drop On Container Click", true, description = "If enabled, shift + left clicking an item in a container will drop a trash item out of the inventory if the player inventory is full.") | ||
| private val dropToPickup by setting( | ||
| "Drop To Pickup", | ||
| true, | ||
| description = "If enabled, the module will drop a trash item out of the inventory when the player is in pickup range of an item to pickup.\nItems to pickup can be configured in the 'Items to pickup' setting.\nTrash items to drop can be configured in the 'Items can trash' setting." | ||
| ) | ||
| private val itemsToPickup by setting("Items To Pickup", setOf(), ITEM.toSet()) { dropToPickup } | ||
|
|
||
| init { | ||
| listen<TickEvent.Pre> { | ||
| checkShouldTrashSomething() | ||
| } | ||
|
|
||
| listen<InventoryEvent.SlotAction.Click> { event -> | ||
| if (event.actionType != SlotActionType.QUICK_MOVE || event.button != 0 || !dropOnContainerClick) { | ||
| return@listen | ||
| } | ||
|
|
||
| val sh = event.screenHandler | ||
| if (sh !is GenericContainerScreenHandler) { | ||
| return@listen | ||
| } | ||
| val rows = sh.rows | ||
|
|
||
| if (event.slotId >= rows * 9) { | ||
| // Clicked in inventory, not in container | ||
| return@listen | ||
| } | ||
|
|
||
| if (sh.slots.subList(rows * 9, sh.slots.size).any { !it.hasStack() }) { | ||
| // There is empty space in the inventory, no need to trash | ||
| return@listen | ||
| } | ||
|
|
||
| StackSelection.selectStack { | ||
| isOneOfItems(itemsCanTrash) | ||
| }.filterSlots(InventoryContainer.slots).firstOrNull()?.let { trashSlot -> | ||
| inventoryRequest { | ||
| pickup(event.slotId, 0) | ||
| pickup(trashSlot.id, 0) | ||
| pickup(ScreenHandler.EMPTY_SPACE_SLOT_INDEX, 0) | ||
| }.submit(true) | ||
| event.cancel() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fun SafeContext.checkShouldTrashSomething() { | ||
| if (!dropToPickup || !player.isAlive || player.gameMode != GameMode.SURVIVAL) { | ||
| return | ||
| } | ||
| val vehicle = player.vehicle | ||
| val box = if (vehicle != null && !vehicle.isRemoved) { | ||
| player.boundingBox.union(vehicle.boundingBox).expand(1.0, 0.0, 1.0) | ||
| } else { | ||
| player.boundingBox.expand(1.0, 0.5, 1.0) | ||
| } | ||
| val items = world.getEntitiesByType<ItemEntity>(EntityType.ITEM, box) { entity -> | ||
| itemsToPickup.contains(entity.stack.item) && entity.isOnGround | ||
| }.map { i -> i.stack.item } | ||
| if (items.isNotEmpty() && InventoryContainer.spaceAvailable(StackSelection.selectStack { isOneOfItems(items) }) <= 0) { | ||
| dropOneTrashStack() | ||
| } | ||
| } | ||
|
|
||
| fun SafeContext.dropOneTrashStack(): Boolean { | ||
| StackSelection.selectStack { | ||
| isOneOfItems(itemsCanTrash) | ||
| }.filterSlots(InventoryContainer.slots).firstOrNull()?.let { | ||
| inventoryRequest { | ||
| throwStack(it.id) | ||
| }.submit(true) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. prob best to just return if the inventory request here is done |
||
| return true | ||
| } | ||
| return false | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setting not inlined