added sorting by sneeking backpack
This commit is contained in:
parent
1b494e0668
commit
492b0f3afc
@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
|
||||
loader_version=0.11.6
|
||||
|
||||
# Mod Properties
|
||||
mod_version = 1.17.1.2
|
||||
mod_version = 1.17.1.3
|
||||
maven_group = de.jottyfan.minecraft
|
||||
archives_base_name = quickiefabric
|
||||
|
||||
|
@ -1,5 +1,12 @@
|
||||
package de.jottyfan.minecraft.quickiefabric.container;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import de.jottyfan.minecraft.quickiefabric.items.ItemBackpack;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.inventory.SimpleInventory;
|
||||
@ -17,6 +24,8 @@ import net.minecraft.util.Hand;
|
||||
*
|
||||
*/
|
||||
public class BackpackInventory extends SimpleInventory {
|
||||
private static final Logger LOGGER = LogManager.getLogger(BackpackInventory.class);
|
||||
|
||||
private static final String NBT_BACKPACK = "backpack";
|
||||
private static final String NBT_SLOT = "slot";
|
||||
private static final String NBT_ITEMS = "items";
|
||||
@ -87,7 +96,7 @@ public class BackpackInventory extends SimpleInventory {
|
||||
return tag;
|
||||
}
|
||||
|
||||
private NbtCompound prepareCompoundTag(Integer slot, ItemStack stack) {
|
||||
private static final NbtCompound prepareCompoundTag(Integer slot, ItemStack stack) {
|
||||
NbtCompound compoundTag = new NbtCompound();
|
||||
compoundTag.putInt(NBT_SLOT, slot);
|
||||
stack.writeNbt(compoundTag);
|
||||
@ -97,4 +106,55 @@ public class BackpackInventory extends SimpleInventory {
|
||||
public void setHand(Hand hand) {
|
||||
this.hand = hand;
|
||||
}
|
||||
|
||||
/**
|
||||
* get the items from the itemStack that contains the backpack
|
||||
*
|
||||
* @param itemStack the itemStack of the backpack
|
||||
* @return the list of found itemStacks in the backpack
|
||||
*/
|
||||
public static List<ItemStack> getItemsFromBackpack(ItemStack itemStack) {
|
||||
NbtCompound backpackNbt = init(itemStack).getOrCreateNbt().getCompound(NBT_BACKPACK);
|
||||
NbtList listTag = backpackNbt.getList(NBT_ITEMS, NbtElement.COMPOUND_TYPE);
|
||||
List<ItemStack> items = new ArrayList<>();
|
||||
for (int i = 0; i < listTag.size(); ++i) {
|
||||
NbtCompound compoundTag = listTag.getCompound(i);
|
||||
int slot = compoundTag.getInt(NBT_SLOT);
|
||||
if (slot >= 0 && slot < ItemBackpack.SLOTSIZE) {
|
||||
ItemStack stack = ItemStack.fromNbt(compoundTag);
|
||||
items.add(stack);
|
||||
}
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
/**
|
||||
* set the items in the itemStack that contains the backpack
|
||||
*
|
||||
* @param itemStack the backpack's itemStack
|
||||
* @param itemStacks the collection of itemStacks for the backpack
|
||||
*/
|
||||
public static void setItemsToBackpack(ItemStack itemStack, Collection<ItemStack> itemStacks) {
|
||||
NbtList listTag = new NbtList();
|
||||
Integer slot = 0;
|
||||
for (ItemStack stack : itemStacks) {
|
||||
if (!(stack == null) && !stack.isEmpty()) {
|
||||
Integer leftCount = stack.getCount();
|
||||
while (leftCount > 0) {
|
||||
LOGGER.info("add {}x{} to backpack", stack.getCount(), stack.getTranslationKey());
|
||||
if (stack.getCount() > stack.getMaxCount()) {
|
||||
leftCount = stack.getCount() - stack.getMaxCount();
|
||||
stack.setCount(leftCount);
|
||||
} else {
|
||||
leftCount = 0;
|
||||
}
|
||||
listTag.add(prepareCompoundTag(slot, stack));
|
||||
slot++;
|
||||
}
|
||||
}
|
||||
}
|
||||
NbtCompound tag = new NbtCompound();
|
||||
tag.put(NBT_ITEMS, listTag);
|
||||
itemStack.getOrCreateNbt().put(NBT_BACKPACK, tag);
|
||||
}
|
||||
}
|
||||
|
@ -83,7 +83,7 @@ public class BackpackScreenHandler extends ScreenHandler {
|
||||
// do not throw but pickup
|
||||
super.onSlotClick(slotId, quickCraftData, SlotActionType.PICKUP, playerEntity);
|
||||
}
|
||||
} else if (actionType.equals(SlotActionType.QUICK_CRAFT)) { // drag - but does not drag the right slot content
|
||||
} else if (actionType.equals(SlotActionType.QUICK_CRAFT)) { // drag - but does not drag the right slot content, stack is empty
|
||||
if (slotId < 0) {
|
||||
return; // do not execute anything if slotId is < 0 as sometimes -999
|
||||
}
|
||||
|
@ -1,5 +1,9 @@
|
||||
package de.jottyfan.minecraft.quickiefabric.items;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import de.jottyfan.minecraft.quickiefabric.container.BackpackInventory;
|
||||
import de.jottyfan.minecraft.quickiefabric.container.BackpackScreenHandler;
|
||||
import de.jottyfan.minecraft.quickiefabric.init.RegistryManager;
|
||||
@ -42,8 +46,11 @@ public class ItemBackpack extends Item {
|
||||
|
||||
@Override
|
||||
public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
|
||||
ItemStack itemStack = player.getStackInHand(hand);
|
||||
if (player.isSneaking()) {
|
||||
player.setStackInHand(hand, sortBackpackContent(player.getStackInHand(hand)));
|
||||
}
|
||||
if (!world.isClient) {
|
||||
final ItemStack itemStack = player.getStackInHand(hand);
|
||||
player.openHandledScreen(new ExtendedScreenHandlerFactory() {
|
||||
@Override
|
||||
public void writeScreenOpeningData(ServerPlayerEntity serverPlayerEntity, PacketByteBuf packetByteBuf) {
|
||||
@ -65,7 +72,31 @@ public class ItemBackpack extends Item {
|
||||
}
|
||||
});
|
||||
}
|
||||
return new TypedActionResult<ItemStack>(ActionResult.SUCCESS, itemStack);
|
||||
return new TypedActionResult<ItemStack>(ActionResult.SUCCESS, player.getStackInHand(hand));
|
||||
}
|
||||
|
||||
/**
|
||||
* sort the content of the backpack; for spreaded items, combine them until the itemstack size is reached
|
||||
*
|
||||
* @param itemStack the itemStack of the backpack
|
||||
* @return the itemStack of the sorted backpack
|
||||
*/
|
||||
private ItemStack sortBackpackContent(ItemStack itemStack) {
|
||||
if (!itemStack.isEmpty()) {
|
||||
List<ItemStack> stacks = BackpackInventory.getItemsFromBackpack(itemStack);
|
||||
Map<String, ItemStack> map = new HashMap<>();
|
||||
for (ItemStack stack : stacks) {
|
||||
String id = stack.getItem().getTranslationKey();
|
||||
ItemStack foundStack = map.get(id);
|
||||
if (foundStack == null) {
|
||||
map.put(id, stack);
|
||||
} else {
|
||||
foundStack.setCount(foundStack.getCount() + stack.getCount());
|
||||
}
|
||||
}
|
||||
BackpackInventory.setItemsToBackpack(itemStack, map.values());
|
||||
}
|
||||
return itemStack;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -109,10 +140,6 @@ public class ItemBackpack extends Item {
|
||||
player.sendMessage(new TranslatableText("msg.backpack.transfer.cancel"), false);
|
||||
}
|
||||
}
|
||||
// alternative:
|
||||
// TODO: create a new dialog for transferring items from backpack to chest and
|
||||
// back
|
||||
// with the help of some buttons (such as all or so)
|
||||
}
|
||||
}
|
||||
return super.useOnBlock(context);
|
||||
|
Loading…
x
Reference in New Issue
Block a user