Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Determination format modernization Part.2 #2689

Open
wants to merge 11 commits into
base: dev
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
Expand Down Expand Up @@ -44,6 +45,9 @@ public class FurnaceStartsSmeltingScriptEvent extends BukkitScriptEvent implemen

public FurnaceStartsSmeltingScriptEvent() {
registerCouldMatcher("furnace starts smelting <item>");
this.<FurnaceStartsSmeltingScriptEvent, DurationTag>registerDetermination(null, DurationTag.class, (evt, context, time) -> {
evt.event.setTotalCookTime(time.getTicksAsInt());
});
}

public ItemTag item;
Expand All @@ -61,24 +65,15 @@ public boolean matches(ScriptPath path) {
return super.matches(path);
}

@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
if (determinationObj.canBeType(DurationTag.class)) {
event.setTotalCookTime(determinationObj.asType(DurationTag.class, getTagContext(path)).getTicksAsInt());
return true;
}
return super.applyDetermination(path, determinationObj);
}

@Override
public ObjectTag getContext(String name) {
switch (name) {
case "location": return location;
case "item": return item;
case "recipe_id": return new ElementTag(event.getRecipe().getKey().toString());
case "total_cook_time": return new DurationTag((long) event.getTotalCookTime());
}
return super.getContext(name);
return switch (name) {
case "location" -> location;
case "item" -> item;
case "recipe_id" -> new ElementTag(Utilities.namespacedKeyToString(event.getRecipe().getKey()), true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a functionality change

case "total_cook_time" -> new DurationTag((long) event.getTotalCookTime());
default -> super.getContext(name);
};
}

@EventHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public class RedstoneScriptEvent extends BukkitScriptEvent implements Listener {

public RedstoneScriptEvent() {
registerCouldMatcher("redstone recalculated");
this.<RedstoneScriptEvent, ElementTag>registerDetermination(null, ElementTag.class, (evt, context, current) -> {
evt.event.setNewCurrent(current.asInt());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these rewrites are removing the error checks that the prior impl had

});
}


Expand All @@ -48,23 +51,14 @@ public boolean matches(ScriptPath path) {
return super.matches(path);
}

@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
if (determinationObj instanceof ElementTag element && element.isInt()) {
event.setNewCurrent(element.asInt());
return true;
}
return super.applyDetermination(path, determinationObj);
}

@Override
public ObjectTag getContext(String name) {
switch (name) {
case "location": return location;
case "old_current": return new ElementTag(event.getOldCurrent());
case "new_current": return new ElementTag(event.getNewCurrent());
}
return super.getContext(name);
return switch (name) {
case "location" -> location;
case "old_current" -> new ElementTag(event.getOldCurrent());
case "new_current" -> new ElementTag(event.getNewCurrent());
default -> super.getContext(name);
};
}

@EventHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public class EntityHealsScriptEvent extends BukkitScriptEvent implements Listene

public EntityHealsScriptEvent() {
registerCouldMatcher("<entity> heals (because <'cause'>)");
this.<EntityHealsScriptEvent, ElementTag>registerDetermination(null, ElementTag.class, (evt, context, health) -> {
if (health.isDouble()) {
evt.event.setAmount(health.asDouble());
}
});
}

public EntityTag entity;
Expand All @@ -60,31 +65,19 @@ public boolean matches(ScriptPath path) {
return super.matches(path);
}

@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
if (determinationObj instanceof ElementTag element && element.isDouble()) {
event.setAmount(element.asDouble());
return true;
}
return super.applyDetermination(path, determinationObj);
}

@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(entity);
}

@Override
public ObjectTag getContext(String name) {
switch (name) {
case "entity":
return entity.getDenizenObject();
case "reason":
return reason;
case "amount":
return new ElementTag(event.getAmount());
}
return super.getContext(name);
return switch (name) {
case "entity" -> entity.getDenizenObject();
case "reason" -> reason;
case "amount" -> new ElementTag(event.getAmount());
default -> super.getContext(name);
};
}

@EventHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public class PlayerCraftsItemScriptEvent extends BukkitScriptEvent implements Li
// -->

public PlayerCraftsItemScriptEvent() {
this.<PlayerCraftsItemScriptEvent, ItemTag>registerDetermination(null, ItemTag.class, (evt, context, result_item) -> {
event.setCurrentItem(result_item.getItemStack());
});
}

public CraftItemEvent event;
Expand All @@ -58,13 +61,10 @@ public PlayerCraftsItemScriptEvent() {

@Override
public boolean couldMatch(ScriptPath path) {
if (!path.eventArgsLowEqualStartingAt(0, "player", "crafts")) {
if (!path.eventLower.startsWith("player crafts ")) {
return false;
}
if (!couldMatchItem(path.eventArgLowerAt(2))) {
return false;
}
return true;
return couldMatchItem(path.eventArgLowerAt(2));
}

@Override
Expand All @@ -75,17 +75,6 @@ public boolean matches(ScriptPath path) {
return super.matches(path);
}

@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
String determination = determinationObj.toString();
if (ItemTag.matches(determination)) {
event.setCurrentItem(ItemTag.valueOf(determination, path.container).getItemStack());
return true;
}

return super.applyDetermination(path, determinationObj);
}

@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(player, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public class PlayerItemTakesDamageScriptEvent extends BukkitScriptEvent implemen

public PlayerItemTakesDamageScriptEvent() {
registerCouldMatcher("player <item> takes damage");
this.<PlayerItemTakesDamageScriptEvent, ElementTag>registerDetermination(null, ElementTag.class, (evt, context, amount) -> {
evt.event.setDamage(amount.asInt());
});
}

public PlayerItemDamageEvent event;
Expand All @@ -66,21 +69,12 @@ public boolean matches(ScriptPath path) {

@Override
public ObjectTag getContext(String name) {
switch (name) {
case "item": return item;
case "damage": return new ElementTag(event.getDamage());
case "slot": return new ElementTag(SlotHelper.slotForItem(event.getPlayer().getInventory(), item.getItemStack()) + 1);
}
return super.getContext(name);
}

@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
if (determinationObj instanceof ElementTag element && element.isInt()) {
event.setDamage(element.asInt());
return true;
}
return super.applyDetermination(path, determinationObj);
return switch (name) {
case "item" -> item;
case "damage" -> new ElementTag(event.getDamage());
case "slot" -> new ElementTag(SlotHelper.slotForItem(event.getPlayer().getInventory(), item.getItemStack()) + 1);
default -> super.getContext(name);
};
}

@Override
Expand Down