Skip to content

Commit a0118b3

Browse files
authored
Merge branch 'dev' into Area_Effect_Cloud_Fixes
2 parents 30525fa + 650c130 commit a0118b3

File tree

5 files changed

+157
-26
lines changed

5 files changed

+157
-26
lines changed

paper/src/main/java/com/denizenscript/denizen/paper/PaperModule.java

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public static void init() {
6666
ScriptEvent.registerScriptEvent(PlayerJumpsScriptEventPaperImpl.class);
6767
ScriptEvent.registerScriptEvent(PlayerLecternPageChangeScriptEvent.class);
6868
ScriptEvent.registerScriptEvent(PlayerLoomPatternSelectScriptEvent.class);
69+
ScriptEvent.registerScriptEvent(PlayerNameEntityScriptEvent.class);
6970
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_20)) {
7071
ScriptEvent.registerScriptEvent(PlayerOpenSignScriptEvent.class);
7172
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.denizenscript.denizen.paper.events;
2+
3+
import com.denizenscript.denizen.events.BukkitScriptEvent;
4+
import com.denizenscript.denizen.objects.EntityTag;
5+
import com.denizenscript.denizen.paper.PaperModule;
6+
import com.denizenscript.denizen.utilities.PaperAPITools;
7+
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
8+
import com.denizenscript.denizencore.objects.ObjectTag;
9+
import com.denizenscript.denizencore.objects.core.ElementTag;
10+
import com.denizenscript.denizencore.scripts.ScriptEntryData;
11+
import io.papermc.paper.event.player.PlayerNameEntityEvent;
12+
import net.md_5.bungee.api.ChatColor;
13+
import org.bukkit.event.EventHandler;
14+
import org.bukkit.event.Listener;
15+
16+
public class PlayerNameEntityScriptEvent extends BukkitScriptEvent implements Listener {
17+
18+
// <--[event]
19+
// @Events
20+
// player names <entity>
21+
//
22+
// @Location true
23+
//
24+
// @Plugin Paper
25+
//
26+
// @Group Paper
27+
//
28+
// @Cancellable true
29+
//
30+
// @Triggers when a player attempts to rename an entity with a name tag.
31+
//
32+
// @Context
33+
// <context.entity> returns an EntityTag of the renamed entity.
34+
// <context.old_name> returns the old name of the entity, if any.
35+
// <context.name> returns the new name of the entity.
36+
// <context.persistent> returns whether this will cause the entity to persist through server restarts.
37+
//
38+
// @Determine
39+
// "NAME:<ElementTag>" to set a different name for the entity.
40+
// "PERSISTENT:<ElementTag(Boolean)>" to set whether the event will cause the entity to persist through restarts. NOTE: Entities may still persist for other reasons. To ensure they do not, use <@link mechanism EntityTag.force_no_persist>.
41+
//
42+
// @Player Always.
43+
//
44+
// -->
45+
46+
public PlayerNameEntityScriptEvent() {
47+
registerCouldMatcher("player names <entity>");
48+
this.<PlayerNameEntityScriptEvent, ElementTag>registerOptionalDetermination("persistent", ElementTag.class, (evt, context, determination) -> {
49+
if (determination.isBoolean()) {
50+
evt.event.setPersistent(determination.asBoolean());
51+
return true;
52+
}
53+
return false;
54+
});
55+
this.<PlayerNameEntityScriptEvent, ElementTag>registerDetermination("name", ElementTag.class, (evt, context, determination) -> {
56+
evt.event.setName(PaperModule.parseFormattedText(determination.toString(), ChatColor.WHITE));
57+
});
58+
}
59+
60+
public PlayerNameEntityEvent event;
61+
public EntityTag entity;
62+
public ElementTag oldName;
63+
64+
@Override
65+
public boolean matches(ScriptPath path) {
66+
if (!runInCheck(path, entity.getLocation())) {
67+
return false;
68+
}
69+
if (!path.tryArgObject(2, entity)) {
70+
return false;
71+
}
72+
return super.matches(path);
73+
}
74+
75+
@Override
76+
public ScriptEntryData getScriptEntryData() {
77+
return new BukkitScriptEntryData(event.getPlayer());
78+
}
79+
80+
@Override
81+
public ObjectTag getContext(String name) {
82+
return switch (name) {
83+
case "entity" -> entity.getDenizenObject();
84+
case "name" -> new ElementTag(PaperModule.stringifyComponent(event.getName()), true);
85+
case "old_name" -> oldName;
86+
case "persistent" -> new ElementTag(event.isPersistent());
87+
default -> super.getContext(name);
88+
};
89+
}
90+
91+
@EventHandler
92+
public void playerNamesEntity(PlayerNameEntityEvent event) {
93+
this.event = event;
94+
entity = new EntityTag(event.getEntity());
95+
String name = PaperAPITools.instance.getCustomName(entity.getBukkitEntity());
96+
oldName = name == null ? null : new ElementTag(name, true);
97+
fire(event);
98+
}
99+
}

plugin/src/main/java/com/denizenscript/denizen/events/player/PlayerChangesWorldScriptEvent.java

+28-20
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.denizenscript.denizen.events.BukkitScriptEvent;
44
import com.denizenscript.denizen.objects.EntityTag;
55
import com.denizenscript.denizen.objects.WorldTag;
6+
import com.denizenscript.denizen.utilities.BukkitImplDeprecations;
67
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
78
import com.denizenscript.denizencore.objects.ObjectTag;
89
import com.denizenscript.denizencore.scripts.ScriptEntryData;
@@ -14,14 +15,15 @@ public class PlayerChangesWorldScriptEvent extends BukkitScriptEvent implements
1415

1516
// <--[event]
1617
// @Events
17-
// player changes world (from <world>) (to <world>)
18-
//
19-
// @Regex ^on player changes world( from [^\s]+)?( to [^\s]+)?$
18+
// player changes world
2019
//
2120
// @Group Player
2221
//
2322
// @Location true
2423
//
24+
// @Switch from:<world> to only run if the player came from the specified world.
25+
// @Switch to:<world> to only run if the player is going to the specified world.
26+
//
2527
// @Triggers when a player moves to a different world.
2628
//
2729
// @Context
@@ -33,29 +35,37 @@ public class PlayerChangesWorldScriptEvent extends BukkitScriptEvent implements
3335
// -->
3436

3537
public PlayerChangesWorldScriptEvent() {
38+
registerCouldMatcher("player changes world (from <world>) (to <world>)");
39+
registerSwitches("from", "to");
3640
}
3741

3842
public WorldTag origin_world;
3943
public WorldTag destination_world;
4044
public PlayerChangedWorldEvent event;
4145

42-
@Override
43-
public boolean couldMatch(ScriptPath path) {
44-
return path.eventLower.startsWith("player changes world");
45-
}
46-
4746
@Override
4847
public boolean matches(ScriptPath path) {
4948
String[] data = path.eventArgsLower;
50-
// TODO: Switches
5149
for (int index = 3; index < data.length; index++) {
52-
if (data[index].equals("from") && !origin_world.tryAdvancedMatcher(data[index + 1], path.context)) {
53-
return false;
50+
if (data[index].equals("from")) {
51+
BukkitImplDeprecations.playerChangesWorldSwitches.warn(getTagContext(path));
52+
if (!origin_world.tryAdvancedMatcher(data[index + 1], path.context)) {
53+
return false;
54+
}
5455
}
55-
else if (data[index].equals("to") && !destination_world.tryAdvancedMatcher(data[index + 1], path.context)) {
56-
return false;
56+
else if (data[index].equals("to")) {
57+
BukkitImplDeprecations.playerChangesWorldSwitches.warn(getTagContext(path));
58+
if (!destination_world.tryAdvancedMatcher(data[index + 1], path.context)) {
59+
return false;
60+
}
5761
}
5862
}
63+
if (!path.tryObjectSwitch("from", origin_world)) {
64+
return false;
65+
}
66+
if (!path.tryObjectSwitch("to", destination_world)) {
67+
return false;
68+
}
5969
return super.matches(path);
6070
}
6171

@@ -66,13 +76,11 @@ public ScriptEntryData getScriptEntryData() {
6676

6777
@Override
6878
public ObjectTag getContext(String name) {
69-
if (name.equals("origin_world")) {
70-
return origin_world;
71-
}
72-
else if (name.equals("destination_world")) {
73-
return destination_world;
74-
}
75-
return super.getContext(name);
79+
return switch (name) {
80+
case "origin_world" -> origin_world;
81+
case "destination_world" -> destination_world;
82+
default -> super.getContext(name);
83+
};
7684
}
7785

7886
@EventHandler

plugin/src/main/java/com/denizenscript/denizen/objects/EntityTag.java

+26-6
Original file line numberDiff line numberDiff line change
@@ -1975,15 +1975,14 @@ else if (object.getLivingEntity() instanceof Steerable) {
19751975
// <--[tag]
19761976
// @attribute <EntityTag.is_sleeping>
19771977
// @returns ElementTag(Boolean)
1978+
// @mechanism EntityTag.is_sleeping
19781979
// @description
1979-
// Returns whether the player, NPC, or villager is currently sleeping.
1980+
// Returns whether a living entity is currently sleeping.
1981+
// If the entity is not a fox, player, or villager, this will always return 'false'.
19801982
// -->
19811983
registerSpawnedOnlyTag(ElementTag.class, "is_sleeping", (attribute, object) -> {
1982-
if (object.getBukkitEntity() instanceof Player) {
1983-
return new ElementTag(((Player) object.getBukkitEntity()).isSleeping());
1984-
}
1985-
else if (object.getBukkitEntity() instanceof Villager) {
1986-
return new ElementTag(((Villager) object.getBukkitEntity()).isSleeping());
1984+
if (object.getBukkitEntity() instanceof LivingEntity livingEntity) {
1985+
return new ElementTag(livingEntity.isSleeping());
19871986
}
19881987
return null;
19891988
});
@@ -3159,6 +3158,27 @@ else if (object.getBukkitEntity() instanceof Hanging hanging) {
31593158
}
31603159
});
31613160

3161+
// <--[mechanism]
3162+
// @object EntityTag
3163+
// @name is_sleeping
3164+
// @input ElementTag(Boolean)
3165+
// @description
3166+
// Sets whether a fox is sleeping.
3167+
// The entity may wake up immediately after setting this to true. If this is not desired, disable <@link mechanism EntityTag.has_ai>.
3168+
// @tags
3169+
// <EntityTag.is_sleeping>
3170+
// -->
3171+
tagProcessor.registerMechanism("is_sleeping", false, ElementTag.class, (object, mechanism, input) -> {
3172+
if (!mechanism.requireBoolean()) {
3173+
return;
3174+
}
3175+
if (!(object.getBukkitEntity() instanceof Fox fox)) {
3176+
mechanism.echoError("'is_sleeping' mechanism is only valid for Fox entities.");
3177+
return;
3178+
}
3179+
fox.setSleeping(input.asBoolean());
3180+
});
3181+
31623182
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_20)) {
31633183

31643184
// <--[mechanism]

plugin/src/main/java/com/denizenscript/denizen/utilities/BukkitImplDeprecations.java

+3
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,9 @@ public class BukkitImplDeprecations {
461461

462462
// Added 2025/03/29
463463
public static Warning areaEffectCloudControls = new FutureWarning("areaEffectCloudControls", "Several tags/mechanisms for controlling area effect clouds have been merged into existing properties, check relevant meta docs for more information.");
464+
465+
// Added 2025/04/27
466+
public static Warning playerChangesWorldSwitches = new FutureWarning("playerChangesWorldSwitches", "The 'from' and 'to' arguments in the 'player changes world' script event have been deprecated in favor of the 'from' and 'to' switches.");
464467

465468
// ==================== PAST deprecations of things that are already gone but still have a warning left behind ====================
466469

0 commit comments

Comments
 (0)