2016-08-02 16:32:13 +02:00
|
|
|
/*
|
|
|
|
* 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
|
2016-08-02 16:32:13 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
package de.ellpeck.actuallyadditions.mod.items;
|
|
|
|
|
2023-12-20 22:02:25 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.inventory.SackContainer;
|
2016-08-02 16:32:13 +02:00
|
|
|
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;
|
2018-06-23 01:39:30 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA;
|
2017-06-17 00:55:55 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
|
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;
|
2016-08-02 16:32:13 +02:00
|
|
|
|
2024-03-04 20:21:48 +01:00
|
|
|
import java.util.Optional;
|
2023-03-16 00:17:57 +01:00
|
|
|
import java.util.UUID;
|
2016-08-02 16:32:13 +02:00
|
|
|
|
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;
|
2016-08-02 16:32:13 +02:00
|
|
|
|
2023-12-20 22:02:25 +01:00
|
|
|
public Sack(boolean isVoid) {
|
2021-08-22 17:09:06 +02:00
|
|
|
super(ActuallyItems.defaultProps().stacksTo(1));
|
2016-08-02 16:32:13 +02:00
|
|
|
this.isVoid = isVoid;
|
|
|
|
}
|
|
|
|
|
2016-08-10 13:32:55 +02:00
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public InteractionResult useOn(UseOnContext context) {
|
2021-08-22 17:09:06 +02:00
|
|
|
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);
|
2021-02-27 22:44:00 +01:00
|
|
|
if (tile != null) {
|
2021-08-22 17:09:06 +02:00
|
|
|
if (!context.getLevel().isClientSide) {
|
2023-03-16 00:17:57 +01:00
|
|
|
ItemStackHandlerAA inv = new ItemStackHandlerAA(28);
|
2016-08-07 01:11:01 +02:00
|
|
|
|
2024-03-04 20:21:48 +01:00
|
|
|
boolean changed = Optional.ofNullable(level.getCapability(Capabilities.ItemHandler.BLOCK, clickedPos, context.getClickedFace()))
|
2021-02-27 22:44:00 +01:00
|
|
|
.map(cap -> {
|
|
|
|
boolean localChanged = false;
|
2021-11-25 21:27:45 +01:00
|
|
|
DrillItem.loadSlotsFromNBT(inv, stack);
|
2016-08-07 01:11:01 +02:00
|
|
|
|
2021-02-27 22:44:00 +01:00
|
|
|
for (int j = 0; j < inv.getSlots(); j++) {
|
|
|
|
ItemStack invStack = inv.getStackInSlot(j);
|
|
|
|
if (StackUtil.isValid(invStack)) {
|
|
|
|
for (int i = 0; i < cap.getSlots(); i++) {
|
|
|
|
ItemStack remain = cap.insertItem(i, invStack, false);
|
2021-08-22 17:09:06 +02:00
|
|
|
if (!ItemStack.matches(remain, invStack)) {
|
2021-02-27 22:44:00 +01:00
|
|
|
inv.setStackInSlot(j, remain.copy());
|
|
|
|
localChanged = true;
|
|
|
|
if (!StackUtil.isValid(remain)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
invStack = remain;
|
2016-08-07 01:11:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-27 22:44:00 +01:00
|
|
|
return localChanged;
|
|
|
|
}).orElse(false);
|
|
|
|
|
|
|
|
if (changed) {
|
2021-11-25 21:27:45 +01:00
|
|
|
DrillItem.writeSlotsToNBT(inv, stack);
|
2016-08-07 01:11:01 +02:00
|
|
|
}
|
|
|
|
}
|
2024-03-02 21:23:08 +01:00
|
|
|
return InteractionResult.SUCCESS;
|
2016-08-07 01:11:01 +02:00
|
|
|
}
|
|
|
|
}
|
2024-03-02 21:23:08 +01:00
|
|
|
return InteractionResult.PASS;
|
2016-08-07 01:11:01 +02:00
|
|
|
}
|
|
|
|
|
2016-08-02 16:32:13 +02:00
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public InteractionResultHolder<ItemStack> use(Level world, Player player, 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)));
|
2023-03-16 00:17:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* NetworkHooks.openGui((ServerPlayerEntity) player,
|
|
|
|
new SimpleNamedContainerProvider((windowId, playerInventory, playerEntity) ->
|
2024-03-03 01:20:53 +01:00
|
|
|
new ContainerBag(windowId, playerInventory, playerEntity.getItemInHand(hand), this.isVoid), StringComponent.empty()));*/
|
2016-08-02 16:32:13 +02:00
|
|
|
}
|
2024-03-02 21:23:08 +01:00
|
|
|
return InteractionResultHolder.pass(player.getItemInHand(hand));
|
2016-08-02 16:32:13 +02:00
|
|
|
}
|
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
|
|
|
}
|
2016-08-02 16:32:13 +02:00
|
|
|
}
|