forked from CloudburstMC/Nukkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdventureSettings.java
More file actions
210 lines (177 loc) · 6.79 KB
/
AdventureSettings.java
File metadata and controls
210 lines (177 loc) · 6.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package cn.nukkit;
import cn.nukkit.network.protocol.UpdateAbilitiesPacket;
import cn.nukkit.network.protocol.UpdateAdventureSettingsPacket;
import cn.nukkit.network.protocol.types.AbilityLayer;
import cn.nukkit.network.protocol.types.PlayerAbility;
import java.util.EnumMap;
import java.util.Map;
/**
* Adventure settings
*
* @author MagicDroidX
* Nukkit Project
*/
public class AdventureSettings implements Cloneable {
public static final int PERMISSION_NORMAL = 0;
public static final int PERMISSION_OPERATOR = 1;
public static final int PERMISSION_HOST = 2;
public static final int PERMISSION_AUTOMATION = 3;
public static final int PERMISSION_ADMIN = 4;
private final Map<Type, Boolean> values = new EnumMap<>(Type.class);
private Player player;
public AdventureSettings(Player player) {
this.player = player;
}
public AdventureSettings clone(Player newPlayer) {
try {
AdventureSettings settings = (AdventureSettings) super.clone();
settings.player = newPlayer;
return settings;
} catch (CloneNotSupportedException e) {
return null;
}
}
/**
* Set an adventure setting value
*
* @param type adventure setting
* @param value new value
* @return AdventureSettings
*/
public AdventureSettings set(Type type, boolean value) {
this.values.put(type, value);
return this;
}
/**
* Get an adventure setting value
*
* @param type adventure setting
* @return value
*/
public boolean get(Type type) {
Boolean value = this.values.get(type);
return value == null ? type.getDefaultValue() : value;
}
/**
* Send adventure settings values to the player
*/
public void update() {
this.update(true);
}
/**
* Send adventure settings values to the player
* @param reset reset in air ticks
*/
void update(boolean reset) {
UpdateAbilitiesPacket packet = new UpdateAbilitiesPacket();
packet.setEntityId(player.getId());
packet.setCommandPermission(player.isOp() ? UpdateAbilitiesPacket.CommandPermission.OPERATOR : UpdateAbilitiesPacket.CommandPermission.NORMAL);
packet.setPlayerPermission(player.isOp() && !player.isSpectator() ? UpdateAbilitiesPacket.PlayerPermission.OPERATOR : UpdateAbilitiesPacket.PlayerPermission.MEMBER); // Spectator: fix operators being able to break blocks on spectator mode
AbilityLayer layer = new AbilityLayer();
layer.setLayerType(AbilityLayer.Type.BASE);
layer.getAbilitiesSet().addAll(PlayerAbility.VALUES);
for (Type type : Type.values()) {
if (type.isAbility() && this.get(type)) {
layer.getAbilityValues().add(type.getAbility());
}
}
// Because we send speed
layer.getAbilityValues().add(PlayerAbility.WALK_SPEED);
layer.getAbilityValues().add(PlayerAbility.FLY_SPEED);
layer.getAbilityValues().add(PlayerAbility.VERTICAL_FLY_SPEED);
if (player.isCreative()) { // Make sure player can interact with creative menu
layer.getAbilityValues().add(PlayerAbility.INSTABUILD);
}
if (player.isOp()) {
layer.getAbilityValues().add(PlayerAbility.OPERATOR_COMMANDS);
}
layer.setWalkSpeed(player.getWalkSpeed());
layer.setFlySpeed(player.getFlySpeed());
layer.setVerticalFlySpeed(player.getVerticalFlySpeed());
packet.getAbilityLayers().add(layer);
if (player.isSpectator()) {
AbilityLayer spectator = new AbilityLayer();
spectator.setLayerType(AbilityLayer.Type.SPECTATOR);
spectator.getAbilitiesSet().addAll(PlayerAbility.VALUES);
spectator.getAbilitiesSet().remove(PlayerAbility.FLY_SPEED);
spectator.getAbilitiesSet().remove(PlayerAbility.WALK_SPEED);
spectator.getAbilitiesSet().remove(PlayerAbility.VERTICAL_FLY_SPEED);
for (Type type : Type.values()) {
if (type.isAbility() && this.get(type)) {
spectator.getAbilityValues().add(type.getAbility());
}
}
if (player.isOp()) {
layer.getAbilityValues().add(PlayerAbility.OPERATOR_COMMANDS);
}
packet.getAbilityLayers().add(spectator);
}
UpdateAdventureSettingsPacket adventurePacket = new UpdateAdventureSettingsPacket();
adventurePacket.setAutoJump(get(Type.AUTO_JUMP));
adventurePacket.setImmutableWorld(get(Type.WORLD_IMMUTABLE));
adventurePacket.setNoMvP(get(Type.NO_MVP));
adventurePacket.setNoPvM(get(Type.NO_PVM));
adventurePacket.setShowNameTags(get(Type.SHOW_NAME_TAGS));
player.dataPacket(packet);
player.dataPacket(adventurePacket);
if (reset) {
player.resetInAirTicks();
}
}
/**
* List of adventure settings
*/
public enum Type {
WORLD_IMMUTABLE(null, false),
NO_PVM(null, false),
NO_MVP(PlayerAbility.INVULNERABLE, false),
SHOW_NAME_TAGS(null, false),
AUTO_JUMP(null, true),
ALLOW_FLIGHT(PlayerAbility.MAY_FLY, false),
NO_CLIP(PlayerAbility.NO_CLIP, false),
WORLD_BUILDER(PlayerAbility.WORLD_BUILDER, false),
FLYING(PlayerAbility.FLYING, false),
MUTED(PlayerAbility.MUTED, false),
MINE(PlayerAbility.MINE, true),
DOORS_AND_SWITCHED(PlayerAbility.DOORS_AND_SWITCHES, true),
OPEN_CONTAINERS(PlayerAbility.OPEN_CONTAINERS, true),
ATTACK_PLAYERS(PlayerAbility.ATTACK_PLAYERS, true),
ATTACK_MOBS(PlayerAbility.ATTACK_MOBS, true),
OPERATOR(PlayerAbility.OPERATOR_COMMANDS, false),
TELEPORT(PlayerAbility.TELEPORT, false),
BUILD(PlayerAbility.BUILD, true),
PRIVILEGED_BUILDER(PlayerAbility.PRIVILEGED_BUILDER, false),
@Deprecated
DEFAULT_LEVEL_PERMISSIONS(null, false);
private final PlayerAbility ability;
private final boolean defaultValue;
Type(PlayerAbility ability, boolean defaultValue) {
this.ability = ability;
this.defaultValue = defaultValue;
}
/**
* Get default value
*
* @return default value
*/
public boolean getDefaultValue() {
return this.defaultValue;
}
/**
* Get player ability type
*
* @return player ability type
*/
public PlayerAbility getAbility() {
return this.ability;
}
/**
* Check whether adventure setting is a valid player ability
*
* @return is a valid player ability
*/
public boolean isAbility() {
return this.ability != null;
}
}
}