use of com.bgsoftware.wildchests.api.objects.chests.Chest in project WildChests by BG-Software-LLC.
the class BlockListener method onChestPlace.
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onChestPlace(BlockPlaceEvent e) {
if (e.getBlockPlaced().getType() != Material.CHEST && e.getBlockPlaced().getType() != Material.TRAPPED_CHEST)
return;
boolean hasNearbyChest = false;
for (BlockFace blockFace : blockFaces) {
Block block = e.getBlockPlaced().getRelative(blockFace);
Material blockMaterial = block.getType();
if (blockMaterial == Material.CHEST || blockMaterial == Material.TRAPPED_CHEST) {
hasNearbyChest = true;
if (plugin.getChestsManager().getChest(block.getLocation()) != null) {
e.setCancelled(true);
return;
}
}
}
ChestData chestData = plugin.getChestsManager().getChestData(e.getItemInHand());
if (chestData == null)
return;
if (hasNearbyChest) {
e.setCancelled(true);
return;
}
Chest chest = plugin.getChestsManager().addChest(e.getPlayer().getUniqueId(), e.getBlockPlaced().getLocation(), chestData);
plugin.getProviders().notifyChestPlaceListeners(chest);
// chest.onPlace(e);
Locale.CHEST_PLACED.send(e.getPlayer(), chestData.getName(), e.getItemInHand().getItemMeta().getDisplayName());
}
use of com.bgsoftware.wildchests.api.objects.chests.Chest in project WildChests by BG-Software-LLC.
the class BlockListener method onChestBreak.
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onChestBreak(BlockBreakEvent e) {
Chest chest = plugin.getChestsManager().getChest(e.getBlock().getLocation());
if (chest == null)
return;
e.setCancelled(true);
if (e.getPlayer().getGameMode() != GameMode.CREATIVE) {
ChestData chestData = chest.getData();
ItemUtils.dropOrCollect(e.getPlayer(), chestData.getItemStack(), chestData.isAutoCollect(), chest.getLocation());
}
chest.onBreak(e);
plugin.getProviders().notifyChestBreakListeners(e.getPlayer(), chest);
chest.remove();
e.getBlock().setType(Material.AIR);
}
use of com.bgsoftware.wildchests.api.objects.chests.Chest in project WildChests by BG-Software-LLC.
the class WildChestsPlugin method onDisable.
@Override
public void onDisable() {
Bukkit.getScheduler().cancelTasks(this);
// Closing all inventories & closing chests
for (Chest chest : chestsManager.getChests()) {
boolean needClose = false;
for (Inventory inventory : chest.getPages()) {
List<HumanEntity> viewers = new ArrayList<>(inventory.getViewers());
for (HumanEntity humanEntity : viewers) {
humanEntity.closeInventory();
needClose = true;
}
}
if (needClose)
nmsAdapter.playChestAction(chest.getLocation(), false);
}
for (Player player : Bukkit.getOnlinePlayers()) player.closeInventory();
int loadedChunks = 0;
for (World world : Bukkit.getWorlds()) {
for (Chunk chunk : world.getLoadedChunks()) {
dataHandler.saveDatabase(chunk, false);
loadedChunks++;
}
}
log("Chunks to save: " + loadedChunks);
log("Terminating executor...");
Executor.stop();
log("Terminating database...");
SQLHelper.close();
}
use of com.bgsoftware.wildchests.api.objects.chests.Chest in project WildChests by BG-Software-LLC.
the class ChestsHandler method loadChest.
public WChest loadChest(UUID placer, Location location, ChestData chestData) {
WChest chest;
switch(chestData.getChestType()) {
case CHEST:
chest = new WRegularChest(placer, location, chestData);
break;
case LINKED_CHEST:
chest = new WLinkedChest(placer, location, chestData);
break;
case STORAGE_UNIT:
chest = new WStorageChest(placer, location, chestData);
break;
default:
throw new IllegalArgumentException("Invalid chest at " + location);
}
chests.put(location, chest);
chestsByChunks.computeIfAbsent(ChunkPosition.of(location), s -> Sets.newConcurrentHashSet()).add(chest);
return chest;
}
use of com.bgsoftware.wildchests.api.objects.chests.Chest in project WildChests by BG-Software-LLC.
the class DataHandler method saveDatabase.
public void saveDatabase(Chunk chunk, boolean async) {
List<Chest> chestList = chunk == null ? plugin.getChestsManager().getChests() : plugin.getChestsManager().getChests(chunk);
List<Chest> regularModifiedChests = chestList.stream().filter(chest -> chest.getChestType() == ChestType.CHEST).collect(Collectors.toList());
List<Chest> storageModifiedChests = chestList.stream().filter(chest -> chest.getChestType() == ChestType.STORAGE_UNIT).collect(Collectors.toList());
List<Chest> linkedChestsModifiedChests = chestList.stream().filter(chest -> chest.getChestType() == ChestType.LINKED_CHEST).collect(Collectors.toList());
if (!regularModifiedChests.isEmpty()) {
StatementHolder chestsUpdateHolder = Query.REGULAR_CHEST_UPDATE_INVENTORIES.getStatementHolder(null);
chestsUpdateHolder.prepareBatch();
regularModifiedChests.forEach(chest -> ((DatabaseObject) chest).setUpdateStatement(chestsUpdateHolder).addBatch());
chestsUpdateHolder.execute(async);
}
if (!storageModifiedChests.isEmpty()) {
StatementHolder chestsUpdateHolder = Query.STORAGE_UNIT_UPDATE_ITEM.getStatementHolder(null);
chestsUpdateHolder.prepareBatch();
storageModifiedChests.forEach(chest -> ((DatabaseObject) chest).setUpdateStatement(chestsUpdateHolder).addBatch());
chestsUpdateHolder.execute(async);
}
if (!linkedChestsModifiedChests.isEmpty()) {
StatementHolder chestsUpdateHolder = Query.LINKED_CHEST_UPDATE_INVENTORIES.getStatementHolder(null);
chestsUpdateHolder.prepareBatch();
linkedChestsModifiedChests.forEach(chest -> ((DatabaseObject) chest).setUpdateStatement(chestsUpdateHolder).addBatch());
chestsUpdateHolder.execute(async);
}
}
Aggregations