Applied-Energistics-2-tiler.../src/main/java/appeng/container/guisync/SyncData.java

169 lines
3.6 KiB
Java
Raw Normal View History

package appeng.container.guisync;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.EnumSet;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.inventory.ICrafting;
import appeng.container.AEBaseContainer;
import appeng.core.AELog;
import appeng.core.sync.network.NetworkHandler;
import appeng.core.sync.packets.PacketProgressBar;
2014-07-16 05:26:12 +02:00
import appeng.core.sync.packets.PacketValueConfig;
2014-09-29 15:25:48 +02:00
public class SyncData
{
private Object clientVersion;
private AEBaseContainer source;
private Field field;
private int channel;
2014-09-29 15:25:48 +02:00
public SyncData(AEBaseContainer container, Field field, GuiSync annotation) {
clientVersion = null;
this.source = container;
this.field = field;
channel = annotation.value();
}
public int getChannel()
{
return channel;
}
public void tick(ICrafting c)
{
try
{
Object val = field.get( source );
2014-09-29 09:47:17 +02:00
if ( val != null && clientVersion == null )
send( c, val );
else if ( !val.equals( clientVersion ) )
send( c, val );
}
catch (IllegalArgumentException e)
{
AELog.error( e );
}
catch (IllegalAccessException e)
{
AELog.error( e );
}
catch (IOException e)
{
AELog.error( e );
}
}
2014-07-16 05:26:12 +02:00
public void update(Object val)
{
try
{
Object oldValue = field.get( source );
2014-07-16 05:26:12 +02:00
if ( val instanceof String )
updateString( oldValue, (String) val );
else
updateValue( oldValue, (Long) val );
}
catch (IllegalArgumentException e)
{
AELog.error( e );
}
catch (IllegalAccessException e)
{
AELog.error( e );
}
}
2014-07-16 05:26:12 +02:00
private void updateString(Object oldValue, String val)
{
try
{
field.set( source, val );
}
catch (IllegalArgumentException e)
{
AELog.error( e );
}
catch (IllegalAccessException e)
{
AELog.error( e );
}
}
private void updateValue(Object oldValue, long val)
{
try
{
if ( field.getType().isEnum() )
{
EnumSet<? extends Enum> valList = EnumSet.allOf( (Class<? extends Enum>) field.getType() );
for (Enum e : valList)
{
if ( e.ordinal() == val )
{
field.set( source, e );
break;
}
}
}
else
{
if ( field.getType().equals( int.class ) )
field.set( source, (int) val );
else if ( field.getType().equals( long.class ) )
2014-09-28 20:56:16 +02:00
field.set( source, val );
else if ( field.getType().equals( boolean.class ) )
field.set( source, val == 1 );
else if ( field.getType().equals( Integer.class ) )
2014-09-28 20:56:16 +02:00
field.set( source, (int) val );
else if ( field.getType().equals( Long.class ) )
2014-09-28 20:56:16 +02:00
field.set( source, val );
else if ( field.getType().equals( Boolean.class ) )
2014-09-28 20:56:16 +02:00
field.set( source, val == 1 );
}
source.onUpdate( field.getName(), oldValue, field.get( source ) );
}
catch (IllegalArgumentException e)
{
AELog.error( e );
}
catch (IllegalAccessException e)
{
AELog.error( e );
}
}
private void send(ICrafting o, Object val) throws IOException
{
2014-07-16 05:26:12 +02:00
if ( val instanceof String )
{
if ( o instanceof EntityPlayerMP )
NetworkHandler.instance.sendTo( new PacketValueConfig( "SyncDat." + channel, (String) val ), (EntityPlayerMP) o );
}
else if ( field.getType().isEnum() )
{
o.sendProgressBarUpdate( source, channel, ((Enum) val).ordinal() );
}
else if ( val instanceof Long || val.getClass() == long.class )
{
NetworkHandler.instance.sendTo( new PacketProgressBar( channel, (Long) val ), (EntityPlayerMP) o );
}
else if ( val instanceof Boolean || val.getClass() == boolean.class )
{
o.sendProgressBarUpdate( source, channel, ((Boolean) val) ? 1 : 0 );
}
else
{
o.sendProgressBarUpdate( source, channel, (Integer) val );
}
clientVersion = val;
}
}