use of dev.rosewood.rosestacker.stack.StackedEntity in project RoseStacker by Rosewood-Development.
the class StackMobConversionHandler method handleConversion.
@Override
public Set<Stack<?>> handleConversion(Set<ConversionData> conversionData) {
Set<LivingEntity> entities = conversionData.stream().map(ConversionData::getEntity).filter(Objects::nonNull).collect(Collectors.toSet());
Set<Stack<?>> stacks = new HashSet<>();
for (LivingEntity entity : entities) {
PersistentDataContainer dataContainer = entity.getPersistentDataContainer();
if (dataContainer.has(PersistentDataUtils.CONVERTED_KEY, PersistentDataType.INTEGER))
continue;
int stackSize = dataContainer.getOrDefault(STACK_KEY, PersistentDataType.INTEGER, -1);
if (stackSize == -1)
continue;
dataContainer.set(PersistentDataUtils.CONVERTED_KEY, PersistentDataType.INTEGER, 1);
StackedEntity stackedEntity = new StackedEntity(entity, this.createEntityStackNBT(entity.getType(), stackSize, entity.getLocation()));
this.stackManager.addEntityStack(stackedEntity);
stacks.add(stackedEntity);
}
return stacks;
}
use of dev.rosewood.rosestacker.stack.StackedEntity 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.stack.StackedEntity in project RoseStacker by Rosewood-Development.
the class MaxNearbyEntityConditionTag method check.
@Override
public boolean check(StackedSpawner stackedSpawner, Block spawnBlock) {
if (this.stackManager == null || this.entityCacheManager == null) {
this.stackManager = RoseStacker.getInstance().getManager(StackManager.class);
this.entityCacheManager = RoseStacker.getInstance().getManager(EntityCacheManager.class);
}
SpawnerStackSettings stackSettings = stackedSpawner.getStackSettings();
int detectionRange = stackSettings.getEntitySearchRange() == -1 ? stackedSpawner.getSpawnerTile().getSpawnRange() : stackSettings.getEntitySearchRange();
Block block = stackedSpawner.getBlock();
EntityType entityType = stackedSpawner.getSpawnerTile().getSpawnedType();
Collection<Entity> nearbyEntities = this.entityCacheManager.getNearbyEntities(block.getLocation().clone().add(0.5, 0.5, 0.5), detectionRange, entity -> entity.getType() == entityType);
if (Setting.SPAWNER_MAX_NEARBY_ENTITIES_INCLUDE_STACKS.getBoolean()) {
return nearbyEntities.stream().mapToInt(x -> {
StackedEntity stackedEntity = this.stackManager.getStackedEntity((LivingEntity) x);
return stackedEntity == null ? 1 : stackedEntity.getStackSize();
}).sum() < this.maxNearbyEntities;
} else {
return nearbyEntities.size() < this.maxNearbyEntities;
}
}
use of dev.rosewood.rosestacker.stack.StackedEntity in project RoseStacker by Rosewood-Development.
the class DataUtils method readStackedEntity.
public static StackedEntity readStackedEntity(LivingEntity entity) {
PersistentDataContainer pdc = entity.getPersistentDataContainer();
NMSHandler nmsHandler = NMSAdapter.getHandler();
byte[] data = pdc.get(ENTITY_KEY, PersistentDataType.BYTE_ARRAY);
if (data == null)
return new StackedEntity(entity, nmsHandler.createEntityDataStorage(entity));
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
ObjectInputStream dataInput = new ObjectInputStream(new GZIPInputStream(inputStream))) {
int dataVersion = dataInput.readInt();
if (dataVersion == 1) {
int length = dataInput.readInt();
byte[] nbt = new byte[length];
for (int i = 0; i < length; i++) nbt[i] = dataInput.readByte();
return new StackedEntity(entity, nmsHandler.deserializeEntityDataStorage(nbt));
}
} catch (Exception e) {
e.printStackTrace();
pdc.remove(ENTITY_KEY);
}
return null;
}
use of dev.rosewood.rosestacker.stack.StackedEntity 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