-
-
Notifications
You must be signed in to change notification settings - Fork 824
/
FabricPlatform.java
227 lines (197 loc) · 7.25 KB
/
FabricPlatform.java
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.fabric;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.extension.platform.AbstractPlatform;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.extension.platform.MultiUserPlatform;
import com.sk89q.worldedit.extension.platform.Preference;
import com.sk89q.worldedit.extension.platform.Watchdog;
import com.sk89q.worldedit.fabric.internal.ExtendedChunk;
import com.sk89q.worldedit.util.SideEffect;
import com.sk89q.worldedit.util.lifecycle.Lifecycled;
import com.sk89q.worldedit.world.DataFixer;
import com.sk89q.worldedit.world.World;
import com.sk89q.worldedit.world.registry.Registries;
import net.minecraft.SharedConstants;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.dedicated.DedicatedServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.players.PlayerList;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraft.world.level.storage.ServerLevelData;
import org.enginehub.piston.CommandManager;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import javax.annotation.Nullable;
class FabricPlatform extends AbstractPlatform implements MultiUserPlatform {
private final FabricWorldEdit mod;
private final FabricDataFixer dataFixer;
private final Lifecycled<Optional<Watchdog>> watchdog;
private boolean hookingEvents = false;
FabricPlatform(FabricWorldEdit mod) {
this.mod = mod;
this.dataFixer = new FabricDataFixer(getDataVersion());
this.watchdog = FabricWorldEdit.LIFECYCLED_SERVER.map(
server -> server instanceof DedicatedServer
? Optional.of((Watchdog) server)
: Optional.empty()
);
}
boolean isHookingEvents() {
return hookingEvents;
}
@Override
public Registries getRegistries() {
return FabricRegistries.getInstance();
}
@Override
public int getDataVersion() {
return SharedConstants.getCurrentVersion().getDataVersion().getVersion();
}
@Override
public DataFixer getDataFixer() {
return dataFixer;
}
@Override
public boolean isValidMobType(String type) {
return Registry.ENTITY_TYPE.containsKey(new ResourceLocation(type));
}
@Override
public void reload() {
getConfiguration().load();
super.reload();
}
@Override
public int schedule(long delay, long period, Runnable task) {
return -1;
}
@Override
@Nullable
public Watchdog getWatchdog() {
return watchdog.value().flatMap(Function.identity()).orElse(null);
}
@Override
public List<? extends World> getWorlds() {
Iterable<ServerLevel> worlds = FabricWorldEdit.LIFECYCLED_SERVER.valueOrThrow().getAllLevels();
List<World> ret = new ArrayList<>();
for (ServerLevel world : worlds) {
ret.add(new FabricWorld(world));
}
return ret;
}
@Nullable
@Override
public Player matchPlayer(Player player) {
if (player instanceof FabricPlayer) {
return player;
} else {
ServerPlayer entity = FabricWorldEdit.LIFECYCLED_SERVER.valueOrThrow()
.getPlayerList().getPlayerByName(player.getName());
return entity != null ? new FabricPlayer(entity) : null;
}
}
@Nullable
@Override
public World matchWorld(World world) {
if (world instanceof FabricWorld) {
return world;
} else {
for (ServerLevel ws : FabricWorldEdit.LIFECYCLED_SERVER.valueOrThrow().getAllLevels()) {
if (((ServerLevelData) ws.getLevelData()).getLevelName().equals(world.getName())) {
return new FabricWorld(ws);
}
}
return null;
}
}
@Override
public void registerCommands(CommandManager manager) {
// No-op, we register using Fabric's event
}
@Override
public void setGameHooksEnabled(boolean enabled) {
this.hookingEvents = enabled;
}
@Override
public FabricConfiguration getConfiguration() {
return mod.getConfig();
}
@Override
public String getVersion() {
return mod.getInternalVersion();
}
@Override
public String getPlatformName() {
return "Fabric-Official";
}
@Override
public String getPlatformVersion() {
return mod.getInternalVersion();
}
@Override
public Map<Capability, Preference> getCapabilities() {
Map<Capability, Preference> capabilities = new EnumMap<>(Capability.class);
capabilities.put(Capability.CONFIGURATION, Preference.PREFER_OTHERS);
capabilities.put(Capability.WORLDEDIT_CUI, Preference.NORMAL);
capabilities.put(Capability.GAME_HOOKS, Preference.NORMAL);
capabilities.put(Capability.PERMISSIONS, Preference.NORMAL);
capabilities.put(Capability.USER_COMMANDS, Preference.NORMAL);
capabilities.put(Capability.WORLD_EDITING, Preference.PREFERRED);
return capabilities;
}
private static final Set<SideEffect> SUPPORTED_SIDE_EFFECTS_NO_MIXIN = Sets.immutableEnumSet(
SideEffect.VALIDATION,
SideEffect.ENTITY_AI,
SideEffect.LIGHTING,
SideEffect.NEIGHBORS
);
private static final Set<SideEffect> SUPPORTED_SIDE_EFFECTS = Sets.immutableEnumSet(
Iterables.concat(SUPPORTED_SIDE_EFFECTS_NO_MIXIN, Collections.singleton(SideEffect.UPDATE))
);
@Override
public Set<SideEffect> getSupportedSideEffects() {
return ExtendedChunk.class.isAssignableFrom(LevelChunk.class)
? SUPPORTED_SIDE_EFFECTS
: SUPPORTED_SIDE_EFFECTS_NO_MIXIN;
}
@Override
public Collection<Actor> getConnectedUsers() {
List<Actor> users = new ArrayList<>();
PlayerList scm = FabricWorldEdit.LIFECYCLED_SERVER.valueOrThrow().getPlayerList();
for (ServerPlayer entity : scm.getPlayers()) {
if (entity != null) {
users.add(new FabricPlayer(entity));
}
}
return users;
}
}