use of dev.rosewood.rosestacker.event.BlockUnstackEvent in project RoseStacker by Rosewood-Development.
the class StackedBlockGui method updateStackedBlock.
/**
* Updates the StackedBlock with the changed item contents
*
* @param player The Player that finalized the change
* @param items The Items that are inside the StackedBlock
*/
private void updateStackedBlock(Player player, List<ItemStack> items) {
StackManager stackManager = this.rosePlugin.getManager(StackManager.class);
// No longer any players viewing
this.guiContainer = null;
int newStackSize = items.stream().mapToInt(ItemStack::getAmount).sum();
if (newStackSize == this.stackedBlock.getStackSize())
return;
int difference = newStackSize - this.stackedBlock.getStackSize();
if (newStackSize > this.stackedBlock.getStackSize()) {
BlockStackEvent blockStackEvent = new BlockStackEvent(player, this.stackedBlock, difference, false);
Bukkit.getPluginManager().callEvent(blockStackEvent);
if (blockStackEvent.isCancelled()) {
ItemUtils.dropItemsToPlayer(player, GuiUtil.getMaterialAmountAsItemStacks(this.stackedBlock.getBlock().getType(), difference));
return;
}
newStackSize = this.stackedBlock.getStackSize() + blockStackEvent.getIncreaseAmount();
} else {
BlockUnstackEvent blockUnstackEvent = new BlockUnstackEvent(player, this.stackedBlock, -difference);
Bukkit.getPluginManager().callEvent(blockUnstackEvent);
if (blockUnstackEvent.isCancelled()) {
this.takeFromPlayer(player, -difference);
return;
}
newStackSize = this.stackedBlock.getStackSize() - blockUnstackEvent.getDecreaseAmount();
}
this.stackedBlock.setStackSize(newStackSize);
int maxStackSize = this.stackedBlock.getStackSettings().getMaxStackSize();
if (newStackSize == 1) {
stackManager.removeBlockStack(this.stackedBlock);
} else if (newStackSize == 0) {
stackManager.removeBlockStack(this.stackedBlock);
this.stackedBlock.getBlock().setType(Material.AIR);
} else if (newStackSize > maxStackSize) {
List<ItemStack> overflowItems = GuiUtil.getMaterialAmountAsItemStacks(this.stackedBlock.getBlock().getType(), newStackSize - maxStackSize);
ItemUtils.dropItemsToPlayer(player, overflowItems);
this.stackedBlock.setStackSize(maxStackSize);
}
}
use of dev.rosewood.rosestacker.event.BlockUnstackEvent in project RoseStacker by Rosewood-Development.
the class StackedBlockGui method destroyStackedBlock.
/**
* Destroys the StackedBlock and drops all its items either onto the ground or a player inventory
*
* @param player The Player that destroyed the stack
*/
private void destroyStackedBlock(Player player) {
this.kickOutViewers();
StackManager stackManager = this.rosePlugin.getManager(StackManager.class);
Bukkit.getScheduler().runTask(this.rosePlugin, () -> {
BlockUnstackEvent blockUnstackEvent = new BlockUnstackEvent(player, this.stackedBlock, this.stackedBlock.getStackSize());
Bukkit.getPluginManager().callEvent(blockUnstackEvent);
if (blockUnstackEvent.isCancelled())
return;
List<ItemStack> itemsToDrop;
if (Setting.BLOCK_BREAK_ENTIRE_STACK_INTO_SEPARATE.getBoolean()) {
itemsToDrop = GuiUtil.getMaterialAmountAsItemStacks(this.stackedBlock.getBlock().getType(), blockUnstackEvent.getDecreaseAmount());
} else {
itemsToDrop = Collections.singletonList(ItemUtils.getBlockAsStackedItemStack(this.stackedBlock.getBlock().getType(), blockUnstackEvent.getDecreaseAmount()));
}
if (Setting.BLOCK_DROP_TO_INVENTORY.getBoolean()) {
ItemUtils.dropItemsToPlayer(player, itemsToDrop);
} else {
stackManager.preStackItems(itemsToDrop, this.stackedBlock.getLocation());
}
this.stackedBlock.setStackSize(this.stackedBlock.getStackSize() - blockUnstackEvent.getDecreaseAmount());
if (this.stackedBlock.getStackSize() <= 0) {
stackManager.removeBlockStack(this.stackedBlock);
this.stackedBlock.getBlock().setType(Material.AIR);
this.stackedBlock.getBlock().getWorld().playSound(this.stackedBlock.getBlock().getLocation(), Sound.BLOCK_ANVIL_LAND, 0.1F, 0.01F);
}
});
}
use of dev.rosewood.rosestacker.event.BlockUnstackEvent in project RoseStacker by Rosewood-Development.
the class BlockListener method onBlockBreak.
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockBreak(BlockBreakEvent event) {
StackManager stackManager = this.rosePlugin.getManager(StackManager.class);
if (stackManager.isWorldDisabled(event.getPlayer().getWorld()))
return;
Block block = event.getBlock();
boolean isStacked = this.isBlockOrSpawnerStack(stackManager, block);
boolean isSpawner = block.getType() == Material.SPAWNER;
if (!isStacked && !isSpawner)
return;
Player player = event.getPlayer();
Location dropLocation = block.getLocation().clone();
if (isSpawner) {
if (!stackManager.isSpawnerStackingEnabled())
return;
StackedSpawner stackedSpawner = stackManager.getStackedSpawner(block);
if (stackedSpawner == null)
stackedSpawner = stackManager.createSpawnerStack(block, 1, false);
EntityType entityType = stackedSpawner.getSpawnerTile().getSpawnedType();
boolean breakEverything = Setting.SPAWNER_BREAK_ENTIRE_STACK_WHILE_SNEAKING.getBoolean() && player.isSneaking();
int breakAmount = breakEverything ? stackedSpawner.getStackSize() : 1;
SpawnerUnstackEvent spawnerUnstackEvent = new SpawnerUnstackEvent(player, stackedSpawner, breakAmount);
Bukkit.getPluginManager().callEvent(spawnerUnstackEvent);
if (spawnerUnstackEvent.isCancelled()) {
event.setCancelled(true);
return;
}
breakAmount = spawnerUnstackEvent.getDecreaseAmount();
if (this.tryDropSpawners(player, dropLocation, entityType, breakAmount, stackedSpawner.isPlacedByPlayer())) {
BlockLoggingHook.recordBlockBreak(player, block);
if (breakAmount == stackedSpawner.getStackSize()) {
stackedSpawner.setStackSize(0);
Bukkit.getScheduler().runTask(this.rosePlugin, () -> block.setType(Material.AIR));
} else {
stackedSpawner.increaseStackSize(-breakAmount);
}
if (stackedSpawner.getStackSize() <= 0) {
stackManager.removeSpawnerStack(stackedSpawner);
return;
}
} else {
event.setCancelled(true);
return;
}
} else {
if (!stackManager.isBlockStackingEnabled())
return;
StackedBlock stackedBlock = stackManager.getStackedBlock(block);
if (stackedBlock == null)
return;
if (stackedBlock.isLocked()) {
event.setCancelled(true);
return;
}
boolean breakEverything = Setting.BLOCK_BREAK_ENTIRE_STACK_WHILE_SNEAKING.getBoolean() && player.isSneaking();
int breakAmount = breakEverything ? stackedBlock.getStackSize() : 1;
BlockUnstackEvent blockUnstackEvent = new BlockUnstackEvent(player, stackedBlock, breakAmount);
Bukkit.getPluginManager().callEvent(blockUnstackEvent);
if (blockUnstackEvent.isCancelled()) {
event.setCancelled(true);
return;
}
breakAmount = blockUnstackEvent.getDecreaseAmount();
if (player.getGameMode() != GameMode.CREATIVE) {
List<ItemStack> items;
if (Setting.BLOCK_BREAK_ENTIRE_STACK_INTO_SEPARATE.getBoolean()) {
items = GuiUtil.getMaterialAmountAsItemStacks(block.getType(), breakAmount);
} else {
items = Collections.singletonList(ItemUtils.getBlockAsStackedItemStack(block.getType(), breakAmount));
}
if (Setting.BLOCK_DROP_TO_INVENTORY.getBoolean()) {
ItemUtils.dropItemsToPlayer(player, items);
} else {
stackManager.preStackItems(items, dropLocation);
}
}
BlockLoggingHook.recordBlockBreak(player, block);
if (breakAmount == stackedBlock.getStackSize()) {
stackedBlock.setStackSize(0);
Bukkit.getScheduler().runTask(this.rosePlugin, () -> block.setType(Material.AIR));
} else {
stackedBlock.increaseStackSize(-1);
}
if (stackedBlock.getStackSize() <= 1)
stackManager.removeBlockStack(stackedBlock);
}
this.damageTool(player);
event.setCancelled(true);
}
use of dev.rosewood.rosestacker.event.BlockUnstackEvent in project RoseStacker by Rosewood-Development.
the class BlockListener method handleExplosion.
private void handleExplosion(Location location, List<Block> blockList) {
StackManager stackManager = this.rosePlugin.getManager(StackManager.class);
if (stackManager.isWorldDisabled(location.getWorld()))
return;
boolean stackedBlockProtection = Setting.BLOCK_EXPLOSION_PROTECTION.getBoolean() && stackManager.isBlockStackingEnabled();
boolean stackedSpawnerProtection = Setting.SPAWNER_EXPLOSION_PROTECTION.getBoolean() && stackManager.isSpawnerStackingEnabled();
if (stackedSpawnerProtection)
blockList.removeIf(stackManager::isSpawnerStacked);
if (stackedBlockProtection)
blockList.removeIf(stackManager::isBlockStacked);
for (Block block : new ArrayList<>(blockList)) {
if (stackManager.isBlockStacked(block)) {
blockList.remove(block);
if (!StackerUtils.passesChance(Setting.BLOCK_EXPLOSION_DESTROY_CHANCE.getDouble() / 100))
continue;
StackedBlock stackedBlock = stackManager.getStackedBlock(block);
stackedBlock.kickOutGuiViewers();
int destroyAmountFixed = Setting.BLOCK_EXPLOSION_DESTROY_AMOUNT_FIXED.getInt();
int destroyAmount;
if (destroyAmountFixed != -1) {
destroyAmount = destroyAmountFixed;
} else {
destroyAmount = stackedBlock.getStackSize() - (int) Math.ceil(stackedBlock.getStackSize() * (Setting.BLOCK_EXPLOSION_DESTROY_AMOUNT_PERCENTAGE.getDouble() / 100));
}
BlockUnstackEvent blockUnstackEvent = new BlockUnstackEvent(null, stackedBlock, destroyAmount);
Bukkit.getPluginManager().callEvent(blockUnstackEvent);
if (blockUnstackEvent.isCancelled())
continue;
destroyAmount = blockUnstackEvent.getDecreaseAmount();
int newStackSize = stackedBlock.getStackSize() - destroyAmount;
if (newStackSize <= 0) {
block.setType(Material.AIR);
stackedBlock.setStackSize(0);
stackManager.removeBlockStack(stackedBlock);
continue;
}
if (Setting.BLOCK_EXPLOSION_DECREASE_STACK_SIZE_ONLY.getBoolean()) {
stackedBlock.setStackSize(newStackSize);
if (newStackSize <= 1)
stackManager.removeBlockStack(stackedBlock);
} else {
stackedBlock.setStackSize(0);
stackManager.removeBlockStack(stackedBlock);
Material type = block.getType();
block.setType(Material.AIR);
Bukkit.getScheduler().runTask(this.rosePlugin, () -> {
List<ItemStack> items;
if (Setting.BLOCK_BREAK_ENTIRE_STACK_INTO_SEPARATE.getBoolean()) {
items = GuiUtil.getMaterialAmountAsItemStacks(type, newStackSize);
} else {
items = Collections.singletonList(ItemUtils.getBlockAsStackedItemStack(type, newStackSize));
}
stackManager.preStackItems(items, block.getLocation().clone().add(0.5, 0.5, 0.5));
});
}
} else if (stackManager.isSpawnerStacked(block)) {
blockList.remove(block);
if (!StackerUtils.passesChance(Setting.SPAWNER_EXPLOSION_DESTROY_CHANCE.getDouble() / 100))
continue;
StackedSpawner stackedSpawner = stackManager.getStackedSpawner(block);
int destroyAmountFixed = Setting.SPAWNER_EXPLOSION_DESTROY_AMOUNT_FIXED.getInt();
int destroyAmount;
if (destroyAmountFixed != -1) {
destroyAmount = destroyAmountFixed;
} else {
destroyAmount = stackedSpawner.getStackSize() - (int) Math.ceil(stackedSpawner.getStackSize() * (Setting.SPAWNER_EXPLOSION_DESTROY_AMOUNT_PERCENTAGE.getDouble() / 100));
}
SpawnerUnstackEvent spawnerUnstackEvent = new SpawnerUnstackEvent(null, stackedSpawner, destroyAmount);
Bukkit.getPluginManager().callEvent(spawnerUnstackEvent);
if (spawnerUnstackEvent.isCancelled())
continue;
destroyAmount = spawnerUnstackEvent.getDecreaseAmount();
int newStackSize = stackedSpawner.getStackSize() - destroyAmount;
if (newStackSize <= 0) {
block.setType(Material.AIR);
stackedSpawner.setStackSize(0);
stackManager.removeSpawnerStack(stackedSpawner);
continue;
}
if (Setting.SPAWNER_EXPLOSION_DECREASE_STACK_SIZE_ONLY.getBoolean()) {
stackedSpawner.setStackSize(newStackSize);
} else {
stackedSpawner.setStackSize(0);
stackManager.removeSpawnerStack(stackedSpawner);
EntityType spawnedType = stackedSpawner.getSpawnerTile().getSpawnedType();
block.setType(Material.AIR);
Bukkit.getScheduler().runTask(this.rosePlugin, () -> {
List<ItemStack> items;
if (Setting.SPAWNER_BREAK_ENTIRE_STACK_INTO_SEPARATE.getBoolean()) {
items = new ArrayList<>();
for (int i = 0; i < newStackSize; i++) items.add(ItemUtils.getSpawnerAsStackedItemStack(spawnedType, 1));
} else {
items = Collections.singletonList(ItemUtils.getSpawnerAsStackedItemStack(spawnedType, newStackSize));
}
stackManager.preStackItems(items, block.getLocation().clone());
});
}
}
}
}
Aggregations