Documented and lomboked events

This commit is contained in:
Auxilor 2020-12-23 14:18:29 +00:00
parent 75507c3967
commit 70f40fc6bc
5 changed files with 81 additions and 56 deletions

View File

@ -11,7 +11,7 @@ import org.bukkit.inventory.ItemStack;
import java.util.List;
public class EntityDeathByEntityBuilder {
class EntityDeathByEntityBuilder {
/**
* The killed {@link LivingEntity}.
*/

View File

@ -55,6 +55,12 @@ public class EntityDeathByEntityListeners extends PluginDependent implements Lis
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();

View File

@ -1,48 +1,57 @@
package com.willfp.eco.util.events.naturalexpgainevent;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.player.PlayerExpChangeEvent;
import org.jetbrains.annotations.NotNull;
class NaturalExpGainBuilder {
private final LivingEntity victim = null;
/**
* 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;
private Location loc;
/**
* The location of the event.
*/
@Getter
@Setter
private Location location;
/**
* The reason why the event was built.
*/
@Getter
@Setter
private BuildReason reason;
public NaturalExpGainBuilder(BuildReason reason) {
/**
* Build a new {@link NaturalExpGainEvent} given a specific reason.
*/
NaturalExpGainBuilder(@NotNull final BuildReason reason) {
this.reason = reason;
}
public LivingEntity getVictim() {
return this.victim;
}
public void setEvent(PlayerExpChangeEvent event) {
this.event = event;
}
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
public void setLoc(Location location) {
this.loc = location;
}
public Location getLoc() {
return this.loc;
}
public BuildReason getReason() {
return reason;
}
/**
* Call the event on the server.
*/
public void push() {
if (this.event == null) return;
if (this.cancelled) return;
Validate.notNull(event);
if (this.cancelled) {
return;
}
NaturalExpGainEvent naturalExpGainEvent = new NaturalExpGainEvent(event);
@ -50,7 +59,14 @@ class NaturalExpGainBuilder {
}
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

@ -1,45 +1,48 @@
package com.willfp.eco.util.events.naturalexpgainevent;
import lombok.Getter;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerExpChangeEvent;
import org.jetbrains.annotations.NotNull;
/**
* Event triggered when player receives experience not from bottle
*/
public class NaturalExpGainEvent extends Event {
/**
* Internal bukkit.
*/
private static final HandlerList HANDLERS = new HandlerList();
/**
* The associated {@link PlayerExpChangeEvent}
*/
private final PlayerExpChangeEvent event;
/**
* Create event based off parameters
*
* @param event The associate PlayerExpChangeEvent
*/
public NaturalExpGainEvent(@NotNull PlayerExpChangeEvent event) {
this.event = event;
}
/**
* Get associated {@link PlayerExpChangeEvent}
* The associated {@link PlayerExpChangeEvent}.
* Use this to modify event parameters.
*
* @return The associated {@link PlayerExpChangeEvent}
*/
public PlayerExpChangeEvent getExpChangeEvent() {
return this.event;
@Getter
private final PlayerExpChangeEvent expChangeEvent;
/**
* Create event based off parameters.
*
* @param event The associated PlayerExpChangeEvent.
*/
public NaturalExpGainEvent(@NotNull final PlayerExpChangeEvent event) {
this.expChangeEvent = event;
}
/**
* Internal bukkit.
*
* @return The handlers.
*/
@Override
public @NotNull HandlerList getHandlers() {
return HANDLERS;
}
/**
* Internal bukkit.
*
* @return The handlers.
*/
public static HandlerList getHandlerList() {
return HANDLERS;
}

View File

@ -18,8 +18,8 @@ public class NaturalExpGainListeners implements Listener {
NaturalExpGainBuilder toRemove = null;
for (NaturalExpGainBuilder searchBuilder : events) {
if(!searchBuilder.getLoc().getWorld().equals(event.getPlayer().getLocation().getWorld())) continue;
if(searchBuilder.getReason().equals(NaturalExpGainBuilder.BuildReason.BOTTLE) && searchBuilder.getLoc().distanceSquared(event.getPlayer().getLocation()) > 52)
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;
}
@ -37,7 +37,7 @@ public class NaturalExpGainListeners implements Listener {
@EventHandler
public void onExpBottle(ExpBottleEvent event) {
NaturalExpGainBuilder builtEvent = new NaturalExpGainBuilder(NaturalExpGainBuilder.BuildReason.BOTTLE);
builtEvent.setLoc(event.getEntity().getLocation());
builtEvent.setLocation(event.getEntity().getLocation());
events.add(builtEvent);
}