use of dev.rosewood.rosestacker.nms.NMSHandler 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.nms.NMSHandler in project RoseStacker by Rosewood-Development.
the class ItemUtils method getEntityAsStackedItemStack.
public static ItemStack getEntityAsStackedItemStack(EntityType entityType, int amount) {
EntityStackSettings stackSettings = RoseStacker.getInstance().getManager(StackSettingManager.class).getEntityStackSettings(entityType);
Material spawnEggMaterial = stackSettings.getEntityTypeData().getSpawnEggMaterial();
if (spawnEggMaterial == null)
return null;
ItemStack itemStack = new ItemStack(spawnEggMaterial);
if (amount == 1)
return itemStack;
ItemMeta itemMeta = itemStack.getItemMeta();
if (itemMeta == null)
return itemStack;
StringPlaceholders placeholders = StringPlaceholders.builder("amount", amount).addPlaceholder("name", stackSettings.getDisplayName()).build();
String displayString = RoseStacker.getInstance().getManager(LocaleManager.class).getLocaleMessage("entity-stack-display-spawn-egg", placeholders);
itemMeta.setDisplayName(displayString);
// Set the lore, if defined
List<String> lore = RoseStacker.getInstance().getManager(LocaleManager.class).getLocaleMessages("stack-item-lore-entity", placeholders);
if (!lore.isEmpty())
itemMeta.setLore(lore);
itemStack.setItemMeta(itemMeta);
// Set stack size
NMSHandler nmsHandler = NMSAdapter.getHandler();
itemStack = nmsHandler.setItemStackNBT(itemStack, "StackSize", amount);
return itemStack;
}
use of dev.rosewood.rosestacker.nms.NMSHandler in project RoseStacker by Rosewood-Development.
the class ItemUtils method getBlockAsStackedItemStack.
public static ItemStack getBlockAsStackedItemStack(Material material, int amount) {
ItemStack itemStack = new ItemStack(material);
if (amount == 1)
return itemStack;
ItemMeta itemMeta = itemStack.getItemMeta();
if (itemMeta == null)
return itemStack;
BlockStackSettings stackSettings = RoseStacker.getInstance().getManager(StackSettingManager.class).getBlockStackSettings(material);
StringPlaceholders placeholders = StringPlaceholders.builder("amount", amount).addPlaceholder("name", stackSettings.getDisplayName()).build();
String displayString = RoseStacker.getInstance().getManager(LocaleManager.class).getLocaleMessage("block-stack-display", placeholders);
itemMeta.setDisplayName(displayString);
// Set the lore, if defined
List<String> lore = RoseStacker.getInstance().getManager(LocaleManager.class).getLocaleMessages("stack-item-lore-block", placeholders);
if (!lore.isEmpty())
itemMeta.setLore(lore);
itemStack.setItemMeta(itemMeta);
// Set stack size
NMSHandler nmsHandler = NMSAdapter.getHandler();
itemStack = nmsHandler.setItemStackNBT(itemStack, "StackSize", amount);
return itemStack;
}
use of dev.rosewood.rosestacker.nms.NMSHandler in project RoseStacker by Rosewood-Development.
the class EntityListener method handleEntityTransformation.
private void handleEntityTransformation(EntityTransformEvent event) {
if (this.stackManager.isWorldDisabled(event.getEntity().getWorld()))
return;
if (!this.stackManager.isEntityStackingEnabled())
return;
if (event.getEntity() instanceof Slime) {
if (PersistentDataUtils.isAiDisabled((LivingEntity) event.getEntity()))
event.getTransformedEntities().stream().map(x -> (Slime) x).forEach(PersistentDataUtils::removeEntityAi);
return;
}
if (!(event.getEntity() instanceof LivingEntity) || !(event.getTransformedEntity() instanceof LivingEntity) || event.getEntity().getType() == event.getTransformedEntity().getType() || !this.stackManager.isEntityStacked((LivingEntity) event.getEntity()))
return;
StackedEntity stackedEntity = this.stackManager.getStackedEntity((LivingEntity) event.getEntity());
if (stackedEntity.getStackSize() == 1)
return;
LivingEntity transformedEntity = (LivingEntity) event.getTransformedEntity();
if (Setting.ENTITY_TRANSFORM_ENTIRE_STACK.getBoolean()) {
NMSHandler nmsHandler = NMSAdapter.getHandler();
StackedEntityDataEntry<?> serialized = nmsHandler.getEntityAsNBT(transformedEntity);
event.setCancelled(true);
// Handle mooshroom shearing
if (event.getEntityType() == EntityType.MUSHROOM_COW) {
MushroomCowStackSettings stackSettings = (MushroomCowStackSettings) stackedEntity.getStackSettings();
int mushroomsDropped = 5;
if (stackSettings.shouldDropAdditionalMushroomsPerCowInStack())
mushroomsDropped += (stackedEntity.getStackSize() - 1) * stackSettings.getExtraMushroomsPerCowInStack();
Material dropType = ((MushroomCow) event.getEntity()).getVariant() == Variant.BROWN ? Material.BROWN_MUSHROOM : Material.RED_MUSHROOM;
this.stackManager.preStackItems(GuiUtil.getMaterialAmountAsItemStacks(dropType, mushroomsDropped), event.getEntity().getLocation());
}
boolean aiDisabled = PersistentDataUtils.isAiDisabled((LivingEntity) event.getEntity());
event.getEntity().remove();
Bukkit.getScheduler().scheduleSyncDelayedTask(this.rosePlugin, () -> {
this.stackManager.setEntityStackingTemporarilyDisabled(true);
LivingEntity newEntity = nmsHandler.createEntityFromNBT(serialized, transformedEntity.getLocation(), true, transformedEntity.getType());
if (aiDisabled)
PersistentDataUtils.removeEntityAi(newEntity);
StackedEntity newStack = this.stackManager.createEntityStack(newEntity, false);
this.stackManager.setEntityStackingTemporarilyDisabled(false);
if (newStack == null)
return;
for (StackedEntityDataEntry<?> serializedEntity : stackedEntity.getStackedEntityNBT().getAll()) {
LivingEntity entity = nmsHandler.createEntityFromNBT(serializedEntity, transformedEntity.getLocation(), false, transformedEntity.getType());
if (aiDisabled)
PersistentDataUtils.removeEntityAi(entity);
newStack.increaseStackSize(entity);
}
});
} else {
// Make sure disabled AI gets transferred
if (PersistentDataUtils.isAiDisabled((LivingEntity) event.getEntity()))
PersistentDataUtils.removeEntityAi((LivingEntity) event.getTransformedEntity());
if (event.getTransformReason() == TransformReason.LIGHTNING) {
// Wait for lightning to disappear
Bukkit.getScheduler().scheduleSyncDelayedTask(this.rosePlugin, stackedEntity::decreaseStackSize, 20);
} else {
Bukkit.getScheduler().runTask(this.rosePlugin, stackedEntity::decreaseStackSize);
}
}
}
use of dev.rosewood.rosestacker.nms.NMSHandler in project RoseStacker by Rosewood-Development.
the class StackingThread method stackEntities.
private void stackEntities() {
boolean itemStackingEnabled = this.stackManager.isItemStackingEnabled();
boolean entityStackingEnabled = this.stackManager.isEntityStackingEnabled();
if (!entityStackingEnabled)
return;
// Auto unstack entities
if (!this.stackManager.isEntityUnstackingTemporarilyDisabled()) {
boolean minSplitIfLower = Setting.ENTITY_MIN_SPLIT_IF_LOWER.getBoolean();
for (StackedEntity stackedEntity : this.stackedEntities.values()) {
if (!stackedEntity.shouldStayStacked() && stackedEntity.getEntity().isValid()) {
Bukkit.getScheduler().runTask(this.rosePlugin, () -> {
if (stackedEntity.getStackSize() > 1)
this.splitEntityStack(stackedEntity);
});
} else if (minSplitIfLower && stackedEntity.getStackSize() < stackedEntity.getStackSettings().getMinStackSize()) {
NMSHandler nmsHandler = NMSAdapter.getHandler();
StackedEntityDataStorage nbt = stackedEntity.getStackedEntityNBT();
stackedEntity.setStackedEntityNBT(nmsHandler.createEntityDataStorage(stackedEntity.getEntity()));
Bukkit.getScheduler().runTask(this.rosePlugin, () -> {
for (StackedEntityDataEntry<?> stackedEntityDataEntry : nbt.getAll()) nmsHandler.createEntityFromNBT(stackedEntityDataEntry, stackedEntity.getLocation(), true, stackedEntity.getEntity().getType());
});
}
}
}
// Auto stack entities
if (this.entityStackSwitch) {
for (StackedEntity stackedEntity : this.stackedEntities.values()) {
LivingEntity livingEntity = stackedEntity.getEntity();
if (this.isRemoved(livingEntity)) {
this.removeEntityStack(stackedEntity);
continue;
}
this.tryStackEntity(stackedEntity);
}
}
// Run entity stacking half as often as the unstacking
this.entityStackSwitch = !this.entityStackSwitch;
// Cleans up entities/items that aren't stacked
this.cleanupTimer++;
if (this.cleanupTimer >= CLEANUP_TIMER_TARGET) {
Bukkit.getScheduler().runTask(this.rosePlugin, () -> {
for (Entity entity : this.targetWorld.getEntities()) {
if (this.isRemoved(entity))
continue;
if (entity instanceof LivingEntity && entity.getType() != EntityType.ARMOR_STAND && entity.getType() != EntityType.PLAYER) {
LivingEntity livingEntity = (LivingEntity) entity;
if (!this.isEntityStacked(livingEntity))
this.createEntityStack(livingEntity, false);
} else if (itemStackingEnabled && entity.getType() == EntityType.DROPPED_ITEM) {
Item item = (Item) entity;
if (!this.isItemStacked(item))
this.createItemStack(item, false);
}
}
});
this.cleanupTimer = 0;
}
}
Aggregations