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);
}
}
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);
}
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;
}
}
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;
}
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);
}
}
Aggregations