From c5e38b6711d43178be6d40d26936b81404bf718a Mon Sep 17 00:00:00 2001 From: jameslfc19 Date: Sat, 18 Jul 2020 18:29:54 +0100 Subject: [PATCH] Barrels! Added Barrels as an option for ChestLinks! --- .../minecraft/chests/ChestOpener.java | 3 +- .../minecraft/chests/TileEntityOpener.java | 9 +++ .../chests/v1_14_R1/ChestOpener_1_14.java | 43 +++++++---- .../tileentities/CustomTileEntityBarrel.java | 75 +++++++++++++++++++ .../CustomTileEntityChest.java | 6 +- .../chests/v1_15_R1/ChestOpener_1_15.java | 44 +++++++---- .../tileentities/CustomTileEntityBarrel.java | 75 +++++++++++++++++++ .../CustomTileEntityChest.java | 5 +- .../chests/v1_16_R1/ChestOpener_1_16.java | 45 ++++++----- .../tileentities/CustomTileEntityBarrel.java | 75 +++++++++++++++++++ .../CustomTileEntityChest.java | 6 +- .../chests/listeners/InventoryListener.java | 5 +- .../minecraft/chests/misc/Utils.java | 12 +-- .../storage/abstracts/AbstractStorage.java | 17 +++-- .../autocraft/AutoCraftingStorage.java | 2 +- .../storage/chestlink/ChestLinkStorage.java | 21 ++++-- .../chestlink/ChestLinkStorageType.java | 26 ++++--- 17 files changed, 380 insertions(+), 89 deletions(-) create mode 100644 ChestsPlusPlusAPI/src/main/java/com/jamesdpeters/minecraft/chests/TileEntityOpener.java create mode 100644 ChestsPlusPlus_1_14/src/main/java/com/jamesdpeters/minecraft/chests/v1_14_R1/tileentities/CustomTileEntityBarrel.java rename ChestsPlusPlus_1_14/src/main/java/com/jamesdpeters/minecraft/chests/v1_14_R1/{ => tileentities}/CustomTileEntityChest.java (90%) create mode 100644 ChestsPlusPlus_1_15/src/main/java/com/jamesdpeters/minecraft/chests/v1_15_R1/tileentities/CustomTileEntityBarrel.java rename ChestsPlusPlus_1_15/src/main/java/com/jamesdpeters/minecraft/chests/v1_15_R1/{ => tileentities}/CustomTileEntityChest.java (91%) create mode 100644 ChestsPlusPlus_1_16/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R1/tileentities/CustomTileEntityBarrel.java rename ChestsPlusPlus_1_16/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R1/{ => tileentities}/CustomTileEntityChest.java (91%) diff --git a/ChestsPlusPlusAPI/src/main/java/com/jamesdpeters/minecraft/chests/ChestOpener.java b/ChestsPlusPlusAPI/src/main/java/com/jamesdpeters/minecraft/chests/ChestOpener.java index 8408543..2909694 100644 --- a/ChestsPlusPlusAPI/src/main/java/com/jamesdpeters/minecraft/chests/ChestOpener.java +++ b/ChestsPlusPlusAPI/src/main/java/com/jamesdpeters/minecraft/chests/ChestOpener.java @@ -1,8 +1,9 @@ package com.jamesdpeters.minecraft.chests; import org.bukkit.block.Chest; +import org.bukkit.block.Container; import org.bukkit.inventory.Inventory; public interface ChestOpener { - void setLidOpen(Inventory inventory, Chest chest, boolean open); + void setLidOpen(Inventory inventory, Container chest, boolean open); } diff --git a/ChestsPlusPlusAPI/src/main/java/com/jamesdpeters/minecraft/chests/TileEntityOpener.java b/ChestsPlusPlusAPI/src/main/java/com/jamesdpeters/minecraft/chests/TileEntityOpener.java new file mode 100644 index 0000000..6e4ba0a --- /dev/null +++ b/ChestsPlusPlusAPI/src/main/java/com/jamesdpeters/minecraft/chests/TileEntityOpener.java @@ -0,0 +1,9 @@ +package com.jamesdpeters.minecraft.chests; + +import org.bukkit.entity.HumanEntity; + +import java.util.List; + +public interface TileEntityOpener { + void setViewers(List viewers); +} diff --git a/ChestsPlusPlus_1_14/src/main/java/com/jamesdpeters/minecraft/chests/v1_14_R1/ChestOpener_1_14.java b/ChestsPlusPlus_1_14/src/main/java/com/jamesdpeters/minecraft/chests/v1_14_R1/ChestOpener_1_14.java index 62f0775..5e5e846 100644 --- a/ChestsPlusPlus_1_14/src/main/java/com/jamesdpeters/minecraft/chests/v1_14_R1/ChestOpener_1_14.java +++ b/ChestsPlusPlus_1_14/src/main/java/com/jamesdpeters/minecraft/chests/v1_14_R1/ChestOpener_1_14.java @@ -1,37 +1,50 @@ package com.jamesdpeters.minecraft.chests.v1_14_R1; import com.jamesdpeters.minecraft.chests.ChestOpener; +import com.jamesdpeters.minecraft.chests.TileEntityOpener; +import com.jamesdpeters.minecraft.chests.v1_14_R1.tileentities.CustomTileEntityBarrel; +import com.jamesdpeters.minecraft.chests.v1_14_R1.tileentities.CustomTileEntityChest; import net.minecraft.server.v1_14_R1.BlockPosition; import net.minecraft.server.v1_14_R1.TileEntity; +import net.minecraft.server.v1_14_R1.TileEntityBarrel; import net.minecraft.server.v1_14_R1.TileEntityChest; import net.minecraft.server.v1_14_R1.World; -import org.bukkit.block.Chest; +import org.bukkit.block.Container; import org.bukkit.craftbukkit.v1_14_R1.CraftWorld; import org.bukkit.craftbukkit.v1_14_R1.block.CraftChest; +import org.bukkit.craftbukkit.v1_14_R1.block.CraftContainer; +import org.bukkit.entity.HumanEntity; import org.bukkit.inventory.Inventory; +import java.util.List; + public class ChestOpener_1_14 implements ChestOpener { @Override - public void setLidOpen(Inventory inventory, Chest chest, boolean open) { - CraftChest craftChest = (CraftChest) chest; - CraftWorld craftWorld = (CraftWorld) craftChest.getWorld(); + public void setLidOpen(Inventory inventory, Container chest, boolean open) { + CraftContainer craftContainer = (CraftContainer) chest; + CraftWorld craftWorld = (CraftWorld) craftContainer.getWorld(); World world = craftWorld.getHandle(); - BlockPosition position = craftChest.getPosition(); + BlockPosition position = craftContainer.getPosition(); TileEntity tileEntity = world.getTileEntity(position); - if(tileEntity instanceof TileEntityChest){ - boolean isCustomTileEntity = tileEntity instanceof CustomTileEntityChest; - CustomTileEntityChest customTileEntityChest; - if(!isCustomTileEntity){ - customTileEntityChest = new CustomTileEntityChest(); - world.removeTileEntity(position); - world.setTileEntity(position, customTileEntityChest); - } else { - customTileEntityChest = (CustomTileEntityChest) tileEntity; + //Checks if Tile Entity has already got custom Opener. + if(tileEntity instanceof TileEntityOpener){ + ((TileEntityOpener) tileEntity).setViewers(inventory.getViewers()); + } else { + //If not set the new tile entity and set the viewers. + if (tileEntity instanceof TileEntityChest) { + setTileEnt(world, position, new CustomTileEntityChest(), inventory.getViewers()); + } else if (tileEntity instanceof TileEntityBarrel) { + setTileEnt(world, position, new CustomTileEntityBarrel(), inventory.getViewers()); } - customTileEntityChest.setViewers(inventory.getViewers()); } } + + private void setTileEnt(World world, BlockPosition position, T tileEntOpener, List viewers){ + world.removeTileEntity(position); + world.setTileEntity(position, tileEntOpener); + tileEntOpener.setViewers(viewers); + } } diff --git a/ChestsPlusPlus_1_14/src/main/java/com/jamesdpeters/minecraft/chests/v1_14_R1/tileentities/CustomTileEntityBarrel.java b/ChestsPlusPlus_1_14/src/main/java/com/jamesdpeters/minecraft/chests/v1_14_R1/tileentities/CustomTileEntityBarrel.java new file mode 100644 index 0000000..8bf96b2 --- /dev/null +++ b/ChestsPlusPlus_1_14/src/main/java/com/jamesdpeters/minecraft/chests/v1_14_R1/tileentities/CustomTileEntityBarrel.java @@ -0,0 +1,75 @@ +package com.jamesdpeters.minecraft.chests.v1_14_R1.tileentities; + +import com.jamesdpeters.minecraft.chests.TileEntityOpener; +import net.minecraft.server.v1_14_R1.BaseBlockPosition; +import net.minecraft.server.v1_14_R1.BlockBarrel; +import net.minecraft.server.v1_14_R1.EntityHuman; +import net.minecraft.server.v1_14_R1.EnumDirection; +import net.minecraft.server.v1_14_R1.IBlockData; +import net.minecraft.server.v1_14_R1.SoundCategory; +import net.minecraft.server.v1_14_R1.SoundEffect; +import net.minecraft.server.v1_14_R1.SoundEffects; +import net.minecraft.server.v1_14_R1.TileEntityBarrel; +import org.bukkit.entity.HumanEntity; + +import java.util.List; + +public class CustomTileEntityBarrel extends TileEntityBarrel implements TileEntityOpener { + + private int phantomViewers = 0; + private List viewers; + + @Override + public List getViewers() { + return viewers; + } + + @Override + public void h() { + //super.h(); + } + + @Override + public void startOpen(EntityHuman entityhuman) { + //super.startOpen(entityhuman); + } + + @Override + public void closeContainer(EntityHuman entityhuman) { + //do nothing. + } + + @Override + public void setViewers(List viewers){ + int previousViewers = phantomViewers; + phantomViewers = viewers.size(); + this.viewers = viewers; + + if(phantomViewers > 1 && previousViewers == 0) open(); + if(phantomViewers == 0) close(); + } + + public void open(){ + IBlockData iblockdata = this.getBlock(); + a(iblockdata, SoundEffects.BLOCK_BARREL_OPEN); + a(iblockdata, true); + } + + public void close(){ + IBlockData iblockdata = this.getBlock(); + a(iblockdata, SoundEffects.BLOCK_BARREL_CLOSE); + a(iblockdata, false); + } + + private void a(IBlockData iblockdata, boolean flag) { + this.world.setTypeAndData(this.getPosition(), (IBlockData)iblockdata.set(BlockBarrel.b, flag), 3); + } + + private void a(IBlockData iblockdata, SoundEffect soundeffect) { + BaseBlockPosition baseblockposition = ((EnumDirection)iblockdata.get(BlockBarrel.a)).n(); + double d0 = (double)this.position.getX() + 0.5D + (double)baseblockposition.getX() / 2.0D; + double d1 = (double)this.position.getY() + 0.5D + (double)baseblockposition.getY() / 2.0D; + double d2 = (double)this.position.getZ() + 0.5D + (double)baseblockposition.getZ() / 2.0D; + this.world.playSound((EntityHuman)null, d0, d1, d2, soundeffect, SoundCategory.BLOCKS, 0.5F, this.world.random.nextFloat() * 0.1F + 0.9F); + } +} diff --git a/ChestsPlusPlus_1_14/src/main/java/com/jamesdpeters/minecraft/chests/v1_14_R1/CustomTileEntityChest.java b/ChestsPlusPlus_1_14/src/main/java/com/jamesdpeters/minecraft/chests/v1_14_R1/tileentities/CustomTileEntityChest.java similarity index 90% rename from ChestsPlusPlus_1_14/src/main/java/com/jamesdpeters/minecraft/chests/v1_14_R1/CustomTileEntityChest.java rename to ChestsPlusPlus_1_14/src/main/java/com/jamesdpeters/minecraft/chests/v1_14_R1/tileentities/CustomTileEntityChest.java index fe52db4..47d4cb0 100644 --- a/ChestsPlusPlus_1_14/src/main/java/com/jamesdpeters/minecraft/chests/v1_14_R1/CustomTileEntityChest.java +++ b/ChestsPlusPlus_1_14/src/main/java/com/jamesdpeters/minecraft/chests/v1_14_R1/tileentities/CustomTileEntityChest.java @@ -1,5 +1,6 @@ -package com.jamesdpeters.minecraft.chests.v1_14_R1; +package com.jamesdpeters.minecraft.chests.v1_14_R1.tileentities; +import com.jamesdpeters.minecraft.chests.TileEntityOpener; import net.minecraft.server.v1_14_R1.Block; import net.minecraft.server.v1_14_R1.BlockChest; import net.minecraft.server.v1_14_R1.BlockPropertyChestType; @@ -12,7 +13,7 @@ import org.bukkit.entity.HumanEntity; import java.util.List; -public class CustomTileEntityChest extends TileEntityChest { +public class CustomTileEntityChest extends TileEntityChest implements TileEntityOpener { private int phantomViewers = 0; private List viewers; @@ -36,6 +37,7 @@ public class CustomTileEntityChest extends TileEntityChest { } } + @Override public void setViewers(List viewers){ int previousViewers = phantomViewers; phantomViewers = viewers.size(); diff --git a/ChestsPlusPlus_1_15/src/main/java/com/jamesdpeters/minecraft/chests/v1_15_R1/ChestOpener_1_15.java b/ChestsPlusPlus_1_15/src/main/java/com/jamesdpeters/minecraft/chests/v1_15_R1/ChestOpener_1_15.java index 3296686..07859f7 100644 --- a/ChestsPlusPlus_1_15/src/main/java/com/jamesdpeters/minecraft/chests/v1_15_R1/ChestOpener_1_15.java +++ b/ChestsPlusPlus_1_15/src/main/java/com/jamesdpeters/minecraft/chests/v1_15_R1/ChestOpener_1_15.java @@ -1,37 +1,49 @@ package com.jamesdpeters.minecraft.chests.v1_15_R1; import com.jamesdpeters.minecraft.chests.ChestOpener; +import com.jamesdpeters.minecraft.chests.TileEntityOpener; +import com.jamesdpeters.minecraft.chests.v1_15_R1.tileentities.CustomTileEntityBarrel; +import com.jamesdpeters.minecraft.chests.v1_15_R1.tileentities.CustomTileEntityChest; import net.minecraft.server.v1_15_R1.BlockPosition; import net.minecraft.server.v1_15_R1.TileEntity; +import net.minecraft.server.v1_15_R1.TileEntityBarrel; import net.minecraft.server.v1_15_R1.TileEntityChest; import net.minecraft.server.v1_15_R1.World; -import org.bukkit.block.Chest; +import org.bukkit.block.Container; import org.bukkit.craftbukkit.v1_15_R1.CraftWorld; -import org.bukkit.craftbukkit.v1_15_R1.block.CraftChest; +import org.bukkit.craftbukkit.v1_15_R1.block.CraftContainer; +import org.bukkit.entity.HumanEntity; import org.bukkit.inventory.Inventory; +import java.util.List; + public class ChestOpener_1_15 implements ChestOpener { @Override - public void setLidOpen(Inventory inventory, Chest chest, boolean open) { - CraftChest craftChest = (CraftChest) chest; - CraftWorld craftWorld = (CraftWorld) craftChest.getWorld(); + public void setLidOpen(Inventory inventory, Container chest, boolean open) { + CraftContainer craftContainer = (CraftContainer) chest; + CraftWorld craftWorld = (CraftWorld) craftContainer.getWorld(); World world = craftWorld.getHandle(); - BlockPosition position = craftChest.getPosition(); + BlockPosition position = craftContainer.getPosition(); TileEntity tileEntity = world.getTileEntity(position); - if(tileEntity instanceof TileEntityChest){ - boolean isCustomTileEntity = tileEntity instanceof CustomTileEntityChest; - CustomTileEntityChest customTileEntityChest; - if(!isCustomTileEntity){ - customTileEntityChest = new CustomTileEntityChest(); - world.removeTileEntity(position); - world.setTileEntity(position, customTileEntityChest); - } else { - customTileEntityChest = (CustomTileEntityChest) tileEntity; + //Checks if Tile Entity has already got custom Opener. + if(tileEntity instanceof TileEntityOpener){ + ((TileEntityOpener) tileEntity).setViewers(inventory.getViewers()); + } else { + //If not set the new tile entity and set the viewers. + if (tileEntity instanceof TileEntityChest) { + setTileEnt(world, position, new CustomTileEntityChest(), inventory.getViewers()); + } else if (tileEntity instanceof TileEntityBarrel) { + setTileEnt(world, position, new CustomTileEntityBarrel(), inventory.getViewers()); } - customTileEntityChest.setViewers(inventory.getViewers()); } } + + private void setTileEnt(World world, BlockPosition position, T tileEntOpener, List viewers){ + world.removeTileEntity(position); + world.setTileEntity(position, tileEntOpener); + tileEntOpener.setViewers(viewers); + } } diff --git a/ChestsPlusPlus_1_15/src/main/java/com/jamesdpeters/minecraft/chests/v1_15_R1/tileentities/CustomTileEntityBarrel.java b/ChestsPlusPlus_1_15/src/main/java/com/jamesdpeters/minecraft/chests/v1_15_R1/tileentities/CustomTileEntityBarrel.java new file mode 100644 index 0000000..01126f2 --- /dev/null +++ b/ChestsPlusPlus_1_15/src/main/java/com/jamesdpeters/minecraft/chests/v1_15_R1/tileentities/CustomTileEntityBarrel.java @@ -0,0 +1,75 @@ +package com.jamesdpeters.minecraft.chests.v1_15_R1.tileentities; + +import com.jamesdpeters.minecraft.chests.TileEntityOpener; +import net.minecraft.server.v1_15_R1.BaseBlockPosition; +import net.minecraft.server.v1_15_R1.BlockBarrel; +import net.minecraft.server.v1_15_R1.EntityHuman; +import net.minecraft.server.v1_15_R1.EnumDirection; +import net.minecraft.server.v1_15_R1.IBlockData; +import net.minecraft.server.v1_15_R1.SoundCategory; +import net.minecraft.server.v1_15_R1.SoundEffect; +import net.minecraft.server.v1_15_R1.SoundEffects; +import net.minecraft.server.v1_15_R1.TileEntityBarrel; +import org.bukkit.entity.HumanEntity; + +import java.util.List; + +public class CustomTileEntityBarrel extends TileEntityBarrel implements TileEntityOpener { + + private int phantomViewers = 0; + private List viewers; + + @Override + public List getViewers() { + return viewers; + } + + @Override + public void h() { + //super.h(); + } + + @Override + public void startOpen(EntityHuman entityhuman) { + //Do nothing + } + + @Override + public void closeContainer(EntityHuman entityhuman) { + //do nothing. + } + + @Override + public void setViewers(List viewers){ + int previousViewers = phantomViewers; + phantomViewers = viewers.size(); + this.viewers = viewers; + + if(phantomViewers > 1 && previousViewers == 0) open(); + if(phantomViewers == 0) close(); + } + + public void open(){ + IBlockData iblockdata = this.getBlock(); + a(iblockdata, SoundEffects.BLOCK_BARREL_OPEN); + a(iblockdata, true); + } + + public void close(){ + IBlockData iblockdata = this.getBlock(); + a(iblockdata, SoundEffects.BLOCK_BARREL_CLOSE); + a(iblockdata, false); + } + + private void a(IBlockData iblockdata, boolean flag) { + this.world.setTypeAndData(this.getPosition(), (IBlockData)iblockdata.set(BlockBarrel.b, flag), 3); + } + + private void a(IBlockData iblockdata, SoundEffect soundeffect) { + BaseBlockPosition baseblockposition = ((EnumDirection)iblockdata.get(BlockBarrel.a)).p(); + double d0 = (double)this.position.getX() + 0.5D + (double)baseblockposition.getX() / 2.0D; + double d1 = (double)this.position.getY() + 0.5D + (double)baseblockposition.getY() / 2.0D; + double d2 = (double)this.position.getZ() + 0.5D + (double)baseblockposition.getZ() / 2.0D; + this.world.playSound((EntityHuman)null, d0, d1, d2, soundeffect, SoundCategory.BLOCKS, 0.5F, this.world.random.nextFloat() * 0.1F + 0.9F); + } +} diff --git a/ChestsPlusPlus_1_15/src/main/java/com/jamesdpeters/minecraft/chests/v1_15_R1/CustomTileEntityChest.java b/ChestsPlusPlus_1_15/src/main/java/com/jamesdpeters/minecraft/chests/v1_15_R1/tileentities/CustomTileEntityChest.java similarity index 91% rename from ChestsPlusPlus_1_15/src/main/java/com/jamesdpeters/minecraft/chests/v1_15_R1/CustomTileEntityChest.java rename to ChestsPlusPlus_1_15/src/main/java/com/jamesdpeters/minecraft/chests/v1_15_R1/tileentities/CustomTileEntityChest.java index 5198927..58bfb9a 100644 --- a/ChestsPlusPlus_1_15/src/main/java/com/jamesdpeters/minecraft/chests/v1_15_R1/CustomTileEntityChest.java +++ b/ChestsPlusPlus_1_15/src/main/java/com/jamesdpeters/minecraft/chests/v1_15_R1/tileentities/CustomTileEntityChest.java @@ -1,5 +1,6 @@ -package com.jamesdpeters.minecraft.chests.v1_15_R1; +package com.jamesdpeters.minecraft.chests.v1_15_R1.tileentities; +import com.jamesdpeters.minecraft.chests.TileEntityOpener; import net.minecraft.server.v1_15_R1.Block; import net.minecraft.server.v1_15_R1.BlockChest; import net.minecraft.server.v1_15_R1.BlockPropertyChestType; @@ -13,7 +14,7 @@ import org.bukkit.entity.HumanEntity; import java.util.List; -public class CustomTileEntityChest extends TileEntityChest { +public class CustomTileEntityChest extends TileEntityChest implements TileEntityOpener { private int phantomViewers = 0; private List viewers; diff --git a/ChestsPlusPlus_1_16/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R1/ChestOpener_1_16.java b/ChestsPlusPlus_1_16/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R1/ChestOpener_1_16.java index 0d34102..54f33ab 100644 --- a/ChestsPlusPlus_1_16/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R1/ChestOpener_1_16.java +++ b/ChestsPlusPlus_1_16/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R1/ChestOpener_1_16.java @@ -1,39 +1,50 @@ package com.jamesdpeters.minecraft.chests.v1_16_R1; import com.jamesdpeters.minecraft.chests.ChestOpener; +import com.jamesdpeters.minecraft.chests.TileEntityOpener; +import com.jamesdpeters.minecraft.chests.v1_16_R1.tileentities.CustomTileEntityBarrel; +import com.jamesdpeters.minecraft.chests.v1_16_R1.tileentities.CustomTileEntityChest; import net.minecraft.server.v1_16_R1.BlockPosition; import net.minecraft.server.v1_16_R1.TileEntity; +import net.minecraft.server.v1_16_R1.TileEntityBarrel; import net.minecraft.server.v1_16_R1.TileEntityChest; import net.minecraft.server.v1_16_R1.World; -import org.bukkit.Bukkit; -import org.bukkit.block.Chest; +import org.bukkit.block.Container; import org.bukkit.craftbukkit.v1_16_R1.CraftWorld; -import org.bukkit.craftbukkit.v1_16_R1.block.CraftChest; +import org.bukkit.craftbukkit.v1_16_R1.block.CraftContainer; +import org.bukkit.entity.HumanEntity; import org.bukkit.inventory.Inventory; +import java.util.List; + public class ChestOpener_1_16 implements ChestOpener { @Override - public void setLidOpen(Inventory inventory, Chest chest, boolean open) { - CraftChest craftChest = (CraftChest) chest; - CraftWorld craftWorld = (CraftWorld) craftChest.getWorld(); + public void setLidOpen(Inventory inventory, Container chest, boolean open) { + CraftContainer craftContainer = (CraftContainer) chest; + CraftWorld craftWorld = (CraftWorld) craftContainer.getWorld(); World world = craftWorld.getHandle(); - BlockPosition position = craftChest.getPosition(); + BlockPosition position = craftContainer.getPosition(); TileEntity tileEntity = world.getTileEntity(position); - if(tileEntity instanceof TileEntityChest){ - boolean isCustomTileEntity = tileEntity instanceof CustomTileEntityChest; - CustomTileEntityChest customTileEntityChest; - if(!isCustomTileEntity){ - customTileEntityChest = new CustomTileEntityChest(); - world.removeTileEntity(position); - world.setTileEntity(position, customTileEntityChest); - } else { - customTileEntityChest = (CustomTileEntityChest) tileEntity; + //Checks if Tile Entity has already got custom Opener. + if(tileEntity instanceof TileEntityOpener){ + ((TileEntityOpener) tileEntity).setViewers(inventory.getViewers()); + } else { + //If not set the new tile entity and set the viewers. + if (tileEntity instanceof TileEntityChest) { + setTileEnt(world, position, new CustomTileEntityChest(), inventory.getViewers()); + } else if (tileEntity instanceof TileEntityBarrel) { + setTileEnt(world, position, new CustomTileEntityBarrel(), inventory.getViewers()); } - customTileEntityChest.setViewers(inventory.getViewers()); } } + + private void setTileEnt(World world, BlockPosition position, T tileEntOpener, List viewers){ + world.removeTileEntity(position); + world.setTileEntity(position, tileEntOpener); + tileEntOpener.setViewers(viewers); + } } diff --git a/ChestsPlusPlus_1_16/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R1/tileentities/CustomTileEntityBarrel.java b/ChestsPlusPlus_1_16/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R1/tileentities/CustomTileEntityBarrel.java new file mode 100644 index 0000000..ce0f250 --- /dev/null +++ b/ChestsPlusPlus_1_16/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R1/tileentities/CustomTileEntityBarrel.java @@ -0,0 +1,75 @@ +package com.jamesdpeters.minecraft.chests.v1_16_R1.tileentities; + +import com.jamesdpeters.minecraft.chests.TileEntityOpener; +import net.minecraft.server.v1_16_R1.BaseBlockPosition; +import net.minecraft.server.v1_16_R1.BlockBarrel; +import net.minecraft.server.v1_16_R1.EntityHuman; +import net.minecraft.server.v1_16_R1.EnumDirection; +import net.minecraft.server.v1_16_R1.IBlockData; +import net.minecraft.server.v1_16_R1.SoundCategory; +import net.minecraft.server.v1_16_R1.SoundEffect; +import net.minecraft.server.v1_16_R1.SoundEffects; +import net.minecraft.server.v1_16_R1.TileEntityBarrel; +import org.bukkit.entity.HumanEntity; + +import java.util.List; + +public class CustomTileEntityBarrel extends TileEntityBarrel implements TileEntityOpener { + + private int phantomViewers = 0; + private List viewers; + + @Override + public List getViewers() { + return viewers; + } + + @Override + public void h() { + //super.h(); + } + + @Override + public void startOpen(EntityHuman entityhuman) { + //Do nothing + } + + @Override + public void closeContainer(EntityHuman entityhuman) { + //do nothing. + } + + @Override + public void setViewers(List viewers){ + int previousViewers = phantomViewers; + phantomViewers = viewers.size(); + this.viewers = viewers; + + if(phantomViewers > 1 && previousViewers == 0) open(); + if(phantomViewers == 0) close(); + } + + public void open(){ + IBlockData iblockdata = this.getBlock(); + a(iblockdata, SoundEffects.BLOCK_BARREL_OPEN); + a(iblockdata, true); + } + + public void close(){ + IBlockData iblockdata = this.getBlock(); + a(iblockdata, SoundEffects.BLOCK_BARREL_CLOSE); + a(iblockdata, false); + } + + private void a(IBlockData iblockdata, boolean flag) { + this.world.setTypeAndData(this.getPosition(), (IBlockData)iblockdata.set(BlockBarrel.b, flag), 3); + } + + private void a(IBlockData iblockdata, SoundEffect soundeffect) { + BaseBlockPosition baseblockposition = ((EnumDirection)iblockdata.get(BlockBarrel.a)).p(); + double d0 = (double)this.position.getX() + 0.5D + (double)baseblockposition.getX() / 2.0D; + double d1 = (double)this.position.getY() + 0.5D + (double)baseblockposition.getY() / 2.0D; + double d2 = (double)this.position.getZ() + 0.5D + (double)baseblockposition.getZ() / 2.0D; + this.world.playSound((EntityHuman)null, d0, d1, d2, soundeffect, SoundCategory.BLOCKS, 0.5F, this.world.random.nextFloat() * 0.1F + 0.9F); + } +} diff --git a/ChestsPlusPlus_1_16/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R1/CustomTileEntityChest.java b/ChestsPlusPlus_1_16/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R1/tileentities/CustomTileEntityChest.java similarity index 91% rename from ChestsPlusPlus_1_16/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R1/CustomTileEntityChest.java rename to ChestsPlusPlus_1_16/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R1/tileentities/CustomTileEntityChest.java index 1d4be0c..30a3b53 100644 --- a/ChestsPlusPlus_1_16/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R1/CustomTileEntityChest.java +++ b/ChestsPlusPlus_1_16/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R1/tileentities/CustomTileEntityChest.java @@ -1,5 +1,6 @@ -package com.jamesdpeters.minecraft.chests.v1_16_R1; +package com.jamesdpeters.minecraft.chests.v1_16_R1.tileentities; +import com.jamesdpeters.minecraft.chests.TileEntityOpener; import net.minecraft.server.v1_16_R1.Block; import net.minecraft.server.v1_16_R1.BlockChest; import net.minecraft.server.v1_16_R1.BlockPropertyChestType; @@ -16,7 +17,7 @@ import org.bukkit.entity.HumanEntity; import java.util.List; -public class CustomTileEntityChest extends TileEntityChest { +public class CustomTileEntityChest extends TileEntityChest implements TileEntityOpener { private int phantomViewers = 0; private List viewers; @@ -40,6 +41,7 @@ public class CustomTileEntityChest extends TileEntityChest { } } + @Override public void setViewers(List viewers){ int previousViewers = phantomViewers; phantomViewers = viewers.size(); diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/listeners/InventoryListener.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/listeners/InventoryListener.java index ce8fb2e..e333f3f 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/listeners/InventoryListener.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/listeners/InventoryListener.java @@ -15,6 +15,7 @@ import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.block.Block; import org.bukkit.block.Chest; +import org.bukkit.block.Container; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -75,8 +76,8 @@ public class InventoryListener implements Listener { event.getViewers().remove(event.getPlayer()); vHolder.getStorage().getLocations().forEach(locationInfo -> { Block block = locationInfo.getLocation().getBlock(); - if(block.getState() instanceof Chest){ - Chest chest = (Chest) block.getState(); + if(block.getState() instanceof Container){ + Container chest = (Container) block.getState(); Bukkit.getScheduler().scheduleSyncDelayedTask(ChestsPlusPlus.PLUGIN, () -> ApiSpecific.getChestOpener().setLidOpen(event.getInventory(),chest,false),1); } }); diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/misc/Utils.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/misc/Utils.java index 0e88f30..7301897 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/misc/Utils.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/misc/Utils.java @@ -10,11 +10,14 @@ import org.bukkit.*; import org.bukkit.block.*; import org.bukkit.entity.ItemFrame; import org.bukkit.entity.Player; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; import org.bukkit.event.inventory.InventoryType; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.persistence.PersistentDataType; +import org.bukkit.plugin.RegisteredListener; import org.bukkit.util.Vector; import java.util.*; @@ -26,10 +29,10 @@ public class Utils { //Check if all chests should perform open animation. if(Settings.isShouldAnimateAllChests()) { storage.getLocations().forEach(locationInfo -> { - int chunkX = locationInfo.getLocation().getBlockX() >> 4; - int chunkZ = locationInfo.getLocation().getBlockZ() >> 4; Location location = locationInfo.getLocation(); if (location != null) { + int chunkX = locationInfo.getLocation().getBlockX() >> 4; + int chunkZ = locationInfo.getLocation().getBlockZ() >> 4; World world = location.getWorld(); if (world != null && world.isChunkLoaded(chunkX, chunkZ)) { chestOpenAnimation(storage.getInventory(), locationInfo.getLocation()); @@ -45,9 +48,8 @@ public class Utils { private static void chestOpenAnimation(Inventory inventory, Location location){ if (location != null) { Block block = location.getBlock(); - if (block.getState() instanceof Chest) { - Chest chest = (Chest) block.getState(); -// ApiSpecific.getChestOpener().setLidOpen(inventory, chest, true); + if (block.getState() instanceof Container) { + Container chest = (Container) block.getState(); Bukkit.getScheduler().scheduleSyncDelayedTask(ChestsPlusPlus.PLUGIN,() -> ApiSpecific.getChestOpener().setLidOpen(inventory, chest, true),1); } } diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/abstracts/AbstractStorage.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/abstracts/AbstractStorage.java index 5c464f1..8fbaeb9 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/abstracts/AbstractStorage.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/abstracts/AbstractStorage.java @@ -192,7 +192,7 @@ public abstract class AbstractStorage implements ConfigurationSerializable { * This is the distance from a full block to the size of the storage block. (e.g Chest is smaller than a regular block.) * @return */ - public abstract double getBlockOffset(); + public abstract double getBlockOffset(Block block); /** * This is called when a block is added to the storage system. @@ -417,6 +417,7 @@ public abstract class AbstractStorage implements ConfigurationSerializable { if(world != null) { if(location.getSignLocation() == null) return; + Block storageBlock = location.getLocation().getBlock(); Block anchor = location.getSignLocation().getBlock(); BlockFace facing; if(anchor.getBlockData() instanceof Directional){ @@ -428,7 +429,7 @@ public abstract class AbstractStorage implements ConfigurationSerializable { boolean isBlock = !ApiSpecific.getMaterialChecker().isGraphically2D(displayItem); boolean isTool = ApiSpecific.getMaterialChecker().isTool(displayItem); - Location standLoc = isTool ? getHeldItemArmorStandLoc(anchor, facing) : getArmorStandLoc(anchor, facing, isBlock); + Location standLoc = isTool ? getHeldItemArmorStandLoc(storageBlock,anchor, facing) : getArmorStandLoc(storageBlock, anchor, facing, isBlock); //Get currently stored armorStand if there isn't one spawn it. ArmorStand stand = isTool ? location.getToolItemStand() : (isBlock ? location.getBlockStand() : location.getItemStand()); @@ -494,29 +495,29 @@ public abstract class AbstractStorage implements ConfigurationSerializable { * @param isBlock - true if the @{@link ItemStack} is a Block / false if an Item. * @return the calculated location for the @{@link ArmorStand} */ - private Location getArmorStandLoc(Block anchor, BlockFace facing, boolean isBlock){ + private Location getArmorStandLoc(Block storageBlock, Block anchor, BlockFace facing, boolean isBlock){ double directionFactor = isBlock ? 0.65 : 0.275; double perpendicularFactor = isBlock ? 0.025 : 0.125; double y = isBlock ? -0.3 : 0.1; float yaw = 180; - return getArmorStandLoc(anchor, facing, directionFactor, perpendicularFactor, y, yaw); + return getArmorStandLoc(storageBlock, anchor, facing, directionFactor, perpendicularFactor, y, yaw); } - private Location getHeldItemArmorStandLoc(Block anchor, BlockFace facing){ + private Location getHeldItemArmorStandLoc(Block storageBlock, Block anchor, BlockFace facing){ double directionFactor = 0.36; double perpendicularFactor = 0; double y = 0.275; float yaw = -90; - return getArmorStandLoc(anchor, facing, directionFactor, perpendicularFactor, y, yaw); + return getArmorStandLoc(storageBlock, anchor, facing, directionFactor, perpendicularFactor, y, yaw); } - private Location getArmorStandLoc(Block anchor, BlockFace facing, double directionFactor, double perpendicularFactor, double y, float yaw){ + private Location getArmorStandLoc(Block storageBlock, Block anchor, BlockFace facing, double directionFactor, double perpendicularFactor, double y, float yaw){ //Get centre of block location. Location standLoc = anchor.getLocation().add(0.5,-0.5,0.5); Vector direction = facing.getDirection(); - directionFactor = directionFactor + getBlockOffset(); + directionFactor = directionFactor + getBlockOffset(storageBlock); double x = directionFactor*direction.getX() - perpendicularFactor*direction.getZ(); double z = directionFactor*direction.getZ() + perpendicularFactor*direction.getX(); diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/autocraft/AutoCraftingStorage.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/autocraft/AutoCraftingStorage.java index baf4a4c..dcf2244 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/autocraft/AutoCraftingStorage.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/autocraft/AutoCraftingStorage.java @@ -83,7 +83,7 @@ public class AutoCraftingStorage extends AbstractStorage implements Configuratio } @Override - public double getBlockOffset() { + public double getBlockOffset(Block block) { return -0.07; } diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/chestlink/ChestLinkStorage.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/chestlink/ChestLinkStorage.java index 1a103f2..72f71f2 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/chestlink/ChestLinkStorage.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/chestlink/ChestLinkStorage.java @@ -17,11 +17,14 @@ import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.OfflinePlayer; +import org.bukkit.block.Barrel; import org.bukkit.block.Block; import org.bukkit.block.Chest; +import org.bukkit.block.Container; import org.bukkit.configuration.serialization.ConfigurationSerializable; import org.bukkit.configuration.serialization.SerializableAs; import org.bukkit.entity.Player; +import org.bukkit.event.inventory.InventoryMoveItemEvent; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.ItemStack; @@ -47,10 +50,10 @@ public class ChestLinkStorage extends AbstractStorage implements ConfigurationSe this.sortMethod = SortMethod.OFF; Block block = location.getBlock(); - if(block.getState() instanceof Chest){ - Chest chest = (Chest) block.getState(); - getInventory().setContents(chest.getInventory().getContents()); - chest.getInventory().clear(); + if(block.getState() instanceof Container) { + Container container = (Container) block.getState(); + getInventory().setContents(container.getInventory().getContents()); + container.getInventory().clear(); updateDisplayItem(); } init(); @@ -101,8 +104,8 @@ public class ChestLinkStorage extends AbstractStorage implements ConfigurationSe @Override public void onStorageAdded(Block block, Player player) { //Migrates that chest into InventoryStorage and if full drops it at the chest location. - if(block.getState() instanceof Chest) { - Chest chest = (Chest) block.getState(); + if(block.getState() instanceof Container) { + Container chest = (Container) block.getState(); boolean hasOverflow = false; for (ItemStack chestItem : chest.getInventory().getContents()) { if (chestItem != null) { @@ -205,8 +208,10 @@ public class ChestLinkStorage extends AbstractStorage implements ConfigurationSe } @Override - public double getBlockOffset() { - return 0; + public double getBlockOffset(Block block) { + if(block.getState() instanceof Chest) return 0; + //Barrel is full block. + else return -0.07; } @Override diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/chestlink/ChestLinkStorageType.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/chestlink/ChestLinkStorageType.java index e077f4c..e673275 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/chestlink/ChestLinkStorageType.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/chestlink/ChestLinkStorageType.java @@ -10,9 +10,11 @@ import com.jamesdpeters.minecraft.chests.storage.abstracts.StorageType; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.OfflinePlayer; +import org.bukkit.block.Barrel; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.block.Chest; +import org.bukkit.block.Container; import org.bukkit.block.data.Directional; import org.bukkit.entity.Player; @@ -38,13 +40,13 @@ public class ChestLinkStorageType extends StorageType { @Override public boolean isValidBlockType(Block block) { - return (block.getState() instanceof Chest); + return (block.getState() instanceof Chest) || (block.getState() instanceof Barrel); } @Override public void onSignRemoval(Block block) { - if(block.getState() instanceof Chest){ - ((Chest) block.getState()).getInventory().clear(); + if(block.getState() instanceof Container){ + ((Container) block.getState()).getInventory().clear(); } } @@ -55,14 +57,18 @@ public class ChestLinkStorageType extends StorageType { @Override public void createStorage(Player player, OfflinePlayer owner, Block block, String identifier, boolean requireSign) { - if(block.getState() instanceof Chest){ + if(block.getState() instanceof Chest) { new ChestLinkVerifier(block).withDelay(0).check(); - if(block.getBlockData() instanceof Directional) { - Directional chest = (Directional) block.getBlockData(); - BlockFace facing = chest.getFacing(); - Block toReplace = block.getRelative(facing); - placeSign(block,toReplace,facing,player,owner,identifier,Values.ChestLinkTag, requireSign); - } + } + createStorageForBlock(player, owner, block, identifier, requireSign); + } + + private void createStorageForBlock(Player player, OfflinePlayer owner, Block block, String identifier, boolean requireSign){ + if(block.getBlockData() instanceof Directional) { + Directional chest = (Directional) block.getBlockData(); + BlockFace facing = chest.getFacing(); + Block toReplace = block.getRelative(facing); + placeSign(block,toReplace,facing,player,owner,identifier,Values.ChestLinkTag, requireSign); } }