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;
|
|
|
|
|
|
|
|
import de.ellpeck.actuallyadditions.mod.inventory.ContainerBag;
|
|
|
|
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;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
2021-02-27 22:44:00 +01:00
|
|
|
import net.minecraft.entity.player.ServerPlayerEntity;
|
|
|
|
import net.minecraft.inventory.container.SimpleNamedContainerProvider;
|
2016-08-02 16:32:13 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
2021-02-27 22:44:00 +01:00
|
|
|
import net.minecraft.item.ItemUseContext;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.nbt.CompoundNBT;
|
2016-08-07 01:11:01 +02:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2016-12-18 17:50:31 +01:00
|
|
|
import net.minecraft.util.ActionResult;
|
2021-02-27 22:44:00 +01:00
|
|
|
import net.minecraft.util.ActionResultType;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.util.Hand;
|
2016-08-02 16:32:13 +02:00
|
|
|
import net.minecraft.world.World;
|
2021-02-27 22:44:00 +01:00
|
|
|
import net.minecraftforge.fml.network.NetworkHooks;
|
2016-08-07 01:11:01 +02:00
|
|
|
import net.minecraftforge.items.CapabilityItemHandler;
|
2016-08-02 16:32:13 +02:00
|
|
|
|
2023-03-16 00:17:57 +01:00
|
|
|
import java.util.UUID;
|
2016-08-02 16:32:13 +02:00
|
|
|
|
2021-02-27 22:44:00 +01:00
|
|
|
public class ItemBag extends ItemBase {
|
2017-06-17 00:55:55 +02:00
|
|
|
public final boolean isVoid;
|
2016-08-02 16:32:13 +02:00
|
|
|
|
2021-02-27 22:44:00 +01:00
|
|
|
public ItemBag(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
|
2021-08-22 17:09:06 +02:00
|
|
|
public ActionResultType useOn(ItemUseContext context) {
|
|
|
|
ItemStack stack = context.getPlayer().getItemInHand(context.getHand());
|
2018-07-28 02:08:42 +02:00
|
|
|
if (!this.isVoid) {
|
2021-08-22 17:09:06 +02:00
|
|
|
TileEntity tile = context.getLevel().getBlockEntity(context.getClickedPos());
|
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
|
|
|
|
2021-08-22 17:09:06 +02:00
|
|
|
boolean changed = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, 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
|
|
|
}
|
|
|
|
}
|
2021-02-27 22:44:00 +01:00
|
|
|
return ActionResultType.SUCCESS;
|
2016-08-07 01:11:01 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-27 22:44:00 +01:00
|
|
|
return ActionResultType.PASS;
|
2016-08-07 01:11:01 +02:00
|
|
|
}
|
|
|
|
|
2016-08-02 16:32:13 +02:00
|
|
|
@Override
|
2021-08-22 17:09:06 +02:00
|
|
|
public ActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
|
2023-03-16 00:17:57 +01:00
|
|
|
ItemStack sackStack = player.getItemInHand(hand);
|
|
|
|
if (!world.isClientSide && hand == Hand.MAIN_HAND && sackStack.getItem() instanceof ItemBag && player instanceof ServerPlayerEntity) {
|
|
|
|
|
|
|
|
if (!isVoid) {
|
|
|
|
SackData data = getData(sackStack);
|
|
|
|
if (data == null)
|
|
|
|
return ActionResult.fail(sackStack);
|
|
|
|
|
|
|
|
UUID uuid = data.getUuid();
|
|
|
|
|
|
|
|
data.updateAccessRecords(player.getName().getString(), System.currentTimeMillis());
|
|
|
|
|
|
|
|
|
|
|
|
NetworkHooks.openGui((ServerPlayerEntity) player, new SimpleNamedContainerProvider((id, inv, entity) ->
|
|
|
|
new ContainerBag(id, inv, uuid, data.getSpecialHandler()), sackStack.getHoverName()), (buffer -> buffer.writeUUID(uuid)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* NetworkHooks.openGui((ServerPlayerEntity) player,
|
|
|
|
new SimpleNamedContainerProvider((windowId, playerInventory, playerEntity) ->
|
|
|
|
new ContainerBag(windowId, playerInventory, playerEntity.getItemInHand(hand), this.isVoid), StringTextComponent.EMPTY));*/
|
2016-08-02 16:32:13 +02:00
|
|
|
}
|
2021-08-22 17:09:06 +02:00
|
|
|
return ActionResult.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) {
|
|
|
|
if (!(stack.getItem() instanceof ItemBag))
|
|
|
|
return null;
|
|
|
|
UUID uuid;
|
|
|
|
CompoundNBT tag = stack.getOrCreateTag();
|
|
|
|
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
|
|
|
}
|