Optimised NaturalExpGainEvent

This commit is contained in:
Auxilor 2020-12-19 22:23:40 +00:00
parent e4e7f860bf
commit 043e3cfb5e
5 changed files with 53 additions and 51 deletions

View File

@ -11,6 +11,7 @@ import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Container;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.block.BlockDropItemEvent;
@ -25,7 +26,6 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
public class InfernalTouch extends EcoEnchant {
private static final HashMap<Material, Pair<Material, Integer>> recipes = new HashMap<>();
@ -45,7 +45,8 @@ public class InfernalTouch extends EcoEnchant {
continue;
}
FurnaceRecipe furnaceRecipe = (FurnaceRecipe) recipe;
recipes.put(furnaceRecipe.getInput().getType(), new Pair<>(furnaceRecipe.getResult().getType(), (int) Math.ceil(furnaceRecipe.getExperience())));
int xp = (int) Math.ceil(furnaceRecipe.getExperience());
recipes.put(furnaceRecipe.getInput().getType(), new Pair<>(furnaceRecipe.getResult().getType(), xp));
}
}
@ -77,30 +78,30 @@ public class InfernalTouch extends EcoEnchant {
if(this.getDisabledWorlds().contains(player.getWorld())) return;
Collection<ItemStack> drops = new ArrayList<>();
event.getItems().forEach((item -> {
drops.add(item.getItemStack());
}));
AtomicInteger experience = new AtomicInteger(0);
for (Item item : event.getItems()) {
drops.add(item.getItemStack());
}
int experience = 0;
int fortune = EnchantChecks.getMainhandLevel(player, Enchantment.LOOT_BONUS_BLOCKS);
drops.forEach(itemStack -> {
for (ItemStack itemStack : drops) {
itemStack.setType(getOutput(itemStack.getType()).getFirst());
experience.addAndGet(getOutput(itemStack.getType()).getSecond());
experience += (getOutput(itemStack.getType()).getSecond());
if(fortune > 0 && allowsFortune.contains(itemStack.getType())) {
itemStack.setAmount((int) Math.ceil(1/((double) fortune + 2) + ((double) fortune + 1)/2));
experience.addAndGet(1);
itemStack.setAmount((int) Math.ceil(1 / ((double) fortune + 2) + ((double) fortune + 1) / 2));
experience++;
}
}
});
event.getItems().clear();
new DropQueue(player)
.setLocation(block.getLocation())
.addItems(drops)
.addXP(experience.get())
.addXP(experience)
.push();
}
}

View File

@ -21,12 +21,12 @@ public class Intellect extends EcoEnchant {
if(event.getExpChangeEvent().getAmount() < 0) return;
if (!EnchantChecks.mainhand(player, this)) return;
int level = EnchantChecks.getMainhandLevel(player, this);
if(level == 0) return;
if(this.getDisabledWorlds().contains(player.getWorld())) return;
int level = EnchantChecks.getMainhandLevel(player, this);
event.getExpChangeEvent().setAmount((int) Math.ceil(event.getExpChangeEvent().getAmount() * (1 + (level * this.getConfig().getDouble(EcoEnchants.CONFIG_LOCATION + "bonus-per-point")))));
}
}

View File

@ -10,9 +10,10 @@ class NaturalExpGainBuilder {
private boolean cancelled = false;
private PlayerExpChangeEvent event;
private Location loc;
private BuildReason reason;
public NaturalExpGainBuilder() {
public NaturalExpGainBuilder(BuildReason reason) {
this.reason = reason;
}
public LivingEntity getVictim() {
@ -35,6 +36,10 @@ class NaturalExpGainBuilder {
return this.loc;
}
public BuildReason getReason() {
return reason;
}
public void push() {
if (this.event == null) return;
if (this.cancelled) return;
@ -43,4 +48,9 @@ class NaturalExpGainBuilder {
Bukkit.getPluginManager().callEvent(naturalExpGainEvent);
}
public enum BuildReason {
BOTTLE,
PLAYER
}
}

View File

@ -1,54 +1,41 @@
package com.willfp.ecoenchants.events.naturalexpgainevent;
import com.willfp.ecoenchants.EcoEnchantsPlugin;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.ExpBottleEvent;
import org.bukkit.event.player.PlayerExpChangeEvent;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
public class NaturalExpGainListeners implements Listener {
final Set<NaturalExpGainBuilder> events = new HashSet<>();
private final Set<NaturalExpGainBuilder> events = new HashSet<>();
@EventHandler
public void onExpChange(PlayerExpChangeEvent event) {
NaturalExpGainBuilder builtEvent = new NaturalExpGainBuilder();
builtEvent.setEvent(event);
public void playerChange(PlayerExpChangeEvent event) {
NaturalExpGainBuilder builder = new NaturalExpGainBuilder(NaturalExpGainBuilder.BuildReason.PLAYER);
builder.setEvent(event);
AtomicBoolean isNatural = new AtomicBoolean(true);
AtomicReference<NaturalExpGainBuilder> atomicBuiltEvent = new AtomicReference<>();
NaturalExpGainBuilder toRemove = null;
for (NaturalExpGainBuilder searchBuilder : events) {
if(searchBuilder.getReason().equals(NaturalExpGainBuilder.BuildReason.BOTTLE) && searchBuilder.getLoc().distanceSquared(event.getPlayer().getLocation()) > 52)
toRemove = searchBuilder;
}
if(toRemove != null) {
events.remove(toRemove);
return;
}
builder.setEvent(event);
builder.push();
Set<NaturalExpGainBuilder> eventsClone = new HashSet<>(events);
eventsClone.forEach((builder) -> {
if (builder.getLoc().getWorld().getNearbyEntities(builder.getLoc(), 7.25, 7.25, 7.25).contains(event.getPlayer())) {
events.remove(builder);
isNatural.set(false);
atomicBuiltEvent.set(builder);
}
});
if (isNatural.get()) {
events.remove(atomicBuiltEvent.get());
builtEvent.push();
}
new BukkitRunnable() {
@Override
public void run() {
events.remove(builtEvent);
}
}.runTaskLater(EcoEnchantsPlugin.getInstance(), 1);
}
@EventHandler
public void onExpBottle(ExpBottleEvent event) {
NaturalExpGainBuilder builtEvent = new NaturalExpGainBuilder();
NaturalExpGainBuilder builtEvent = new NaturalExpGainBuilder(NaturalExpGainBuilder.BuildReason.BOTTLE);
builtEvent.setLoc(event.getEntity().getLocation());
events.add(builtEvent);

View File

@ -34,7 +34,7 @@ public class FastCollatedDropQueue extends InternalDropQueue {
@Override
public void push() {
CollatedDrops fetched = COLLATED_MAP.get(player);
CollatedDrops collatedDrops = fetched == null ? new CollatedDrops(items, loc, xp) : fetched.addDrops(items).setLocation(loc);
CollatedDrops collatedDrops = fetched == null ? new CollatedDrops(items, loc, xp) : fetched.addDrops(items).setLocation(loc).addXp(xp);
COLLATED_MAP.put(player, collatedDrops);
}
@ -64,7 +64,7 @@ public class FastCollatedDropQueue extends InternalDropQueue {
private static class CollatedDrops {
private final List<ItemStack> drops;
private Location location;
private final int xp;
private int xp;
private CollatedDrops(List<ItemStack> drops, Location location, int xp) {
this.drops = drops;
@ -94,5 +94,9 @@ public class FastCollatedDropQueue extends InternalDropQueue {
return this;
}
public CollatedDrops addXp(int xp) {
this.xp += xp;
return this;
}
}
}