use of dev.rosewood.rosestacker.manager.StackManager 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.manager.StackManager 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.manager.StackManager in project RoseStacker by Rosewood-Development.
the class MobSpawningMethod method spawnEntitiesIntoNearbyStacks.
private int spawnEntitiesIntoNearbyStacks(StackedSpawner stackedSpawner, int spawnAmount, Set<Location> locations, List<StackedEntity> nearbyEntities, StackManager stackManager) {
EntityStackSettings entityStackSettings = RoseStacker.getInstance().getManager(StackSettingManager.class).getEntityStackSettings(this.entityType);
List<StackedEntity> stackedEntities = new ArrayList<>(nearbyEntities);
List<Location> possibleLocations = new ArrayList<>(locations);
if (this.entityType.getEntityClass() == null)
return 0;
boolean ageable = Ageable.class.isAssignableFrom(this.entityType.getEntityClass());
int successfulSpawns = 0;
if (stackManager.isEntityStackingEnabled() && entityStackSettings.isStackingEnabled() && Setting.SPAWNER_SPAWN_INTO_NEARBY_STACKS.getBoolean()) {
List<StackedEntity> newStacks = new ArrayList<>();
NMSHandler nmsHandler = NMSAdapter.getHandler();
for (int i = 0; i < spawnAmount; i++) {
if (possibleLocations.isEmpty())
break;
Location location = possibleLocations.get(this.random.nextInt(possibleLocations.size()));
LivingEntity entity = nmsHandler.createNewEntityUnspawned(this.entityType, location, CreatureSpawnEvent.SpawnReason.SPAWNER);
SpawnerFlagPersistenceHook.flagSpawnerSpawned(entity);
if (ageable)
((Ageable) entity).setAdult();
if ((stackedSpawner.getStackSettings().isMobAIDisabled() && (!Setting.SPAWNER_DISABLE_MOB_AI_ONLY_PLAYER_PLACED.getBoolean() || stackedSpawner.isPlacedByPlayer())) || Setting.ENTITY_DISABLE_ALL_MOB_AI.getBoolean())
PersistentDataUtils.removeEntityAi(entity);
PersistentDataUtils.tagSpawnedFromSpawner(entity);
entityStackSettings.applySpawnerSpawnedProperties(entity);
StackedEntity newStack = new StackedEntity(entity);
Optional<StackedEntity> matchingEntity = stackedEntities.stream().filter(x -> WorldGuardHook.testLocation(x.getLocation()) && entityStackSettings.testCanStackWith(x, newStack, false, true)).findFirst();
if (matchingEntity.isPresent()) {
matchingEntity.get().increaseStackSize(entity);
} else {
stackedEntities.add(newStack);
newStacks.add(newStack);
possibleLocations.remove(location);
}
successfulSpawns++;
}
Bukkit.getScheduler().runTask(RoseStacker.getInstance(), () -> {
stackManager.setEntityStackingTemporarilyDisabled(true);
for (StackedEntity stackedEntity : newStacks) {
LivingEntity entity = stackedEntity.getEntity();
SpawnerSpawnEvent spawnerSpawnEvent = new SpawnerSpawnEvent(entity, stackedSpawner.getSpawner());
Bukkit.getPluginManager().callEvent(spawnerSpawnEvent);
if (spawnerSpawnEvent.isCancelled())
continue;
nmsHandler.spawnExistingEntity(stackedEntity.getEntity(), CreatureSpawnEvent.SpawnReason.SPAWNER, Setting.SPAWNER_BYPASS_REGION_SPAWNING_RULES.getBoolean());
entity.setVelocity(Vector.getRandom().multiply(0.01));
stackManager.addEntityStack(stackedEntity);
}
stackManager.setEntityStackingTemporarilyDisabled(false);
// Spawn particles for new entities and update nametags
for (StackedEntity entity : newStacks) {
entity.updateDisplay();
World world = entity.getLocation().getWorld();
if (world != null)
world.spawnParticle(Particle.EXPLOSION_NORMAL, entity.getLocation().clone().add(0, 0.75, 0), 5, 0.25, 0.25, 0.25, 0.01);
}
});
} else {
successfulSpawns = Math.min(spawnAmount, possibleLocations.size());
Bukkit.getScheduler().runTask(RoseStacker.getInstance(), () -> {
NMSHandler nmsHandler = NMSAdapter.getHandler();
for (int i = 0; i < spawnAmount; i++) {
if (possibleLocations.isEmpty())
break;
Location location = possibleLocations.remove(this.random.nextInt(possibleLocations.size()));
World world = location.getWorld();
if (world == null)
continue;
LivingEntity entity = nmsHandler.spawnEntityWithReason(this.entityType, location, CreatureSpawnEvent.SpawnReason.SPAWNER, Setting.SPAWNER_BYPASS_REGION_SPAWNING_RULES.getBoolean());
entityStackSettings.applySpawnerSpawnedProperties(entity);
SpawnerFlagPersistenceHook.flagSpawnerSpawned(entity);
SpawnerSpawnEvent spawnerSpawnEvent = new SpawnerSpawnEvent(entity, stackedSpawner.getSpawner());
Bukkit.getPluginManager().callEvent(spawnerSpawnEvent);
if (spawnerSpawnEvent.isCancelled()) {
entity.remove();
continue;
}
// Spawn Particles
if (entity.isValid())
entity.getWorld().spawnParticle(Particle.EXPLOSION_NORMAL, entity.getLocation().clone().add(0, 0.75, 0), 5, 0.25, 0.25, 0.25, 0.01);
}
});
}
return successfulSpawns;
}
use of dev.rosewood.rosestacker.manager.StackManager in project RoseStacker by Rosewood-Development.
the class UltimateStackerPluginConverter method convert.
@Override
public void convert() {
// If EpicSpawners is installed, spawner stacking functionality is handled by that plugin instead
if (Bukkit.getPluginManager().isPluginEnabled("EpicSpawners"))
return;
DataManager dataManager = this.rosePlugin.getManager(DataManager.class);
// Force save loaded data
this.ultimateStacker.getDataManager().bulkUpdateSpawners(this.ultimateStacker.getSpawnerStackManager().getStacks());
// Go through the database to be able to load all spawner information
DatabaseConnector connector = this.ultimateStacker.getDatabaseConnector();
connector.connect(connection -> {
// Load entities
try (Statement statement = connection.createStatement()) {
String query = "SELECT he.uuid AS uuid, COUNT(se.host) + 1 AS stackAmount FROM ultimatestacker_host_entities he " + "JOIN ultimatestacker_stacked_entities se ON he.id = se.host " + "GROUP BY se.host";
ResultSet result = statement.executeQuery(query);
Set<ConversionData> entityConversionData = new HashSet<>();
while (result.next()) {
UUID uuid = UUID.fromString(result.getString("uuid"));
int amount = result.getInt("stackAmount");
entityConversionData.add(new ConversionData(uuid, amount));
}
Map<StackType, Set<ConversionData>> conversionData = new HashMap<>();
conversionData.put(StackType.ENTITY, entityConversionData);
dataManager.setConversionData(conversionData);
}
// Load blocks
Set<StackedBlock> stackedBlocks = new HashSet<>();
try (Statement statement = connection.createStatement()) {
ResultSet result = statement.executeQuery("SELECT amount, world, x, y, z FROM ultimatestacker_blocks");
while (result.next()) {
World world = Bukkit.getWorld(result.getString("world"));
if (world == null)
continue;
int amount = result.getInt("amount");
if (amount == 1)
continue;
Block block = world.getBlockAt(result.getInt("x"), result.getInt("y"), result.getInt("z"));
stackedBlocks.add(new StackedBlock(amount, block));
}
}
// Load spawners
Set<StackedSpawner> stackedSpawners = new HashSet<>();
try (Statement statement = connection.createStatement()) {
ResultSet result = statement.executeQuery("SELECT amount, world, x, y, z FROM ultimatestacker_spawners");
while (result.next()) {
World world = Bukkit.getWorld(result.getString("world"));
if (world == null)
continue;
int x = result.getInt("x");
int y = result.getInt("y");
int z = result.getInt("z");
Location location = new Location(world, x, y, z);
int amount = result.getInt("amount");
stackedSpawners.add(new StackedSpawner(amount, location));
}
}
if (!stackedBlocks.isEmpty() || !stackedSpawners.isEmpty()) {
StackManager stackManager = this.rosePlugin.getManager(StackManager.class);
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.manager.StackManager in project RoseStacker by Rosewood-Development.
the class BeeListener method onBeeEnterHive.
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onBeeEnterHive(EntityEnterBlockEvent event) {
if (event.getEntityType() != EntityType.BEE)
return;
StackManager stackManager = this.rosePlugin.getManager(StackManager.class);
if (stackManager.isWorldDisabled(event.getBlock().getWorld()))
return;
if (!stackManager.isEntityStackingEnabled())
return;
Bee beeEntity = (Bee) event.getEntity();
StackedEntity stackedEntity = stackManager.getStackedEntity(beeEntity);
if (stackedEntity == null)
return;
if (stackedEntity.getStackSize() == 1) {
stackManager.removeEntityStack(stackedEntity);
return;
}
Bukkit.getScheduler().runTask(this.rosePlugin, stackedEntity::decreaseStackSize);
}
Aggregations