2019-02-17 22:51:05 +01:00
|
|
|
package de.ellpeck.naturesaura.blocks.tiles;
|
|
|
|
|
2019-02-18 19:30:35 +01:00
|
|
|
import de.ellpeck.naturesaura.NaturesAura;
|
2019-02-18 13:00:54 +01:00
|
|
|
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
|
2021-12-04 15:40:09 +01:00
|
|
|
import de.ellpeck.naturesaura.api.misc.ILevelData;
|
2019-02-18 19:30:35 +01:00
|
|
|
import de.ellpeck.naturesaura.blocks.BlockEnderCrate;
|
2020-01-24 17:05:41 +01:00
|
|
|
import de.ellpeck.naturesaura.gui.ContainerEnderCrate;
|
|
|
|
import de.ellpeck.naturesaura.gui.ModContainers;
|
2021-12-08 00:31:29 +01:00
|
|
|
import net.minecraft.ChatFormatting;
|
2021-12-06 14:38:12 +01:00
|
|
|
import net.minecraft.core.BlockPos;
|
2021-12-04 15:40:09 +01:00
|
|
|
import net.minecraft.nbt.CompoundTag;
|
2021-12-08 00:31:29 +01:00
|
|
|
import net.minecraft.network.chat.Component;
|
2021-12-06 14:38:12 +01:00
|
|
|
import net.minecraft.world.MenuProvider;
|
2021-12-08 00:31:29 +01:00
|
|
|
import net.minecraft.world.entity.player.Inventory;
|
|
|
|
import net.minecraft.world.entity.player.Player;
|
|
|
|
import net.minecraft.world.inventory.AbstractContainerMenu;
|
2021-12-06 14:38:12 +01:00
|
|
|
import net.minecraft.world.item.ItemStack;
|
|
|
|
import net.minecraft.world.level.block.state.BlockState;
|
2024-02-03 14:56:07 +01:00
|
|
|
import net.neoforged.neoforge.items.IItemHandlerModifiable;
|
2019-02-17 22:51:05 +01:00
|
|
|
|
2019-02-18 13:00:54 +01:00
|
|
|
import javax.annotation.Nonnull;
|
2020-01-24 17:05:41 +01:00
|
|
|
import javax.annotation.Nullable;
|
2019-02-18 13:00:54 +01:00
|
|
|
|
2021-12-06 14:38:12 +01:00
|
|
|
public class BlockEntityEnderCrate extends BlockEntityImpl implements MenuProvider {
|
2019-02-17 22:51:05 +01:00
|
|
|
|
2020-02-07 15:22:30 +01:00
|
|
|
public String name;
|
2024-03-10 15:54:58 +01:00
|
|
|
public final IItemHandlerModifiable wrappedEnderStorage = new IItemHandlerModifiable() {
|
2019-02-18 13:00:54 +01:00
|
|
|
@Override
|
|
|
|
public void setStackInSlot(int slot, @Nonnull ItemStack stack) {
|
|
|
|
this.getStorage().setStackInSlot(slot, stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSlots() {
|
|
|
|
return this.getStorage().getSlots();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public ItemStack getStackInSlot(int slot) {
|
|
|
|
return this.getStorage().getStackInSlot(slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate) {
|
2023-02-16 19:03:26 +01:00
|
|
|
if (!BlockEntityEnderCrate.this.canUseRightNow(20))
|
|
|
|
return stack;
|
2021-12-15 16:30:22 +01:00
|
|
|
var remain = this.getStorage().insertItem(slot, stack, simulate);
|
2019-02-18 13:00:54 +01:00
|
|
|
if (!simulate)
|
2021-12-04 15:40:09 +01:00
|
|
|
BlockEntityEnderCrate.this.drainAura((stack.getCount() - remain.getCount()) * 20);
|
2019-02-18 13:00:54 +01:00
|
|
|
return remain;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public ItemStack extractItem(int slot, int amount, boolean simulate) {
|
2023-02-16 19:03:26 +01:00
|
|
|
if (!BlockEntityEnderCrate.this.canUseRightNow(20))
|
|
|
|
return ItemStack.EMPTY;
|
2021-12-15 16:30:22 +01:00
|
|
|
var extracted = this.getStorage().extractItem(slot, amount, simulate);
|
2019-02-18 13:00:54 +01:00
|
|
|
if (!simulate)
|
2021-12-04 15:40:09 +01:00
|
|
|
BlockEntityEnderCrate.this.drainAura(extracted.getCount() * 20);
|
2019-02-18 13:00:54 +01:00
|
|
|
return extracted;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSlotLimit(int slot) {
|
|
|
|
return this.getStorage().getSlotLimit(slot);
|
|
|
|
}
|
|
|
|
|
2020-01-21 23:02:39 +01:00
|
|
|
@Override
|
|
|
|
public boolean isItemValid(int slot, @Nonnull ItemStack stack) {
|
|
|
|
return this.getStorage().isItemValid(slot, stack);
|
|
|
|
}
|
|
|
|
|
2019-02-18 13:00:54 +01:00
|
|
|
private IItemHandlerModifiable getStorage() {
|
2021-12-04 15:40:09 +01:00
|
|
|
return ILevelData.getOverworldData(BlockEntityEnderCrate.this.level).getEnderStorage(BlockEntityEnderCrate.this.name);
|
2019-02-18 13:00:54 +01:00
|
|
|
}
|
|
|
|
};
|
2019-02-17 22:51:05 +01:00
|
|
|
|
2021-12-06 14:38:12 +01:00
|
|
|
public BlockEntityEnderCrate(BlockPos pos, BlockState state) {
|
2021-12-19 15:32:45 +01:00
|
|
|
super(ModBlockEntities.ENDER_CRATE, pos, state);
|
2020-01-21 23:02:39 +01:00
|
|
|
}
|
|
|
|
|
2019-02-17 22:51:05 +01:00
|
|
|
public boolean canOpen() {
|
|
|
|
return this.name != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void dropInventory() {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-01-23 16:05:52 +01:00
|
|
|
public void modifyDrop(ItemStack regularItem) {
|
2019-02-18 19:30:35 +01:00
|
|
|
if (this.name != null) {
|
2020-01-23 16:05:52 +01:00
|
|
|
if (!regularItem.hasTag())
|
2021-12-04 15:40:09 +01:00
|
|
|
regularItem.setTag(new CompoundTag());
|
2020-01-23 16:05:52 +01:00
|
|
|
regularItem.getTag().putString(NaturesAura.MOD_ID + ":ender_name", this.name);
|
2019-02-18 19:30:35 +01:00
|
|
|
}
|
2019-02-17 22:51:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void loadDataOnPlace(ItemStack stack) {
|
|
|
|
super.loadDataOnPlace(stack);
|
2021-12-04 15:40:09 +01:00
|
|
|
if (!this.level.isClientSide) {
|
2021-12-15 16:30:22 +01:00
|
|
|
var name = BlockEnderCrate.getEnderName(stack);
|
2019-02-18 19:30:35 +01:00
|
|
|
if (name != null && !name.isEmpty())
|
|
|
|
this.name = name;
|
|
|
|
}
|
2019-02-17 22:51:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-04 15:40:09 +01:00
|
|
|
public void writeNBT(CompoundTag compound, SaveType type) {
|
2019-02-17 22:51:05 +01:00
|
|
|
super.writeNBT(compound, type);
|
|
|
|
if (type != SaveType.BLOCK) {
|
|
|
|
if (this.name != null)
|
2020-01-21 23:02:39 +01:00
|
|
|
compound.putString("name", this.name);
|
2019-02-17 22:51:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-04 15:40:09 +01:00
|
|
|
public void readNBT(CompoundTag compound, SaveType type) {
|
2019-02-17 22:51:05 +01:00
|
|
|
super.readNBT(compound, type);
|
|
|
|
if (type != SaveType.BLOCK) {
|
2020-01-21 23:02:39 +01:00
|
|
|
if (compound.contains("name"))
|
2019-02-17 22:51:05 +01:00
|
|
|
this.name = compound.getString("name");
|
|
|
|
}
|
|
|
|
}
|
2019-02-18 13:00:54 +01:00
|
|
|
|
|
|
|
public void drainAura(int amount) {
|
|
|
|
if (amount > 0) {
|
2021-12-15 16:30:22 +01:00
|
|
|
var spot = IAuraChunk.getHighestSpot(this.level, this.worldPosition, 35, this.worldPosition);
|
2021-12-04 15:40:09 +01:00
|
|
|
IAuraChunk.getAuraChunk(this.level, spot).drainAura(spot, amount);
|
2019-02-18 13:00:54 +01:00
|
|
|
}
|
|
|
|
}
|
2020-01-24 17:05:41 +01:00
|
|
|
|
|
|
|
@Override
|
2021-12-08 00:31:29 +01:00
|
|
|
public Component getDisplayName() {
|
2022-06-27 15:24:04 +02:00
|
|
|
return Component.translatable("info." + NaturesAura.MOD_ID + ".ender_crate", ChatFormatting.ITALIC + this.name + ChatFormatting.RESET);
|
2020-01-24 17:05:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
2021-12-08 00:31:29 +01:00
|
|
|
public AbstractContainerMenu createMenu(int window, Inventory inv, Player player) {
|
2024-03-10 15:54:58 +01:00
|
|
|
return new ContainerEnderCrate(ModContainers.ENDER_CRATE, window, player, this.wrappedEnderStorage);
|
2020-01-24 17:05:41 +01:00
|
|
|
}
|
2023-02-16 19:03:26 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean allowsLowerLimiter() {
|
|
|
|
return true;
|
|
|
|
}
|
2024-03-10 15:54:58 +01:00
|
|
|
|
2019-02-17 22:51:05 +01:00
|
|
|
}
|