Search in sources :

Example 1 with NMSHandler

use of dev.rosewood.rosestacker.nms.NMSHandler in project RoseStacker by Rosewood-Development.

the class ItemUtils method getStackedItemEntityType.

public static EntityType getStackedItemEntityType(ItemStack itemStack) {
    if (itemStack.getType() != Material.SPAWNER)
        return null;
    // First, check our NBT value
    NMSHandler nmsHandler = NMSAdapter.getHandler();
    String entityTypeName = nmsHandler.getItemStackNBTString(itemStack, "EntityType");
    if (!entityTypeName.isEmpty()) {
        try {
            return EntityType.valueOf(entityTypeName);
        } catch (Exception ignored) {
        }
    }
    // Try formats from other plugins/servers
    // Purpur servers
    entityTypeName = nmsHandler.getItemStackNBTString(itemStack, "Purpur.mob_type");
    // EpicSpawners Pre-v7
    if (entityTypeName.isEmpty())
        entityTypeName = nmsHandler.getItemStackNBTString(itemStack, "type").toUpperCase().replace(' ', '_');
    // EpicSpawners Post-v7
    if (entityTypeName.isEmpty())
        entityTypeName = nmsHandler.getItemStackNBTString(itemStack, "data");
    if (!entityTypeName.isEmpty()) {
        try {
            NamespacedKey entityTypeKey = NamespacedKey.fromString(entityTypeName);
            for (EntityType entityType : EntityType.values()) if (entityType != EntityType.UNKNOWN && entityType.getKey().equals(entityTypeKey) || entityTypeName.equalsIgnoreCase(entityType.name()))
                return EntityType.valueOf(entityTypeName);
        } catch (Exception ignored) {
        }
    }
    // Try checking the spawner data then?
    ItemMeta itemMeta = itemStack.getItemMeta();
    if (itemMeta == null)
        return EntityType.PIG;
    BlockStateMeta blockStateMeta = (BlockStateMeta) itemMeta;
    CreatureSpawner creatureSpawner = (CreatureSpawner) blockStateMeta.getBlockState();
    if (creatureSpawner.getSpawnedType() != EntityType.PIG)
        return creatureSpawner.getSpawnedType();
    // Use the name to determine the type, name must be colored
    String name = ChatColor.stripColor(itemMeta.getDisplayName());
    if (!name.equals(itemMeta.getDisplayName())) {
        try {
            // This tries to support other spawner plugins by checking the item name
            name = name.toUpperCase();
            int spawnerIndex = name.indexOf("SPAWNER");
            String entityName = name.substring(0, spawnerIndex).trim();
            return EntityType.valueOf(entityName.replaceAll(" ", "_"));
        } catch (Exception ignored) {
        }
    }
    return EntityType.PIG;
}
Also used : EntityType(org.bukkit.entity.EntityType) BlockStateMeta(org.bukkit.inventory.meta.BlockStateMeta) NamespacedKey(org.bukkit.NamespacedKey) NMSHandler(dev.rosewood.rosestacker.nms.NMSHandler) ItemMeta(org.bukkit.inventory.meta.ItemMeta) CreatureSpawner(org.bukkit.block.CreatureSpawner)

Example 2 with NMSHandler

use of dev.rosewood.rosestacker.nms.NMSHandler in project RoseStacker by Rosewood-Development.

the class ItemUtils method getStackedItemStackAmount.

public static int getStackedItemStackAmount(ItemStack itemStack) {
    NMSHandler nmsHandler = NMSAdapter.getHandler();
    int stackSize = nmsHandler.getItemStackNBTInt(itemStack, "StackSize");
    return Math.max(stackSize, 1);
}
Also used : NMSHandler(dev.rosewood.rosestacker.nms.NMSHandler)

Example 3 with NMSHandler

use of dev.rosewood.rosestacker.nms.NMSHandler in project RoseStacker by Rosewood-Development.

the class ItemUtils method getSpawnerAsStackedItemStack.

public static ItemStack getSpawnerAsStackedItemStack(EntityType entityType, int amount) {
    ItemStack itemStack = new ItemStack(Material.SPAWNER);
    ItemMeta itemMeta = itemStack.getItemMeta();
    if (itemMeta == null)
        return itemStack;
    SpawnerStackSettings stackSettings = RoseStacker.getInstance().getManager(StackSettingManager.class).getSpawnerStackSettings(entityType);
    StringPlaceholders placeholders = StringPlaceholders.builder("amount", amount).addPlaceholder("name", stackSettings.getDisplayName()).build();
    String displayString;
    if (amount == 1) {
        displayString = RoseStacker.getInstance().getManager(LocaleManager.class).getLocaleMessage("spawner-stack-display-single", placeholders);
    } else {
        displayString = RoseStacker.getInstance().getManager(LocaleManager.class).getLocaleMessage("spawner-stack-display", placeholders);
    }
    itemMeta.setDisplayName(displayString);
    // Set the lore, if defined
    List<String> lore = RoseStacker.getInstance().getManager(LocaleManager.class).getLocaleMessages("stack-item-lore-spawner", placeholders);
    if (!lore.isEmpty())
        itemMeta.setLore(lore);
    // Set the spawned type directly onto the spawner item for hopeful compatibility with other plugins
    BlockStateMeta blockStateMeta = (BlockStateMeta) itemMeta;
    CreatureSpawner creatureSpawner = (CreatureSpawner) blockStateMeta.getBlockState();
    creatureSpawner.setSpawnedType(entityType);
    blockStateMeta.setBlockState(creatureSpawner);
    itemStack.setItemMeta(itemMeta);
    // Set stack size and spawned entity type
    NMSHandler nmsHandler = NMSAdapter.getHandler();
    itemStack = nmsHandler.setItemStackNBT(itemStack, "StackSize", amount);
    itemStack = nmsHandler.setItemStackNBT(itemStack, "EntityType", entityType.name());
    return itemStack;
}
Also used : BlockStateMeta(org.bukkit.inventory.meta.BlockStateMeta) SpawnerStackSettings(dev.rosewood.rosestacker.stack.settings.SpawnerStackSettings) StringPlaceholders(dev.rosewood.rosegarden.utils.StringPlaceholders) StackSettingManager(dev.rosewood.rosestacker.manager.StackSettingManager) LocaleManager(dev.rosewood.rosestacker.manager.LocaleManager) ItemStack(org.bukkit.inventory.ItemStack) NMSHandler(dev.rosewood.rosestacker.nms.NMSHandler) ItemMeta(org.bukkit.inventory.meta.ItemMeta) CreatureSpawner(org.bukkit.block.CreatureSpawner)

Example 4 with NMSHandler

use of dev.rosewood.rosestacker.nms.NMSHandler in project RoseStacker by Rosewood-Development.

the class StackerUtils method deconstructStackedEntities.

public static List<LivingEntity> deconstructStackedEntities(StackedEntity stackedEntity) {
    List<StackedEntityDataEntry<?>> stackedEntityDataEntry = stackedEntity.getStackedEntityNBT().getAll();
    List<LivingEntity> livingEntities = new ArrayList<>(stackedEntityDataEntry.size());
    Location location = stackedEntity.getLocation();
    NMSHandler nmsHandler = NMSAdapter.getHandler();
    for (StackedEntityDataEntry<?> nbt : stackedEntityDataEntry) livingEntities.add(nmsHandler.createEntityFromNBT(nbt, location, false, stackedEntity.getEntity().getType()));
    return livingEntities;
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) ArrayList(java.util.ArrayList) StackedEntityDataEntry(dev.rosewood.rosestacker.nms.storage.StackedEntityDataEntry) NMSHandler(dev.rosewood.rosestacker.nms.NMSHandler) Location(org.bukkit.Location)

Example 5 with NMSHandler

use of dev.rosewood.rosestacker.nms.NMSHandler in project RoseStacker by Rosewood-Development.

the class PersistentDataUtils method applyDisabledAi.

public static void applyDisabledAi(LivingEntity entity) {
    if (isAiDisabled(entity) || Setting.ENTITY_DISABLE_ALL_MOB_AI.getBoolean()) {
        if (Setting.SPAWNER_DISABLE_MOB_AI_OPTIONS_REMOVE_GOALS.getBoolean()) {
            NMSHandler nmsHandler = NMSAdapter.getHandler();
            nmsHandler.removeEntityGoals(entity);
        }
        if (Setting.SPAWNER_DISABLE_MOB_AI_OPTIONS_SILENCE.getBoolean())
            entity.setSilent(true);
        if (Setting.SPAWNER_DISABLE_MOB_AI_OPTIONS_NO_KNOCKBACK.getBoolean()) {
            // Make the entity unable to take knockback
            AttributeInstance knockbackAttribute = entity.getAttribute(Attribute.GENERIC_KNOCKBACK_RESISTANCE);
            if (knockbackAttribute != null)
                knockbackAttribute.setBaseValue(Double.MAX_VALUE);
        }
    }
}
Also used : AttributeInstance(org.bukkit.attribute.AttributeInstance) NMSHandler(dev.rosewood.rosestacker.nms.NMSHandler)

Aggregations

NMSHandler (dev.rosewood.rosestacker.nms.NMSHandler)19 LivingEntity (org.bukkit.entity.LivingEntity)10 StackSettingManager (dev.rosewood.rosestacker.manager.StackSettingManager)6 ArrayList (java.util.ArrayList)6 ItemStack (org.bukkit.inventory.ItemStack)6 StackManager (dev.rosewood.rosestacker.manager.StackManager)4 StackedEntityDataEntry (dev.rosewood.rosestacker.nms.storage.StackedEntityDataEntry)4 StackedEntityDataStorage (dev.rosewood.rosestacker.nms.storage.StackedEntityDataStorage)4 EntityStackSettings (dev.rosewood.rosestacker.stack.settings.EntityStackSettings)4 Collection (java.util.Collection)4 Material (org.bukkit.Material)4 StringPlaceholders (dev.rosewood.rosegarden.utils.StringPlaceholders)3 WorldGuardHook (dev.rosewood.rosestacker.hook.WorldGuardHook)3 Setting (dev.rosewood.rosestacker.manager.ConfigurationManager.Setting)3 EntityCacheManager (dev.rosewood.rosestacker.manager.EntityCacheManager)3 LocaleManager (dev.rosewood.rosestacker.manager.LocaleManager)3 NMSAdapter (dev.rosewood.rosestacker.nms.NMSAdapter)3 StackedEntity (dev.rosewood.rosestacker.stack.StackedEntity)3 PersistentDataUtils (dev.rosewood.rosestacker.utils.PersistentDataUtils)3 StackerUtils (dev.rosewood.rosestacker.utils.StackerUtils)3