Skip to content

Commit

Permalink
Better way to handle registering commands and config, added heal and …
Browse files Browse the repository at this point in the history
…feed command.

Messages still have to be added.
  • Loading branch information
woutwoot committed Oct 25, 2014
1 parent c9aed8d commit 326a680
Show file tree
Hide file tree
Showing 4 changed files with 274 additions and 15 deletions.
81 changes: 81 additions & 0 deletions src/main/java/net/doubledoordev/d3commands/CommandEntry.java
@@ -0,0 +1,81 @@
/*
* 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;

import net.minecraft.command.CommandBase;

/**
* Created by Wout on 25/10/2014.
*/
public class CommandEntry {

private CommandBase command;
private boolean enabled;
private String permission;

public CommandEntry(CommandBase command, boolean enabled, String permission) {
this.command = command;
this.enabled = enabled;
this.permission = permission;
}

public CommandEntry(CommandBase command, boolean enabled) {
this.command = command;
this.enabled = enabled;
}

public CommandBase getCommand() {
return command;
}

public void setCommand(CommandBase command) {
this.command = command;
}

public boolean isEnabled() {
return enabled;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public String getPermission() {
if(permission == null){
return "d3.commands." + this.getCommand().getCommandName().toLowerCase();
}
return permission;
}

public void setPermission(String permission) {
this.permission = permission;
}
}
30 changes: 15 additions & 15 deletions src/main/java/net/doubledoordev/d3commands/D3Commands.java
Expand Up @@ -9,6 +9,7 @@
import net.minecraftforge.common.config.ConfigElement;
import net.minecraftforge.common.config.Configuration;

import java.util.ArrayList;
import java.util.List;

import static net.doubledoordev.d3commands.util.Constants.MODID;
Expand All @@ -20,11 +21,8 @@ public class D3Commands implements ID3Mod
@Mod.Instance(MODID)
public static D3Commands instance;

private boolean tps = true;
private boolean tpx = true;
private boolean top = true;
private boolean kill = true;
private boolean getuuid = true;
private List<CommandEntry> commands = new ArrayList<>();

public Configuration configuration;

@Mod.EventHandler
Expand All @@ -41,11 +39,13 @@ public void syncConfig()
configuration.addCustomCategoryComment(MODID, "Set any value to false to disable the command.");
configuration.setCategoryRequiresWorldRestart(MODID, true);

tps = configuration.getBoolean("tps", MODID, tps, "A TPS command for all players, not just ops.");
tpx = configuration.getBoolean("tpx", MODID, tpx, "Interdimensional TP command.");
top = configuration.getBoolean("top", MODID, top, "Teleport yourself to the highest block above you.");
kill = configuration.getBoolean("kill", MODID, kill, "Allow you to kill other players.");
getuuid = configuration.getBoolean("getuuid", MODID, getuuid, "Allows easy UUID grabbing.");
commands.add(new CommandEntry(new CommandTps(), configuration.getBoolean("tps", MODID, true, "A TPS command for all players, not just ops.")));
commands.add(new CommandEntry(new CommandTpx(), configuration.getBoolean("tpx", MODID, true, "Interdimensional TP command.")));
commands.add(new CommandEntry(new CommandTop(), configuration.getBoolean("top", MODID, true, "Teleport yourself to the highest block above you.")));
commands.add(new CommandEntry(new CommandKill(), configuration.getBoolean("kill", MODID, true, "Allow you to kill other players.")));
commands.add(new CommandEntry(new CommandHeal(), configuration.getBoolean("heal", MODID, true, "Heal yourself or other players.")));
commands.add(new CommandEntry(new CommandFeed(), configuration.getBoolean("feed", MODID, true, "Feed yourself or other players.")));
commands.add(new CommandEntry(new CommandGetUUID(), configuration.getBoolean("getuuid", MODID, true, "Allows easy UUID grabbing.")));

if (configuration.hasChanged()) configuration.save();
}
Expand All @@ -59,11 +59,11 @@ public void addConfigElements(List<IConfigElement> configElements)
@Mod.EventHandler
public void serverStarting(FMLServerStartingEvent event)
{
if (tps) event.registerServerCommand(new CommandTps());
if (tpx) event.registerServerCommand(new CommandTpx());
if (top) event.registerServerCommand(new CommandTop());
if (kill) event.registerServerCommand(new CommandKill());
if (getuuid) event.registerServerCommand(new CommandGetUUID());
for(CommandEntry e : commands){
if(e.isEnabled()){
event.registerServerCommand(e.getCommand());
}
}
}
}

@@ -0,0 +1,88 @@
/*
* 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 java.util.List;

public class CommandFeed extends CommandBase
{
@Override
public String getCommandName() {
return "feed";
}

@Override
public String getCommandUsage(ICommandSender icommandsender)
{
return "/feed [target player]";
}

@Override
public void processCommand(final ICommandSender sender, final String[] args)
{
if (args.length == 1)
{
if (sender instanceof MinecraftServer || MinecraftServer.getServer().getConfigurationManager().func_152596_g(MinecraftServer.getServer().getConfigurationManager().func_152612_a(sender.getCommandSenderName()).getGameProfile()))
{
EntityPlayerMP playerFeed = getPlayer(sender, args[0]);
playerFeed.getFoodStats().setFoodLevel(20);
//TODO: Add succes message, need to ask dries how to get stuff from the .lang files.
}
else sender.addChatMessage(new ChatComponentTranslation("commands.generic.permission"));
}
else
{
EntityPlayerMP playerFeed = getCommandSenderAsPlayer(sender);
if (args.length == 0)
{
playerFeed.getFoodStats().setFoodLevel(20);
}
else
{
playerFeed.addChatMessage(new ChatComponentText(getCommandUsage(sender)));
}
}
}

@Override
public List addTabCompletionOptions(final ICommandSender sender, final String[] args)
{
if (args.length == 1) return getListOfStringsMatchingLastWord(args, MinecraftServer.getServer().getAllUsernames());
return null;
}
}
@@ -0,0 +1,90 @@
/*
* 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 java.util.List;

public class CommandHeal extends CommandBase
{
@Override
public String getCommandName() {
return "heal";
}

@Override
public String getCommandUsage(ICommandSender icommandsender)
{
return "/heal [target player]";
}

@Override
public void processCommand(final ICommandSender sender, final String[] args)
{
if (args.length == 1)
{
if (sender instanceof MinecraftServer || MinecraftServer.getServer().getConfigurationManager().func_152596_g(MinecraftServer.getServer().getConfigurationManager().func_152612_a(sender.getCommandSenderName()).getGameProfile()))
{
EntityPlayerMP playerHeal = getPlayer(sender, args[0]);
playerHeal.setHealth(20);
playerHeal.getFoodStats().setFoodLevel(20);
//TODO: Add succes message, need to ask dries how to get stuff from the .lang files.
}
else sender.addChatMessage(new ChatComponentTranslation("commands.generic.permission"));
}
else
{
EntityPlayerMP playerHeal = getCommandSenderAsPlayer(sender);
if (args.length == 0)
{
playerHeal.setHealth(20);
playerHeal.getFoodStats().setFoodLevel(20);
}
else
{
playerHeal.addChatMessage(new ChatComponentText(getCommandUsage(sender)));
}
}
}

@Override
public List addTabCompletionOptions(final ICommandSender sender, final String[] args)
{
if (args.length == 1) return getListOfStringsMatchingLastWord(args, MinecraftServer.getServer().getAllUsernames());
return null;
}
}

0 comments on commit 326a680

Please sign in to comment.