From 1502348da84003e44328ee896f0e8609602b3368 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Mon, 18 Feb 2019 13:00:54 +0100 Subject: [PATCH] make the ender crate use aura --- .../naturesaura/blocks/BlockEnderCrate.java | 6 +- .../blocks/tiles/TileEntityEnderCrate.java | 57 ++++++++++++++++++- .../naturesaura/items/ItemEnderAccess.java | 4 +- 3 files changed, 62 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/ellpeck/naturesaura/blocks/BlockEnderCrate.java b/src/main/java/de/ellpeck/naturesaura/blocks/BlockEnderCrate.java index 105a38bb..8509fcc3 100644 --- a/src/main/java/de/ellpeck/naturesaura/blocks/BlockEnderCrate.java +++ b/src/main/java/de/ellpeck/naturesaura/blocks/BlockEnderCrate.java @@ -6,7 +6,6 @@ import de.ellpeck.naturesaura.blocks.tiles.render.RenderEnderCrate; import de.ellpeck.naturesaura.reg.ITESRProvider; import net.minecraft.block.SoundType; import net.minecraft.block.material.Material; -import net.minecraft.block.state.BlockFaceShape; import net.minecraft.block.state.IBlockState; import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer; import net.minecraft.entity.player.EntityPlayer; @@ -16,7 +15,6 @@ import net.minecraft.util.EnumHand; import net.minecraft.util.EnumParticleTypes; import net.minecraft.util.Tuple; import net.minecraft.util.math.BlockPos; -import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; @@ -37,8 +35,10 @@ public class BlockEnderCrate extends BlockContainerImpl implements ITESRProvider TileEntity tile = worldIn.getTileEntity(pos); if (tile instanceof TileEntityEnderCrate) { TileEntityEnderCrate crate = (TileEntityEnderCrate) tile; - if (crate.canOpen()) + if (crate.canOpen()) { + crate.drainAura(5000); playerIn.openGui(NaturesAura.MOD_ID, 0, worldIn, pos.getX(), pos.getY(), pos.getZ()); + } } } return true; diff --git a/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityEnderCrate.java b/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityEnderCrate.java index 53d3ec49..953491e2 100644 --- a/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityEnderCrate.java +++ b/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityEnderCrate.java @@ -1,20 +1,68 @@ package de.ellpeck.naturesaura.blocks.tiles; +import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk; import de.ellpeck.naturesaura.api.misc.IWorldData; import net.minecraft.block.state.IBlockState; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.EnumFacing; +import net.minecraft.util.math.BlockPos; import net.minecraftforge.items.IItemHandlerModifiable; +import javax.annotation.Nonnull; + public class TileEntityEnderCrate extends TileEntityImpl { + private final IItemHandlerModifiable wrappedEnderStorage = new IItemHandlerModifiable() { + @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) { + ItemStack remain = this.getStorage().insertItem(slot, stack, simulate); + if (!simulate) + TileEntityEnderCrate.this.drainAura((stack.getCount() - remain.getCount()) * 200); + return remain; + } + + @Nonnull + @Override + public ItemStack extractItem(int slot, int amount, boolean simulate) { + ItemStack extracted = this.getStorage().extractItem(slot, amount, simulate); + if (!simulate) + TileEntityEnderCrate.this.drainAura(extracted.getCount() * 200); + return extracted; + } + + @Override + public int getSlotLimit(int slot) { + return this.getStorage().getSlotLimit(slot); + } + + private IItemHandlerModifiable getStorage() { + return IWorldData.getOverworldData(TileEntityEnderCrate.this.world).getEnderStorage(TileEntityEnderCrate.this.name); + } + }; public String name; @Override public IItemHandlerModifiable getItemHandler(EnumFacing facing) { if (this.canOpen()) - return IWorldData.getOverworldData(this.world).getEnderStorage(this.name); + return this.wrappedEnderStorage; return null; } @@ -58,4 +106,11 @@ public class TileEntityEnderCrate extends TileEntityImpl { this.name = compound.getString("name"); } } + + public void drainAura(int amount) { + if (amount > 0) { + BlockPos spot = IAuraChunk.getHighestSpot(this.world, this.pos, 35, this.pos); + IAuraChunk.getAuraChunk(this.world, spot).drainAura(spot, amount); + } + } } diff --git a/src/main/java/de/ellpeck/naturesaura/items/ItemEnderAccess.java b/src/main/java/de/ellpeck/naturesaura/items/ItemEnderAccess.java index d2ee6a5b..f0f4953e 100644 --- a/src/main/java/de/ellpeck/naturesaura/items/ItemEnderAccess.java +++ b/src/main/java/de/ellpeck/naturesaura/items/ItemEnderAccess.java @@ -1,6 +1,7 @@ package de.ellpeck.naturesaura.items; import de.ellpeck.naturesaura.NaturesAura; +import de.ellpeck.naturesaura.api.NaturesAuraAPI; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.util.ActionResult; @@ -19,8 +20,9 @@ public class ItemEnderAccess extends ItemImpl { return new ActionResult<>(EnumActionResult.PASS, playerIn.getHeldItem(handIn)); ItemStack stack = playerIn.getHeldItemMainhand(); if (stack.hasDisplayName()) { - if (!worldIn.isRemote) + if (!worldIn.isRemote && NaturesAuraAPI.instance().extractAuraFromPlayer(playerIn, 5000, false)) { playerIn.openGui(NaturesAura.MOD_ID, 1, worldIn, (int) playerIn.posX, (int) playerIn.posY, (int) playerIn.posZ); + } return new ActionResult<>(EnumActionResult.SUCCESS, stack); } return new ActionResult<>(EnumActionResult.FAIL, stack);