Skip to content

Commit

Permalink
excessive total event system cleanup (mainly couldMatch)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jun 5, 2020
1 parent 3b39f18 commit ab36b74
Show file tree
Hide file tree
Showing 145 changed files with 1,095 additions and 279 deletions.
Expand Up @@ -58,8 +58,13 @@ public EntityKnocksbackEntityScriptEvent() {

@Override
public boolean couldMatch(ScriptPath path) {
return path.eventArgLowerAt(1).equals("knocks") &&
path.eventArgLowerAt(2).equals("back");
if (!path.eventArgLowerAt(1).equals("knocks") || !path.eventArgLowerAt(2).equals("back")) {
return false;
}
if (!couldMatchEntity(path.eventArgLowerAt(0)) || !couldMatchEntity(path.eventArgLowerAt(3))) {
return false;
}
return true;
}

@Override
Expand Down
Expand Up @@ -53,7 +53,13 @@ public EntityPathfindScriptEvent() {

@Override
public boolean couldMatch(ScriptPath path) {
return path.eventArgLowerAt(1).equals("pathfinds");
if (!path.eventArgLowerAt(1).equals("pathfinds")) {
return false;
}
if (!couldMatchEntity(path.eventArgLowerAt(0))) {
return false;
}
return true;
}

@Override
Expand Down
Expand Up @@ -46,9 +46,9 @@ public PlayerClicksFakeEntityScriptEvent() {

@Override
public boolean couldMatch(ScriptEvent.ScriptPath path) {
return path.eventLower.startsWith("player clicks fake")
|| path.eventLower.startsWith("player right clicks fake")
|| path.eventLower.startsWith("player left clicks fake");
return path.eventLower.startsWith("player clicks fake entity")
|| path.eventLower.startsWith("player right clicks fake entity")
|| path.eventLower.startsWith("player left clicks fake entity");
}

@Override
Expand Down
Expand Up @@ -43,7 +43,13 @@ public PlayerSpectatesEntityScriptEvent() {

@Override
public boolean couldMatch(ScriptPath path) {
return path.eventLower.startsWith("player spectates ");
if (!path.eventLower.startsWith("player spectates ")) {
return false;
}
if (!couldMatchEntity(path.eventArgLowerAt(2))) {
return false;
}
return true;
}

@Override
Expand Down
Expand Up @@ -51,7 +51,13 @@ public PreEntitySpawnScriptEvent() {

@Override
public boolean couldMatch(ScriptPath path) {
return path.eventArgLowerAt(1).equals("prespawns");
if (!path.eventArgLowerAt(1).equals("prespawns")) {
return false;
}
if (!couldMatchEntity(path.eventArgLowerAt(0))) {
return false;
}
return true;
}

@Override
Expand Down
Expand Up @@ -44,7 +44,13 @@ public ProjectileCollideScriptEvent() {

@Override
public boolean couldMatch(ScriptPath path) {
return path.eventArgLowerAt(1).equals("collides") && path.eventArgLowerAt(2).equals("with");
if (!path.eventArgLowerAt(1).equals("collides") || !path.eventArgLowerAt(2).equals("with")) {
return false;
}
if (!couldMatchEntity(path.eventArgLowerAt(0)) || !couldMatchEntity(path.eventArgLowerAt(3))) {
return false;
}
return true;
}

@Override
Expand Down
Expand Up @@ -24,6 +24,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
import java.util.function.Function;
import java.util.regex.Pattern;

public abstract class BukkitScriptEvent extends ScriptEvent {
Expand All @@ -47,38 +48,92 @@ public boolean couldMatchInArea(String lower) {
return true;
}

public boolean couldMatchEnum(String text, final Enum<?>[] enumVals) {
for (Enum<?> val : enumVals) {
if (val.name().equalsIgnoreCase(text)) {
return true;
}
}
return genericCouldMatchChecks(text, (t) -> couldMatchEnum(t, enumVals));
}

public boolean couldMatchInventory(String text) {
if (text.equals("inventory")) {
return true;
}
if (InventoryTag.matches(text)) {
return true;
}
if (CoreUtilities.contains(text, '*')) {
return genericCouldMatchChecks(text, this::couldMatchInventory);
}

public static HashSet<String> specialEntityMatchables = new HashSet<>(Arrays.asList("player", "entity", "npc", "vehicle", "fish", "projectile", "hanging"));

public boolean couldMatchEntity(String text) {
if (specialEntityMatchables.contains(text)) {
return true;
}
if (text.startsWith("regex:")) {
if (EntityTag.matches(text)) {
return true;
}
// This one must be last.
if (CoreUtilities.contains(text, '|')) {
for (String subMatch : text.split("\\|")) {
if (!couldMatchInventory(subMatch)) {
return false;
}
return genericCouldMatchChecks(text, this::couldMatchEntity);
}

public boolean couldMatchVehicle(String text) {
if (text.equals("vehicle")) {
return true;
}
if (specialEntityMatchables.contains(text)) {
return false;
}
if (EntityTag.matches(text)) {
EntityTag entity = EntityTag.valueOf(text, CoreUtilities.noDebugContext);
if (entity == null) {
return false;
}
if (!Vehicle.class.isAssignableFrom(entity.getEntityType().getBukkitEntityType().getEntityClass())) {
return false;
}
return true;
}
return false;
return genericCouldMatchChecks(text, this::couldMatchVehicle);
}

public boolean couldMatchBlock(String text) {
if (text.equals("block") || text.equals("material")) {
return true;
}
if (text.equals("item")) {
return false;
}
if (MaterialTag.matches(text)) {
MaterialTag mat = MaterialTag.valueOf(text, CoreUtilities.noDebugContext);
if (mat == null || !mat.getMaterial().isBlock()) {
return false;
}
return true;
}
return genericCouldMatchChecks(text, this::couldMatchBlock);
}

public boolean couldMatchItem(String text) {
if (text.equals("item")) {
return true;
}
if (MaterialTag.matches(text) || ItemTag.matches(text)) {
if (MaterialTag.matches(text)) {
MaterialTag mat = MaterialTag.valueOf(text, CoreUtilities.noDebugContext);
if (mat == null || !mat.getMaterial().isItem()) {
return false;
}
return true;
}
if (ItemTag.matches(text)) {
return true;
}
return genericCouldMatchChecks(text, this::couldMatchItem);
}

public boolean genericCouldMatchChecks(String text, Function<String, Boolean> checkType) {
if (CoreUtilities.contains(text, '*')) {
return true;
}
Expand All @@ -88,7 +143,7 @@ public boolean couldMatchItem(String text) {
// This one must be last.
if (CoreUtilities.contains(text, '|')) {
for (String subMatch : text.split("\\|")) {
if (!couldMatchItem(subMatch)) {
if (!checkType.apply(subMatch)) {
return false;
}
}
Expand Down
Expand Up @@ -4,7 +4,9 @@
import com.denizenscript.denizen.events.block.*;
import com.denizenscript.denizen.events.core.*;
import com.denizenscript.denizen.events.entity.*;
import com.denizenscript.denizen.events.item.*;
import com.denizenscript.denizen.events.player.*;
import com.denizenscript.denizen.events.vehicle.*;
import com.denizenscript.denizen.events.world.*;
import com.denizenscript.denizen.utilities.depends.Depends;
import com.denizenscript.denizen.nms.NMSHandler;
Expand All @@ -14,6 +16,7 @@
public class ScriptEventRegistry {

public static void registerMainEvents() {

// Block events
ScriptEvent.registerScriptEvent(new BlockBuiltScriptEvent());
ScriptEvent.registerScriptEvent(new BlockBurnsScriptEvent());
Expand Down Expand Up @@ -89,10 +92,6 @@ public static void registerMainEvents() {
ScriptEvent.registerScriptEvent(new FireworkBurstsScriptEvent());
ScriptEvent.registerScriptEvent(new HangingBreaksScriptEvent());
ScriptEvent.registerScriptEvent(new HorseJumpsScriptEvent());
ScriptEvent.registerScriptEvent(new ItemDespawnsScriptEvent());
ScriptEvent.registerScriptEvent(new ItemEnchantedScriptEvent());
ScriptEvent.registerScriptEvent(new ItemMergesScriptEvent());
ScriptEvent.registerScriptEvent(new ItemSpawnsScriptEvent());
if (Depends.citizens != null) {
ScriptEvent.registerScriptEvent(new NPCSpawnScriptEvent());
}
Expand All @@ -102,19 +101,21 @@ public static void registerMainEvents() {
ScriptEvent.registerScriptEvent(new SheepDyedScriptEvent());
ScriptEvent.registerScriptEvent(new SheepRegrowsScriptEvent());
ScriptEvent.registerScriptEvent(new SlimeSplitsScriptEvent());
ScriptEvent.registerScriptEvent(new VehicleCollidesBlockScriptEvent());
ScriptEvent.registerScriptEvent(new VehicleCollidesEntityScriptEvent());
ScriptEvent.registerScriptEvent(new VehicleCreatedScriptEvent());
ScriptEvent.registerScriptEvent(new VehicleDamagedScriptEvent());
ScriptEvent.registerScriptEvent(new VehicleDestroyedScriptEvent());
ScriptEvent.registerScriptEvent(new VehicleMoveScriptEvent());

// Item events
ScriptEvent.registerScriptEvent(new InventoryPicksUpItemScriptEvent());
ScriptEvent.registerScriptEvent(new ItemDespawnsScriptEvent());
ScriptEvent.registerScriptEvent(new ItemEnchantedScriptEvent());
ScriptEvent.registerScriptEvent(new ItemMergesScriptEvent());
ScriptEvent.registerScriptEvent(new ItemMoveScriptEvent());
ScriptEvent.registerScriptEvent(new ItemRecipeFormedScriptEvent());
ScriptEvent.registerScriptEvent(new ItemSpawnsScriptEvent());

// Player events
ScriptEvent.registerScriptEvent(new BiomeEnterExitScriptEvent());
ScriptEvent.registerScriptEvent(new ChatScriptEvent());
ScriptEvent.registerScriptEvent(new HotbarScrollScriptEvent());
ScriptEvent.registerScriptEvent(new ExperienceBottleBreaksScriptEvent());
ScriptEvent.registerScriptEvent(new ItemRecipeFormedScriptEvent());
ScriptEvent.registerScriptEvent(new ItemScrollScriptEvent());
ScriptEvent.registerScriptEvent(new PlayerAnimatesScriptEvent());
ScriptEvent.registerScriptEvent(new PlayerBreaksBlockScriptEvent());
ScriptEvent.registerScriptEvent(new PlayerBreaksItemScriptEvent());
Expand Down Expand Up @@ -184,12 +185,18 @@ public static void registerMainEvents() {
ScriptEvent.registerScriptEvent(new PlayerWalksOverScriptEvent());
ScriptEvent.registerScriptEvent(new ResourcePackStatusScriptEvent());

// Vehicle
ScriptEvent.registerScriptEvent(new VehicleCollidesBlockScriptEvent());
ScriptEvent.registerScriptEvent(new VehicleCollidesEntityScriptEvent());
ScriptEvent.registerScriptEvent(new VehicleCreatedScriptEvent());
ScriptEvent.registerScriptEvent(new VehicleDamagedScriptEvent());
ScriptEvent.registerScriptEvent(new VehicleDestroyedScriptEvent());
ScriptEvent.registerScriptEvent(new VehicleMoveScriptEvent());

// World events
ScriptEvent.registerScriptEvent(new ChunkLoadScriptEvent());
ScriptEvent.registerScriptEvent(new ChunkUnloadScriptEvent());
ScriptEvent.registerScriptEvent(new CommandScriptEvent());
ScriptEvent.registerScriptEvent(new InventoryPicksUpItemScriptEvent());
ScriptEvent.registerScriptEvent(new ItemMoveScriptEvent());
ScriptEvent.registerScriptEvent(new LightningStrikesScriptEvent());
ScriptEvent.registerScriptEvent(new LingeringPotionSplashScriptEvent());
ScriptEvent.registerScriptEvent(new PortalCreateScriptEvent());
Expand Down
Expand Up @@ -52,7 +52,13 @@ public BlockBuiltScriptEvent() {

@Override
public boolean couldMatch(ScriptPath path) {
return path.eventArgLowerAt(1).equals("being") && path.eventArgLowerAt(2).equals("built");
if (!path.eventArgLowerAt(1).equals("being") ||!path.eventArgLowerAt(2).equals("built")) {
return false;
}
if (!couldMatchBlock(path.eventArgLowerAt(0))) {
return false;
}
return true;
}

@Override
Expand Down
Expand Up @@ -42,7 +42,13 @@ public BlockBurnsScriptEvent() {

@Override
public boolean couldMatch(ScriptPath path) {
return path.eventArgLowerAt(1).equals("burns");
if (!path.eventArgLowerAt(1).equals("burns")) {
return false;
}
if (!couldMatchBlock(path.eventArgLowerAt(0))) {
return false;
}
return true;
}

@Override
Expand Down
Expand Up @@ -54,7 +54,16 @@ public BlockDispensesScriptEvent() {

@Override
public boolean couldMatch(ScriptPath path) {
return path.eventArgLowerAt(1).equals("dispenses");
if (!path.eventArgLowerAt(1).equals("dispenses")) {
return false;
}
if (!couldMatchBlock(path.eventArgLowerAt(0))) {
return false;
}
if (!couldMatchItem(path.eventArgLowerAt(2))) {
return false;
}
return true;
}

@Override
Expand Down
Expand Up @@ -42,7 +42,13 @@ public BlockFadesScriptEvent() {

@Override
public boolean couldMatch(ScriptPath path) {
return path.eventArgLowerAt(1).equals("fades");
if (!path.eventArgLowerAt(1).equals("fades")) {
return false;
}
if (!couldMatchBlock(path.eventArgLowerAt(0))) {
return false;
}
return true;
}

@Override
Expand Down
Expand Up @@ -44,7 +44,13 @@ public BlockFallsScriptEvent() {

@Override
public boolean couldMatch(ScriptPath path) {
return path.eventArgLowerAt(1).equals("falls");
if (!path.eventArgLowerAt(1).equals("falls")) {
return false;
}
if (!couldMatchBlock(path.eventArgLowerAt(0))) {
return false;
}
return true;
}

@Override
Expand Down
Expand Up @@ -42,7 +42,13 @@ public BlockFormsScriptEvent() {

@Override
public boolean couldMatch(ScriptPath path) {
return path.eventArgLowerAt(1).equals("forms");
if (!path.eventArgLowerAt(1).equals("forms")) {
return false;
}
if (!couldMatchBlock(path.eventArgLowerAt(0))) {
return false;
}
return true;
}

@Override
Expand Down

0 comments on commit ab36b74

Please sign in to comment.