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

210 lines
5 KiB
Java
Raw Normal View History

2014-11-14 12:02:52 +01:00
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
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;
2014-12-29 21:59:05 +01:00
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 final AEBaseContainer source;
private final Field field;
private final int channel;
private Object clientVersion;
public SyncData( final AEBaseContainer container, final Field field, final GuiSync annotation )
{
2014-12-29 15:13:47 +01:00
this.clientVersion = null;
this.source = container;
this.field = field;
2014-12-29 15:13:47 +01:00
this.channel = annotation.value();
}
public int getChannel()
{
2014-12-29 15:13:47 +01:00
return this.channel;
}
public void tick( final ICrafting c )
{
try
{
final Object val = this.field.get( this.source );
if( val != null && this.clientVersion == null )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.send( c, val );
2015-04-29 02:30:53 +02:00
}
else if( !val.equals( this.clientVersion ) )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.send( c, val );
2015-04-29 02:30:53 +02:00
}
}
catch( final IllegalArgumentException e )
{
AELog.debug( e );
}
catch( final IllegalAccessException e )
{
AELog.debug( e );
}
catch( final IOException e )
{
AELog.debug( e );
}
}
private void send( final ICrafting o, final Object val ) throws IOException
{
if( val instanceof String )
{
if( o instanceof EntityPlayerMP )
2015-04-29 02:30:53 +02:00
{
NetworkHandler.instance.sendTo( new PacketValueConfig( "SyncDat." + this.channel, (String) val ), (EntityPlayerMP) o );
2015-04-29 02:30:53 +02:00
}
}
else if( this.field.getType().isEnum() )
{
o.sendProgressBarUpdate( this.source, this.channel, ( (Enum) val ).ordinal() );
}
else if( val instanceof Long || val.getClass() == long.class )
{
NetworkHandler.instance.sendTo( new PacketProgressBar( this.channel, (Long) val ), (EntityPlayerMP) o );
}
else if( val instanceof Boolean || val.getClass() == boolean.class )
{
o.sendProgressBarUpdate( this.source, this.channel, ( (Boolean) val ) ? 1 : 0 );
}
else
{
o.sendProgressBarUpdate( this.source, this.channel, (Integer) val );
}
this.clientVersion = val;
}
public void update( final Object val )
{
try
{
final Object oldValue = this.field.get( this.source );
if( val instanceof String )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.updateString( oldValue, (String) val );
2015-04-29 02:30:53 +02:00
}
2014-07-16 05:26:12 +02:00
else
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.updateValue( oldValue, (Long) val );
2015-04-29 02:30:53 +02:00
}
}
catch( final IllegalArgumentException e )
{
AELog.debug( e );
}
catch( final IllegalAccessException e )
{
AELog.debug( e );
}
}
private void updateString( final Object oldValue, final String val )
2014-07-16 05:26:12 +02:00
{
try
{
2014-12-29 15:13:47 +01:00
this.field.set( this.source, val );
2014-07-16 05:26:12 +02:00
}
catch( final IllegalArgumentException e )
2014-07-16 05:26:12 +02:00
{
AELog.debug( e );
2014-07-16 05:26:12 +02:00
}
catch( final IllegalAccessException e )
2014-07-16 05:26:12 +02:00
{
AELog.debug( e );
2014-07-16 05:26:12 +02:00
}
}
private void updateValue( final Object oldValue, final long val )
{
try
{
if( this.field.getType().isEnum() )
{
final EnumSet<? extends Enum> valList = EnumSet.allOf( (Class<? extends Enum>) this.field.getType() );
for( final Enum e : valList )
{
if( e.ordinal() == val )
{
2014-12-29 15:13:47 +01:00
this.field.set( this.source, e );
break;
}
}
}
else
{
if( this.field.getType().equals( int.class ) )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.field.set( this.source, (int) val );
2015-04-29 02:30:53 +02:00
}
else if( this.field.getType().equals( long.class ) )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.field.set( this.source, val );
2015-04-29 02:30:53 +02:00
}
else if( this.field.getType().equals( boolean.class ) )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.field.set( this.source, val == 1 );
2015-04-29 02:30:53 +02:00
}
else if( this.field.getType().equals( Integer.class ) )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.field.set( this.source, (int) val );
2015-04-29 02:30:53 +02:00
}
else if( this.field.getType().equals( Long.class ) )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.field.set( this.source, val );
2015-04-29 02:30:53 +02:00
}
else if( this.field.getType().equals( Boolean.class ) )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.field.set( this.source, val == 1 );
2015-04-29 02:30:53 +02:00
}
}
2014-12-29 15:13:47 +01:00
this.source.onUpdate( this.field.getName(), oldValue, this.field.get( this.source ) );
}
catch( final IllegalArgumentException e )
{
AELog.debug( e );
}
catch( final IllegalAccessException e )
{
AELog.debug( e );
}
}
}