Java Code Examples for org.bukkit.block.DoubleChest#getLeftSide()

The following examples show how to use org.bukkit.block.DoubleChest#getLeftSide() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GameMap.java    From SkyWarsReloaded with GNU General Public License v3.0 6 votes vote down vote up
public void addChest(Chest chest, ChestPlacementType cpt) {
	ArrayList<CoordLoc> list;
	if (cpt == ChestPlacementType.NORMAL) {
		list = chests;
	} else {
		list = centerChests;
	}
	InventoryHolder ih = chest.getInventory().getHolder();
       if (ih instanceof DoubleChest) {
       	DoubleChest dc = (DoubleChest) ih;
		Chest left = (Chest) dc.getLeftSide();
		Chest right = (Chest) dc.getRightSide();
		CoordLoc locLeft = new CoordLoc(left.getX(), left.getY(), left.getZ());
		CoordLoc locRight = new CoordLoc(right.getX(), right.getY(), right.getZ());
		if (!(list.contains(locLeft) || list.contains(locRight))) {
			addChest(locLeft, cpt, true);
		}
       } else {
       	CoordLoc loc = new CoordLoc(chest.getX(), chest.getY(), chest.getZ());
           if (!list.contains(loc)){
			addChest(loc, cpt, true);
           }
       }
}
 
Example 2
Source File: GameMap.java    From SkyWarsReloaded with GNU General Public License v3.0 6 votes vote down vote up
public void removeChest(Chest chest) {
	InventoryHolder ih = chest.getInventory().getHolder();
	if (ih instanceof DoubleChest) {
		DoubleChest dc = (DoubleChest) ih;
		Chest left = (Chest) dc.getLeftSide();
		Chest right = (Chest) dc.getRightSide();
		CoordLoc locLeft = new CoordLoc(left.getX(), left.getY(), left.getZ());
		CoordLoc locRight = new CoordLoc(right.getX(), right.getY(), right.getZ());
		chests.remove(locLeft);
		centerChests.remove(locLeft);
		chests.remove(locRight);
		centerChests.remove(locLeft);
		saveArenaData();
	} else {
		CoordLoc loc = new CoordLoc(chest.getX(), chest.getY(), chest.getZ());
		chests.remove(loc);
		centerChests.remove(loc);
		saveArenaData();
	}

}
 
Example 3
Source File: ChestProtectListener.java    From ShopChest with MIT License 6 votes vote down vote up
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onItemMove(InventoryMoveItemEvent e) {
    if ((e.getSource().getType().equals(InventoryType.CHEST)) && (!e.getInitiator().getType().equals(InventoryType.PLAYER))) {

        if (e.getSource().getHolder() instanceof DoubleChest) {
            DoubleChest dc = (DoubleChest) e.getSource().getHolder();
            Chest r = (Chest) dc.getRightSide();
            Chest l = (Chest) dc.getLeftSide();

            if (shopUtils.isShop(r.getLocation()) || shopUtils.isShop(l.getLocation())) e.setCancelled(true);

        } else if (e.getSource().getHolder() instanceof Chest) {
            Chest c = (Chest) e.getSource().getHolder();

            if (shopUtils.isShop(c.getLocation())) e.setCancelled(true);
        }
    }
}
 
Example 4
Source File: ShopUtils.java    From ShopChest with MIT License 5 votes vote down vote up
/**
 * Add a shop
 * @param shop Shop to add
 * @param addToDatabase Whether the shop should also be added to the database
 * @param callback Callback that - if succeeded - returns the ID the shop had or was given (as {@code int})
 */
public void addShop(Shop shop, boolean addToDatabase, Callback<Integer> callback) {
    InventoryHolder ih = shop.getInventoryHolder();
    plugin.debug("Adding shop... (#" + shop.getID() + ")");

    if (ih instanceof DoubleChest) {
        DoubleChest dc = (DoubleChest) ih;
        Chest r = (Chest) dc.getRightSide();
        Chest l = (Chest) dc.getLeftSide();

        plugin.debug("Added shop as double chest. (#" + shop.getID() + ")");

        shopLocation.put(r.getLocation(), shop);
        shopLocation.put(l.getLocation(), shop);
    } else {
        plugin.debug("Added shop as single chest. (#" + shop.getID() + ")");

        shopLocation.put(shop.getLocation(), shop);
    }

    if (addToDatabase) {
        if (shop.getShopType() != ShopType.ADMIN) {
            playerShopAmount.compute(shop.getVendor().getUniqueId(), (uuid, amount) -> amount == null ? new Counter(1) : amount.increment());
        }
        plugin.getShopDatabase().addShop(shop, callback);
    } else {
        if (callback != null) callback.callSyncResult(shop.getID());
    }

}
 
Example 5
Source File: ShopUtils.java    From ShopChest with MIT License 5 votes vote down vote up
/** Remove a shop. May not work properly if double chest doesn't exist!
 * @param shop Shop to remove
 * @param removeFromDatabase Whether the shop should also be removed from the database
 * @param callback Callback that - if succeeded - returns null
 * @see ShopUtils#removeShopById(int, boolean, Callback)
 */
public void removeShop(Shop shop, boolean removeFromDatabase, Callback<Void> callback) {
    plugin.debug("Removing shop (#" + shop.getID() + ")");

    if (shop.isCreated()) {
        InventoryHolder ih = shop.getInventoryHolder();

        if (ih instanceof DoubleChest) {
            DoubleChest dc = (DoubleChest) ih;
            Chest r = (Chest) dc.getRightSide();
            Chest l = (Chest) dc.getLeftSide();

            shopLocation.remove(r.getLocation());
            shopLocation.remove(l.getLocation());
        } else {
            shopLocation.remove(shop.getLocation());
        }

        shop.removeItem();
        shop.removeHologram();
    }

    if (removeFromDatabase) {
        if (shop.getShopType() != ShopType.ADMIN) {
            playerShopAmount.compute(shop.getVendor().getUniqueId(), (uuid, amount) -> amount == null ? new Counter() : amount.decrement());
        }
        plugin.getShopDatabase().removeShop(shop, callback);
    } else {
        if (callback != null) callback.callSyncResult(null);
    }
}
 
Example 6
Source File: Shop.java    From ShopChest with MIT License 5 votes vote down vote up
/**
 * Runs everything that needs to be called synchronously in order 
 * to prepare creating the hologram.
 */
private PreCreateResult preCreateHologram() {
    plugin.debug("Creating hologram (#" + id + ")");

    InventoryHolder ih = getInventoryHolder();

    if (ih == null) return null;

    Chest[] chests = new Chest[2];
    BlockFace face;

    if (ih instanceof DoubleChest) {
        DoubleChest dc = (DoubleChest) ih;
        Chest r = (Chest) dc.getRightSide();
        Chest l = (Chest) dc.getLeftSide();

        chests[0] = r;
        chests[1] = l;
    } else {
        chests[0] = (Chest) ih;
    }

    if (Utils.getMajorVersion() < 13) {
        face = ((org.bukkit.material.Directional) chests[0].getData()).getFacing();
    } else {
        face = ((Directional) chests[0].getBlockData()).getFacing();
    }

    return new PreCreateResult(ih.getInventory(), chests, face);
}
 
Example 7
Source File: ChestProtectListener.java    From ShopChest with MIT License 4 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void onBlockPlace(BlockPlaceEvent e) {
    final Player p = e.getPlayer();
    final Block b = e.getBlockPlaced();

    if (!b.getType().equals(Material.CHEST) && !b.getType().equals(Material.TRAPPED_CHEST)) {
        return;
    }
    
    Chest c = (Chest) b.getState();
    Block b2;

    // Can't use Utils::getChestLocations since inventory holder
    // has not been updated yet in this event (for 1.13+)

    if (Utils.getMajorVersion() < 13) {
        InventoryHolder ih = c.getInventory().getHolder();
        if (!(ih instanceof DoubleChest)) {
            return;
        }

        DoubleChest dc = (DoubleChest) ih;
        Chest l = (Chest) dc.getLeftSide();
        Chest r = (Chest) dc.getRightSide();

        if (b.getLocation().equals(l.getLocation())) {
            b2 = r.getBlock();
        } else {
            b2 = l.getBlock();
        }
    } else {
        org.bukkit.block.data.type.Chest data = (org.bukkit.block.data.type.Chest) c.getBlockData();

        if (data.getType() == Type.SINGLE) {
            return;
        }

        BlockFace neighborFacing;

        switch (data.getFacing()) {
            case NORTH:
                neighborFacing = data.getType() == Type.LEFT ? BlockFace.EAST : BlockFace.WEST;
                break;
            case EAST:
                neighborFacing = data.getType() == Type.LEFT ? BlockFace.SOUTH : BlockFace.NORTH;
                break;
            case SOUTH:
                neighborFacing = data.getType() == Type.LEFT ? BlockFace.WEST : BlockFace.EAST;
                break;
            case WEST:
                neighborFacing = data.getType() == Type.LEFT ? BlockFace.NORTH : BlockFace.SOUTH;
                break;
            default:
                neighborFacing = null;
        }

        b2 = b.getRelative(neighborFacing);
    }

    final Shop shop = shopUtils.getShop(b2.getLocation());
    if (shop == null)
        return;

    plugin.debug(String.format("%s tries to extend %s's shop (#%d)", p.getName(), shop.getVendor().getName(), shop.getID()));

    ShopExtendEvent event = new ShopExtendEvent(p, shop, b.getLocation());
    Bukkit.getPluginManager().callEvent(event);
    if (event.isCancelled() && !p.hasPermission(Permissions.EXTEND_PROTECTED)) {
        e.setCancelled(true);
        p.sendMessage(LanguageUtils.getMessage(Message.NO_PERMISSION_EXTEND_PROTECTED));
        return;
    }

    if (!p.getUniqueId().equals(shop.getVendor().getUniqueId()) && !p.hasPermission(Permissions.EXTEND_OTHER)) {
        e.setCancelled(true);
        p.sendMessage(LanguageUtils.getMessage(Message.NO_PERMISSION_EXTEND_OTHERS));
        return;
    }

    if (!ItemUtils.isAir(b.getRelative(BlockFace.UP).getType())) {
        e.setCancelled(true);
        p.sendMessage(LanguageUtils.getMessage(Message.CHEST_BLOCKED));
        return;
    }

    final Shop newShop = new Shop(shop.getID(), plugin, shop.getVendor(), shop.getProduct(), shop.getLocation(), shop.getBuyPrice(), shop.getSellPrice(), shop.getShopType());

    shopUtils.removeShop(shop, true, new Callback<Void>(plugin) {
        @Override
        public void onResult(Void result) {
            newShop.create(true);
            shopUtils.addShop(newShop, true);
            plugin.debug(String.format("%s extended %s's shop (#%d)", p.getName(), shop.getVendor().getName(), shop.getID()));
        }
    });
}