2016-03-02 19:28:01 +00:00
|
|
|
package me.totalfreedom.totalfreedommod.command;
|
2013-07-28 21:31:41 +00:00
|
|
|
|
|
|
|
import java.lang.reflect.Field;
|
2016-03-06 15:56:15 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.rank.Rank;
|
2019-07-31 16:19:23 +00:00
|
|
|
import org.apache.commons.lang.ArrayUtils;
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
2013-07-28 21:31:41 +00:00
|
|
|
import org.bukkit.command.Command;
|
|
|
|
import org.bukkit.command.CommandSender;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
2016-03-06 15:56:15 +00:00
|
|
|
@CommandPermissions(level = Rank.SENIOR_ADMIN, source = SourceType.ONLY_CONSOLE)
|
2013-07-28 21:31:41 +00:00
|
|
|
@CommandParameters(description = "For developers only - debug things via reflection.", usage = "/<command>")
|
2015-10-19 17:43:46 +00:00
|
|
|
public class Command_debug extends FreedomCommand
|
2013-07-28 21:31:41 +00:00
|
|
|
{
|
2015-11-22 18:26:47 +00:00
|
|
|
|
2013-07-28 21:31:41 +00:00
|
|
|
@Override
|
2015-11-22 18:26:47 +00:00
|
|
|
public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
|
2013-07-28 21:31:41 +00:00
|
|
|
{
|
2013-07-29 00:50:57 +00:00
|
|
|
if (args.length < 3)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
String className = args[0];
|
|
|
|
String fieldName = args[1];
|
|
|
|
String newValue = StringUtils.join(ArrayUtils.subarray(args, 2, args.length), " ");
|
|
|
|
|
|
|
|
if (className.equalsIgnoreCase("_"))
|
|
|
|
{
|
2018-07-30 07:23:01 +00:00
|
|
|
className = "me.totalfreedom.totalfreedommod.TotalFreedomMod";
|
2013-07-29 00:50:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setStaticValue(className, fieldName, newValue);
|
|
|
|
|
|
|
|
sender.sendMessage("Debug: OK");
|
|
|
|
}
|
2013-08-14 13:28:19 +00:00
|
|
|
catch (Exception ex)
|
2013-07-29 00:50:57 +00:00
|
|
|
{
|
2013-08-14 13:28:19 +00:00
|
|
|
sender.sendMessage(ex.getMessage());
|
2013-07-29 00:50:57 +00:00
|
|
|
}
|
|
|
|
|
2013-07-28 21:31:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-29 00:50:57 +00:00
|
|
|
public static void setStaticValue(final String className, final String fieldName, final String newValueString) throws Exception
|
2013-07-28 21:31:41 +00:00
|
|
|
{
|
2013-07-29 00:50:57 +00:00
|
|
|
Class<?> forName = Class.forName(className);
|
|
|
|
if (forName != null)
|
2013-07-28 21:31:41 +00:00
|
|
|
{
|
2013-07-29 00:50:57 +00:00
|
|
|
final Field field = forName.getDeclaredField(fieldName);
|
|
|
|
if (field != null)
|
2013-07-28 21:31:41 +00:00
|
|
|
{
|
2013-07-29 00:50:57 +00:00
|
|
|
Object newValue;
|
2013-07-28 21:31:41 +00:00
|
|
|
|
2013-07-29 00:50:57 +00:00
|
|
|
Class<?> type = field.getType();
|
|
|
|
if (type.isPrimitive())
|
|
|
|
{
|
|
|
|
if (type.getName().equals("int"))
|
|
|
|
{
|
|
|
|
newValue = Integer.parseInt(newValueString);
|
|
|
|
}
|
|
|
|
else if (type.getName().equals("double"))
|
|
|
|
{
|
|
|
|
newValue = Double.parseDouble(newValueString);
|
|
|
|
}
|
|
|
|
else if (type.getName().equals("boolean"))
|
|
|
|
{
|
|
|
|
newValue = Boolean.parseBoolean(newValueString);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new Exception("Unknown primitive field type.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (type.isAssignableFrom(Integer.class))
|
|
|
|
{
|
|
|
|
newValue = new Integer(newValueString);
|
|
|
|
}
|
|
|
|
else if (type.isAssignableFrom(Double.class))
|
|
|
|
{
|
|
|
|
newValue = new Double(newValueString);
|
|
|
|
}
|
|
|
|
else if (type.isAssignableFrom(Boolean.class))
|
|
|
|
{
|
|
|
|
newValue = Boolean.valueOf(newValueString);
|
|
|
|
}
|
|
|
|
else if (type.isAssignableFrom(String.class))
|
|
|
|
{
|
|
|
|
newValue = newValueString;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new Exception("Unknown complex field type.");
|
|
|
|
}
|
|
|
|
}
|
2013-07-28 21:31:41 +00:00
|
|
|
|
2013-07-29 00:50:57 +00:00
|
|
|
field.setAccessible(true);
|
2013-07-28 21:31:41 +00:00
|
|
|
|
2013-07-29 00:50:57 +00:00
|
|
|
final Object oldValue = field.get(Class.forName(className));
|
|
|
|
if (oldValue != null)
|
|
|
|
{
|
|
|
|
field.set(oldValue, newValue);
|
2013-07-28 21:31:41 +00:00
|
|
|
}
|
2013-07-29 00:50:57 +00:00
|
|
|
|
|
|
|
field.setAccessible(false);
|
2013-07-28 21:31:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|