Some things...

This commit is contained in:
Flanks255 2024-07-25 18:43:15 -05:00
parent 8cc262156e
commit ff222b6e69
4 changed files with 105 additions and 32 deletions

View file

@ -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<ItemStack, Boolean> VALIDATOR;
protected AAContainer(@Nullable MenuType<?> menuType, int containerId, int numSlots, Function<ItemStack, Boolean> 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;
}
}

View file

@ -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<ItemAbility> ACTIONS = List.of(ItemAbilities.SHOVEL_DIG, ItemAbilities.PICKAXE_DIG);
private final Multimap<Holder<Attribute>, AttributeModifier> attributes_unpowered = ArrayListMultimap.create();
private final Multimap<Holder<Attribute>, 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<ItemStack> stacks = new ArrayList<>();
for (int i = 0; i < slots.getSlots(); i++) {
stacks.add(slots.getStackInSlot(i));
}
stack.set(ActuallyComponents.CONTENTS, ItemContainerContents.fromItems(stacks));
}
@Nonnull

View file

@ -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<Component> tooltip, TooltipFlag flagIn) {
public void appendHoverText(ItemStack stack, @Nonnull TooltipContext pContext, List<Component> 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)) {

View file

@ -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;
}