Skip to content
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.BukkitImplDeprecations;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import org.bukkit.event.EventHandler;
Expand Down Expand Up @@ -31,13 +32,34 @@ public class BrewingStandFueledScriptEvent extends BukkitScriptEvent implements
//
// @Determine
// "FUEL_POWER:<ElementTag(Number)>" to set the fuel power level to be added.
// "CONSUMING" to indicate that the fuel item should be consumed.
// "NOT_CONSUMING" to indicate that the fuel item should not be consumed.
// "CONSUME:<ElementTag(Boolean)>" to indicate whether the fuel item should be consumed.
//
// -->

public BrewingStandFueledScriptEvent() {
registerCouldMatcher("brewing stand fueled (with <item>)");
this.<BrewingStandFueledScriptEvent, ElementTag>registerOptionalDetermination("fuel_power", ElementTag.class, (evt, context, power) -> {
if (power.isInt()) {
evt.event.setFuelPower(power.asInt());
return true;
}
return false;
});
this.<BrewingStandFueledScriptEvent, ElementTag>registerOptionalDetermination("consume", ElementTag.class, (evt, context, value) -> {
if (value.isBoolean()) {
evt.event.setConsuming(value.asBoolean());
return true;
}
return false;
});
this.<BrewingStandFueledScriptEvent>registerTextDetermination("consuming", (evt) -> {
BukkitImplDeprecations.brewingStandConsumeDetermination.warn();
evt.event.setConsuming(true);
});
this.<BrewingStandFueledScriptEvent>registerTextDetermination("not_consuming", (evt) -> {
BukkitImplDeprecations.brewingStandConsumeDetermination.warn();
evt.event.setConsuming(false);
});
}

public LocationTag location;
Expand All @@ -55,35 +77,15 @@ public boolean matches(ScriptPath path) {
return super.matches(path);
}

@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
if (determinationObj instanceof ElementTag element) {
String val = element.asString();
if (val.startsWith("fuel_power:")) {
event.setFuelPower(Integer.parseInt(val.substring("fuel_power:".length())));
return true;
}
else if (val.equalsIgnoreCase("consuming")) {
event.setConsuming(true);
return true;
}
else if (val.equalsIgnoreCase("not_consuming")) {
event.setConsuming(false);
return true;
}
}
return super.applyDetermination(path, determinationObj);
}

@Override
public ObjectTag getContext(String name) {
switch (name) {
case "location": return location;
case "item": return item;
case "fuel_power": return new ElementTag(event.getFuelPower());
case "consuming": return new ElementTag(event.isConsuming());
}
return super.getContext(name);
return switch (name) {
case "location" -> location;
case "item" -> item;
case "fuel_power" -> new ElementTag(event.getFuelPower());
case "consuming" -> new ElementTag(event.isConsuming());
default -> super.getContext(name);
};
}

@EventHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
import org.bukkit.block.data.Levelled;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.CauldronLevelChangeEvent;
Expand Down Expand Up @@ -39,6 +42,29 @@ public class CauldronLevelChangeScriptEvent extends BukkitScriptEvent implements
public CauldronLevelChangeScriptEvent() {
registerCouldMatcher("cauldron level changes|raises|lowers");
registerSwitches("cause");
this.<CauldronLevelChangeScriptEvent, ElementTag>registerOptionalDetermination(null, ElementTag.class, (evt, context, level) -> {
if (!level.isInt()) {
return false;
}
BlockState cauldronState = evt.event.getNewState();
if (level.asInt() <= 0) {
cauldronState.setType(Material.CAULDRON);
return true;
}
if (level.asInt() > 3) {
return false;
}
Copy link
Member

Choose a reason for hiding this comment

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

You call asInt several times here, should go in a variable right after the int check.
Also this is an early return, I'd put it before all the logic & right after the int check as well.

Material cauldronType = cauldronState.getType();
if (cauldronType != Material.WATER_CAULDRON && cauldronType != Material.LAVA_CAULDRON) {
cauldronState.setType(event.getBlock().getType());
Copy link
Member

Choose a reason for hiding this comment

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

I'd store the type once the event fires instead of getting it dynamically here (like I did in the original example iirc) - otherwise it could lead to weird situations where some script runs a determination after the fact and the block state instance gets set to something weird (and maybe breaks another plugin that's using it or whatever).
Not a very likely scenario, but still probably better to make sure the types we set here are the correct ones & also avoid reading the block type every determination performance wise.

}
if (cauldronState.getBlockData() instanceof Levelled levelled) {
levelled.setLevel(level.asInt());
cauldronState.setBlockData(levelled);
return true;
}
return false;
});
}

public LocationTag location;
Expand All @@ -53,13 +79,15 @@ public boolean matches(ScriptPath path) {
return false;
}
String changeType = path.eventArgLowerAt(2);
int oldLevel = event.getBlock().getBlockData() instanceof Levelled levelled ? levelled.getLevel() : 0;
int newLevel = event.getNewState().getBlockData() instanceof Levelled levelled ? levelled.getLevel() : 0;
if (changeType.equals("raises")) {
if (event.getNewLevel() <= event.getOldLevel()) {
if (newLevel <= oldLevel) {
return false;
}
}
else if (changeType.equals("lowers")) {
if (event.getNewLevel() >= event.getOldLevel()) {
if (newLevel >= oldLevel) {
return false;
}
}
Expand All @@ -69,28 +97,16 @@ else if (!changeType.equals("changes")) {
return super.matches(path);
}

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

@Override
public ObjectTag getContext(String name) {
switch (name) {
case "location": return location;
case "cause": return new ElementTag(event.getReason());
case "old_level": return new ElementTag(event.getOldLevel());
case "new_level": return new ElementTag(event.getNewLevel());
case "entity":
if (event.getEntity() != null) {
return new EntityTag(event.getEntity()).getDenizenObject();
}
break;
}
return super.getContext(name);
return switch (name) {
case "location" -> location;
case "cause" -> new ElementTag(event.getReason());
case "old_level" -> new ElementTag(event.getBlock().getBlockData() instanceof Levelled levelled ? levelled.getLevel() : 0);
Copy link
Member

Choose a reason for hiding this comment

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

Same here, need to store the old level otherwise it'd keep reading the current level from the world and the context would change as the block changes (in e.g. an after event).
Can probably just store the old BlockData and use it for both this and the determination? Or storing an int and a Material separately would be fine as well.

case "new_level" -> new ElementTag(event.getNewState().getBlockData() instanceof Levelled levelled ? levelled.getLevel() : 0);
case "entity" -> event.getEntity() != null ? new EntityTag(event.getEntity()).getDenizenObject() : null;
default -> super.getContext(name);
};
}

@EventHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ public class FurnaceBurnsItemScriptEvent extends BukkitScriptEvent implements Li

public FurnaceBurnsItemScriptEvent() {
registerCouldMatcher("furnace burns <item>");
this.<FurnaceBurnsItemScriptEvent, ObjectTag>registerOptionalDetermination(null, ObjectTag.class, (evt, context, time) -> {
if (time instanceof ElementTag elementTag && elementTag.isInt()) {
Copy link
Member

Choose a reason for hiding this comment

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

Include a small comment here because it's a bit unclear why this special case exists (// Backwards compatibility for non-duration tick input or something)

evt.event.setBurnTime(elementTag.asInt());
return true;
}
else if (time.canBeType(DurationTag.class)) {
evt.event.setBurnTime(time.asType(DurationTag.class, context).getTicksAsInt());
return true;
}
return false;
});
}

public ItemTag item;
Expand All @@ -52,30 +63,17 @@ 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.setBurnTime(element.asInt());
return true;
}
else if (determinationObj.canBeType(DurationTag.class)) {
event.setBurnTime(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;
}
return super.getContext(name);
return switch (name) {
case "location" -> location;
case "item" -> item;
default -> super.getContext(name);
};
}

@EventHandler
public void onBrews(FurnaceBurnEvent event) {
public void onFurnaceBurns(FurnaceBurnEvent event) {
location = new LocationTag(event.getBlock().getLocation());
item = new ItemTag(event.getFuel());
this.event = event;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,13 @@ public class FurnaceStartsSmeltingScriptEvent extends BukkitScriptEvent implemen
// @Determine
// DurationTag to set the total cook time for the item being smelted.
//
// @Example
// # Sets the total cook time of every item to always be 2 seconds.
// on furnace starts smelting item:
// - determine 2s
//
// @Example
// # Sets the total cook time of iron ore to be 2 seconds.
// on furnace starts smelting iron_ore:
// - determine 2s
// -->

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 +55,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(event.getRecipe().getKey().toString(), true);
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 @@ -52,11 +52,11 @@ public boolean matches(ScriptPath path) {

@Override
public ObjectTag getContext(String name) {
switch (name) {
case "location": return location;
case "material": return material;
}
return super.getContext(name);
return switch (name) {
case "location" -> location;
case "material" -> material;
default -> super.getContext(name);
};
}

@EventHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ public boolean matches(ScriptPath path) {

@Override
public ObjectTag getContext(String name) {
switch (name) {
case "location": return location;
case "old_material": return old_material;
case "new_material": return new MaterialTag(event.getNewData());
}
return super.getContext(name);
return switch (name) {
case "location" -> location;
case "old_material" -> old_material;
case "new_material" -> new MaterialTag(event.getNewData());
default -> super.getContext(name);
};
}

@EventHandler
Expand Down
Loading