下面列出了怎么用org.bukkit.inventory.DoubleChestInventory的API类实例代码及写法,或者点击链接到github查看源代码。
public void previewInventory(Player viewer, Inventory realInventory) {
if(viewer == null) { return; }
if(realInventory instanceof PlayerInventory) {
previewPlayerInventory(viewer, (PlayerInventory) realInventory);
}else {
Inventory fakeInventory;
if(realInventory instanceof DoubleChestInventory) {
if(realInventory.hasCustomName()) {
fakeInventory = Bukkit.createInventory(viewer, realInventory.getSize(), realInventory.getName());
} else {
fakeInventory = Bukkit.createInventory(viewer, realInventory.getSize());
}
} else {
if(realInventory.hasCustomName()) {
fakeInventory = Bukkit.createInventory(viewer, realInventory.getType(), realInventory.getName());
} else {
fakeInventory = Bukkit.createInventory(viewer, realInventory.getType());
}
}
fakeInventory.setContents(realInventory.contents());
this.showInventoryPreview(viewer, realInventory, fakeInventory);
}
}
/**
* Gets the single block inventory for a potentially double chest.
* Handles people who have an old version of Bukkit.
* This should be replaced with {@link org.bukkit.block.Chest#getBlockInventory()}
* in a few months (now = March 2012) // note from future dev - lol
*
* @param chest The chest to get a single block inventory for
* @return The chest's inventory
*/
private Inventory getBlockInventory(Chest chest) {
try {
return chest.getBlockInventory();
} catch (Throwable t) {
if (chest.getInventory() instanceof DoubleChestInventory) {
DoubleChestInventory inven = (DoubleChestInventory) chest.getInventory();
if (inven.getLeftSide().getHolder().equals(chest)) {
return inven.getLeftSide();
} else if (inven.getRightSide().getHolder().equals(chest)) {
return inven.getRightSide();
} else {
return inven;
}
} else {
return chest.getInventory();
}
}
}
@EventHandler(priority = EventPriority.HIGHEST)
public void onInventoryOpenEvent(InventoryOpenEvent event) {
if (event.getInventory() instanceof DoubleChestInventory) {
DoubleChestInventory doubleInv = (DoubleChestInventory)event.getInventory();
Chest leftChest = (Chest)doubleInv.getHolder().getLeftSide();
/*Generate a new player 'switch' event for the left and right chests. */
PlayerInteractEvent interactLeft = new PlayerInteractEvent((Player)event.getPlayer(), Action.RIGHT_CLICK_BLOCK, null, leftChest.getBlock(), null);
BlockListener.OnPlayerSwitchEvent(interactLeft);
if (interactLeft.isCancelled()) {
event.setCancelled(true);
return;
}
Chest rightChest = (Chest)doubleInv.getHolder().getRightSide();
PlayerInteractEvent interactRight = new PlayerInteractEvent((Player)event.getPlayer(), Action.RIGHT_CLICK_BLOCK, null, rightChest.getBlock(), null);
BlockListener.OnPlayerSwitchEvent(interactRight);
if (interactRight.isCancelled()) {
event.setCancelled(true);
return;
}
}
}
public void previewInventory(Player viewer, Inventory realInventory) {
if (viewer == null) {
return;
}
if (realInventory instanceof PlayerInventory) {
previewPlayerInventory(viewer, (PlayerInventory) realInventory);
} else {
Inventory fakeInventory;
if (realInventory instanceof DoubleChestInventory) {
if (realInventory.hasCustomName()) {
fakeInventory =
Bukkit.createInventory(viewer, realInventory.getSize(), realInventory.getName());
} else {
fakeInventory = Bukkit.createInventory(viewer, realInventory.getSize());
}
} else {
if (realInventory.hasCustomName()) {
fakeInventory =
Bukkit.createInventory(viewer, realInventory.getType(), realInventory.getName());
} else {
fakeInventory = Bukkit.createInventory(viewer, realInventory.getType());
}
}
fakeInventory.setContents(realInventory.getContents());
this.showInventoryPreview(viewer, realInventory, fakeInventory);
}
}
public static boolean isDoubleChest(@Nullable Block b) {
if (b == null) {
return false;
}
if (!(b.getState() instanceof Container)) {
return false;
}
final Container container = (Container) b.getState();
return (container.getInventory() instanceof DoubleChestInventory);
}
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
public void onPlace(BlockPlaceEvent e) {
final Material type = e.getBlock().getType();
final Block placingBlock = e.getBlock();
final Player player = e.getPlayer();
if (type != Material.CHEST) {
return;
}
Block chest = null;
//Chest combine mechanic based checking
if (player.isSneaking()) {
Block blockAgainst = e.getBlockAgainst();
if (blockAgainst.getType() == Material.CHEST && placingBlock.getFace(blockAgainst) != BlockFace.UP && placingBlock.getFace(blockAgainst) != BlockFace.DOWN && !(((Chest) blockAgainst.getState()).getInventory() instanceof DoubleChestInventory)) {
chest = e.getBlockAgainst();
} else {
return;
}
} else {
//Get all chest in vertical Location
BlockFace placingChestFacing = ((Directional) (placingBlock.getState().getBlockData())).getFacing();
for (BlockFace face : Util.getVerticalFacing()) {
//just check the right side and left side
if (face != placingChestFacing && face != placingChestFacing.getOppositeFace()) {
Block nearByBlock = placingBlock.getRelative(face);
if (nearByBlock.getType() == Material.CHEST
//non double chest
&& !(((Chest) nearByBlock.getState()).getInventory() instanceof DoubleChestInventory)
//same facing
&& placingChestFacing == ((Directional) nearByBlock.getState().getBlockData()).getFacing()) {
if (chest == null) {
chest = nearByBlock;
} else {
//when multiply chests competed, minecraft will always combine with right side
if (placingBlock.getFace(nearByBlock) == Util.getRightSide(placingChestFacing)) {
chest = nearByBlock;
}
}
}
}
}
}
if (chest == null) {
return;
}
Shop shop = getShopPlayer(chest.getLocation(), false);
if (shop != null) {
if (!QuickShop.getPermissionManager().hasPermission(player, "quickshop.create.double")) {
e.setCancelled(true);
MsgUtil.sendMessage(player, MsgUtil.getMessage("no-double-chests", player));
} else if (!shop.getModerator().isModerator(player.getUniqueId())) {
e.setCancelled(true);
MsgUtil.sendMessage(player, MsgUtil.getMessage("not-managed-shop", player));
}
}
}
public DoubleChest(DoubleChestInventory chest) {
inventory = chest;
}
public void addInventory (DoubleChestInventory inv) {
invs.add(inv.getLeftSide());
invs.add(inv.getRightSide());
}