Skip to content

Commit

Permalink
Add entity_unleash event (#1227)
Browse files Browse the repository at this point in the history
  • Loading branch information
malonnnn authored and PseudoKnight committed Jul 26, 2020
1 parent bf4419a commit 566f8c7
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@
import com.laytonsmith.abstraction.enums.MCRemoveCause;
import com.laytonsmith.abstraction.enums.MCSpawnReason;
import com.laytonsmith.abstraction.enums.MCTargetReason;
import com.laytonsmith.abstraction.enums.MCUnleashReason;
import com.laytonsmith.abstraction.enums.MCVersion;
import com.laytonsmith.abstraction.enums.bukkit.BukkitMCDamageCause;
import com.laytonsmith.abstraction.enums.bukkit.BukkitMCEntityType;
import com.laytonsmith.abstraction.enums.bukkit.BukkitMCRegainReason;
import com.laytonsmith.abstraction.enums.bukkit.BukkitMCRemoveCause;
import com.laytonsmith.abstraction.enums.bukkit.BukkitMCSpawnReason;
import com.laytonsmith.abstraction.enums.bukkit.BukkitMCUnleashReason;
import com.laytonsmith.abstraction.events.MCCreatureSpawnEvent;
import com.laytonsmith.abstraction.events.MCEntityChangeBlockEvent;
import com.laytonsmith.abstraction.events.MCEntityDamageByEntityEvent;
Expand All @@ -53,6 +55,7 @@
import com.laytonsmith.abstraction.events.MCEntityRegainHealthEvent;
import com.laytonsmith.abstraction.events.MCEntityTargetEvent;
import com.laytonsmith.abstraction.events.MCEntityToggleGlideEvent;
import com.laytonsmith.abstraction.events.MCEntityUnleashEvent;
import com.laytonsmith.abstraction.events.MCFireworkExplodeEvent;
import com.laytonsmith.abstraction.events.MCHangingBreakEvent;
import com.laytonsmith.abstraction.events.MCItemDespawnEvent;
Expand Down Expand Up @@ -110,6 +113,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.bukkit.event.entity.EntityUnleashEvent;

public class BukkitEntityEvents {

Expand Down Expand Up @@ -985,4 +989,28 @@ public void setSearchRadius(int radius) {
}
}
}

public static class BukkitMCEntityUnleashEvent implements MCEntityUnleashEvent {

EntityUnleashEvent ide;

public BukkitMCEntityUnleashEvent(Event event) {
ide = (EntityUnleashEvent) event;
}

@Override
public Object _GetObject() {
return ide;
}

@Override
public MCEntity getEntity() {
return new BukkitMCEntity(ide.getEntity());
}

@Override
public MCUnleashReason getReason() {
return BukkitMCUnleashReason.getConvertor().getAbstractedEnum(ide.getReason());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.laytonsmith.abstraction.bukkit.events.BukkitEntityEvents.BukkitMCEntityPortalEvent;
import com.laytonsmith.abstraction.bukkit.events.BukkitEntityEvents.BukkitMCEntityRegainHealthEvent;
import com.laytonsmith.abstraction.bukkit.events.BukkitEntityEvents.BukkitMCEntityToggleGlideEvent;
import com.laytonsmith.abstraction.bukkit.events.BukkitEntityEvents.BukkitMCEntityUnleashEvent;
import com.laytonsmith.abstraction.bukkit.events.BukkitEntityEvents.BukkitMCFireworkExplodeEvent;
import com.laytonsmith.abstraction.bukkit.events.BukkitEntityEvents.BukkitMCHangingBreakEvent;
import com.laytonsmith.abstraction.bukkit.events.BukkitEntityEvents.BukkitMCItemDespawnEvent;
Expand Down Expand Up @@ -45,6 +46,7 @@
import org.bukkit.event.entity.EntityRegainHealthEvent;
import org.bukkit.event.entity.EntityTargetEvent;
import org.bukkit.event.entity.EntityToggleGlideEvent;
import org.bukkit.event.entity.EntityUnleashEvent;
import org.bukkit.event.entity.FireworkExplodeEvent;
import org.bukkit.event.entity.ItemDespawnEvent;
import org.bukkit.event.entity.ItemSpawnEvent;
Expand Down Expand Up @@ -206,4 +208,10 @@ public void onPortalTravel(EntityPortalEvent event) {
BukkitMCEntityPortalEvent epe = new BukkitMCEntityPortalEvent(event);
EventUtils.TriggerListener(Driver.ENTITY_PORTAL_TRAVEL, "entity_portal_travel", epe);
}

@EventHandler(priority = EventPriority.LOWEST)
public void onEntityUnleash(EntityUnleashEvent event) {
BukkitMCEntityUnleashEvent epe = new BukkitMCEntityUnleashEvent(event);
EventUtils.TriggerListener(Driver.ENTITY_UNLEASH, "entity_unleash", epe);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.laytonsmith.abstraction.enums;

public enum MCUnleashReason {
HOLDER_GONE,
PLAYER_UNLEASH,
DISTANCE,
UNKNOWN
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.laytonsmith.abstraction.enums.bukkit;

import com.laytonsmith.abstraction.Implementation;
import com.laytonsmith.abstraction.enums.EnumConvertor;
import com.laytonsmith.abstraction.enums.MCUnleashReason;
import com.laytonsmith.annotations.abstractionenum;
import org.bukkit.event.entity.EntityUnleashEvent.UnleashReason;

@abstractionenum(
implementation = Implementation.Type.BUKKIT,
forAbstractEnum = MCUnleashReason.class,
forConcreteEnum = UnleashReason.class
)
public class BukkitMCUnleashReason extends EnumConvertor<MCUnleashReason, UnleashReason> {

private static BukkitMCUnleashReason instance;

public static BukkitMCUnleashReason getConvertor() {
if(instance == null) {
instance = new BukkitMCUnleashReason();
}
return instance;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.laytonsmith.abstraction.events;
import com.laytonsmith.abstraction.MCEntity;
import com.laytonsmith.abstraction.enums.MCUnleashReason;
import com.laytonsmith.core.events.BindableEvent;

public interface MCEntityUnleashEvent extends BindableEvent {

MCEntity getEntity();

MCUnleashReason getReason();

}
1 change: 1 addition & 0 deletions src/main/java/com/laytonsmith/core/events/Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public enum Driver {
ENTITY_INTERACT,
ENTITY_REGAIN_HEALTH,
ENTITY_PORTAL_TRAVEL,
ENTITY_UNLEASH,
HANGING_BREAK,
ITEM_DROP,
ITEM_PICKUP,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.laytonsmith.abstraction.enums.MCRemoveCause;
import com.laytonsmith.abstraction.enums.MCSpawnReason;
import com.laytonsmith.abstraction.enums.MCTargetReason;
import com.laytonsmith.abstraction.enums.MCUnleashReason;
import com.laytonsmith.abstraction.events.MCCreatureSpawnEvent;
import com.laytonsmith.abstraction.events.MCEntityChangeBlockEvent;
import com.laytonsmith.abstraction.events.MCEntityDamageByEntityEvent;
Expand All @@ -36,6 +37,7 @@
import com.laytonsmith.abstraction.events.MCEntityRegainHealthEvent;
import com.laytonsmith.abstraction.events.MCEntityTargetEvent;
import com.laytonsmith.abstraction.events.MCEntityToggleGlideEvent;
import com.laytonsmith.abstraction.events.MCEntityUnleashEvent;
import com.laytonsmith.abstraction.events.MCFireworkExplodeEvent;
import com.laytonsmith.abstraction.events.MCHangingBreakEvent;
import com.laytonsmith.abstraction.events.MCItemDespawnEvent;
Expand Down Expand Up @@ -2153,4 +2155,69 @@ public boolean modifyEvent(String key, Mixed value, BindableEvent event) {
return false;
}
}

@api
public static class entity_unleash extends AbstractEvent {

@Override
public String getName() {
return "entity_unleash";
}

@Override
public String docs() {
return "{type: <macro> | reason: <macro>}"
+ " This event is called when a leash is broken."
+ " {id: The entityID of the entity | type: The entity type of the entity"
+ " reason: The reason the leash broke. Can be one of "
+ StringUtils.Join(MCUnleashReason.values(), ", ", ", or ", " or ") + "}"
+ " {}";
}

@Override
public boolean matches(Map<String, Mixed> prefilter, BindableEvent e) throws PrefilterNonMatchException {
if(e instanceof MCEntityUnleashEvent) {
MCEntityUnleashEvent event = (MCEntityUnleashEvent) e;
Prefilters.match(prefilter, "reason", event.getReason().name(), PrefilterType.MACRO);
Prefilters.match(prefilter, "type", event.getEntity().getType().name(), PrefilterType.MACRO);
return true;
}
return false;
}

@Override
public BindableEvent convert(CArray manualObject, Target t) {
return null;
}

@Override
public Map<String, Mixed> evaluate(BindableEvent e) throws EventException {
if(e instanceof MCEntityUnleashEvent) {
MCEntityUnleashEvent event = (MCEntityUnleashEvent) e;

Map<String, Mixed> ret = evaluate_helper(e);
ret.put("id", new CString(event.getEntity().getUniqueId().toString(), Target.UNKNOWN));
ret.put("reason", new CString(event.getReason().name(), Target.UNKNOWN));
ret.put("type", new CString(event.getEntity().getType().name(), Target.UNKNOWN));
return ret;
} else {
throw new EventException("Could not convert to MCEntityUnleashEvent");
}
}

@Override
public Driver driver() {
return Driver.ENTITY_UNLEASH;
}

@Override
public boolean modifyEvent(String key, Mixed value, BindableEvent event) {
return false;
}

@Override
public Version since() {
return MSVersion.V3_3_4;
}
}
}

0 comments on commit 566f8c7

Please sign in to comment.