From 31ad0e63d006b06d5e901a8ad3f3dfdbbfd301e3 Mon Sep 17 00:00:00 2001 From: woutwoot Date: Tue, 28 Oct 2014 15:46:32 +0100 Subject: [PATCH] Attempt at adding /gm --- README.md | 2 +- .../doubledoordev/d3commands/D3Commands.java | 1 + .../d3commands/commands/CommandGm.java | 140 ++++++++++++++++++ 3 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 src/main/java/net/doubledoordev/d3commands/commands/CommandGm.java diff --git a/README.md b/README.md index e7537a2..f7c79d3 100644 --- a/README.md +++ b/README.md @@ -17,9 +17,9 @@ Commands currently included: * /mem * /sethome *DOES NOT SAVE OVER SERVER RESTARTS YET!* * /home +* /gm Commands to be added soon: -* /gm * /tpa (Use json chat formatting to accept, no command) * ... diff --git a/src/main/java/net/doubledoordev/d3commands/D3Commands.java b/src/main/java/net/doubledoordev/d3commands/D3Commands.java index d0d350a..5a1d826 100644 --- a/src/main/java/net/doubledoordev/d3commands/D3Commands.java +++ b/src/main/java/net/doubledoordev/d3commands/D3Commands.java @@ -65,6 +65,7 @@ public void syncConfig() commands.add(new CommandEntry(new CommandBack(), configuration.getBoolean("back", MODID, true, "Teleport back to where you died the last time."))); commands.add(new CommandEntry(new CommandSetHome(), configuration.getBoolean("sethome", MODID, true, "Set your home location."))); commands.add(new CommandEntry(new CommandHome(), configuration.getBoolean("home", MODID, true, "Teleport back to your home."))); + commands.add(new CommandEntry(new CommandGm(), configuration.getBoolean("gm", MODID, true, "Shorter /gamemode command."))); if (configuration.hasChanged()) configuration.save(); } diff --git a/src/main/java/net/doubledoordev/d3commands/commands/CommandGm.java b/src/main/java/net/doubledoordev/d3commands/commands/CommandGm.java new file mode 100644 index 0000000..059ddd4 --- /dev/null +++ b/src/main/java/net/doubledoordev/d3commands/commands/CommandGm.java @@ -0,0 +1,140 @@ +/* + * Copyright (c) 2014, DoubleDoorDevelopment + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * Neither the name of the project nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package net.doubledoordev.d3commands.commands; + +import net.minecraft.command.CommandBase; +import net.minecraft.command.ICommandSender; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.server.MinecraftServer; +import net.minecraft.util.ChatComponentText; +import net.minecraft.util.ChatComponentTranslation; +import net.minecraft.world.WorldSettings; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by Wout on 28/10/2014. + */ +public class CommandGm extends CommandBase{ + + @Override + public String getCommandName() { + return "gm"; + } + + @Override + public String getCommandUsage(ICommandSender sender) { + return "/gm (mode/id) (username)"; + } + + @Override + public void processCommand(ICommandSender sender, String[] args) { + if (sender instanceof MinecraftServer || MinecraftServer.getServer().getConfigurationManager().func_152596_g(MinecraftServer.getServer().getConfigurationManager().func_152612_a(sender.getCommandSenderName()).getGameProfile())) { + EntityPlayerMP player; + switch (args.length) { + case 1: + player = getCommandSenderAsPlayer(sender); + setMode(args, player); + break; + case 0: + player = getCommandSenderAsPlayer(sender); + toggleMode(player); + break; + default: + player = getCommandSenderAsPlayer(sender); + player.addChatMessage(new ChatComponentText(getCommandUsage(sender))); + break; + case 2: + try { + player = getPlayer(sender, args[1]); + }catch(Exception e){ + sender.addChatMessage(new ChatComponentText("Player does not exist.")); + break; + } + setMode(args, player); + break; + } + }else{ + sender.addChatMessage(new ChatComponentTranslation("commands.generic.permission")); + } + } + + private void setMode(String[] args, EntityPlayerMP player) { + int id; + try { + id = Integer.parseInt(args[0]); + }catch (Exception e){ + id = -1; + } + if(id != -1 && id >= 0 && id <= WorldSettings.GameType.values().length) { + player.setGameType(WorldSettings.GameType.getByID(id)); + }else{ + player.setGameType(getGameModeFromString(args[0])); + } + } + + private WorldSettings.GameType getGameModeFromString(String st){ + for( WorldSettings.GameType t : WorldSettings.GameType.values()){ + if(t.getName().equalsIgnoreCase(st)){ + return t; + } + } + return WorldSettings.GameType.SURVIVAL; + } + + private void toggleMode(EntityPlayerMP player){ + if(player.capabilities.isCreativeMode){ + player.setGameType(WorldSettings.GameType.SURVIVAL); + }else{ + player.setGameType(WorldSettings.GameType.CREATIVE); + } + } + + private List getGameModes(){ + List res = new ArrayList(); + for( WorldSettings.GameType t : WorldSettings.GameType.values()){ + res.add(t.getName()); + } + return res; + } + + @Override + public List addTabCompletionOptions(ICommandSender sender, String[] args) + { + List res = new ArrayList(); + res.addAll(getListOfStringsMatchingLastWord(args, MinecraftServer.getServer().getAllUsernames())); + res.addAll(this.getGameModes()); + return res; + } + + +}