PageRenderTime 80ms CodeModel.GetById 68ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgePlatform.java

http://github.com/sk89q/worldedit
Java | 227 lines | 178 code | 30 blank | 19 comment | 15 complexity | 6fc43a8584c66b3bacb921d99d44c44b MD5 | raw file
  1/*
  2 * WorldEdit, a Minecraft world manipulation toolkit
  3 * Copyright (C) sk89q <http://www.sk89q.com>
  4 * Copyright (C) WorldEdit team and contributors
  5 *
  6 * This program is free software: you can redistribute it and/or modify it
  7 * under the terms of the GNU Lesser General Public License as published by the
  8 * Free Software Foundation, either version 3 of the License, or
  9 * (at your option) any later version.
 10 *
 11 * This program is distributed in the hope that it will be useful, but WITHOUT
 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 14 * for more details.
 15 *
 16 * You should have received a copy of the GNU Lesser General Public License
 17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 18 */
 19
 20package com.sk89q.worldedit.forge;
 21
 22import com.google.common.collect.Sets;
 23import com.sk89q.worldedit.command.util.PermissionCondition;
 24import com.sk89q.worldedit.entity.Player;
 25import com.sk89q.worldedit.extension.platform.AbstractPlatform;
 26import com.sk89q.worldedit.extension.platform.Actor;
 27import com.sk89q.worldedit.extension.platform.Capability;
 28import com.sk89q.worldedit.extension.platform.MultiUserPlatform;
 29import com.sk89q.worldedit.extension.platform.Preference;
 30import com.sk89q.worldedit.util.SideEffect;
 31import com.sk89q.worldedit.world.DataFixer;
 32import com.sk89q.worldedit.world.World;
 33import com.sk89q.worldedit.world.registry.Registries;
 34import net.minecraft.command.Commands;
 35import net.minecraft.entity.player.ServerPlayerEntity;
 36import net.minecraft.server.MinecraftServer;
 37import net.minecraft.server.dedicated.DedicatedServer;
 38import net.minecraft.server.management.PlayerList;
 39import net.minecraft.util.ResourceLocation;
 40import net.minecraft.util.SharedConstants;
 41import net.minecraft.world.server.ServerWorld;
 42import net.minecraftforge.fml.server.ServerLifecycleHooks;
 43import org.enginehub.piston.Command;
 44import org.enginehub.piston.CommandManager;
 45
 46import javax.annotation.Nullable;
 47import java.util.ArrayList;
 48import java.util.Collection;
 49import java.util.Collections;
 50import java.util.EnumMap;
 51import java.util.List;
 52import java.util.Map;
 53import java.util.Set;
 54
 55import static java.util.stream.Collectors.toList;
 56
 57class ForgePlatform extends AbstractPlatform implements MultiUserPlatform {
 58
 59    private final ForgeWorldEdit mod;
 60    private final MinecraftServer server;
 61    private final ForgeDataFixer dataFixer;
 62    private final @Nullable ForgeWatchdog watchdog;
 63    private boolean hookingEvents = false;
 64
 65    ForgePlatform(ForgeWorldEdit mod) {
 66        this.mod = mod;
 67        this.server = ServerLifecycleHooks.getCurrentServer();
 68        this.dataFixer = new ForgeDataFixer(getDataVersion());
 69        this.watchdog = server instanceof DedicatedServer
 70            ? new ForgeWatchdog((DedicatedServer) server) : null;
 71    }
 72
 73    boolean isHookingEvents() {
 74        return hookingEvents;
 75    }
 76
 77    @Override
 78    public Registries getRegistries() {
 79        return ForgeRegistries.getInstance();
 80    }
 81
 82    @Override
 83    public int getDataVersion() {
 84        return SharedConstants.getVersion().getWorldVersion();
 85    }
 86
 87    @Override
 88    public DataFixer getDataFixer() {
 89        return dataFixer;
 90    }
 91
 92    @Override
 93    public boolean isValidMobType(String type) {
 94        return net.minecraftforge.registries.ForgeRegistries.ENTITIES.containsKey(new ResourceLocation(type));
 95    }
 96
 97    @Override
 98    public void reload() {
 99        getConfiguration().load();
100    }
101
102    @Override
103    public int schedule(long delay, long period, Runnable task) {
104        return -1;
105    }
106
107    @Override
108    @Nullable
109    public ForgeWatchdog getWatchdog() {
110        return watchdog;
111    }
112
113    @Override
114    public List<? extends World> getWorlds() {
115        Iterable<ServerWorld> worlds = server.getWorlds();
116        List<World> ret = new ArrayList<>();
117        for (ServerWorld world : worlds) {
118            ret.add(new ForgeWorld(world));
119        }
120        return ret;
121    }
122
123    @Nullable
124    @Override
125    public Player matchPlayer(Player player) {
126        if (player instanceof ForgePlayer) {
127            return player;
128        } else {
129            ServerPlayerEntity entity = server.getPlayerList().getPlayerByUsername(player.getName());
130            return entity != null ? new ForgePlayer(entity) : null;
131        }
132    }
133
134    @Nullable
135    @Override
136    public World matchWorld(World world) {
137        if (world instanceof ForgeWorld) {
138            return world;
139        } else {
140            for (ServerWorld ws : server.getWorlds()) {
141                if (ws.getWorldInfo().getWorldName().equals(world.getName())) {
142                    return new ForgeWorld(ws);
143                }
144            }
145
146            return null;
147        }
148    }
149
150    @Override
151    public void registerCommands(CommandManager manager) {
152        if (server == null) return;
153        Commands mcMan = server.getCommandManager();
154
155        for (Command command : manager.getAllCommands().collect(toList())) {
156            CommandWrapper.register(mcMan.getDispatcher(), command);
157            Set<String> perms = command.getCondition().as(PermissionCondition.class)
158                .map(PermissionCondition::getPermissions)
159                .orElseGet(Collections::emptySet);
160            if (!perms.isEmpty()) {
161                perms.forEach(ForgeWorldEdit.inst.getPermissionsProvider()::registerPermission);
162            }
163        }
164    }
165
166    @Override
167    public void registerGameHooks() {
168        // We registered the events already anyway, so we just 'turn them on'
169        hookingEvents = true;
170    }
171
172    @Override
173    public ForgeConfiguration getConfiguration() {
174        return mod.getConfig();
175    }
176
177    @Override
178    public String getVersion() {
179        return mod.getInternalVersion();
180    }
181
182    @Override
183    public String getPlatformName() {
184        return "Forge-Official";
185    }
186
187    @Override
188    public String getPlatformVersion() {
189        return mod.getInternalVersion();
190    }
191
192    @Override
193    public Map<Capability, Preference> getCapabilities() {
194        Map<Capability, Preference> capabilities = new EnumMap<>(Capability.class);
195        capabilities.put(Capability.CONFIGURATION, Preference.PREFER_OTHERS);
196        capabilities.put(Capability.WORLDEDIT_CUI, Preference.NORMAL);
197        capabilities.put(Capability.GAME_HOOKS, Preference.NORMAL);
198        capabilities.put(Capability.PERMISSIONS, Preference.NORMAL);
199        capabilities.put(Capability.USER_COMMANDS, Preference.NORMAL);
200        capabilities.put(Capability.WORLD_EDITING, Preference.PREFERRED);
201        return capabilities;
202    }
203
204    private static final Set<SideEffect> SUPPORTED_SIDE_EFFECTS = Sets.immutableEnumSet(
205            SideEffect.VALIDATION,
206            SideEffect.ENTITY_AI,
207            SideEffect.LIGHTING,
208            SideEffect.NEIGHBORS
209    );
210
211    @Override
212    public Set<SideEffect> getSupportedSideEffects() {
213        return SUPPORTED_SIDE_EFFECTS;
214    }
215
216    @Override
217    public Collection<Actor> getConnectedUsers() {
218        List<Actor> users = new ArrayList<>();
219        PlayerList scm = server.getPlayerList();
220        for (ServerPlayerEntity entity : scm.getPlayers()) {
221            if (entity != null) {
222                users.add(new ForgePlayer(entity));
223            }
224        }
225        return users;
226    }
227}