ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/items/Sack.java

129 lines
5.3 KiB
Java
Raw Normal View History

/*
* This file ("ItemBag.java") is part of the Actually Additions mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://ellpeck.de/actaddlicense
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
*/
package de.ellpeck.actuallyadditions.mod.items;
2023-12-20 22:02:25 +01:00
import de.ellpeck.actuallyadditions.mod.inventory.SackContainer;
import de.ellpeck.actuallyadditions.mod.inventory.VoidSackContainer;
import de.ellpeck.actuallyadditions.mod.items.base.ItemBase;
2023-03-16 00:17:57 +01:00
import de.ellpeck.actuallyadditions.mod.sack.SackData;
import de.ellpeck.actuallyadditions.mod.sack.SackManager;
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA;
2024-03-04 20:21:48 +01:00
import net.minecraft.core.BlockPos;
2024-03-02 21:23:08 +01:00
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerPlayer;
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.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
2024-03-04 20:21:48 +01:00
import net.neoforged.neoforge.capabilities.Capabilities;
import javax.annotation.Nonnull;
2024-03-04 20:21:48 +01:00
import java.util.Optional;
2023-03-16 00:17:57 +01:00
import java.util.UUID;
2023-12-20 22:02:25 +01:00
public class Sack extends ItemBase {
2017-06-17 00:55:55 +02:00
public final boolean isVoid;
2023-12-20 22:02:25 +01:00
public Sack(boolean isVoid) {
super(ActuallyItems.defaultProps().stacksTo(1));
this.isVoid = isVoid;
}
@Nonnull
2016-08-10 13:32:55 +02:00
@Override
2024-03-02 21:23:08 +01:00
public InteractionResult useOn(UseOnContext context) {
ItemStack stack = context.getPlayer().getItemInHand(context.getHand());
2018-07-28 02:08:42 +02:00
if (!this.isVoid) {
2024-03-04 20:21:48 +01:00
Level level = context.getLevel();
BlockPos clickedPos = context.getClickedPos();
BlockEntity tile = level.getBlockEntity(clickedPos);
if (tile != null) {
if (!context.getLevel().isClientSide) {
2023-03-16 00:17:57 +01:00
ItemStackHandlerAA inv = new ItemStackHandlerAA(28);
2024-03-04 20:21:48 +01:00
boolean changed = Optional.ofNullable(level.getCapability(Capabilities.ItemHandler.BLOCK, clickedPos, context.getClickedFace()))
.map(cap -> {
boolean localChanged = false;
2021-11-25 21:27:45 +01:00
DrillItem.loadSlotsFromNBT(inv, stack);
for (int j = 0; j < inv.getSlots(); j++) {
ItemStack invStack = inv.getStackInSlot(j);
if (!invStack.isEmpty()) {
for (int i = 0; i < cap.getSlots(); i++) {
ItemStack remain = cap.insertItem(i, invStack, false);
if (!ItemStack.matches(remain, invStack)) {
inv.setStackInSlot(j, remain.copy());
localChanged = true;
if (remain.isEmpty()) {
break;
}
invStack = remain;
}
}
}
}
return localChanged;
}).orElse(false);
if (changed) {
2021-11-25 21:27:45 +01:00
DrillItem.writeSlotsToNBT(inv, stack);
}
}
2024-03-02 21:23:08 +01:00
return InteractionResult.SUCCESS;
}
}
2024-03-02 21:23:08 +01:00
return InteractionResult.PASS;
}
@Nonnull
@Override
public InteractionResultHolder<ItemStack> use(Level world, Player player, @Nonnull InteractionHand hand) {
2023-03-16 00:17:57 +01:00
ItemStack sackStack = player.getItemInHand(hand);
2024-03-02 21:23:08 +01:00
if (!world.isClientSide && hand == InteractionHand.MAIN_HAND && sackStack.getItem() instanceof Sack && player instanceof ServerPlayer) {
2023-03-16 00:17:57 +01:00
if (!isVoid) {
SackData data = getData(sackStack);
if (data == null)
2024-03-02 21:23:08 +01:00
return InteractionResultHolder.fail(sackStack);
2023-03-16 00:17:57 +01:00
UUID uuid = data.getUuid();
data.updateAccessRecords(player.getName().getString(), System.currentTimeMillis());
2024-03-04 20:21:48 +01:00
player.openMenu(new SimpleMenuProvider((id, inv, entity) ->
2023-12-20 22:02:25 +01:00
new SackContainer(id, inv, uuid, data.getSpecialHandler()), sackStack.getHoverName()), (buffer -> buffer.writeUUID(uuid)));
} else
player.openMenu(new SimpleMenuProvider((id, inv, entity) -> new VoidSackContainer(id, inv), sackStack.getHoverName()));
}
2024-03-02 21:23:08 +01:00
return InteractionResultHolder.pass(player.getItemInHand(hand));
}
2016-08-02 20:06:31 +02:00
2023-03-16 00:17:57 +01:00
public static SackData getData(ItemStack stack) {
2023-12-20 22:02:25 +01:00
if (!(stack.getItem() instanceof Sack))
2023-03-16 00:17:57 +01:00
return null;
UUID uuid;
2024-03-02 21:23:08 +01:00
CompoundTag tag = stack.getOrCreateTag();
2023-03-16 00:17:57 +01:00
if (!tag.contains("UUID")) {
uuid = UUID.randomUUID();
tag.putUUID("UUID", uuid);
} else
uuid = tag.getUUID("UUID");
return SackManager.get().getOrCreateSack(uuid);
2016-08-02 20:06:31 +02:00
}
}