Moved to event-driven loot generation

This commit is contained in:
Auxilor
2022-07-07 23:04:50 +01:00
parent 0bbe16376f
commit 0e13cb35c1
2 changed files with 24 additions and 96 deletions

View File

@@ -1,6 +1,5 @@
package com.willfp.ecoenchants;
import com.willfp.eco.core.Prerequisite;
import com.willfp.eco.core.command.impl.PluginCommand;
import com.willfp.eco.core.display.DisplayModule;
import com.willfp.eco.core.fast.FastItemStack;
@@ -21,9 +20,8 @@ import com.willfp.ecoenchants.enchantments.custom.CustomEnchantLookup;
import com.willfp.ecoenchants.enchantments.support.merging.anvil.AnvilListeners;
import com.willfp.ecoenchants.enchantments.support.merging.grindstone.GrindstoneListeners;
import com.willfp.ecoenchants.enchantments.support.obtaining.EnchantingListeners;
import com.willfp.ecoenchants.enchantments.support.obtaining.LootPopulator;
import com.willfp.ecoenchants.enchantments.support.obtaining.LootGenerateListeners;
import com.willfp.ecoenchants.enchantments.support.obtaining.VillagerListeners;
import com.willfp.ecoenchants.enchantments.util.EnchantmentUtils;
import com.willfp.ecoenchants.enchantments.util.ItemConversions;
import com.willfp.ecoenchants.enchantments.util.LazyHealthFixListener;
import com.willfp.ecoenchants.enchantments.util.TimedRunnable;
@@ -34,11 +32,6 @@ import com.willfp.ecoenchants.integrations.registration.RegistrationManager;
import com.willfp.ecoenchants.integrations.registration.plugins.IntegrationCMI;
import com.willfp.ecoenchants.integrations.registration.plugins.IntegrationEssentials;
import com.willfp.libreforge.LibReforgePlugin;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.enchantments.EnchantmentWrapper;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.jetbrains.annotations.NotNull;
@@ -97,13 +90,6 @@ public class EcoEnchantsPlugin extends LibReforgePlugin {
TelekinesisUtils.registerTest(player -> FastItemStack.wrap(player.getInventory().getItemInMainHand()).getEnchantmentLevel(EcoEnchants.TELEKINESIS, false) > 0);
}
@Override
public void handleDisableAdditional() {
for (World world : Bukkit.getServer().getWorlds()) {
world.getPopulators().removeIf(blockPopulator -> blockPopulator instanceof LootPopulator);
}
}
@Override
public void handleReloadAdditional() {
this.getDisplayModule().update();
@@ -126,16 +112,6 @@ public class EcoEnchantsPlugin extends LibReforgePlugin {
}, 300, 300);
}
@Override
protected void handleAfterLoad() {
if (this.getConfigYml().getBool("loot.enabled")) {
for (World world : Bukkit.getServer().getWorlds()) {
world.getPopulators().removeIf(blockPopulator -> blockPopulator instanceof LootPopulator);
world.getPopulators().add(new LootPopulator(this));
}
}
}
@Override
@NotNull
public List<IntegrationLoader> loadAdditionalIntegrations() {
@@ -165,7 +141,8 @@ public class EcoEnchantsPlugin extends LibReforgePlugin {
new ItemConversions(this),
new CustomEnchantEnableListeners(this),
new CustomEcoEnchantRequirementListeners(this),
new LazyHealthFixListener(this)
new LazyHealthFixListener(this),
new LootGenerateListeners(this)
);
}

View File

@@ -1,5 +1,6 @@
package com.willfp.ecoenchants.enchantments.support.obtaining;
import com.willfp.eco.core.PluginDependent;
import com.willfp.eco.core.fast.FastItemStack;
import com.willfp.eco.util.NumberUtils;
import com.willfp.ecoenchants.EcoEnchantsPlugin;
@@ -7,17 +8,11 @@ import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.EcoEnchants;
import com.willfp.ecoenchants.enchantments.meta.EnchantmentTarget;
import com.willfp.ecoenchants.enchantments.meta.EnchantmentType;
import org.bukkit.Chunk;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Chest;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Entity;
import org.bukkit.entity.minecart.StorageMinecart;
import org.bukkit.generator.BlockPopulator;
import org.bukkit.inventory.Inventory;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.world.LootGenerateEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.meta.ItemMeta;
@@ -29,54 +24,27 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
@SuppressWarnings("deprecation")
public class LootPopulator extends BlockPopulator {
/**
* Instance of ecoenchants.
*/
private final EcoEnchantsPlugin plugin;
public class LootGenerateListeners extends PluginDependent<EcoEnchantsPlugin> implements Listener {
/**
* Create a new loot populator.
*
* @param plugin The plugin.
* @param plugin The this.getPlugin().
*/
public LootPopulator(@NotNull final EcoEnchantsPlugin plugin) {
this.plugin = plugin;
public LootGenerateListeners(@NotNull final EcoEnchantsPlugin plugin) {
super(plugin);
}
/**
* Populate a chunk's loot chests and minecarts with chests.
*
* @param world The world to populate.
* @param random Bukkit parity.
* @param chunk The chunk to populate.
*/
public void populate(@NotNull final World world,
@NotNull final Random random,
@NotNull final Chunk chunk) {
if (!plugin.getConfigYml().getBool("loot.enabled")) {
@EventHandler
public void onGenerate(@NotNull final LootGenerateEvent event) {
if (!this.getPlugin().getConfigYml().getBool("loot.enabled")) {
return;
}
for (Entity entity : chunk.getEntities()) {
if (!(entity instanceof StorageMinecart minecart)) {
continue;
}
modifyInventory(minecart.getInventory(), chunk);
}
for (BlockState state : chunk.getTileEntities()) {
Block block = state.getBlock();
if (!(block.getState() instanceof Chest chestState)) {
continue;
}
Inventory inventory = chestState.getBlockInventory();
modifyInventory(inventory, chunk);
for (ItemStack itemStack : event.getLoot()) {
modifyItem(itemStack);
}
}
@@ -100,11 +68,11 @@ public class LootPopulator extends BlockPopulator {
double multiplier = 0.01;
if (item.getType().equals(Material.BOOK) || item.getType().equals(Material.ENCHANTED_BOOK)) {
multiplier /= plugin.getConfigYml().getInt("loot.book-times-less-likely");
multiplier /= this.getPlugin().getConfigYml().getInt("loot.book-times-less-likely");
}
if (plugin.getConfigYml().getBool("loot.reduce-probability.enabled")) {
multiplier /= plugin.getConfigYml().getDouble("loot.reduce-probability.factor");
if (this.getPlugin().getConfigYml().getBool("loot.reduce-probability.enabled")) {
multiplier /= this.getPlugin().getConfigYml().getDouble("loot.reduce-probability.factor");
}
int cap = 0;
@@ -157,7 +125,7 @@ public class LootPopulator extends BlockPopulator {
if (enchantment.getType().equals(EnchantmentType.SPECIAL)) {
double enchantlevel1 = NumberUtils.randFloat(0, 1);
double enchantlevel2 = NumberUtils.bias(enchantlevel1, plugin.getConfigYml().getDouble("enchanting-table.special-bias"));
double enchantlevel2 = NumberUtils.bias(enchantlevel1, this.getPlugin().getConfigYml().getDouble("enchanting-table.special-bias"));
double enchantlevel3 = 1 / (double) enchantment.getMaxLevel();
level = (int) Math.ceil(enchantlevel2 / enchantlevel3);
} else {
@@ -168,16 +136,16 @@ public class LootPopulator extends BlockPopulator {
toAdd.put(enchantment, level);
if (plugin.getConfigYml().getBool("loot.reduce-probability.enabled")) {
multiplier /= plugin.getConfigYml().getDouble("loot.reduce-probability.factor");
if (this.getPlugin().getConfigYml().getBool("loot.reduce-probability.enabled")) {
multiplier /= this.getPlugin().getConfigYml().getDouble("loot.reduce-probability.factor");
}
if (!enchantment.hasFlag("hard-cap-ignore")) {
cap++;
}
if (plugin.getConfigYml().getBool("anvil.hard-cap.enabled")) {
if (cap >= plugin.getConfigYml().getInt("anvil.hard-cap.cap")) {
if (this.getPlugin().getConfigYml().getBool("anvil.hard-cap.enabled")) {
if (cap >= this.getPlugin().getConfigYml().getInt("anvil.hard-cap.cap")) {
break;
}
}
@@ -192,21 +160,4 @@ public class LootPopulator extends BlockPopulator {
item.setItemMeta(meta);
}
}
/**
* Modify given inventory with EcoEnchants enchantments.
*
* @param inventory The target inventory.
* @param chunk The chunk.
*/
public void modifyInventory(@NotNull final Inventory inventory,
@NotNull final Chunk chunk) {
this.plugin.getScheduler().runLater(1, () -> {
if (chunk.isLoaded()) {
for (ItemStack item : inventory) {
modifyItem(item);
}
}
});
}
}