Skip to content

Commit

Permalink
Call InventoryOpenEvent when using SilentChest
Browse files Browse the repository at this point in the history
Backport fix for incorrect parameter as well, just in case.
Fixed 1.7 and lower forcing all chests' default names to be "Large chest" instead of the translatable string.
  • Loading branch information
Jikoo committed Jan 10, 2017
1 parent ef48603 commit 2f7d3bc
Show file tree
Hide file tree
Showing 19 changed files with 650 additions and 453 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
// Volatile
import net.minecraft.server.v1_4_5.AxisAlignedBB;
import net.minecraft.server.v1_4_5.BlockEnderChest;
import net.minecraft.server.v1_4_5.Container;
import net.minecraft.server.v1_4_5.EntityOcelot;
import net.minecraft.server.v1_4_5.EntityPlayer;
import net.minecraft.server.v1_4_5.IInventory;
Expand All @@ -36,6 +37,7 @@
import net.minecraft.server.v1_4_5.World;

import org.bukkit.craftbukkit.v1_4_5.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_4_5.event.CraftEventFactory;

public class AnySilentContainer implements IAnySilentContainer {

Expand Down Expand Up @@ -122,37 +124,47 @@ public boolean activateContainer(Player p, boolean silentchest, org.bukkit.block
return false;
}

IInventory inventory = (IInventory) tile;
int id = world.getTypeId(block.getX(), block.getY(), block.getZ());

if (world.getTypeId(block.getX(), block.getY(), block.getZ() + 1) == id) {
tile = new InventoryLargeChest("Large chest", (IInventory) tile, (TileEntityChest) world.getTileEntity(block.getX(), block.getY(), block.getZ() + 1));
inventory = new InventoryLargeChest("container.chestDouble", inventory, (TileEntityChest) world.getTileEntity(block.getX(), block.getY(), block.getZ() + 1));
} else if (world.getTypeId(block.getX(), block.getY(), block.getZ() - 1) == id) {
tile = new InventoryLargeChest("Large chest", (TileEntityChest) world.getTileEntity(block.getX(), block.getY(), block.getZ() - 1), (IInventory) tile);
inventory = new InventoryLargeChest("container.chestDouble", (TileEntityChest) world.getTileEntity(block.getX(), block.getY(), block.getZ() - 1), inventory);
} else if (world.getTypeId(block.getX() + 1, block.getY(), block.getZ()) == id) {
tile = new InventoryLargeChest("Large chest", (IInventory) tile, (TileEntityChest) world.getTileEntity(block.getX() + 1, block.getY(), block.getZ()));
inventory = new InventoryLargeChest("container.chestDouble", inventory, (TileEntityChest) world.getTileEntity(block.getX() + 1, block.getY(), block.getZ()));
} else if (world.getTypeId(block.getX() - 1, block.getY(), block.getZ()) == id) {
tile = new InventoryLargeChest("Large chest", (TileEntityChest) world.getTileEntity(block.getX() - 1, block.getY(), block.getZ()), (IInventory) tile);
inventory = new InventoryLargeChest("container.chestDouble", (TileEntityChest) world.getTileEntity(block.getX() - 1, block.getY(), block.getZ()), inventory);
}

boolean returnValue = false;
// AnyChest only
if (!silentchest) {
player.openContainer((IInventory) tile);
returnValue = true;
} else {
try {
int windowId = player.nextContainerCounter();
player.netServerHandler.sendPacket(new Packet100OpenWindow(windowId, 0, ((IInventory) tile).getName(), ((IInventory) tile).getSize()));
player.activeContainer = new SilentContainerChest(player.inventory, ((IInventory) tile));
player.activeContainer.windowId = windowId;
player.activeContainer.addSlotListener(player);
returnValue = true;
} catch (Exception e) {
e.printStackTrace();
p.sendMessage(ChatColor.RED + "Error while sending silent chest.");
}
player.openContainer(inventory);
return true;
}

return returnValue;
// SilentChest
try {
// Call InventoryOpenEvent
Container container = new SilentContainerChest(player.inventory, inventory);
container = CraftEventFactory.callInventoryOpenEvent(player, container);
if (container == null) {
return false;
}

// Open window
int windowId = player.nextContainerCounter();
player.netServerHandler.sendPacket(new Packet100OpenWindow(windowId, 0, inventory.getName(), inventory.getSize()));
player.activeContainer = container;
player.activeContainer.windowId = windowId;
player.activeContainer.addSlotListener(player);

return true;
} catch (Exception e) {
e.printStackTrace();
p.sendMessage(ChatColor.RED + "Error while sending silent container.");
return false;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
// Volatile
import net.minecraft.server.v1_4_6.AxisAlignedBB;
import net.minecraft.server.v1_4_6.BlockEnderChest;
import net.minecraft.server.v1_4_6.Container;
import net.minecraft.server.v1_4_6.EntityOcelot;
import net.minecraft.server.v1_4_6.EntityPlayer;
import net.minecraft.server.v1_4_6.IInventory;
Expand All @@ -36,6 +37,7 @@
import net.minecraft.server.v1_4_6.World;

import org.bukkit.craftbukkit.v1_4_6.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_4_6.event.CraftEventFactory;

public class AnySilentContainer implements IAnySilentContainer {

Expand Down Expand Up @@ -122,37 +124,47 @@ public boolean activateContainer(Player p, boolean silentchest, org.bukkit.block
return false;
}

IInventory inventory = (IInventory) tile;
int id = world.getTypeId(block.getX(), block.getY(), block.getZ());

if (world.getTypeId(block.getX(), block.getY(), block.getZ() + 1) == id) {
tile = new InventoryLargeChest("Large chest", (IInventory) tile, (TileEntityChest) world.getTileEntity(block.getX(), block.getY(), block.getZ() + 1));
inventory = new InventoryLargeChest("container.chestDouble", inventory, (TileEntityChest) world.getTileEntity(block.getX(), block.getY(), block.getZ() + 1));
} else if (world.getTypeId(block.getX(), block.getY(), block.getZ() - 1) == id) {
tile = new InventoryLargeChest("Large chest", (TileEntityChest) world.getTileEntity(block.getX(), block.getY(), block.getZ() - 1), (IInventory) tile);
inventory = new InventoryLargeChest("container.chestDouble", (TileEntityChest) world.getTileEntity(block.getX(), block.getY(), block.getZ() - 1), inventory);
} else if (world.getTypeId(block.getX() + 1, block.getY(), block.getZ()) == id) {
tile = new InventoryLargeChest("Large chest", (IInventory) tile, (TileEntityChest) world.getTileEntity(block.getX() + 1, block.getY(), block.getZ()));
inventory = new InventoryLargeChest("container.chestDouble", inventory, (TileEntityChest) world.getTileEntity(block.getX() + 1, block.getY(), block.getZ()));
} else if (world.getTypeId(block.getX() - 1, block.getY(), block.getZ()) == id) {
tile = new InventoryLargeChest("Large chest", (TileEntityChest) world.getTileEntity(block.getX() - 1, block.getY(), block.getZ()), (IInventory) tile);
inventory = new InventoryLargeChest("container.chestDouble", (TileEntityChest) world.getTileEntity(block.getX() - 1, block.getY(), block.getZ()), inventory);
}

boolean returnValue = false;
// AnyChest only
if (!silentchest) {
player.openContainer((IInventory) tile);
returnValue = true;
} else {
try {
int windowId = player.nextContainerCounter();
player.playerConnection.sendPacket(new Packet100OpenWindow(windowId, 0, ((IInventory) tile).getName(), ((IInventory) tile).getSize()));
player.activeContainer = new SilentContainerChest(player.inventory, ((IInventory) tile));
player.activeContainer.windowId = windowId;
player.activeContainer.addSlotListener(player);
returnValue = true;
} catch (Exception e) {
e.printStackTrace();
p.sendMessage(ChatColor.RED + "Error while sending silent chest.");
}
player.openContainer(inventory);
return true;
}

return returnValue;
// SilentChest
try {
// Call InventoryOpenEvent
Container container = new SilentContainerChest(player.inventory, inventory);
container = CraftEventFactory.callInventoryOpenEvent(player, container);
if (container == null) {
return false;
}

// Open window
int windowId = player.nextContainerCounter();
player.playerConnection.sendPacket(new Packet100OpenWindow(windowId, 0, inventory.getName(), inventory.getSize()));
player.activeContainer = container;
player.activeContainer.windowId = windowId;
player.activeContainer.addSlotListener(player);

return true;
} catch (Exception e) {
e.printStackTrace();
p.sendMessage(ChatColor.RED + "Error while sending silent container.");
return false;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,13 @@
import net.minecraft.server.v1_10_R1.AxisAlignedBB;
import net.minecraft.server.v1_10_R1.Block;
import net.minecraft.server.v1_10_R1.BlockChest;
import net.minecraft.server.v1_10_R1.BlockChest.Type;
import net.minecraft.server.v1_10_R1.BlockEnderChest;
import net.minecraft.server.v1_10_R1.BlockPosition;
import net.minecraft.server.v1_10_R1.Container;
import net.minecraft.server.v1_10_R1.Entity;
import net.minecraft.server.v1_10_R1.EntityOcelot;
import net.minecraft.server.v1_10_R1.EntityPlayer;
import net.minecraft.server.v1_10_R1.EnumDirection;
import net.minecraft.server.v1_10_R1.IInventory;
import net.minecraft.server.v1_10_R1.ITileInventory;
import net.minecraft.server.v1_10_R1.InventoryEnderChest;
import net.minecraft.server.v1_10_R1.InventoryLargeChest;
Expand All @@ -46,6 +44,7 @@
import net.minecraft.server.v1_10_R1.World;

import org.bukkit.craftbukkit.v1_10_R1.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_10_R1.event.CraftEventFactory;

public class AnySilentContainer implements IAnySilentContainer {

Expand Down Expand Up @@ -141,10 +140,11 @@ public boolean activateContainer(Player p, boolean silentchest, org.bukkit.block
return true;
}

if (!(tile instanceof IInventory)) {
if (!(tile instanceof ITileInventory)) {
return false;
}

ITileInventory tileInventory = (ITileInventory) tile;
Block block = world.getType(blockPosition).getBlock();
Container container = null;

Expand All @@ -163,47 +163,54 @@ public boolean activateContainer(Player p, boolean silentchest, org.bukkit.block
}

if ((localEnumDirection == EnumDirection.WEST) || (localEnumDirection == EnumDirection.NORTH)) {
tile = new InventoryLargeChest("container.chestDouble",
(TileEntityChest) localTileEntity, (ITileInventory) tile);
tileInventory = new InventoryLargeChest("container.chestDouble",
(TileEntityChest) localTileEntity, tileInventory);
} else {
tile = new InventoryLargeChest("container.chestDouble",
(ITileInventory) tile, (TileEntityChest) localTileEntity);
tileInventory = new InventoryLargeChest("container.chestDouble",
tileInventory, (TileEntityChest) localTileEntity);
}
break;
}

if (silentchest) {
container = new SilentContainerChest(player.inventory, ((IInventory) tile), player);
}

if (((BlockChest) block).g == Type.BASIC) {
BlockChest blockChest = (BlockChest) block;
if (blockChest.g == BlockChest.Type.BASIC) {
player.b(StatisticList.ac);
} else if (((BlockChest) block).g == Type.TRAP) {
} else if (blockChest.g == BlockChest.Type.TRAP) {
player.b(StatisticList.W);
}
}

boolean returnValue = false;
final IInventory iInventory = (IInventory) tile;
if (silentchest) {
container = new SilentContainerChest(player.inventory, tileInventory, player);
}
}

// AnyChest only - SilentChest not active or container unsupported
if (!silentchest || container == null) {
player.openContainer(iInventory);
returnValue = true;
} else {
try {
int windowId = player.nextContainerCounter();
player.playerConnection.sendPacket(new PacketPlayOutOpenWindow(windowId, iInventory.getName(), iInventory.getScoreboardDisplayName(), iInventory.getSize()));
player.activeContainer = container;
player.activeContainer.windowId = windowId;
player.activeContainer.addSlotListener(player);
returnValue = true;
} catch (Exception e) {
e.printStackTrace();
p.sendMessage(ChatColor.RED + "Error while sending silent chest.");
}
player.openContainer(tileInventory);
return true;
}

return returnValue;
// SilentChest
try {
// Call InventoryOpenEvent
container = CraftEventFactory.callInventoryOpenEvent(player, container, false);
if (container == null) {
return false;
}

// Open window
int windowId = player.nextContainerCounter();
player.playerConnection.sendPacket(new PacketPlayOutOpenWindow(windowId, tileInventory.getContainerName(), tileInventory.getScoreboardDisplayName(), tileInventory.getSize()));
player.activeContainer = container;
player.activeContainer.windowId = windowId;
player.activeContainer.addSlotListener(player);

return true;
} catch (Exception e) {
e.printStackTrace();
p.sendMessage(ChatColor.RED + "Error while sending silent container.");
return false;
}
}

}
Loading

0 comments on commit 2f7d3bc

Please sign in to comment.