Reworked FastCollatedDropQueue into it's own system

This commit is contained in:
Auxilor 2020-12-18 13:54:16 +00:00
parent c75af2c7cc
commit 2c783c6467
8 changed files with 113 additions and 79 deletions

View File

@ -5,6 +5,7 @@ import com.willfp.ecoenchants.command.AbstractCommand;
import com.willfp.ecoenchants.display.EnchantmentCache;
import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.EcoEnchants;
import com.willfp.ecoenchants.util.internal.FastCollatedDropQueue;
import com.willfp.ecoenchants.util.internal.Logger;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
@ -89,6 +90,9 @@ public class CommandEcodebug extends AbstractCommand {
Logger.info("Enchantments with evident issues: " + withIssues.toString());
Logger.info("");
Logger.info("Collate? " + FastCollatedDropQueue.use());
Logger.info("");
Logger.info("Server Information: ");
Logger.info("Players Online: " + Bukkit.getServer().getOnlinePlayers().size());
Logger.info("Bukkit IP: " + Bukkit.getIp());

View File

@ -142,9 +142,14 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Regist
enabled = config.getBool("enabled", true);
EnchantmentUtils.registerPlaceholders(this);
postUpdate();
this.register();
}
protected void postUpdate() {
// Unused as some enchantments may have postUpdate tasks, however most won't.
}
/**
* Register the enchantment with spigot
* Only used internally

View File

@ -4,6 +4,7 @@ import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.util.EnchantChecks;
import com.willfp.ecoenchants.integrations.antigrief.AntigriefManager;
import com.willfp.ecoenchants.util.internal.DropQueue;
import com.willfp.ecoenchants.util.internal.FastCollatedDropQueue;
import com.willfp.ecoenchants.util.tuplets.Pair;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
@ -24,8 +25,6 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
@ -52,8 +51,9 @@ public class InfernalTouch extends EcoEnchant {
}
private static Pair<Material, Integer> getOutput(Material input) {
Optional<Pair<Material, Integer>> matching = recipes.entrySet().parallelStream().filter(materialPairEntry -> materialPairEntry.getKey().equals(input)).map(Map.Entry::getValue).findFirst();
return matching.orElse(new Pair<>(input, 0));
Pair<Material, Integer> toReturn = recipes.get(input);
if(toReturn == null) return new Pair<>(input, 0);
return toReturn;
}
// START OF LISTENERS
@ -98,6 +98,11 @@ public class InfernalTouch extends EcoEnchant {
event.getItems().clear();
if(FastCollatedDropQueue.use()) {
FastCollatedDropQueue.collateDrop(player, drops, experience.get(), block.getLocation());
return;
}
new DropQueue(player)
.setLocation(block.getLocation())
.addItems(drops)

View File

@ -1,6 +1,5 @@
package com.willfp.ecoenchants.enchantments.ecoenchants.normal;
import com.willfp.ecoenchants.EcoEnchantsPlugin;
import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.EcoEnchants;
import com.willfp.ecoenchants.enchantments.ecoenchants.special.Soulbound;
@ -9,9 +8,8 @@ import com.willfp.ecoenchants.events.entitydeathbyentity.EntityDeathByEntityEven
import com.willfp.ecoenchants.integrations.antigrief.AntigriefManager;
import com.willfp.ecoenchants.nms.TridentStack;
import com.willfp.ecoenchants.util.internal.DropQueue;
import org.bukkit.Bukkit;
import com.willfp.ecoenchants.util.internal.FastCollatedDropQueue;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Arrow;
@ -24,15 +22,10 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockDropItemEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
public class Telekinesis extends EcoEnchant {
public Telekinesis() {
@ -41,29 +34,6 @@ public class Telekinesis extends EcoEnchant {
);
}
private static boolean collate = false;
private static final HashMap<Player, CollatedDrops> COLLATED_MAP = new HashMap<>();
private static final BukkitRunnable COLLATED_RUNNABLE = new BukkitRunnable() {
@Override
public void run() {
for(Map.Entry<Player, CollatedDrops> entry : COLLATED_MAP.entrySet()) {
new DropQueue(entry.getKey())
.setLocation(entry.getValue().getLocation())
.addItems(entry.getValue().getDrops())
.push();
}
COLLATED_MAP.clear();
}
};
private static BukkitTask collatedRunnableTask = null;
public void update() {
collate = this.getConfig().getBool(EcoEnchants.CONFIG_LOCATION + "collate");
if(collate) collatedRunnableTask = COLLATED_RUNNABLE.runTaskTimer(EcoEnchantsPlugin.getInstance(), 0, 1);
else if(collatedRunnableTask != null) Bukkit.getScheduler().cancelTask(collatedRunnableTask.getTaskId());
}
// START OF LISTENERS
// For block drops
@ -79,18 +49,18 @@ public class Telekinesis extends EcoEnchant {
Block block = event.getBlock();
//if (!AntigriefManager.canBreakBlock(player, block)) return;
if (!AntigriefManager.canBreakBlock(player, block)) return;
List<ItemStack> drops = new ArrayList<>();
for(Item item : event.getItems()) drops.add(item.getItemStack());
event.getItems().clear();
if(collate) collateDropItem(event, player, drops, block);
else defaultDropItem(event, player, drops, block);
}
if(FastCollatedDropQueue.use()) {
FastCollatedDropQueue.collateDrop(player, drops, 0, block.getLocation());
return;
}
private void collateDropItem(BlockDropItemEvent event, Player player, List<ItemStack> drops, Block block) {
new DropQueue(player)
.setLocation(block.getLocation())
.addItems(drops)
@ -99,19 +69,6 @@ public class Telekinesis extends EcoEnchant {
player.updateInventory();
}
private void defaultDropItem(BlockDropItemEvent event, Player player, List<ItemStack> drops, Block block) {
CollatedDrops collatedDrops;
if(COLLATED_MAP.containsKey(player)) {
HashSet<ItemStack> dropSet = COLLATED_MAP.get(player).getDrops();
dropSet.addAll(drops);
collatedDrops = new CollatedDrops(dropSet, block.getLocation());
} else {
collatedDrops = new CollatedDrops(new HashSet<>(drops), block.getLocation());
}
COLLATED_MAP.put(player, collatedDrops);
}
// For exp drops, blockdropitemevent doesn't cover xp
@EventHandler(priority = EventPriority.HIGH)
public void telekinesisBreak(BlockBreakEvent event) {
@ -187,26 +144,4 @@ public class Telekinesis extends EcoEnchant {
event.getDeathEvent().setDroppedExp(0);
event.getDeathEvent().getDrops().clear();
}
static {
EcoEnchants.TELEKINESIS.update();
}
private static class CollatedDrops {
private final HashSet<ItemStack> drops;
private final Location location;
private CollatedDrops(HashSet<ItemStack> drops, Location location) {
this.drops = drops;
this.location = location;
}
public HashSet<ItemStack> getDrops() {
return drops;
}
public Location getLocation() {
return location;
}
}
}

View File

@ -0,0 +1,79 @@
package com.willfp.ecoenchants.util.internal;
import com.willfp.ecoenchants.EcoEnchantsPlugin;
import com.willfp.ecoenchants.config.ConfigManager;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class FastCollatedDropQueue {
private static boolean collate = false;
private static final HashMap<Player, CollatedDrops> COLLATED_MAP = new HashMap<>();
static {
Bukkit.getScheduler().runTaskTimer(EcoEnchantsPlugin.getInstance(), () -> {
if (!collate) return;
for (Map.Entry<Player, CollatedDrops> entry : COLLATED_MAP.entrySet()) {
new DropQueue(entry.getKey())
.setLocation(entry.getValue().getLocation())
.addItems(entry.getValue().getDrops())
.addXP(entry.getValue().getXp())
.push();
}
COLLATED_MAP.clear();
}, 0, 1);
update();
}
public static void collateDrop(Player player, Collection<ItemStack> items, int xp, Location location) {
CollatedDrops collatedDrops;
if(COLLATED_MAP.containsKey(player)) {
List<ItemStack> dropSet = COLLATED_MAP.get(player).getDrops();
dropSet.addAll(items);
collatedDrops = new CollatedDrops(dropSet, location, xp);
} else {
collatedDrops = new CollatedDrops(new ArrayList<>(items), location, xp);
}
COLLATED_MAP.put(player, collatedDrops);
}
public static boolean use() {
return collate;
}
public static void update() {
collate = ConfigManager.getConfig().getBool( "drops.collate");
}
private static class CollatedDrops {
private final List<ItemStack> drops;
private final Location location;
private final int xp;
private CollatedDrops(List<ItemStack> drops, Location location, int xp) {
this.drops = drops;
this.location = location;
this.xp = xp;
}
public List<ItemStack> getDrops() {
return drops;
}
public Location getLocation() {
return location;
}
public int getXp() {
return xp;
}
}
}

View File

@ -370,9 +370,11 @@ public class Loader {
DropQueue.update();
EnchantDisplay.update();
TabCompleterEnchantinfo.reload();
EcoEnchants.TELEKINESIS.update();
FastCollatedDropQueue.update();
EcoEnchant.EnchantmentType.update();
Bukkit.getScheduler().cancelTasks(EcoEnchantsPlugin.getInstance());
EcoEnchants.getAll().forEach((ecoEnchant -> {
HandlerList.unregisterAll(ecoEnchant);

View File

@ -99,3 +99,9 @@ rarity:
vanilla-rarity: uncommon # Vanilla enchantments do not have an EcoEnchants rarity - what rarity should they have?
vanilla-treasure-rarity: legendary # Treasure enchantments do not appear in vanilla enchanting tables (Mending, Soul Speed, Frost Walker)
# The above options *do not* affect actual enchantment rarities. They are purely for cosmetic purposes, like in item lores.
drops:
collate: false
# Instead of directly dropping all drops and xp, some intensive enchantments (eg Telekinesis and Infernal Touch) will push to a collated queue.
# At the end of each tick, the queue will be pushed rather than pushing on every drop creation
# This will massively improve mining performance on servers, however it is experimental and may not function correctly.

View File

@ -28,6 +28,4 @@ general-config:
config:
use-orb: true # Use experience orb above player to give xp. Use if you need mending interaction.
not-on-players: false # Disallow getting drops from players
collate: false # Experimental feature - may help performance on larger servers. Stores all drops temporarily and only pushes them at the end of this tick. This should reduce the DropQueue calls from thousands a second to about 10
not-on-players: false # Disallow getting drops from players