use of dev.rosewood.rosestacker.stack.StackedSpawner in project RoseStacker by Rosewood-Development.
the class EntityListener method onSpawnerSpawn.
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onSpawnerSpawn(SpawnerSpawnEvent event) {
if (!(event.getEntity() instanceof LivingEntity))
return;
StackManager stackManager = this.rosePlugin.getManager(StackManager.class);
if (stackManager.isWorldDisabled(event.getEntity().getWorld()))
return;
LivingEntity entity = (LivingEntity) event.getEntity();
PersistentDataUtils.tagSpawnedFromSpawner(entity);
SpawnerStackSettings stackSettings = this.stackSettingManager.getSpawnerStackSettings(event.getSpawner());
StackedSpawner stackedSpawner = this.stackManager.getStackedSpawner(event.getSpawner().getBlock());
if (stackedSpawner == null)
stackedSpawner = stackManager.createSpawnerStack(event.getSpawner().getBlock(), 1, false);
boolean placedByPlayer = stackedSpawner != null && stackedSpawner.isPlacedByPlayer();
if (stackSettings.isMobAIDisabled() && (!Setting.SPAWNER_DISABLE_MOB_AI_ONLY_PLAYER_PLACED.getBoolean() || placedByPlayer))
PersistentDataUtils.removeEntityAi(entity);
}
use of dev.rosewood.rosestacker.stack.StackedSpawner 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.stack.StackedSpawner 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());
});
}
}
}
}
use of dev.rosewood.rosestacker.stack.StackedSpawner in project RoseStacker by Rosewood-Development.
the class WildStackerPluginConverter method convert.
@Override
public void convert() {
StackManager stackManager = this.rosePlugin.getManager(StackManager.class);
// Force save loaded data
SystemManager systemHandler = WildStackerAPI.getWildStacker().getSystemManager();
systemHandler.performCacheSave();
// Go through the database to be able to load all information
DatabaseConnector connector = new SQLiteConnector(this.wildStacker, "database");
connector.connect(connection -> {
// Load barrels (blocks)
Set<StackedBlock> stackedBlocks = new HashSet<>();
try (Statement statement = connection.createStatement()) {
ResultSet result = statement.executeQuery("SELECT location, stackAmount FROM barrels");
while (result.next()) {
Location location = this.parseLocation(result.getString("location"), ',');
if (location == null)
continue;
int amount = result.getInt("stackAmount");
Material type = systemHandler.getStackedSnapshot(location.getChunk()).getStackedBarrelItem(location).getValue().getType();
Block block = location.getBlock();
// Remove hologram thingy
StackedBarrel barrel = systemHandler.getStackedBarrel(block);
if (barrel != null)
barrel.removeDisplayBlock();
// Set the block type to the stack type since we just removed the hologram thingy
block.setType(type);
// Stacks of 1 aren't really stacks
if (amount == 1)
continue;
stackedBlocks.add(new StackedBlock(amount, block));
}
}
// Load spawners
Set<StackedSpawner> stackedSpawners = new HashSet<>();
try (Statement statement = connection.createStatement()) {
ResultSet result = statement.executeQuery("SELECT location, stackAmount FROM spawners");
while (result.next()) {
Location location = this.parseLocation(result.getString("location"), ',');
if (location == null)
continue;
Block block = location.getBlock();
BlockState blockState = block.getState();
if (!(blockState instanceof CreatureSpawner))
continue;
int amount = result.getInt("stackAmount");
stackedSpawners.add(new StackedSpawner(amount, location));
}
}
if (!stackedBlocks.isEmpty() || !stackedSpawners.isEmpty()) {
Bukkit.getScheduler().runTask(this.rosePlugin, () -> {
for (StackedBlock stackedBlock : stackedBlocks) stackManager.createBlockStack(stackedBlock.getLocation().getBlock(), stackedBlock.getStackSize());
for (StackedSpawner stackedSpawner : stackedSpawners) stackManager.createSpawnerStack(stackedSpawner.getLocation().getBlock(), stackedSpawner.getStackSize(), false);
});
}
});
}
use of dev.rosewood.rosestacker.stack.StackedSpawner in project RoseStacker by Rosewood-Development.
the class ItemSpawningMethod method spawn.
@Override
public void spawn(StackedSpawner stackedSpawner) {
StackedSpawnerTile spawnerTile = stackedSpawner.getSpawnerTile();
SpawnerStackSettings stackSettings = stackedSpawner.getStackSettings();
// Mob spawning logic
List<ConditionTag> spawnRequirements = new ArrayList<>();
// Check general spawner conditions // TODO
List<ConditionTag> perSpawnConditions = spawnRequirements.stream().filter(ConditionTag::isRequiredPerSpawn).collect(Collectors.toList());
spawnRequirements.removeAll(perSpawnConditions);
Set<ConditionTag> invalidSpawnConditions = spawnRequirements.stream().filter(x -> !x.check(stackedSpawner, stackedSpawner.getBlock())).collect(Collectors.toSet());
if (Setting.SPAWNER_SPAWN_ONLY_PLAYER_PLACED.getBoolean() && !stackedSpawner.isPlacedByPlayer())
invalidSpawnConditions.add(NotPlayerPlacedConditionTag.INSTANCE);
boolean passedSpawnerChecks = invalidSpawnConditions.isEmpty();
// Will be removed when they pass
invalidSpawnConditions.addAll(perSpawnConditions);
// Spawn the items
int spawnAmount;
if (Setting.SPAWNER_SPAWN_COUNT_STACK_SIZE_RANDOMIZED.getBoolean()) {
if (stackSettings.getSpawnCountStackSizeMultiplier() != -1) {
int spawnerSpawnCount = Math.max(spawnerTile.getSpawnCount(), 0);
spawnAmount = StackerUtils.randomInRange(stackedSpawner.getStackSize(), spawnerSpawnCount);
} else {
spawnAmount = this.random.nextInt(spawnerTile.getSpawnCount()) + 1;
}
} else {
spawnAmount = spawnerTile.getSpawnCount();
}
Bukkit.getScheduler().runTaskAsynchronously(RoseStacker.getInstance(), () -> {
Set<Location> spawnLocations = new HashSet<>();
int spawnRange = spawnerTile.getSpawnRange();
for (int i = 0; i < spawnAmount; i++) {
int attempts = 0;
while (attempts < Setting.SPAWNER_MAX_FAILED_SPAWN_ATTEMPTS.getInt()) {
int xOffset = this.random.nextInt(spawnRange * 2 + 1) - spawnRange;
int yOffset = !Setting.SPAWNER_USE_VERTICAL_SPAWN_RANGE.getBoolean() ? this.random.nextInt(3) - 1 : this.random.nextInt(spawnRange * 2 + 1) - spawnRange;
int zOffset = this.random.nextInt(spawnRange * 2 + 1) - spawnRange;
Location spawnLocation = stackedSpawner.getLocation().clone().add(xOffset + 0.5, yOffset, zOffset + 0.5);
Block target = stackedSpawner.getLocation().clone().add(xOffset, yOffset, zOffset).getBlock();
boolean invalid = false;
for (ConditionTag conditionTag : perSpawnConditions) {
if (!conditionTag.check(stackedSpawner, target)) {
invalid = true;
} else {
invalidSpawnConditions.remove(conditionTag);
}
}
if (invalid) {
attempts++;
continue;
}
if (!passedSpawnerChecks)
break;
spawnLocations.add(spawnLocation);
break;
}
}
int successfulSpawns = spawnLocations.size() > 0 ? spawnAmount : 0;
// Drop items
Bukkit.getScheduler().runTask(RoseStacker.getInstance(), () -> {
// Assign each location a portion of the total items to drop
int amountPerLocation = (int) Math.ceil((double) spawnAmount / spawnLocations.size());
int amountLeft = spawnAmount;
for (Location location : spawnLocations) {
int amount = Math.min(amountPerLocation, amountLeft);
amountLeft -= amount;
for (ItemStack itemStack : GuiUtil.getMaterialAmountAsItemStacks(this.itemType, amount)) stackedSpawner.getWorld().dropItemNaturally(location.clone().add(0.5, 0.5, 0.5), itemStack);
stackedSpawner.getWorld().spawnParticle(Particle.EXPLOSION_NORMAL, location.clone().add(0, 0.75, 0), 2, 0.25, 0.25, 0.25, 0.01);
}
});
stackedSpawner.getLastInvalidConditions().clear();
if (successfulSpawns <= 0) {
if (invalidSpawnConditions.isEmpty()) {
stackedSpawner.getLastInvalidConditions().add(NoneConditionTag.class);
} else {
List<Class<? extends ConditionTag>> invalidSpawnConditionClasses = new ArrayList<>();
for (ConditionTag conditionTag : invalidSpawnConditions) invalidSpawnConditionClasses.add(conditionTag.getClass());
stackedSpawner.getLastInvalidConditions().addAll(invalidSpawnConditionClasses);
}
// Spawn particles indicating the spawn did not occur
stackedSpawner.getWorld().spawnParticle(Particle.SMOKE_NORMAL, stackedSpawner.getLocation().clone().add(0.5, 0.5, 0.5), 50, 0.5, 0.5, 0.5, 0);
} else {
// Spawn particles indicating the spawn occurred
stackedSpawner.getWorld().spawnParticle(Particle.FLAME, stackedSpawner.getLocation().clone().add(0.5, 0.5, 0.5), 50, 0.5, 0.5, 0.5, 0);
Bukkit.getScheduler().runTask(RoseStacker.getInstance(), () -> {
if (stackedSpawner.getBlock().getType() == Material.SPAWNER)
PersistentDataUtils.increaseSpawnCount(spawnerTile, successfulSpawns);
});
}
});
}
Aggregations