Chest Opener Fix

Fixed custom chests that weren't opening, and playing close sounds too early.
This commit is contained in:
jameslfc19 2020-07-10 14:10:21 +01:00
parent 8fa5a1c781
commit 5ae0cf664c
10 changed files with 92 additions and 68 deletions

View File

@ -1,7 +1,8 @@
package com.jamesdpeters.minecraft.chests;
import org.bukkit.block.Chest;
import org.bukkit.inventory.Inventory;
public interface ChestOpener {
void setLidOpen(Chest chest, boolean open);
void setLidOpen(Inventory inventory, Chest chest, boolean open);
}

View File

@ -8,11 +8,12 @@ import net.minecraft.server.v1_14_R1.World;
import org.bukkit.block.Chest;
import org.bukkit.craftbukkit.v1_14_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_14_R1.block.CraftChest;
import org.bukkit.inventory.Inventory;
public class ChestOpener_1_14 implements ChestOpener {
@Override
public void setLidOpen(Chest chest, boolean open) {
public void setLidOpen(Inventory inventory, Chest chest, boolean open) {
CraftChest craftChest = (CraftChest) chest;
CraftWorld craftWorld = (CraftWorld) craftChest.getWorld();
World world = craftWorld.getHandle();
@ -20,6 +21,7 @@ public class ChestOpener_1_14 implements ChestOpener {
TileEntity tileEntity = world.getTileEntity(position);
if(tileEntity instanceof TileEntityChest){
boolean isCustomTileEntity = tileEntity instanceof CustomTileEntityChest;
CustomTileEntityChest customTileEntityChest;
if(!isCustomTileEntity){
@ -29,8 +31,7 @@ public class ChestOpener_1_14 implements ChestOpener {
} else {
customTileEntityChest = (CustomTileEntityChest) tileEntity;
}
customTileEntityChest.setOpen(open);
customTileEntityChest.animate();
customTileEntityChest.setViewers(inventory.getViewers());
}
}
}

View File

@ -3,45 +3,48 @@ package com.jamesdpeters.minecraft.chests.v1_14_R1;
import net.minecraft.server.v1_14_R1.Block;
import net.minecraft.server.v1_14_R1.BlockChest;
import net.minecraft.server.v1_14_R1.BlockPropertyChestType;
import net.minecraft.server.v1_14_R1.EntityHuman;
import net.minecraft.server.v1_14_R1.EnumDirection;
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.TileEntityChest;
import org.bukkit.entity.HumanEntity;
import java.util.List;
public class CustomTileEntityChest extends TileEntityChest {
private int phantomViewers = 0;
private int previousViewers = 0;
private List<HumanEntity> viewers;
@Override
public List<HumanEntity> getViewers() {
return viewers;
}
@Override
public void tick() {
//Don't need to tick
//Do nothing.
}
@Override
public void onOpen() {
//Do nothing
}
public void animate(){
protected void onOpen() {
Block block = this.getBlock().getBlock();
if (block instanceof BlockChest) {
this.world.playBlockAction(this.position, block, 1, phantomViewers);
this.world.playBlockAction(this.position, block, 1, viewers.size());
this.world.applyPhysics(this.position, block);
//Play block sound.
if(phantomViewers == 1 && previousViewers == 0) this.a(SoundEffects.BLOCK_CHEST_OPEN);
if(phantomViewers == 0) this.a(SoundEffects.BLOCK_CHEST_CLOSE);
}
}
public void setOpen(boolean open){
previousViewers = phantomViewers;
if(open) phantomViewers++;
else phantomViewers--;
if(phantomViewers < 0) phantomViewers = 0;
public void setViewers(List<HumanEntity> viewers){
int previousViewers = phantomViewers;
phantomViewers = viewers.size();
this.viewers = viewers;
if(phantomViewers > 1 && previousViewers == 0) this.a(SoundEffects.BLOCK_CHEST_OPEN);
if(phantomViewers == 0) this.a(SoundEffects.BLOCK_CHEST_CLOSE);
onOpen();
}
private void a(SoundEffect soundeffect) {

View File

@ -8,11 +8,12 @@ import net.minecraft.server.v1_15_R1.World;
import org.bukkit.block.Chest;
import org.bukkit.craftbukkit.v1_15_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_15_R1.block.CraftChest;
import org.bukkit.inventory.Inventory;
public class ChestOpener_1_15 implements ChestOpener {
@Override
public void setLidOpen(Chest chest, boolean open) {
public void setLidOpen(Inventory inventory, Chest chest, boolean open) {
CraftChest craftChest = (CraftChest) chest;
CraftWorld craftWorld = (CraftWorld) craftChest.getWorld();
World world = craftWorld.getHandle();
@ -20,6 +21,7 @@ public class ChestOpener_1_15 implements ChestOpener {
TileEntity tileEntity = world.getTileEntity(position);
if(tileEntity instanceof TileEntityChest){
boolean isCustomTileEntity = tileEntity instanceof CustomTileEntityChest;
CustomTileEntityChest customTileEntityChest;
if(!isCustomTileEntity){
@ -29,8 +31,7 @@ public class ChestOpener_1_15 implements ChestOpener {
} else {
customTileEntityChest = (CustomTileEntityChest) tileEntity;
}
customTileEntityChest.setOpen(open);
customTileEntityChest.animate();
customTileEntityChest.setViewers(inventory.getViewers());
}
}
}

View File

@ -9,39 +9,43 @@ 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.TileEntityChest;
import org.bukkit.entity.HumanEntity;
import java.util.List;
public class CustomTileEntityChest extends TileEntityChest {
private int phantomViewers = 0;
private int previousViewers = 0;
private List<HumanEntity> viewers;
@Override
public List<HumanEntity> getViewers() {
return viewers;
}
@Override
public void tick() {
//Don't need to tick
//Do nothing.
}
@Override
public void onOpen() {
//Do nothing
}
public void animate(){
Block block = this.getBlock().getBlock();
if (block instanceof BlockChest) {
this.world.playBlockAction(this.position, block, 1, phantomViewers);
this.world.playBlockAction(this.position, block, 1, viewers.size());
this.world.applyPhysics(this.position, block);
//Play block sound.
if(phantomViewers == 1 && previousViewers == 0) this.a(SoundEffects.BLOCK_CHEST_OPEN);
if(phantomViewers == 0) this.a(SoundEffects.BLOCK_CHEST_CLOSE);
}
}
public void setOpen(boolean open){
previousViewers = phantomViewers;
if(open) phantomViewers++;
else phantomViewers--;
if(phantomViewers < 0) phantomViewers = 0;
public void setViewers(List<HumanEntity> viewers){
int previousViewers = phantomViewers;
phantomViewers = viewers.size();
this.viewers = viewers;
if(phantomViewers > 1 && previousViewers == 0) this.a(SoundEffects.BLOCK_CHEST_OPEN);
if(phantomViewers == 0) this.a(SoundEffects.BLOCK_CHEST_CLOSE);
onOpen();
}
private void a(SoundEffect soundeffect) {

View File

@ -5,15 +5,17 @@ import net.minecraft.server.v1_16_R1.BlockPosition;
import net.minecraft.server.v1_16_R1.TileEntity;
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.craftbukkit.v1_16_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_16_R1.block.CraftChest;
import org.bukkit.inventory.Inventory;
public class ChestOpener_1_16 implements ChestOpener {
@Override
public void setLidOpen(Chest chest, boolean open) {
public void setLidOpen(Inventory inventory, Chest chest, boolean open) {
CraftChest craftChest = (CraftChest) chest;
CraftWorld craftWorld = (CraftWorld) craftChest.getWorld();
World world = craftWorld.getHandle();
@ -21,6 +23,7 @@ public class ChestOpener_1_16 implements ChestOpener {
TileEntity tileEntity = world.getTileEntity(position);
if(tileEntity instanceof TileEntityChest){
boolean isCustomTileEntity = tileEntity instanceof CustomTileEntityChest;
CustomTileEntityChest customTileEntityChest;
if(!isCustomTileEntity){
@ -30,8 +33,7 @@ public class ChestOpener_1_16 implements ChestOpener {
} else {
customTileEntityChest = (CustomTileEntityChest) tileEntity;
}
customTileEntityChest.setOpen(open);
customTileEntityChest.animate();
customTileEntityChest.setViewers(inventory.getViewers());
}
}
}

View File

@ -3,6 +3,7 @@ package com.jamesdpeters.minecraft.chests.v1_16_R1;
import net.minecraft.server.v1_16_R1.Block;
import net.minecraft.server.v1_16_R1.BlockChest;
import net.minecraft.server.v1_16_R1.BlockPropertyChestType;
import net.minecraft.server.v1_16_R1.Blocks;
import net.minecraft.server.v1_16_R1.EntityHuman;
import net.minecraft.server.v1_16_R1.EnumDirection;
import net.minecraft.server.v1_16_R1.SoundCategory;
@ -10,40 +11,44 @@ import net.minecraft.server.v1_16_R1.SoundEffect;
import net.minecraft.server.v1_16_R1.SoundEffects;
import net.minecraft.server.v1_16_R1.TileEntityChest;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.v1_16_R1.event.CraftEventFactory;
import org.bukkit.entity.HumanEntity;
import java.util.List;
public class CustomTileEntityChest extends TileEntityChest {
private int phantomViewers = 0;
private int previousViewers = 0;
private List<HumanEntity> viewers;
@Override
public List<HumanEntity> getViewers() {
return viewers;
}
@Override
public void tick() {
//Don't need to tick
//Do nothing.
}
@Override
protected void onOpen() {
//Do nothing
}
public void animate(){
Block block = this.getBlock().getBlock();
if (block instanceof BlockChest) {
this.world.playBlockAction(this.position, block, 1, phantomViewers);
this.world.playBlockAction(this.position, block, 1, viewers.size());
this.world.applyPhysics(this.position, block);
//Play block sound.
Bukkit.broadcastMessage("Viewers: "+phantomViewers+" Prev: "+previousViewers);
if(phantomViewers == 1 && previousViewers == 0) this.a(SoundEffects.BLOCK_CHEST_OPEN);
if(phantomViewers == 0) this.a(SoundEffects.BLOCK_CHEST_CLOSE);
}
}
public void setOpen(boolean open){
previousViewers = phantomViewers;
if(open) phantomViewers++;
else phantomViewers--;
if(phantomViewers < 0) phantomViewers = 0;
public void setViewers(List<HumanEntity> viewers){
int previousViewers = phantomViewers;
phantomViewers = viewers.size();
this.viewers = viewers;
if(phantomViewers > 1 && previousViewers == 0) this.a(SoundEffects.BLOCK_CHEST_OPEN);
if(phantomViewers == 0) this.a(SoundEffects.BLOCK_CHEST_CLOSE);
onOpen();
}
private void a(SoundEffect soundeffect) {
@ -62,4 +67,6 @@ public class CustomTileEntityChest extends TileEntityChest {
}
}
}

View File

@ -8,7 +8,7 @@ import com.jamesdpeters.minecraft.chests.v1_16_R1.MaterialChecker_1_16;
public class NMSProviderDefault implements NMSProvider {
@Override
public ChestOpener getChestOpener() {
return (chest, open) -> {
return (storage, chest, open) -> {
//Default to doing nothing.
};
}

View File

@ -1,5 +1,6 @@
package com.jamesdpeters.minecraft.chests.misc;
import com.jamesdpeters.minecraft.chests.ChestsPlusPlus;
import com.jamesdpeters.minecraft.chests.api.ApiSpecific;
import com.jamesdpeters.minecraft.chests.filters.Filter;
import com.jamesdpeters.minecraft.chests.filters.HopperFilter;
@ -24,21 +25,22 @@ public class Utils {
if(Settings.isShouldAnimateAllChests()) {
storage.getLocations().forEach(locationInfo -> {
if (locationInfo.getLocation() != null) {
chestOpenAnimation(locationInfo.getLocation());
chestOpenAnimation(storage.getInventory(), locationInfo.getLocation());
}
});
} else {
chestOpenAnimation(openedChestLocation);
chestOpenAnimation(storage.getInventory(), openedChestLocation);
}
player.openInventory(storage.getInventory());
}
private static void chestOpenAnimation(Location location){
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(chest, true);
// ApiSpecific.getChestOpener().setLidOpen(inventory, chest, true);
Bukkit.getScheduler().scheduleSyncDelayedTask(ChestsPlusPlus.PLUGIN,() -> ApiSpecific.getChestOpener().setLidOpen(inventory, chest, true),1);
}
}
}
@ -76,6 +78,7 @@ public class Utils {
HashMap<Integer, ItemStack> leftOvers = to.addItem(removed);
for (ItemStack leftOver : leftOvers.values()) {
from.addItem(leftOver);
if(removed.equals(leftOver)) return false;
}
return true;
}

View File

@ -4,9 +4,9 @@ import com.jamesdpeters.minecraft.chests.ChestsPlusPlus;
import com.jamesdpeters.minecraft.chests.filters.HopperFilter;
import com.jamesdpeters.minecraft.chests.misc.Settings;
import com.jamesdpeters.minecraft.chests.misc.Utils;
import com.jamesdpeters.minecraft.chests.storage.chestlink.ChestLinkStorage;
import com.jamesdpeters.minecraft.chests.serialize.LocationInfo;
import com.jamesdpeters.minecraft.chests.serialize.SpigotConfig;
import com.jamesdpeters.minecraft.chests.storage.chestlink.ChestLinkStorage;
import org.bukkit.Location;
import org.bukkit.block.Hopper;
import org.bukkit.scheduler.BukkitRunnable;
@ -42,7 +42,9 @@ public class VirtualChestToHopper extends BukkitRunnable {
continue;
}
int hopperAmount = SpigotConfig.getWorldSettings(location.getLocation().getWorld().getName()).getHopperAmount();
Utils.moveToOtherInventory(storage.getInventory(), hopperAmount, hopper.getInventory(), HopperFilter.getHopperFilters(below.getBlock()));
if(Utils.moveToOtherInventory(storage.getInventory(), hopperAmount, hopper.getInventory(), HopperFilter.getHopperFilters(below.getBlock()))){
storage.updateDisplayItem();
}
if(storage.getInventory().getViewers().size() > 0) storage.sort();
}
}