From e9a97eeff620eaad6e50893e1fb29c41569dbd7b Mon Sep 17 00:00:00 2001 From: luacs1998 Date: Wed, 26 Nov 2014 18:50:36 +0800 Subject: [PATCH] Rough draft of WEPIF for WorldEdit-Forge. Mainly for FE compatibility, but other mods can implement this if they so want. This commit removes ForgeUtil, with its 2 methods relocated. To set a permissions provider, get the instance of ForgeWorldEdit, and call setPermissionsProvider with an instance of a class implementing ForgePermissionsProvider. --- .../sk89q/worldedit/forge/CommandWrapper.java | 77 +++++++++++++++++++ .../forge/ForgePermissionsProvider.java | 52 +++++++++++++ .../sk89q/worldedit/forge/ForgePlatform.java | 47 ++--------- .../sk89q/worldedit/forge/ForgePlayer.java | 2 +- .../com/sk89q/worldedit/forge/ForgeUtil.java | 52 ------------- .../com/sk89q/worldedit/forge/ForgeWorld.java | 2 +- .../sk89q/worldedit/forge/ForgeWorldEdit.java | 24 ++++++ 7 files changed, 162 insertions(+), 94 deletions(-) create mode 100644 worldedit-forge/src/main/java/com/sk89q/worldedit/forge/CommandWrapper.java create mode 100644 worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgePermissionsProvider.java delete mode 100644 worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeUtil.java diff --git a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/CommandWrapper.java b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/CommandWrapper.java new file mode 100644 index 000000000..1a725fad1 --- /dev/null +++ b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/CommandWrapper.java @@ -0,0 +1,77 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * 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 . + */ + +package com.sk89q.worldedit.forge; + +import com.sk89q.worldedit.util.command.CommandMapping; +import net.minecraft.command.CommandBase; +import net.minecraft.command.ICommand; +import net.minecraft.command.ICommandSender; + +import javax.annotation.Nullable; +import java.util.Arrays; +import java.util.List; + +public class CommandWrapper extends CommandBase { + private CommandMapping command; + + protected CommandWrapper(CommandMapping command) { + this.command = command; + } + + @Override + public String getCommandName() { + return command.getPrimaryAlias(); + } + + @Override + public List getCommandAliases() { + return Arrays.asList(command.getAllAliases()); + } + + @Override + public void processCommand(ICommandSender var1, String[] var2) {} + + @Override + public String getCommandUsage(ICommandSender icommandsender) { + return "/" + command.getPrimaryAlias() + " " + command.getDescription().getUsage(); + } + + @Override + public int getRequiredPermissionLevel() { + return 0; + } + + @Override + public boolean canCommandSenderUseCommand(ICommandSender sender) { + return true; + } + + @Override + public int compareTo(@Nullable + Object o) { + if (o == null) { + return 0; + } else if (o instanceof ICommand) { + return super.compareTo((ICommand) o); + } else { + return 0; + } + } +} diff --git a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgePermissionsProvider.java b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgePermissionsProvider.java new file mode 100644 index 000000000..60408df6c --- /dev/null +++ b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgePermissionsProvider.java @@ -0,0 +1,52 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * 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 . + */ + +package com.sk89q.worldedit.forge; + +import cpw.mods.fml.common.FMLCommonHandler; +import net.minecraft.command.ICommand; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.world.WorldSettings.GameType; + +public interface ForgePermissionsProvider { + + public boolean hasPermission(EntityPlayerMP player, String permission); + + public void registerPermission(ICommand command, String permission); + + public static class VanillaPermissionsProvider implements ForgePermissionsProvider { + + private ForgePlatform platform; + + public VanillaPermissionsProvider(ForgePlatform platform) { + this.platform = platform; + } + + @Override + public boolean hasPermission(EntityPlayerMP player, String permission) { + ForgeConfiguration configuration = platform.getConfiguration(); + return configuration.cheatMode || + FMLCommonHandler.instance().getMinecraftServerInstance().getConfigurationManager().func_152596_g(player.getGameProfile()) || + (configuration.creativeEnable && player.theItemInWorldManager.getGameType() == GameType.CREATIVE); + } + + @Override + public void registerPermission(ICommand command, String permission) {} + } +} diff --git a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgePlatform.java b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgePlatform.java index 7004ace44..d4021ff92 100644 --- a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgePlatform.java +++ b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgePlatform.java @@ -151,47 +151,14 @@ class ForgePlatform extends AbstractPlatform implements MultiUserPlatform { ServerCommandManager mcMan = (ServerCommandManager) server.getCommandManager(); for (final CommandMapping command : dispatcher.getCommands()) { - final Description description = command.getDescription(); - mcMan.registerCommand(new CommandBase() { - @Override - public String getCommandName() { - return command.getPrimaryAlias(); + CommandWrapper wrapper = new CommandWrapper(command); + mcMan.registerCommand(wrapper); + ForgeWorldEdit.inst.getPermissionsProvider().registerPermission(wrapper, command.getDescription().getPermissions().get(0)); + if (command.getDescription().getPermissions().size() > 1) { + for (int i = 1; i < command.getDescription().getPermissions().size(); i++) { + ForgeWorldEdit.inst.getPermissionsProvider().registerPermission(null, command.getDescription().getPermissions().get(i)); } - - @Override - public List getCommandAliases() { - return Arrays.asList(command.getAllAliases()); - } - - @Override - public void processCommand(ICommandSender var1, String[] var2) {} - - @Override - public String getCommandUsage(ICommandSender icommandsender) { - return "/" + command.getPrimaryAlias() + " " + description.getUsage(); - } - - @Override - public int getRequiredPermissionLevel() { - return 0; - } - - @Override - public boolean canCommandSenderUseCommand(ICommandSender sender) { - return true; - } - - @Override - public int compareTo(@Nullable Object o) { - if (o == null) { - return 0; - } else if (o instanceof ICommand) { - return super.compareTo((ICommand) o); - } else { - return 0; - } - } - }); + } } } diff --git a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgePlayer.java b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgePlayer.java index f18b628c0..945b790a4 100644 --- a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgePlayer.java +++ b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgePlayer.java @@ -163,7 +163,7 @@ public class ForgePlayer extends AbstractPlayerActor { @Override public boolean hasPermission(String perm) { - return ForgeUtil.hasPermission(platform, this.player, perm); + return ForgeWorldEdit.inst.getPermissionsProvider().hasPermission(player, perm); } @Nullable diff --git a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeUtil.java b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeUtil.java deleted file mode 100644 index f9b58ffd0..000000000 --- a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeUtil.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * WorldEdit, a Minecraft world manipulation toolkit - * Copyright (C) sk89q - * 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 . - */ - -package com.sk89q.worldedit.forge; - -import com.sk89q.worldedit.blocks.BaseItemStack; -import cpw.mods.fml.common.FMLCommonHandler; -import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.item.Item; -import net.minecraft.item.ItemStack; -import net.minecraft.world.WorldSettings.GameType; - -import java.util.Map; - -public final class ForgeUtil { - - private ForgeUtil() { - } - - public static boolean hasPermission(ForgePlatform platform, EntityPlayerMP player, String perm) { - // TODO fix WEPIF - ForgeConfiguration configuration = platform.getConfiguration(); - return configuration.cheatMode || - FMLCommonHandler.instance().getMinecraftServerInstance().getConfigurationManager().func_152596_g(player.getGameProfile()) || - (configuration.creativeEnable && player.theItemInWorldManager.getGameType() == GameType.CREATIVE); - } - - public static ItemStack toForgeItemStack(BaseItemStack item) { - ItemStack ret = new ItemStack(Item.getItemById(item.getType()), item.getAmount(), item.getData()); - for (Map.Entry entry : item.getEnchantments().entrySet()) { - ret.addEnchantment(net.minecraft.enchantment.Enchantment.enchantmentsList[((Integer) entry.getKey())], (Integer) entry.getValue()); - } - - return ret; - } -} \ No newline at end of file diff --git a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeWorld.java b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeWorld.java index e5514c388..92edac931 100644 --- a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeWorld.java +++ b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeWorld.java @@ -208,7 +208,7 @@ public class ForgeWorld extends AbstractWorld { return; } - EntityItem entity = new EntityItem(getWorld(), position.getX(), position.getY(), position.getZ(), ForgeUtil.toForgeItemStack(item)); + EntityItem entity = new EntityItem(getWorld(), position.getX(), position.getY(), position.getZ(), ForgeWorldEdit.toForgeItemStack(item)); entity.delayBeforeCanPickup = 10; getWorld().spawnEntityInWorld(entity); } diff --git a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeWorldEdit.java b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeWorldEdit.java index 2fb256bea..7284796e6 100644 --- a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeWorldEdit.java +++ b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeWorldEdit.java @@ -23,6 +23,7 @@ import com.google.common.base.Joiner; 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; import com.sk89q.worldedit.extension.platform.Platform; import com.sk89q.worldedit.internal.LocalWorldAdapter; @@ -40,6 +41,8 @@ import cpw.mods.fml.common.event.FMLServerStoppingEvent; import cpw.mods.fml.common.eventhandler.Event.Result; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; import net.minecraft.world.World; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.CommandEvent; @@ -47,6 +50,7 @@ import net.minecraftforge.event.entity.player.PlayerInteractEvent; import org.apache.logging.log4j.Logger; import java.io.File; +import java.util.Map; import static com.google.common.base.Preconditions.checkNotNull; import static net.minecraftforge.event.entity.player.PlayerInteractEvent.Action; @@ -61,6 +65,8 @@ public class ForgeWorldEdit { public static final String MOD_ID = "worldedit"; public static final String CUI_PLUGIN_CHANNEL = "WECUI"; + private ForgePermissionsProvider provider; + @Instance(MOD_ID) public static ForgeWorldEdit inst; @@ -107,6 +113,7 @@ public class ForgeWorldEdit { this.platform = new ForgePlatform(this); WorldEdit.getInstance().getPlatformManager().register(platform); + this.provider = new ForgePermissionsProvider.VanillaPermissionsProvider(platform); } @EventHandler @@ -184,6 +191,15 @@ public class ForgeWorldEdit { } } + public static ItemStack toForgeItemStack(BaseItemStack item) { + ItemStack ret = new ItemStack(Item.getItemById(item.getType()), item.getAmount(), item.getData()); + for (Map.Entry entry : item.getEnchantments().entrySet()) { + ret.addEnchantment(net.minecraft.enchantment.Enchantment.enchantmentsList[((Integer) entry.getKey())], (Integer) entry.getValue()); + } + + return ret; + } + /** * Get the configuration. * @@ -253,4 +269,12 @@ public class ForgeWorldEdit { return ForgeWorldEdit.class.getAnnotation(Mod.class).version(); } + public void setPermissionsProvider(ForgePermissionsProvider provider) { + this.provider = provider; + } + + public ForgePermissionsProvider getPermissionsProvider() { + return provider; + } + }