diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/AAContainer.java b/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/AAContainer.java new file mode 100644 index 000000000..5fe672a9c --- /dev/null +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/AAContainer.java @@ -0,0 +1,68 @@ +package de.ellpeck.actuallyadditions.mod.inventory; + +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.inventory.AbstractContainerMenu; +import net.minecraft.world.inventory.MenuType; +import net.minecraft.world.inventory.Slot; +import net.minecraft.world.item.ItemStack; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.function.Function; + +public abstract class AAContainer extends AbstractContainerMenu { + public final int numSlots; + public final Function VALIDATOR; + + protected AAContainer(@Nullable MenuType menuType, int containerId, int numSlots, Function validator) { + super(menuType, containerId); + this.numSlots = numSlots; + VALIDATOR = validator; + } + + @Nonnull + @Override + public ItemStack quickMoveStack(@Nonnull Player player, int index) { + int inventoryEnd = numSlots + 26; + int hotbarStart = inventoryEnd + 1; + int hotbarEnd = hotbarStart + 8; + + Slot theSlot = this.slots.get(index); + + if (theSlot.hasItem()) { + ItemStack newStack = theSlot.getItem(); + ItemStack currentStack = newStack.copy(); + + if (index >= numSlots) { //Shift from inventory to container. + if (numSlots > 0 && VALIDATOR.apply(newStack)) { //Check if the item is valid. + if (!this.moveItemStackTo(newStack, 0, numSlots, false)) { // try to move to container. + return ItemStack.EMPTY; + } + } + else if (index <= inventoryEnd) { //Shift from inventory to hotbar. + if (!this.moveItemStackTo(newStack, hotbarStart, hotbarEnd + 1, false)) { // try to move to hotbar. + return ItemStack.EMPTY; + } + } else if (index >= inventoryEnd + 1 && index < hotbarEnd + 1 && !this.moveItemStackTo(newStack, numSlots, inventoryEnd + 1, false)) { //Shift from hotbar to inventory. + return ItemStack.EMPTY; + } + } else if (!this.moveItemStackTo(newStack, numSlots, hotbarEnd + 1, false)) { //Shift from container to inventory. + return ItemStack.EMPTY; + } + + if (newStack.isEmpty()) { + theSlot.set(ItemStack.EMPTY); + } else { + theSlot.setChanged(); + } + + if (newStack.getCount() == currentStack.getCount()) { + return ItemStack.EMPTY; + } + theSlot.onTake(player, newStack); + + return currentStack; + } + return ItemStack.EMPTY; + } +} diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/items/DrillItem.java b/src/main/java/de/ellpeck/actuallyadditions/mod/items/DrillItem.java index d75ea2773..de9e72fa8 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/items/DrillItem.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/items/DrillItem.java @@ -10,41 +10,35 @@ package de.ellpeck.actuallyadditions.mod.items; -import com.google.common.collect.ArrayListMultimap; -import com.google.common.collect.Multimap; import de.ellpeck.actuallyadditions.api.ActuallyTags; import de.ellpeck.actuallyadditions.mod.ActuallyAdditions; +import de.ellpeck.actuallyadditions.mod.components.ActuallyComponents; import de.ellpeck.actuallyadditions.mod.config.CommonConfig; import de.ellpeck.actuallyadditions.mod.inventory.ContainerDrill; import de.ellpeck.actuallyadditions.mod.items.base.ItemEnergy; -import de.ellpeck.actuallyadditions.mod.tile.TileEntityInventoryBase; import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA; import de.ellpeck.actuallyadditions.mod.util.ItemUtil; import de.ellpeck.actuallyadditions.mod.util.Util; import de.ellpeck.actuallyadditions.mod.util.WorldUtil; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; -import net.minecraft.core.Holder; import net.minecraft.core.component.DataComponents; import net.minecraft.core.registries.BuiltInRegistries; -import net.minecraft.nbt.CompoundTag; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; -import net.minecraft.tags.BlockTags; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.InteractionResultHolder; import net.minecraft.world.SimpleMenuProvider; -import net.minecraft.world.entity.EquipmentSlot; +import net.minecraft.world.entity.EquipmentSlotGroup; import net.minecraft.world.entity.LivingEntity; -import net.minecraft.world.entity.ai.attributes.Attribute; import net.minecraft.world.entity.ai.attributes.AttributeModifier; import net.minecraft.world.entity.ai.attributes.Attributes; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; -import net.minecraft.world.item.Tier; import net.minecraft.world.item.Tiers; import net.minecraft.world.item.component.ItemAttributeModifiers; +import net.minecraft.world.item.component.ItemContainerContents; import net.minecraft.world.item.context.UseOnContext; import net.minecraft.world.item.enchantment.Enchantments; import net.minecraft.world.level.Level; @@ -67,15 +61,25 @@ public class DrillItem extends ItemEnergy { private static final int ENERGY_USE = 100; private static final List ACTIONS = List.of(ItemAbilities.SHOVEL_DIG, ItemAbilities.PICKAXE_DIG); - private final Multimap, AttributeModifier> attributes_unpowered = ArrayListMultimap.create(); - private final Multimap, AttributeModifier> attributes_powered = ArrayListMultimap.create(); + private final ItemAttributeModifiers attributes_unpowered; + private final ItemAttributeModifiers attributes_powered; public DrillItem() { - super(ActuallyItems.defaultProps().durability(0).stacksTo(1).component(DataComponents.TOOL, Tiers.NETHERITE.createToolProperties(ActuallyTags.Blocks.MINEABLE_WITH_DRILL)), 250000, 1000); - attributes_powered.put(Attributes.ATTACK_DAMAGE, new AttributeModifier(ActuallyAdditions.modLoc("drill_speed_powered"), 8.0F, AttributeModifier.Operation.ADD_VALUE)); - attributes_powered.put(Attributes.ATTACK_SPEED, new AttributeModifier(ActuallyAdditions.modLoc("drill_speed_powered"), 1.5F, AttributeModifier.Operation.ADD_VALUE)); - attributes_unpowered.put(Attributes.ATTACK_DAMAGE, new AttributeModifier(ActuallyAdditions.modLoc("drill_attack"), 0.1F, AttributeModifier.Operation.ADD_VALUE)); - attributes_unpowered.put(Attributes.ATTACK_SPEED, new AttributeModifier(ActuallyAdditions.modLoc("drill_speed"), 1.5F, AttributeModifier.Operation.ADD_VALUE)); + super(ActuallyItems.defaultProps() + .durability(0) + .stacksTo(1) + .component(DataComponents.TOOL, Tiers.NETHERITE.createToolProperties(ActuallyTags.Blocks.MINEABLE_WITH_DRILL)) + , 250000, 1000); + + attributes_unpowered = ItemAttributeModifiers.builder() + .add(Attributes.ATTACK_DAMAGE, new AttributeModifier(ActuallyAdditions.modLoc("drill_attack"), 0.1F, AttributeModifier.Operation.ADD_VALUE), EquipmentSlotGroup.MAINHAND) + .add(Attributes.ATTACK_SPEED, new AttributeModifier(ActuallyAdditions.modLoc("drill_speed"), 1.5F, AttributeModifier.Operation.ADD_VALUE), EquipmentSlotGroup.MAINHAND) + .build(); + + attributes_powered = ItemAttributeModifiers.builder() + .add(Attributes.ATTACK_DAMAGE, new AttributeModifier(ActuallyAdditions.modLoc("drill_attack"), 8.0F, AttributeModifier.Operation.ADD_VALUE), EquipmentSlotGroup.MAINHAND) + .add(Attributes.ATTACK_SPEED, new AttributeModifier(ActuallyAdditions.modLoc("drill_speed"), 1.5F, AttributeModifier.Operation.ADD_VALUE), EquipmentSlotGroup.MAINHAND) + .build(); } @Override @@ -103,8 +107,13 @@ public class DrillItem extends ItemEnergy { * @param stack The Drill */ public static void loadSlotsFromNBT(IItemHandlerModifiable slots, ItemStack stack) { - CompoundTag compound = stack.getOrCreateTag(); - TileEntityInventoryBase.loadSlots(slots, compound); + if (stack.has(ActuallyComponents.CONTENTS)) { + ItemContainerContents containerContents = stack.getOrDefault(ActuallyComponents.CONTENTS, ItemContainerContents.EMPTY); + int slotCount = slots.getSlots(); + for (int i = 0; i < slotCount; i++) { + slots.setStackInSlot(i, i < containerContents.getSlots()? containerContents.getStackInSlot(i): ItemStack.EMPTY); + } + } } /** @@ -114,10 +123,11 @@ public class DrillItem extends ItemEnergy { * @param stack The Drill */ public static void writeSlotsToNBT(IItemHandler slots, ItemStack stack) { - CompoundTag compound = stack.getOrCreateTag(); - - TileEntityInventoryBase.saveSlots(slots, compound); - stack.setTag(compound); + List stacks = new ArrayList<>(); + for (int i = 0; i < slots.getSlots(); i++) { + stacks.add(slots.getStackInSlot(i)); + } + stack.set(ActuallyComponents.CONTENTS, ItemContainerContents.fromItems(stacks)); } @Nonnull diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/items/ItemFilter.java b/src/main/java/de/ellpeck/actuallyadditions/mod/items/ItemFilter.java index 96c0d5e81..d54443f1b 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/items/ItemFilter.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/items/ItemFilter.java @@ -23,7 +23,7 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.TooltipFlag; import net.minecraft.world.level.Level; -import javax.annotation.Nullable; +import javax.annotation.Nonnull; import java.util.List; public class ItemFilter extends ItemBase { @@ -43,11 +43,11 @@ public class ItemFilter extends ItemBase { @Override - public void appendHoverText(ItemStack stack, @Nullable TooltipContext pContext, List tooltip, TooltipFlag flagIn) { + public void appendHoverText(ItemStack stack, @Nonnull TooltipContext pContext, List tooltip, TooltipFlag flagIn) { super.appendHoverText(stack, pContext, tooltip, flagIn); ItemStackHandlerAA inv = new ItemStackHandlerAA(ContainerFilter.SLOT_AMOUNT); - DrillItem.loadSlotsFromNBT(inv, stack); + DrillItem.loadSlotsFromNBT(inv, stack, pContext.registries()); for (int i = 0; i < inv.getSlots(); i++) { ItemStack slot = inv.getStackInSlot(i); if (StackUtil.isValid(slot)) { diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/items/Sack.java b/src/main/java/de/ellpeck/actuallyadditions/mod/items/Sack.java index 06cd80f41..5fb3cad22 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/items/Sack.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/items/Sack.java @@ -16,8 +16,6 @@ import de.ellpeck.actuallyadditions.mod.inventory.VoidSackContainer; import de.ellpeck.actuallyadditions.mod.items.base.ItemBase; import de.ellpeck.actuallyadditions.mod.sack.SackData; import de.ellpeck.actuallyadditions.mod.sack.SackManager; -import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA; -import net.minecraft.core.BlockPos; import net.minecraft.core.component.DataComponents; import net.minecraft.nbt.CompoundTag; import net.minecraft.server.level.ServerPlayer; @@ -30,11 +28,8 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.component.CustomData; import net.minecraft.world.item.context.UseOnContext; import net.minecraft.world.level.Level; -import net.minecraft.world.level.block.entity.BlockEntity; -import net.neoforged.neoforge.capabilities.Capabilities; import javax.annotation.Nonnull; -import java.util.Optional; import java.util.UUID; public class Sack extends ItemBase { @@ -49,7 +44,7 @@ public class Sack extends ItemBase { @Override public InteractionResult useOn(UseOnContext context) { ItemStack stack = context.getPlayer().getItemInHand(context.getHand()); - if (!this.isVoid) { +/* if (!this.isVoid) { //TODO oh boy this is still old... needs to be converted to the new UUID system. Level level = context.getLevel(); BlockPos clickedPos = context.getClickedPos(); BlockEntity tile = level.getBlockEntity(clickedPos); @@ -88,7 +83,7 @@ public class Sack extends ItemBase { } return InteractionResult.SUCCESS; } - } + }*/ return InteractionResult.PASS; }