Skip to content

Commit 10bfe22

Browse files
committed
Use new player abilities over adventure settings
1 parent c1d0643 commit 10bfe22

24 files changed

Lines changed: 904 additions & 406 deletions

api/src/main/java/org/cloudburstmc/api/Server.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public interface Server {
7575

7676
Map<UUID, ? extends Player> getOnlinePlayers();
7777

78-
GameMode getGamemode();
78+
GameMode getGameMode();
7979

8080
Level getDefaultLevel();
8181

api/src/main/java/org/cloudburstmc/api/event/Cancellable.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package org.cloudburstmc.api.event;
22

3-
/**
4-
* Created by Nukkit Team.
5-
*/
63
public interface Cancellable {
74

85
boolean isCancelled();
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.cloudburstmc.api.event.player;
2+
3+
import org.cloudburstmc.api.event.Cancellable;
4+
import org.cloudburstmc.api.player.Ability;
5+
import org.cloudburstmc.api.player.Player;
6+
7+
/**
8+
* Fired when a boolean ability flag on a player is about to change.
9+
*
10+
* <p>Plugins may redirect the outcome to a different value by calling
11+
* {@link #setNewValue(boolean)}.</p>
12+
*/
13+
public final class PlayerAbilityChangeEvent extends PlayerEvent implements Cancellable {
14+
15+
private final Ability ability;
16+
private final boolean oldValue;
17+
private boolean newValue;
18+
19+
public PlayerAbilityChangeEvent(Player player, Ability ability, boolean oldValue, boolean newValue) {
20+
super(player);
21+
this.ability = ability;
22+
this.oldValue = oldValue;
23+
this.newValue = newValue;
24+
}
25+
26+
/**
27+
* The ability flag that is changing.
28+
*/
29+
public Ability getAbility() {
30+
return ability;
31+
}
32+
33+
/**
34+
* The value of the ability flag before this change is applied.
35+
*/
36+
public boolean getOldValue() {
37+
return oldValue;
38+
}
39+
40+
/**
41+
* The value that will be applied if this event is not cancelled.
42+
*/
43+
public boolean getNewValue() {
44+
return newValue;
45+
}
46+
47+
/**
48+
* Overrides the value that will be applied.
49+
*
50+
* @param newValue the value to apply to the ability flag
51+
*/
52+
public void setNewValue(boolean newValue) {
53+
this.newValue = newValue;
54+
}
55+
}
Lines changed: 92 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,106 @@
11
package org.cloudburstmc.api.event.player;
22

3+
import net.kyori.adventure.text.Component;
4+
import org.checkerframework.checker.nullness.qual.Nullable;
35
import org.cloudburstmc.api.event.Cancellable;
46
import org.cloudburstmc.api.player.GameMode;
57
import org.cloudburstmc.api.player.Player;
68

9+
/**
10+
* Called when a player's game mode is about to change.
11+
*/
712
public final class PlayerGameModeChangeEvent extends PlayerEvent implements Cancellable {
813

9-
protected final GameMode gamemode;
14+
private final GameMode newGameMode;
15+
private final Cause cause;
16+
private boolean cancelled;
17+
@Nullable
18+
private Component cancelMessage;
1019

11-
public PlayerGameModeChangeEvent(Player player, GameMode newGameMode) {
20+
/**
21+
* @param player the player whose game mode is changing
22+
* @param newGameMode the game mode being applied
23+
* @param cause the reason the game mode is changing
24+
* @param cancelMessage the message sent to the command sender if the event is
25+
* canceled, or {@code null} if no message should be sent.
26+
* Only meaningful when the cause is {@link Cause#COMMAND}.
27+
*/
28+
public PlayerGameModeChangeEvent(Player player, GameMode newGameMode, Cause cause, @Nullable Component cancelMessage) {
1229
super(player);
13-
this.gamemode = newGameMode;
30+
this.newGameMode = newGameMode;
31+
this.cause = cause;
32+
this.cancelMessage = cancelMessage;
1433
}
1534

16-
public GameMode getNewGamemode() {
17-
return gamemode;
35+
@Override
36+
public boolean isCancelled() {
37+
return cancelled;
38+
}
39+
40+
@Override
41+
public void setCancelled(boolean cancelled) {
42+
this.cancelled = cancelled;
43+
}
44+
45+
/**
46+
* Returns the game mode the player is switching to.
47+
*/
48+
public GameMode getNewGameMode() {
49+
return newGameMode;
50+
}
51+
52+
/**
53+
* Returns the reason this game mode change was triggered.
54+
*/
55+
public Cause getCause() {
56+
return cause;
57+
}
58+
59+
/**
60+
* Returns the message that will be sent to the command sender if this event
61+
* is canceled. Only meaningful when the cause is {@link Cause#COMMAND}.
62+
*
63+
* @return the cancel message, or {@code null} if none is set
64+
*/
65+
@Nullable
66+
public Component cancelMessage() {
67+
return cancelMessage;
68+
}
69+
70+
/**
71+
* Sets the message that will be sent to the command sender if this event is
72+
* canceled.
73+
*
74+
* @param cancelMessage the message to send, or {@code null} to suppress it
75+
*/
76+
public void cancelMessage(@Nullable Component cancelMessage) {
77+
this.cancelMessage = cancelMessage;
78+
}
79+
80+
/**
81+
* The reason a player's game mode was changed.
82+
*/
83+
public enum Cause {
84+
/**
85+
* A plugin changed the player's game mode via {@link Player#setGameMode(GameMode)}.
86+
*/
87+
PLUGIN,
88+
/**
89+
* The {@code /gamemode} command was used.
90+
*/
91+
COMMAND,
92+
/**
93+
* The player's game mode was changed because the server's default game mode
94+
* was applied (e.g. on join when {@code force-gamemode} is enabled).
95+
*
96+
* <p>The player may not be fully initialized when this cause is in use.
97+
* Check {@link Player#isSpawned()} before modifying player state.</p>
98+
*/
99+
DEFAULT_GAMEMODE,
100+
/**
101+
* Fallback cause for use by third-party code constructing this event
102+
* without a specific reason.
103+
*/
104+
UNKNOWN
18105
}
19106
}

api/src/main/java/org/cloudburstmc/api/event/server/QueryRegenerateEvent.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
44
import org.cloudburstmc.api.Server;
5+
import org.cloudburstmc.api.player.GameMode;
56
import org.cloudburstmc.api.player.Player;
67
import org.cloudburstmc.api.plugin.PluginContainer;
78

@@ -49,7 +50,7 @@ public QueryRegenerateEvent(Server server, int timeout) {
4950
//this.listPlugins = server.getConfig().getSettings().isQueryPlugins();
5051
this.plugins = server.getPluginManager().getAllPlugins().toArray(new PluginContainer[0]);
5152
this.players = server.getOnlinePlayers().values().toArray(new Player[0]);
52-
this.gameType = server.getGamemode().isSurvival() ? "SMP" : "CMP";
53+
this.gameType = server.getGameMode() == GameMode.SURVIVAL ? "SMP" : "CMP";
5354
this.version = server.getVersion();
5455
this.server_engine = server.getName() + " " + server.getImplementationVersion();
5556
this.map = server.getDefaultLevel() == null ? "unknown" : server.getDefaultLevel().getName();
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.cloudburstmc.api.player;
2+
3+
/**
4+
* Boolean ability flags that can be granted or revoked on a player.
5+
*/
6+
public enum Ability {
7+
BUILD,
8+
MINE,
9+
DOORS_AND_SWITCHES,
10+
OPEN_CONTAINERS,
11+
ATTACK_PLAYERS,
12+
ATTACK_MOBS,
13+
OPERATOR_COMMANDS,
14+
TELEPORT,
15+
INVULNERABLE,
16+
FLYING,
17+
MAY_FLY,
18+
INSTABUILD,
19+
LIGHTNING,
20+
MUTED,
21+
WORLD_BUILDER,
22+
NO_CLIP,
23+
PRIVILEGED_BUILDER
24+
}

api/src/main/java/org/cloudburstmc/api/player/AdventureSetting.java

Lines changed: 0 additions & 33 deletions
This file was deleted.

api/src/main/java/org/cloudburstmc/api/player/AdventureSettings.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)