Search in sources :

Example 1 with ItemStackSettings

use of dev.rosewood.rosestacker.stack.settings.ItemStackSettings in project RoseStacker by Rosewood-Development.

the class EntityListener method onEntitySpawn.

@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onEntitySpawn(EntitySpawnEvent event) {
    Entity entity = event.getEntity();
    if (this.stackManager.isWorldDisabled(entity.getWorld()))
        return;
    if (!this.stackManager.isItemStackingEnabled() || this.stackManager.isEntityStackingTemporarilyDisabled())
        return;
    if (entity instanceof Item) {
        ItemStackSettings itemStackSettings = this.stackSettingManager.getItemStackSettings((Item) event.getEntity());
        if (itemStackSettings != null && !itemStackSettings.isStackingEnabled())
            return;
        this.entityCacheManager.preCacheEntity(entity);
        this.stackManager.createItemStack((Item) entity, true);
    }
}
Also used : ItemStackSettings(dev.rosewood.rosestacker.stack.settings.ItemStackSettings) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) StackedEntity(dev.rosewood.rosestacker.stack.StackedEntity) Item(org.bukkit.entity.Item) StackedItem(dev.rosewood.rosestacker.stack.StackedItem) EventHandler(org.bukkit.event.EventHandler)

Example 2 with ItemStackSettings

use of dev.rosewood.rosestacker.stack.settings.ItemStackSettings in project RoseStacker by Rosewood-Development.

the class ItemListener method onItemMerge.

@EventHandler(priority = EventPriority.LOWEST)
public void onItemMerge(ItemMergeEvent event) {
    if (this.stackManager.isWorldDisabled(event.getEntity().getWorld()) || !this.stackManager.isItemStackingEnabled())
        return;
    ItemStackSettings itemStackSettings = this.stackSettingManager.getItemStackSettings(event.getEntity());
    if (itemStackSettings != null && !itemStackSettings.isStackingEnabled())
        return;
    // We will handle all item merging ourselves, thank you very much
    event.setCancelled(true);
}
Also used : ItemStackSettings(dev.rosewood.rosestacker.stack.settings.ItemStackSettings) EventHandler(org.bukkit.event.EventHandler)

Example 3 with ItemStackSettings

use of dev.rosewood.rosestacker.stack.settings.ItemStackSettings in project RoseStacker by Rosewood-Development.

the class StackSettingManager method reload.

@Override
public void reload() {
    // Settings files
    File blockSettingsFile = this.getBlockSettingsFile();
    File entitySettingsFile = this.getEntitySettingsFile();
    File itemSettingsFile = this.getItemSettingsFile();
    File spawnerSettingsFile = this.getSpawnerSettingsFile();
    // Flags for if we should save the files
    AtomicBoolean saveBlockSettingsFile = new AtomicBoolean(false);
    AtomicBoolean saveEntitySettingsFile = new AtomicBoolean(false);
    AtomicBoolean saveItemSettingsFile = new AtomicBoolean(false);
    AtomicBoolean saveSpawnerSettingsFile = new AtomicBoolean(false);
    // Load block settings
    CommentedFileConfiguration blockSettingsConfiguration = CommentedFileConfiguration.loadConfiguration(blockSettingsFile);
    StackerUtils.getPossibleStackableBlockMaterials().forEach(x -> {
        BlockStackSettings blockStackSettings = new BlockStackSettings(blockSettingsConfiguration, x);
        this.blockSettings.put(x, blockStackSettings);
        if (blockStackSettings.hasChanges())
            saveBlockSettingsFile.set(true);
    });
    // Load entity settings and data from entity_data.json
    CommentedFileConfiguration entitySettingsConfiguration = CommentedFileConfiguration.loadConfiguration(entitySettingsFile);
    try (InputStream entityDataStream = this.getClass().getResourceAsStream("/entity_data.json");
        Reader entityDataReader = new InputStreamReader(entityDataStream)) {
        JsonParser jsonParser = new JsonParser();
        JsonObject jsonObject = jsonParser.parse(entityDataReader).getAsJsonObject();
        List<Class<EntityStackSettings>> classes = ClassUtils.getClassesOf(this.rosePlugin, PACKAGE_PATH, EntityStackSettings.class);
        List<String> ignoredLoading = new ArrayList<>();
        for (Class<EntityStackSettings> clazz : classes) {
            try {
                EntityStackSettings entityStackSetting = clazz.getConstructor(CommentedFileConfiguration.class, JsonObject.class).newInstance(entitySettingsConfiguration, jsonObject);
                this.entitySettings.put(entityStackSetting.getEntityType(), entityStackSetting);
                if (entityStackSetting.hasChanges())
                    saveEntitySettingsFile.set(true);
            } catch (Exception e) {
                // Log entity settings that failed to load
                // This should only be caused by version incompatibilities
                String className = clazz.getSimpleName();
                ignoredLoading.add(className.substring(0, className.length() - 13));
            }
        }
        if (!ignoredLoading.isEmpty())
            this.rosePlugin.getLogger().warning("Ignored loading stack settings for entities: " + ignoredLoading);
    } catch (Exception e) {
        e.printStackTrace();
    }
    // Load item settings
    CommentedFileConfiguration itemSettingsConfiguration = CommentedFileConfiguration.loadConfiguration(itemSettingsFile);
    Stream.of(Material.values()).sorted(Comparator.comparing(Enum::name)).forEach(x -> {
        ItemStackSettings itemStackSettings = new ItemStackSettings(itemSettingsConfiguration, x);
        this.itemSettings.put(x, itemStackSettings);
        if (itemStackSettings.hasChanges())
            saveItemSettingsFile.set(true);
    });
    // Load spawner settings
    boolean addSpawnerHeaderComments = !spawnerSettingsFile.exists();
    CommentedFileConfiguration spawnerSettingsConfiguration = CommentedFileConfiguration.loadConfiguration(spawnerSettingsFile);
    if (addSpawnerHeaderComments) {
        saveSpawnerSettingsFile.set(true);
        Map<String, String> conditionTags = ConditionTags.getTagDescriptionMap();
        spawnerSettingsConfiguration.addComments("Available Spawn Requirements:", "");
        for (Entry<String, String> entry : conditionTags.entrySet()) {
            String tag = entry.getKey();
            String description = entry.getValue();
            spawnerSettingsConfiguration.addComments(tag + " - " + description);
        }
        spawnerSettingsConfiguration.addComments("", "Valid Blocks: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Material.html", "Valid Biomes: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/block/Biome.html", "", "Want to remove all requirements? Set the value to the following:", "spawn-requirements: []");
    }
    StackerUtils.getAlphabeticalStackableEntityTypes().forEach(x -> {
        SpawnerStackSettings spawnerStackSettings = new SpawnerStackSettings(spawnerSettingsConfiguration, x);
        this.spawnerSettings.put(x, spawnerStackSettings);
        if (spawnerStackSettings.hasChanges())
            saveSpawnerSettingsFile.set(true);
    });
    // Save files if changes were made
    if (saveBlockSettingsFile.get())
        blockSettingsConfiguration.save(true);
    if (saveEntitySettingsFile.get())
        entitySettingsConfiguration.save(true);
    if (saveItemSettingsFile.get())
        itemSettingsConfiguration.save(true);
    if (saveSpawnerSettingsFile.get())
        spawnerSettingsConfiguration.save(true);
    // Register dynamic permissions on first load
    if (!this.registeredPermissions) {
        PluginManager pluginManager = Bukkit.getPluginManager();
        List<Permission> silktouch = new ArrayList<>();
        List<Permission> nosilk = new ArrayList<>();
        List<Permission> spawnerplace = new ArrayList<>();
        for (EntityType entityType : this.entitySettings.keySet()) {
            String type = entityType.name().toLowerCase();
            silktouch.add(new Permission("rosestacker.silktouch." + type));
            nosilk.add(new Permission("rosestacker.nosilk." + type));
            spawnerplace.add(new Permission("rosestacker.spawnerplace." + type));
        }
        // Register silktouch permissions
        silktouch.forEach(pluginManager::addPermission);
        pluginManager.addPermission(new Permission("rosestacker.silktouch.*", silktouch.stream().collect(Collectors.toMap(Permission::getName, x -> true))));
        // Register nosilk permissions
        nosilk.forEach(pluginManager::addPermission);
        pluginManager.addPermission(new Permission("rosestacker.nosilk.*", nosilk.stream().collect(Collectors.toMap(Permission::getName, x -> true))));
        // Register spawnerplace permissions
        spawnerplace.forEach(pluginManager::addPermission);
        pluginManager.addPermission(new Permission("rosestacker.spawnerplace.*", spawnerplace.stream().collect(Collectors.toMap(Permission::getName, x -> true))));
        this.registeredPermissions = true;
    }
}
Also used : JsonObject(com.google.gson.JsonObject) Manager(dev.rosewood.rosegarden.manager.Manager) Item(org.bukkit.entity.Item) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClassUtils(dev.rosewood.rosegarden.utils.ClassUtils) HashMap(java.util.HashMap) JsonParser(com.google.gson.JsonParser) ItemUtils(dev.rosewood.rosestacker.utils.ItemUtils) ArrayList(java.util.ArrayList) EntityStackSettings(dev.rosewood.rosestacker.stack.settings.EntityStackSettings) Block(org.bukkit.block.Block) Map(java.util.Map) CommentedFileConfiguration(dev.rosewood.rosegarden.config.CommentedFileConfiguration) ItemStackSettings(dev.rosewood.rosestacker.stack.settings.ItemStackSettings) Material(org.bukkit.Material) Bukkit(org.bukkit.Bukkit) CreatureSpawner(org.bukkit.block.CreatureSpawner) Set(java.util.Set) Reader(java.io.Reader) EntityType(org.bukkit.entity.EntityType) LivingEntity(org.bukkit.entity.LivingEntity) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) RosePlugin(dev.rosewood.rosegarden.RosePlugin) ConditionTags(dev.rosewood.rosestacker.spawner.conditions.ConditionTags) File(java.io.File) BlockStackSettings(dev.rosewood.rosestacker.stack.settings.BlockStackSettings) Permission(org.bukkit.permissions.Permission) List(java.util.List) Stream(java.util.stream.Stream) Entry(java.util.Map.Entry) StackerUtils(dev.rosewood.rosestacker.utils.StackerUtils) Comparator(java.util.Comparator) SpawnerStackSettings(dev.rosewood.rosestacker.stack.settings.SpawnerStackSettings) InputStream(java.io.InputStream) PluginManager(org.bukkit.plugin.PluginManager) ArrayList(java.util.ArrayList) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) JsonObject(com.google.gson.JsonObject) BlockStackSettings(dev.rosewood.rosestacker.stack.settings.BlockStackSettings) ItemStackSettings(dev.rosewood.rosestacker.stack.settings.ItemStackSettings) PluginManager(org.bukkit.plugin.PluginManager) CommentedFileConfiguration(dev.rosewood.rosegarden.config.CommentedFileConfiguration) Permission(org.bukkit.permissions.Permission) JsonParser(com.google.gson.JsonParser) EntityStackSettings(dev.rosewood.rosestacker.stack.settings.EntityStackSettings) SpawnerStackSettings(dev.rosewood.rosestacker.stack.settings.SpawnerStackSettings) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) EntityType(org.bukkit.entity.EntityType) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) File(java.io.File)

Example 4 with ItemStackSettings

use of dev.rosewood.rosestacker.stack.settings.ItemStackSettings in project RoseStacker by Rosewood-Development.

the class StackingThread method createItemStack.

@Override
public StackedItem createItemStack(Item item, boolean tryStack) {
    if (!this.stackManager.isItemStackingEnabled())
        return null;
    ItemStackSettings itemStackSettings = this.rosePlugin.getManager(StackSettingManager.class).getItemStackSettings(item);
    if (itemStackSettings != null && !itemStackSettings.isStackingEnabled())
        return null;
    StackedItem newStackedItem = new StackedItem(item.getItemStack().getAmount(), item);
    this.stackedItems.put(item.getUniqueId(), newStackedItem);
    if (tryStack) {
        item.setMetadata(NEW_METADATA, new FixedMetadataValue(this.rosePlugin, true));
        this.tryStackItem(newStackedItem);
        item.removeMetadata(NEW_METADATA, this.rosePlugin);
    }
    return newStackedItem;
}
Also used : ItemStackSettings(dev.rosewood.rosestacker.stack.settings.ItemStackSettings) FixedMetadataValue(org.bukkit.metadata.FixedMetadataValue) StackSettingManager(dev.rosewood.rosestacker.manager.StackSettingManager)

Example 5 with ItemStackSettings

use of dev.rosewood.rosestacker.stack.settings.ItemStackSettings in project RoseStacker by Rosewood-Development.

the class StackingThread method tryStackItem.

/**
 * Tries to stack a StackedItem with all other StackedItems
 *
 * @param stackedItem the StackedItem to try to stack
 */
private void tryStackItem(StackedItem stackedItem) {
    ItemStackSettings stackSettings = stackedItem.getStackSettings();
    if (stackSettings == null || !stackSettings.isStackingEnabled() || stackedItem.getItem().getPickupDelay() > 40)
        return;
    Item item = stackedItem.getItem();
    if (this.isRemoved(item))
        return;
    Predicate<Entity> predicate = x -> x.getType() == EntityType.DROPPED_ITEM;
    Set<Item> nearbyItems = this.entityCacheManager.getNearbyEntities(stackedItem.getLocation(), Setting.ITEM_MERGE_RADIUS.getDouble(), predicate).stream().map(x -> (Item) x).collect(Collectors.toSet());
    Set<StackedItem> targetItems = new HashSet<>();
    for (Item otherItem : nearbyItems) {
        if (item == otherItem || otherItem.getPickupDelay() > 40 || !item.getItemStack().isSimilar(otherItem.getItemStack()) || this.isRemoved(otherItem))
            continue;
        StackedItem other = this.stackedItems.get(otherItem.getUniqueId());
        if (other != null)
            targetItems.add(other);
    }
    int totalSize = stackedItem.getStackSize();
    Set<StackedItem> removable = new HashSet<>();
    for (StackedItem target : targetItems) {
        if (totalSize + target.getStackSize() <= stackSettings.getMaxStackSize()) {
            totalSize += target.getStackSize();
            removable.add(target);
        }
    }
    StackedItem headStack = stackedItem;
    for (StackedItem other : removable) {
        StackedItem increased = headStack.compareTo(other) > 0 ? headStack : other;
        StackedItem removed = increased == headStack ? other : headStack;
        headStack = increased;
        ItemStackEvent itemStackEvent = new ItemStackEvent(removed, increased);
        Bukkit.getPluginManager().callEvent(itemStackEvent);
        if (itemStackEvent.isCancelled())
            continue;
        increased.increaseStackSize(removed.getStackSize(), true);
        if (Setting.ITEM_RESET_DESPAWN_TIMER_ON_MERGE.getBoolean())
            // Reset the 5 minute pickup timer
            increased.getItem().setTicksLived(1);
        // Don't allow the item we just merged to get picked up or stacked
        removed.getItem().setPickupDelay(100);
        increased.getItem().setPickupDelay(5);
        Runnable removeTask = () -> removed.getItem().remove();
        if (Bukkit.isPrimaryThread()) {
            removeTask.run();
        } else {
            Bukkit.getScheduler().runTask(this.rosePlugin, removeTask);
        }
        this.removeItemStack(removed);
    }
}
Also used : ItemStackSettings(dev.rosewood.rosestacker.stack.settings.ItemStackSettings) StackedEntityDataEntry(dev.rosewood.rosestacker.nms.storage.StackedEntityDataEntry) EntityUtils(dev.rosewood.rosestacker.utils.EntityUtils) Player(org.bukkit.entity.Player) ItemUtils(dev.rosewood.rosestacker.utils.ItemUtils) PreDropStackedItemsEvent(dev.rosewood.rosestacker.event.PreDropStackedItemsEvent) EntityStackSettings(dev.rosewood.rosestacker.stack.settings.EntityStackSettings) StackSettingManager(dev.rosewood.rosestacker.manager.StackSettingManager) Block(org.bukkit.block.Block) Location(org.bukkit.Location) World(org.bukkit.World) Map(java.util.Map) PersistentDataUtils(dev.rosewood.rosestacker.utils.PersistentDataUtils) Chunk(org.bukkit.Chunk) ItemStackSettings(dev.rosewood.rosestacker.stack.settings.ItemStackSettings) Material(org.bukkit.Material) Bukkit(org.bukkit.Bukkit) Setting(dev.rosewood.rosestacker.manager.ConfigurationManager.Setting) Entity(org.bukkit.entity.Entity) Predicate(java.util.function.Predicate) ItemStackClearEvent(dev.rosewood.rosestacker.event.ItemStackClearEvent) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) UUID(java.util.UUID) EntityType(org.bukkit.entity.EntityType) LivingEntity(org.bukkit.entity.LivingEntity) EntityCacheManager(dev.rosewood.rosestacker.manager.EntityCacheManager) Collectors(java.util.stream.Collectors) RosePlugin(dev.rosewood.rosegarden.RosePlugin) EntityUnstackEvent(dev.rosewood.rosestacker.event.EntityUnstackEvent) StackedEntityDataStorage(dev.rosewood.rosestacker.nms.storage.StackedEntityDataStorage) ItemStack(org.bukkit.inventory.ItemStack) Objects(java.util.Objects) StackManager(dev.rosewood.rosestacker.manager.StackManager) List(java.util.List) Entry(java.util.Map.Entry) BukkitTask(org.bukkit.scheduler.BukkitTask) Optional(java.util.Optional) StackerUtils(dev.rosewood.rosestacker.utils.StackerUtils) NPCsHook(dev.rosewood.rosestacker.hook.NPCsHook) DustOptions(org.bukkit.Particle.DustOptions) Item(org.bukkit.entity.Item) WorldGuardHook(dev.rosewood.rosestacker.hook.WorldGuardHook) HashMap(java.util.HashMap) FixedMetadataValue(org.bukkit.metadata.FixedMetadataValue) DataUtils(dev.rosewood.rosestacker.utils.DataUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ItemStackEvent(dev.rosewood.rosestacker.event.ItemStackEvent) NMSHandler(dev.rosewood.rosestacker.nms.NMSHandler) SpawnReason(org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason) CreatureSpawner(org.bukkit.block.CreatureSpawner) Particle(org.bukkit.Particle) Vector(org.bukkit.util.Vector) EntityStackEvent(dev.rosewood.rosestacker.event.EntityStackEvent) EntityStackClearEvent(dev.rosewood.rosestacker.event.EntityStackClearEvent) ArmorStand(org.bukkit.entity.ArmorStand) NMSAdapter(dev.rosewood.rosestacker.nms.NMSAdapter) Item(org.bukkit.entity.Item) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) HashSet(java.util.HashSet) ItemStackEvent(dev.rosewood.rosestacker.event.ItemStackEvent)

Aggregations

ItemStackSettings (dev.rosewood.rosestacker.stack.settings.ItemStackSettings)5 Item (org.bukkit.entity.Item)3 LivingEntity (org.bukkit.entity.LivingEntity)3 RosePlugin (dev.rosewood.rosegarden.RosePlugin)2 StackSettingManager (dev.rosewood.rosestacker.manager.StackSettingManager)2 EntityStackSettings (dev.rosewood.rosestacker.stack.settings.EntityStackSettings)2 ItemUtils (dev.rosewood.rosestacker.utils.ItemUtils)2 StackerUtils (dev.rosewood.rosestacker.utils.StackerUtils)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 Bukkit (org.bukkit.Bukkit)2 Material (org.bukkit.Material)2 Block (org.bukkit.block.Block)2 CreatureSpawner (org.bukkit.block.CreatureSpawner)2 EntityType (org.bukkit.entity.EntityType)2