2015-02-18 16:47:59 +01:00
package com.pahimar.ee3.command ;
2016-05-24 20:58:56 +02:00
import com.pahimar.ee3.api.blacklist.BlacklistRegistryProxy ;
2015-05-07 19:45:06 +02:00
import com.pahimar.ee3.api.exchange.EnergyValue ;
2016-05-18 19:53:13 +02:00
import com.pahimar.ee3.api.exchange.EnergyValueRegistryProxy ;
2016-05-22 19:59:05 +02:00
import com.pahimar.ee3.exchange.EnergyValueRegistry ;
2015-02-19 06:06:18 +01:00
import com.pahimar.ee3.exchange.WrappedStack ;
2016-05-25 05:12:49 +02:00
import com.pahimar.ee3.network.PacketHandler ;
2016-05-25 16:20:04 +02:00
import com.pahimar.ee3.network.message.MessageSetBlacklistEntry ;
2016-05-25 05:12:49 +02:00
import com.pahimar.ee3.network.message.MessageSetEnergyValue ;
2015-02-18 16:47:59 +01:00
import com.pahimar.ee3.reference.Messages ;
import com.pahimar.ee3.reference.Names ;
import net.minecraft.command.CommandBase ;
import net.minecraft.command.ICommandSender ;
2015-02-19 06:06:18 +01:00
import net.minecraft.command.WrongUsageException ;
import net.minecraft.entity.player.EntityPlayer ;
import net.minecraft.item.ItemStack ;
import java.util.List ;
2015-02-18 16:47:59 +01:00
public class CommandSetEnergyValueCurrentItem extends CommandBase
{
@Override
public String getCommandName ( )
{
return Names . Commands . SET_ENERGY_VALUE_CURRENT_ITEM ;
}
@Override
public int getRequiredPermissionLevel ( )
{
return 2 ;
}
@Override
public String getCommandUsage ( ICommandSender commandSender )
{
return Messages . Commands . SET_ENERGY_VALUE_CURRENT_ITEM_USAGE ;
}
@Override
public void processCommand ( ICommandSender commandSender , String [ ] args )
{
2015-02-19 06:06:18 +01:00
if ( args . length < 3 )
{
throw new WrongUsageException ( Messages . Commands . SET_ENERGY_VALUE_CURRENT_ITEM_USAGE ) ;
}
else
{
2015-05-04 14:18:15 +02:00
float energyValue = 0 ;
2015-02-19 06:06:18 +01:00
if ( args . length > = 3 )
{
2015-05-04 14:18:15 +02:00
energyValue = ( float ) parseDoubleWithMin ( commandSender , args [ 2 ] , 0 ) ;
2015-02-19 06:06:18 +01:00
}
ItemStack itemStack = ( ( EntityPlayer ) commandSender ) . getCurrentEquippedItem ( ) ;
if ( itemStack ! = null )
{
2015-04-19 21:01:35 +02:00
WrappedStack wrappedStack = WrappedStack . wrap ( itemStack ) ;
2015-02-19 06:06:18 +01:00
EnergyValue newEnergyValue = new EnergyValue ( energyValue ) ;
2016-05-24 20:58:56 +02:00
if ( wrappedStack ! = null & & newEnergyValue ! = null )
2015-02-19 06:06:18 +01:00
{
2016-05-24 20:58:56 +02:00
if ( Float . compare ( newEnergyValue . getValue ( ) , 0 ) > 0 ) {
if ( args [ 1 ] . equalsIgnoreCase ( " pre " ) ) {
EnergyValueRegistryProxy . setEnergyValue ( wrappedStack , newEnergyValue , EnergyValueRegistryProxy . Phase . PRE_CALCULATION ) ;
}
else if ( args [ 1 ] . equalsIgnoreCase ( " post " ) ) {
EnergyValueRegistryProxy . setEnergyValue ( wrappedStack , newEnergyValue ) ;
2016-05-25 05:12:49 +02:00
PacketHandler . INSTANCE . sendToAll ( new MessageSetEnergyValue ( wrappedStack , newEnergyValue ) ) ;
2016-05-24 20:58:56 +02:00
}
else {
throw new WrongUsageException ( Messages . Commands . SET_ENERGY_VALUE_CURRENT_ITEM_USAGE ) ;
}
// Notify admins and log the value change
func_152373_a ( commandSender , this , Messages . Commands . SET_ENERGY_VALUE_CURRENT_ITEM_SUCCESS , new Object [ ] { commandSender . getCommandSenderName ( ) , args [ 1 ] , itemStack . func_151000_E ( ) , newEnergyValue . getChatComponent ( ) } ) ;
2015-06-04 03:13:50 +02:00
}
2016-05-24 20:58:56 +02:00
else if ( Float . compare ( newEnergyValue . getValue ( ) , 0 ) = = 0 ) {
2015-02-19 06:06:18 +01:00
2016-05-24 20:58:56 +02:00
BlacklistRegistryProxy . setAsNotLearnable ( wrappedStack ) ;
BlacklistRegistryProxy . setAsNotExchangeable ( wrappedStack ) ;
2016-05-25 05:12:49 +02:00
// TODO Remove energy value from EnergyValueRegistry
// TODO Sync change with client
2016-05-25 16:20:04 +02:00
PacketHandler . INSTANCE . sendToAll ( new MessageSetBlacklistEntry ( itemStack , BlacklistRegistryProxy . Blacklist . KNOWLEDGE ) ) ;
PacketHandler . INSTANCE . sendToAll ( new MessageSetBlacklistEntry ( itemStack , BlacklistRegistryProxy . Blacklist . EXCHANGE ) ) ;
2016-05-24 20:58:56 +02:00
func_152373_a ( commandSender , this , " %s set %s as not learnable and not exchangeable " , new Object [ ] { commandSender . getCommandSenderName ( ) , itemStack . func_151000_E ( ) } ) ;
}
2015-02-19 06:06:18 +01:00
}
else
{
throw new WrongUsageException ( Messages . Commands . SET_ENERGY_VALUE_CURRENT_ITEM_USAGE ) ;
}
2016-05-22 19:59:05 +02:00
EnergyValueRegistry . INSTANCE . save ( ) ;
2015-02-19 06:06:18 +01:00
}
else
{
throw new WrongUsageException ( Messages . Commands . NO_ITEM ) ;
}
}
}
@Override
public List addTabCompletionOptions ( ICommandSender commandSender , String [ ] args )
{
if ( args . length = = 2 )
{
2016-05-18 19:53:13 +02:00
return getListOfStringsMatchingLastWord ( args , " pre " , " post " ) ;
2015-02-19 06:06:18 +01:00
}
2015-02-18 16:47:59 +01:00
2015-02-19 06:06:18 +01:00
return null ;
2015-02-18 16:47:59 +01:00
}
}