Refactoring to remove internal components from API

This commit is contained in:
Auxilor
2021-02-14 15:23:42 +00:00
parent 5c29859d49
commit 57cf144c57
16 changed files with 84 additions and 18 deletions

View File

@@ -1,11 +1,11 @@
package com.willfp.eco.spigot;
import com.willfp.eco.spigot.display.packets.PacketAutoRecipe;
import com.willfp.eco.spigot.display.packets.PacketChat;
import com.willfp.eco.spigot.display.packets.PacketOpenWindowMerchant;
import com.willfp.eco.spigot.display.packets.PacketSetCreativeSlot;
import com.willfp.eco.spigot.display.packets.PacketSetSlot;
import com.willfp.eco.spigot.display.packets.PacketWindowItems;
import com.willfp.eco.spigot.display.PacketAutoRecipe;
import com.willfp.eco.spigot.display.PacketChat;
import com.willfp.eco.spigot.display.PacketOpenWindowMerchant;
import com.willfp.eco.spigot.display.PacketSetCreativeSlot;
import com.willfp.eco.spigot.display.PacketSetSlot;
import com.willfp.eco.spigot.display.PacketWindowItems;
import com.willfp.eco.spigot.integrations.anticheat.AnticheatAAC;
import com.willfp.eco.spigot.integrations.anticheat.AnticheatMatrix;
import com.willfp.eco.spigot.integrations.anticheat.AnticheatNCP;
@@ -21,8 +21,8 @@ import com.willfp.eco.util.display.Display;
import com.willfp.eco.util.drops.internal.FastCollatedDropQueue;
import com.willfp.eco.util.events.armorequip.ArmorListener;
import com.willfp.eco.util.events.armorequip.DispenserArmorListener;
import com.willfp.eco.util.events.entitydeathbyentity.EntityDeathByEntityListeners;
import com.willfp.eco.util.events.naturalexpgainevent.NaturalExpGainListeners;
import com.willfp.eco.spigot.eventlisteners.EntityDeathByEntityListeners;
import com.willfp.eco.spigot.eventlisteners.NaturalExpGainListeners;
import com.willfp.eco.util.integrations.IntegrationLoader;
import com.willfp.eco.util.integrations.anticheat.AnticheatManager;
import com.willfp.eco.util.integrations.antigrief.AntigriefManager;

View File

@@ -1,4 +1,4 @@
package com.willfp.eco.spigot.display.packets;
package com.willfp.eco.spigot.display;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;

View File

@@ -1,4 +1,4 @@
package com.willfp.eco.spigot.display.packets;
package com.willfp.eco.spigot.display;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.ListenerPriority;

View File

@@ -1,4 +1,4 @@
package com.willfp.eco.spigot.display.packets;
package com.willfp.eco.spigot.display;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer;

View File

@@ -1,4 +1,4 @@
package com.willfp.eco.spigot.display.packets;
package com.willfp.eco.spigot.display;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer;

View File

@@ -1,4 +1,4 @@
package com.willfp.eco.spigot.display.packets;
package com.willfp.eco.spigot.display;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer;

View File

@@ -1,4 +1,4 @@
package com.willfp.eco.spigot.display.packets;
package com.willfp.eco.spigot.display;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer;

View File

@@ -0,0 +1,61 @@
package com.willfp.eco.spigot.eventlisteners;
import com.willfp.eco.util.events.entitydeathbyentity.EntityDeathByEntityEvent;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.inventory.ItemStack;
import java.util.List;
class EntityDeathByEntityBuilder {
/**
* The killed {@link LivingEntity}.
*/
@Getter
@Setter
private LivingEntity victim = null;
/**
* The killer.
*/
@Getter
@Setter
private Entity damager;
/**
* The associated {@link EntityDeathEvent}.
*/
@Getter
@Setter
private EntityDeathEvent deathEvent;
/**
* The drops to create.
*/
@Getter
@Setter
private List<ItemStack> drops;
/**
* The experience to drop.
*/
@Getter
@Setter
private int xp = 0;
public void push() {
Validate.notNull(victim);
Validate.notNull(damager);
Validate.notNull(drops);
Validate.notNull(deathEvent);
EntityDeathByEntityEvent event = new EntityDeathByEntityEvent(victim, damager, drops, xp, deathEvent);
Bukkit.getPluginManager().callEvent(event);
}
}

View File

@@ -0,0 +1,96 @@
package com.willfp.eco.spigot.eventlisteners;
import com.willfp.eco.util.internal.PluginDependent;
import com.willfp.eco.util.plugin.AbstractEcoPlugin;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
public class EntityDeathByEntityListeners extends PluginDependent implements Listener {
/**
* The events currently being built.
*/
private final Set<EntityDeathByEntityBuilder> events = new HashSet<>();
/**
* Create a listener associated with an {@link AbstractEcoPlugin}.
*
* @param plugin The plugin to associate with.
*/
@ApiStatus.Internal
public EntityDeathByEntityListeners(@NotNull final AbstractEcoPlugin plugin) {
super(plugin);
}
/**
* Called when an entity is damaged by another entity.
* Used to find the damager.
*
* @param event The event to listen for.
*/
@EventHandler(priority = EventPriority.HIGH)
public void onEntityDamage(@NotNull final EntityDamageByEntityEvent event) {
if (!(event.getEntity() instanceof LivingEntity)) {
return;
}
LivingEntity victim = (LivingEntity) event.getEntity();
if (victim.getHealth() > event.getFinalDamage()) {
return;
}
EntityDeathByEntityBuilder builtEvent = new EntityDeathByEntityBuilder();
builtEvent.setVictim(victim);
builtEvent.setDamager(event.getDamager());
events.add(builtEvent);
this.getPlugin().getScheduler().runLater(() -> events.remove(builtEvent), 1);
}
/**
* Called when an entity is killed.
* Used to find the killer and associate the event.
*
* @param event The event to listen for.
*/
@EventHandler
public void onEntityDeath(@NotNull final EntityDeathEvent event) {
LivingEntity victim = event.getEntity();
List<ItemStack> drops = event.getDrops();
int xp = event.getDroppedExp();
AtomicReference<EntityDeathByEntityBuilder> atomicBuiltEvent = new AtomicReference<>(null);
EntityDeathByEntityBuilder builtEvent;
events.forEach(deathByEntityEvent -> {
if (deathByEntityEvent.getVictim().equals(victim)) {
atomicBuiltEvent.set(deathByEntityEvent);
}
});
if (atomicBuiltEvent.get() == null) {
return;
}
builtEvent = atomicBuiltEvent.get();
events.remove(builtEvent);
builtEvent.setDrops(drops);
builtEvent.setXp(xp);
builtEvent.setDeathEvent(event);
builtEvent.push();
}
}

View File

@@ -0,0 +1,75 @@
package com.willfp.eco.spigot.eventlisteners;
import com.willfp.eco.util.events.naturalexpgainevent.NaturalExpGainEvent;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.event.player.PlayerExpChangeEvent;
import org.jetbrains.annotations.NotNull;
class NaturalExpGainBuilder {
/**
* If the event has been cancelled and no experience should be given.
*/
@Getter
@Setter
private boolean cancelled = false;
/**
* The linked {@link PlayerExpChangeEvent}.
*/
@Getter
@Setter
private PlayerExpChangeEvent event;
/**
* The location of the event.
*/
@Getter
@Setter
private Location location;
/**
* The reason why the event was built.
*/
@Getter
@Setter
private BuildReason reason;
/**
* Build a new {@link NaturalExpGainEvent} given a specific reason.
*
* @param reason The {@link BuildReason}.
*/
NaturalExpGainBuilder(@NotNull final BuildReason reason) {
this.reason = reason;
}
/**
* Call the event on the server.
*/
public void push() {
Validate.notNull(event);
if (this.cancelled) {
return;
}
NaturalExpGainEvent naturalExpGainEvent = new NaturalExpGainEvent(event);
Bukkit.getPluginManager().callEvent(naturalExpGainEvent);
}
public enum BuildReason {
/**
* If the event was triggered by an experience bottle.
*/
BOTTLE,
/**
* If the event was triggered by a natural experience change.
*/
PLAYER
}
}

View File

@@ -0,0 +1,63 @@
package com.willfp.eco.spigot.eventlisteners;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.ExpBottleEvent;
import org.bukkit.event.player.PlayerExpChangeEvent;
import org.jetbrains.annotations.NotNull;
import java.util.HashSet;
import java.util.Set;
public class NaturalExpGainListeners implements Listener {
/**
* The events currently being built.
*/
private final Set<NaturalExpGainBuilder> events = new HashSet<>();
/**
* Called when the player's xp level changes.
* Used to store properties.
*
* @param event The event to listen for.
*/
@EventHandler
public void playerChange(@NotNull final PlayerExpChangeEvent event) {
NaturalExpGainBuilder builder = new NaturalExpGainBuilder(NaturalExpGainBuilder.BuildReason.PLAYER);
builder.setEvent(event);
NaturalExpGainBuilder toRemove = null;
for (NaturalExpGainBuilder searchBuilder : events) {
if (!searchBuilder.getLocation().getWorld().equals(event.getPlayer().getLocation().getWorld())) {
continue;
}
if (searchBuilder.getReason().equals(NaturalExpGainBuilder.BuildReason.BOTTLE) && searchBuilder.getLocation().distanceSquared(event.getPlayer().getLocation()) > 52) {
toRemove = searchBuilder;
}
}
if (toRemove != null) {
events.remove(toRemove);
return;
}
builder.setEvent(event);
builder.push();
events.remove(builder);
}
/**
* Called when an xp bottle breaks.
* Used to remove some built events to ensure only natural events are pushed.
*
* @param event The even to listen for.
*/
@EventHandler
public void onExpBottle(@NotNull final ExpBottleEvent event) {
NaturalExpGainBuilder builtEvent = new NaturalExpGainBuilder(NaturalExpGainBuilder.BuildReason.BOTTLE);
builtEvent.setLocation(event.getEntity().getLocation());
events.add(builtEvent);
}
}