Plex-FAWE/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongeWorldEdit.java

333 lines
10 KiB
Java
Raw Normal View History

2016-02-02 06:28:27 +00:00
/*
* 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 Lesser 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 Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.sponge;
import com.google.inject.Inject;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.WorldVector;
import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.event.platform.PlatformReadyEvent;
2016-02-03 00:28:05 +00:00
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extension.platform.Capability;
2016-02-02 06:28:27 +00:00
import com.sk89q.worldedit.extension.platform.Platform;
import com.sk89q.worldedit.internal.LocalWorldAdapter;
import com.sk89q.worldedit.sponge.adapter.AdapterLoadException;
import com.sk89q.worldedit.sponge.adapter.SpongeImplAdapter;
import com.sk89q.worldedit.sponge.adapter.SpongeImplLoader;
2016-05-18 22:30:22 +00:00
import com.sk89q.worldedit.sponge.config.SpongeConfiguration;
import org.slf4j.Logger;
2016-02-02 06:28:27 +00:00
import org.spongepowered.api.block.BlockSnapshot;
import org.spongepowered.api.block.BlockType;
import org.spongepowered.api.block.BlockTypes;
2016-02-02 23:14:30 +00:00
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.config.ConfigDir;
2016-02-02 06:28:27 +00:00
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.block.InteractBlockEvent;
import org.spongepowered.api.event.filter.cause.Root;
2016-02-02 06:28:27 +00:00
import org.spongepowered.api.event.game.state.*;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.plugin.Plugin;
import org.spongepowered.api.plugin.PluginContainer;
2016-02-02 23:14:30 +00:00
import org.spongepowered.api.scheduler.Task;
2016-02-02 06:28:27 +00:00
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;
2016-02-03 00:28:05 +00:00
import java.io.File;
import java.io.IOException;
2016-02-03 00:28:05 +00:00
import java.util.Optional;
import java.util.concurrent.TimeUnit;
2016-02-02 06:28:27 +00:00
import static com.google.common.base.Preconditions.checkNotNull;
/**
* The Sponge implementation of WorldEdit.
*/
@Plugin(id = SpongeWorldEdit.MOD_ID, name = "WorldEdit",
description = "WorldEdit is an easy-to-use in-game world editor for Minecraft",
url = "http://www.enginehub.org/worldedit")
2016-02-02 06:28:27 +00:00
public class SpongeWorldEdit {
@Inject
private Logger logger;
2016-02-02 06:28:27 +00:00
public static final String MOD_ID = "worldedit";
private SpongePermissionsProvider provider;
2016-02-02 06:28:27 +00:00
@Inject
private PluginContainer container;
private static SpongeWorldEdit inst;
public static PluginContainer container() {
return inst.container;
}
public static SpongeWorldEdit inst() {
return inst;
}
2016-02-02 06:28:27 +00:00
private SpongePlatform platform;
private SpongeImplAdapter spongeAdapter;
@Inject
2016-02-02 06:28:27 +00:00
private SpongeConfiguration config;
@Inject @ConfigDir(sharedRoot = false)
2016-02-02 06:28:27 +00:00
private File workingDir;
2016-02-02 23:14:30 +00:00
public SpongeWorldEdit() {
inst = this;
}
2016-02-02 06:28:27 +00:00
@Listener
public void preInit(GamePreInitializationEvent event) {
// Load configuration
2016-02-02 06:28:27 +00:00
config.load();
2016-02-02 23:14:30 +00:00
Task.builder().interval(30, TimeUnit.SECONDS).execute(ThreadSafeCache.getInstance()).submit(this);
2016-02-02 06:28:27 +00:00
}
@Listener
public void init(GameInitializationEvent event) {
2016-05-18 21:38:46 +00:00
CUIChannelHandler.init();
2016-02-02 06:28:27 +00:00
}
@Listener
public void postInit(GamePostInitializationEvent event) {
2016-02-02 23:14:30 +00:00
logger.info("WorldEdit for Sponge (version " + getInternalVersion() + ") is loaded");
2016-02-02 06:28:27 +00:00
}
@Listener
public void serverAboutToStart(GameAboutToStartServerEvent event) {
if (this.platform != null) {
logger.warn("GameAboutToStartServerEvent occurred when GameStoppingServerEvent hasn't");
2016-02-02 06:28:27 +00:00
WorldEdit.getInstance().getPlatformManager().unregister(platform);
}
this.platform = new SpongePlatform(this);
this.provider = new SpongePermissionsProvider();
2016-02-02 06:28:27 +00:00
WorldEdit.getInstance().getPlatformManager().register(platform);
}
@Listener
public void serverStopping(GameStoppingServerEvent event) {
WorldEdit.getInstance().getPlatformManager().unregister(platform);
}
@Listener
public void serverStarted(GameStartedServerEvent event) {
WorldEdit.getInstance().getEventBus().post(new PlatformReadyEvent());
loadAdapter();
}
private void loadAdapter() {
WorldEdit worldEdit = WorldEdit.getInstance();
// Attempt to load a Sponge adapter
SpongeImplLoader adapterLoader = new SpongeImplLoader();
try {
adapterLoader.addFromPath(getClass().getClassLoader());
} catch (IOException e) {
logger.warn("Failed to search path for Sponge adapters");
}
try {
adapterLoader.addFromJar(container.getSource().get().toFile());
} catch (IOException e) {
logger.warn("Failed to search " + container.getSource().get().toFile() + " for Sponge adapters", e);
}
try {
spongeAdapter = adapterLoader.loadAdapter();
logger.info("Using " + spongeAdapter.getClass().getCanonicalName() + " as the Sponge adapter");
} catch (AdapterLoadException e) {
Platform platform = worldEdit.getPlatformManager().queryCapability(Capability.WORLD_EDITING);
if (platform instanceof SpongePlatform) {
logger.warn(e.getMessage());
} else {
logger.info("WorldEdit could not find a Sponge adapter for this MC version, " +
"but it seems that you have another implementation of WorldEdit installed (" + platform.getPlatformName() + ") " +
"that handles the world editing.");
}
}
}
public SpongeImplAdapter getAdapter() {
return this.spongeAdapter;
2016-02-02 06:28:27 +00:00
}
@Listener
public void onPlayerInteract(InteractBlockEvent event, @Root Player spongePlayer) {
2016-02-02 06:28:27 +00:00
if (platform == null) {
return;
}
if (!platform.isHookingEvents()) return; // We have to be told to catch these events
WorldEdit we = WorldEdit.getInstance();
SpongePlayer player = wrapPlayer(spongePlayer);
2016-02-02 06:28:27 +00:00
com.sk89q.worldedit.world.World world = player.getWorld();
BlockSnapshot targetBlock = event.getTargetBlock();
Optional<Location<World>> optLoc = targetBlock.getLocation();
2016-02-02 06:28:27 +00:00
BlockType interactedType = targetBlock.getState().getType();
if (event instanceof InteractBlockEvent.Primary) {
if (interactedType != BlockTypes.AIR) {
if (!optLoc.isPresent()) {
return;
}
Location<World> loc = optLoc.get();
2016-02-02 06:28:27 +00:00
WorldVector pos = new WorldVector(LocalWorldAdapter.adapt(world), loc.getX(), loc.getY(), loc.getZ());
if (we.handleBlockLeftClick(player, pos)) {
event.setCancelled(true);
}
if (we.handleArmSwing(player)) {
event.setCancelled(true);
}
} else {
if (we.handleArmSwing(player)) {
event.setCancelled(true);
}
2016-02-02 06:28:27 +00:00
}
} else if (event instanceof InteractBlockEvent.Secondary) {
if (interactedType != BlockTypes.AIR) {
if (!optLoc.isPresent()) {
return;
}
Location<World> loc = optLoc.get();
2016-02-02 06:28:27 +00:00
WorldVector pos = new WorldVector(LocalWorldAdapter.adapt(world), loc.getX(), loc.getY(), loc.getZ());
if (we.handleBlockRightClick(player, pos)) {
event.setCancelled(true);
}
if (we.handleRightClick(player)) {
event.setCancelled(true);
}
} else {
if (we.handleRightClick(player)) {
event.setCancelled(true);
}
}
}
}
public static ItemStack toSpongeItemStack(BaseItemStack item) {
return inst().getAdapter().makeSpongeStack(item);
2016-02-02 06:28:27 +00:00
}
/**
* Get the configuration.
*
* @return the Sponge configuration
2016-02-02 06:28:27 +00:00
*/
SpongeConfiguration getConfig() {
return this.config;
}
/**
* Get the WorldEdit proxy for the given player.
*
* @param player the player
* @return the WorldEdit player
*/
2016-02-02 23:14:30 +00:00
public SpongePlayer wrapPlayer(Player player) {
2016-02-02 06:28:27 +00:00
checkNotNull(player);
return new SpongePlayer(platform, player);
}
2016-02-02 23:14:30 +00:00
public Actor wrapCommandSource(CommandSource sender) {
if (sender instanceof Player) {
return wrapPlayer((Player) sender);
}
return new SpongeCommandSender(this, sender);
}
2016-02-02 06:28:27 +00:00
/**
* Get the session for a player.
*
* @param player the player
* @return the session
*/
public LocalSession getSession(Player player) {
checkNotNull(player);
2016-02-02 23:14:30 +00:00
return WorldEdit.getInstance().getSessionManager().get(wrapPlayer(player));
2016-02-02 06:28:27 +00:00
}
/**
* Get the WorldEdit proxy for the given world.
*
* @param world the world
* @return the WorldEdit world
*/
public SpongeWorld getWorld(World world) {
checkNotNull(world);
return getAdapter().getWorld(world);
2016-02-02 06:28:27 +00:00
}
/**
* Get the WorldEdit proxy for the platform.
*
* @return the WorldEdit platform
*/
public Platform getPlatform() {
return this.platform;
}
/**
* Get the working directory where WorldEdit's files are stored.
*
* @return the working directory
*/
public File getWorkingDir() {
return this.workingDir;
}
/**
* Get the version of the WorldEdit Sponge implementation.
2016-02-02 06:28:27 +00:00
*
* @return a version string
*/
String getInternalVersion() {
return container.getVersion().orElse("Unknown");
2016-02-02 06:28:27 +00:00
}
public void setPermissionsProvider(SpongePermissionsProvider provider) {
2016-02-02 06:28:27 +00:00
this.provider = provider;
}
public SpongePermissionsProvider getPermissionsProvider() {
2016-02-02 06:28:27 +00:00
return provider;
}
}