2014-09-04 21:28:43 +02:00
|
|
|
package com.pahimar.ee3.command;
|
|
|
|
|
2014-09-10 04:52:46 +02:00
|
|
|
import com.pahimar.ee3.api.EnergyValue;
|
|
|
|
import com.pahimar.ee3.exchange.EnergyValueRegistry;
|
|
|
|
import com.pahimar.ee3.exchange.WrappedStack;
|
2014-09-11 22:13:39 +02:00
|
|
|
import com.pahimar.ee3.network.PacketHandler;
|
2014-09-15 22:06:20 +02:00
|
|
|
import com.pahimar.ee3.network.message.MessageSetEnergyValue;
|
|
|
|
import com.pahimar.ee3.reference.Files;
|
2015-02-06 03:50:52 +01:00
|
|
|
import com.pahimar.ee3.reference.Messages;
|
2015-02-06 05:06:53 +01:00
|
|
|
import com.pahimar.ee3.reference.Names;
|
2015-02-13 06:14:57 +01:00
|
|
|
import com.pahimar.ee3.reference.Reference;
|
2014-09-15 22:06:20 +02:00
|
|
|
import com.pahimar.ee3.util.SerializationHelper;
|
2015-02-13 06:14:57 +01:00
|
|
|
import cpw.mods.fml.common.FMLCommonHandler;
|
2014-09-15 22:06:20 +02:00
|
|
|
import net.minecraft.command.CommandBase;
|
2014-09-09 21:59:07 +02:00
|
|
|
import net.minecraft.command.ICommandSender;
|
|
|
|
import net.minecraft.command.WrongUsageException;
|
|
|
|
import net.minecraft.item.Item;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.JsonToNBT;
|
|
|
|
import net.minecraft.nbt.NBTBase;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
2015-02-13 06:14:57 +01:00
|
|
|
import java.io.File;
|
2014-09-09 21:59:07 +02:00
|
|
|
import java.util.List;
|
2014-09-15 22:06:20 +02:00
|
|
|
import java.util.Map;
|
2014-09-09 21:59:07 +02:00
|
|
|
|
2015-02-06 05:06:53 +01:00
|
|
|
public class CommandSetEnergyValue extends CommandBase
|
2014-09-04 21:28:43 +02:00
|
|
|
{
|
2014-09-09 21:59:07 +02:00
|
|
|
@Override
|
|
|
|
public String getCommandName()
|
|
|
|
{
|
2015-02-06 05:06:53 +01:00
|
|
|
return Names.Commands.SET_ENERGY_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getRequiredPermissionLevel()
|
|
|
|
{
|
|
|
|
return 2;
|
2014-09-15 22:06:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getCommandUsage(ICommandSender commandSender)
|
|
|
|
{
|
2015-02-06 05:06:53 +01:00
|
|
|
return Messages.Commands.SET_ENERGY_VALUE_USAGE;
|
2014-09-09 21:59:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void processCommand(ICommandSender commandSender, String[] args)
|
|
|
|
{
|
2015-02-06 05:06:53 +01:00
|
|
|
if (args.length < 4)
|
2014-09-09 21:59:07 +02:00
|
|
|
{
|
2015-02-06 05:06:53 +01:00
|
|
|
throw new WrongUsageException(Messages.Commands.SET_ENERGY_VALUE_USAGE);
|
2014-09-09 21:59:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-06 05:06:53 +01:00
|
|
|
Item item = getItemByText(commandSender, args[2]);
|
2014-09-09 21:59:07 +02:00
|
|
|
double energyValue = 0;
|
|
|
|
int metaData = 0;
|
|
|
|
|
2015-02-06 05:06:53 +01:00
|
|
|
if (args.length >= 4)
|
2014-09-09 21:59:07 +02:00
|
|
|
{
|
2015-02-06 05:06:53 +01:00
|
|
|
energyValue = parseDoubleWithMin(commandSender, args[3], 0);
|
2014-09-09 21:59:07 +02:00
|
|
|
}
|
2015-02-06 05:06:53 +01:00
|
|
|
else if (args.length >= 5)
|
2014-09-09 21:59:07 +02:00
|
|
|
{
|
2015-02-06 05:06:53 +01:00
|
|
|
metaData = parseInt(commandSender, args[4]);
|
2014-09-09 21:59:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ItemStack itemStack = new ItemStack(item, 1, metaData);
|
|
|
|
|
2015-02-06 05:06:53 +01:00
|
|
|
if (args.length >= 6)
|
2014-09-09 21:59:07 +02:00
|
|
|
{
|
2015-02-06 05:06:53 +01:00
|
|
|
String stringNBTData = func_147178_a(commandSender, args, 5).getUnformattedText();
|
2014-09-09 21:59:07 +02:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
NBTBase nbtBase = JsonToNBT.func_150315_a(stringNBTData);
|
|
|
|
|
|
|
|
if (!(nbtBase instanceof NBTTagCompound))
|
|
|
|
{
|
2015-02-06 03:50:52 +01:00
|
|
|
func_152373_a(commandSender, this, Messages.Commands.INVALID_NBT_TAG_ERROR, new Object[]{"Not a valid tag"});
|
2014-09-09 21:59:07 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
itemStack.setTagCompound((NBTTagCompound) nbtBase);
|
|
|
|
}
|
2014-09-10 03:12:59 +02:00
|
|
|
catch (Exception exception)
|
2014-09-09 21:59:07 +02:00
|
|
|
{
|
2015-02-06 03:50:52 +01:00
|
|
|
func_152373_a(commandSender, this, Messages.Commands.INVALID_NBT_TAG_ERROR, new Object[]{exception.getMessage()});
|
2014-09-09 21:59:07 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-09-10 04:52:46 +02:00
|
|
|
|
2014-09-11 22:13:39 +02:00
|
|
|
WrappedStack wrappedStack = new WrappedStack(itemStack);
|
|
|
|
EnergyValue newEnergyValue = new EnergyValue(energyValue);
|
|
|
|
|
|
|
|
if (wrappedStack != null && newEnergyValue != null && Float.compare(newEnergyValue.getEnergyValue(), 0) > 0)
|
|
|
|
{
|
2015-02-13 06:14:57 +01:00
|
|
|
File energyValuesDataDirectory = new File(FMLCommonHandler.instance().getMinecraftServerInstance().getEntityWorld().getSaveHandler().getWorldDirectory(), "data" + File.separator + Reference.LOWERCASE_MOD_ID + File.separator + "energyvalues");
|
|
|
|
if (args[1].equalsIgnoreCase("pre"))
|
2014-09-15 22:06:20 +02:00
|
|
|
{
|
2015-02-13 06:14:57 +01:00
|
|
|
File preAssignedValuesFile = new File(energyValuesDataDirectory, Files.PRE_ASSIGNED_ENERGY_VALUES);
|
|
|
|
|
|
|
|
Map<WrappedStack, EnergyValue> preAssignedValues = SerializationHelper.readEnergyValueStackMapFromJsonFile(preAssignedValuesFile);
|
2014-09-15 22:06:20 +02:00
|
|
|
preAssignedValues.put(wrappedStack, newEnergyValue);
|
2015-02-13 06:14:57 +01:00
|
|
|
|
|
|
|
SerializationHelper.writeEnergyValueStackMapToJsonFile(preAssignedValuesFile, preAssignedValues);
|
2014-09-16 18:08:16 +02:00
|
|
|
EnergyValueRegistry.getInstance().setShouldRegenNextRestart(true);
|
2014-09-15 22:06:20 +02:00
|
|
|
}
|
2015-02-13 06:14:57 +01:00
|
|
|
else if (args[1].equalsIgnoreCase("post"))
|
2014-09-15 22:06:20 +02:00
|
|
|
{
|
|
|
|
EnergyValueRegistry.getInstance().setEnergyValue(wrappedStack, newEnergyValue);
|
2015-02-13 06:14:57 +01:00
|
|
|
|
|
|
|
File postAssignedValuesFile = new File(energyValuesDataDirectory, Files.POST_ASSIGNED_ENERGY_VALUES);
|
|
|
|
|
|
|
|
Map<WrappedStack, EnergyValue> postAssignedValues = SerializationHelper.readEnergyValueStackMapFromJsonFile(postAssignedValuesFile);
|
2014-09-15 22:06:20 +02:00
|
|
|
postAssignedValues.put(wrappedStack, newEnergyValue);
|
2015-02-13 06:14:57 +01:00
|
|
|
|
|
|
|
SerializationHelper.writeEnergyValueStackMapToJsonFile(postAssignedValuesFile, postAssignedValues);
|
2014-09-15 22:06:20 +02:00
|
|
|
PacketHandler.INSTANCE.sendToAll(new MessageSetEnergyValue(wrappedStack, newEnergyValue));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify admins and log the value change
|
2015-02-06 05:06:53 +01:00
|
|
|
func_152373_a(commandSender, this, Messages.Commands.SET_ENERGY_VALUE_SUCCESS, new Object[]{commandSender.getCommandSenderName(), args[1], itemStack.func_151000_E(), newEnergyValue.getChatComponent()});
|
2014-09-11 22:13:39 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-06 05:06:53 +01:00
|
|
|
throw new WrongUsageException(Messages.Commands.SET_ENERGY_VALUE_USAGE);
|
2014-09-11 22:13:39 +02:00
|
|
|
}
|
2014-09-09 21:59:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List addTabCompletionOptions(ICommandSender commandSender, String[] args)
|
|
|
|
{
|
2015-02-06 05:06:53 +01:00
|
|
|
if (args.length == 2)
|
2014-09-15 22:06:20 +02:00
|
|
|
{
|
|
|
|
return getListOfStringsMatchingLastWord(args, "pre", "post");
|
|
|
|
}
|
2015-02-06 05:06:53 +01:00
|
|
|
else if (args.length == 3)
|
2014-09-15 22:06:20 +02:00
|
|
|
{
|
|
|
|
return getListOfStringsFromIterableMatchingLastWord(args, Item.itemRegistry.getKeys());
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2014-09-09 21:59:07 +02:00
|
|
|
}
|
2014-09-04 21:28:43 +02:00
|
|
|
}
|