Merge pull request #1372 from thatsIch/b-1368-vibration-chamber-comparator

Fixes #1368: Vibration Chamber notifies neighbors on inventory change and state change
This commit is contained in:
thatsIch 2015-05-03 16:24:26 +02:00
commit 1baed48b0a
8 changed files with 123 additions and 84 deletions

View file

@ -39,7 +39,7 @@ import appeng.tile.misc.TileVibrationChamber;
import appeng.util.Platform; import appeng.util.Platform;
public class BlockVibrationChamber extends AEBaseBlock public final class BlockVibrationChamber extends AEBaseBlock
{ {
public BlockVibrationChamber() public BlockVibrationChamber()

View file

@ -11,7 +11,7 @@ import java.lang.annotation.Target;
* Marker interface to help identify invocation of reflection * Marker interface to help identify invocation of reflection
*/ */
@Retention( RetentionPolicy.SOURCE ) @Retention( RetentionPolicy.SOURCE )
@Target( { ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.TYPE } ) @Target( { ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.TYPE, ElementType.METHOD } )
public @interface Reflected public @interface Reflected
{ {

View file

@ -19,6 +19,8 @@
package appeng.tile; package appeng.tile;
import javax.annotation.Nullable;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory; import net.minecraft.inventory.IInventory;
@ -93,7 +95,7 @@ public abstract class AEBaseInvTile extends AEBaseTile implements ISidedInventor
} }
@Override @Override
public void setInventorySlotContents( int i, ItemStack itemstack ) public void setInventorySlotContents( int i, @Nullable ItemStack itemstack )
{ {
this.getInternalInventory().setInventorySlotContents( i, itemstack ); this.getInternalInventory().setInventorySlotContents( i, itemstack );
} }

View file

@ -26,6 +26,9 @@ import java.util.EnumMap;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
@ -61,14 +64,15 @@ public class AEBaseTile extends TileEntity implements IOrientable, ICommonTile,
{ {
public static final ThreadLocal<WeakReference<AEBaseTile>> DROP_NO_ITEMS = new ThreadLocal<WeakReference<AEBaseTile>>(); public static final ThreadLocal<WeakReference<AEBaseTile>> DROP_NO_ITEMS = new ThreadLocal<WeakReference<AEBaseTile>>();
private static final HashMap<Class, EnumMap<TileEventType, List<AETileEventHandler>>> HANDLERS = new HashMap<Class, EnumMap<TileEventType, List<AETileEventHandler>>>(); private static final Map<Class<? extends AEBaseTile>, Map<TileEventType, List<AETileEventHandler>>> HANDLERS = new HashMap<Class<? extends AEBaseTile>, Map<TileEventType, List<AETileEventHandler>>>();
private static final HashMap<Class, ItemStackSrc> ITEM_STACKS = new HashMap<Class, ItemStackSrc>(); private static final Map<Class<? extends TileEntity>, ItemStackSrc> ITEM_STACKS = new HashMap<Class<? extends TileEntity>, ItemStackSrc>();
public int renderFragment = 0; private int renderFragment = 0;
@Nullable
public String customName; public String customName;
private ForgeDirection forward = ForgeDirection.UNKNOWN; private ForgeDirection forward = ForgeDirection.UNKNOWN;
private ForgeDirection up = ForgeDirection.UNKNOWN; private ForgeDirection up = ForgeDirection.UNKNOWN;
public static void registerTileItem( Class c, ItemStackSrc wat ) public static void registerTileItem( Class<? extends TileEntity> c, ItemStackSrc wat )
{ {
ITEM_STACKS.put( c, wat ); ITEM_STACKS.put( c, wat );
} }
@ -84,11 +88,13 @@ public class AEBaseTile extends TileEntity implements IOrientable, ICommonTile,
return !this.worldObj.blockExists( this.xCoord, this.yCoord, this.zCoord ); return !this.worldObj.blockExists( this.xCoord, this.yCoord, this.zCoord );
} }
@Nonnull
public TileEntity getTile() public TileEntity getTile()
{ {
return this; return this;
} }
@Nullable
protected ItemStack getItemFromTile( Object obj ) protected ItemStack getItemFromTile( Object obj )
{ {
ItemStackSrc src = ITEM_STACKS.get( obj.getClass() ); ItemStackSrc src = ITEM_STACKS.get( obj.getClass() );
@ -99,11 +105,6 @@ public class AEBaseTile extends TileEntity implements IOrientable, ICommonTile,
return src.stack( 1 ); return src.stack( 1 );
} }
public final void Tick()
{
}
/** /**
* for dormant chunk cache. * for dormant chunk cache.
*/ */
@ -176,7 +177,7 @@ public class AEBaseTile extends TileEntity implements IOrientable, ICommonTile,
{ {
for( AETileEventHandler h : this.getHandlerListFor( TileEventType.TICK ) ) for( AETileEventHandler h : this.getHandlerListFor( TileEventType.TICK ) )
{ {
h.Tick( this ); h.tick( this );
} }
} }
@ -211,10 +212,11 @@ public class AEBaseTile extends TileEntity implements IOrientable, ICommonTile,
return this.hasHandlerFor( TileEventType.TICK ); return this.hasHandlerFor( TileEventType.TICK );
} }
protected boolean hasHandlerFor( TileEventType type ) private boolean hasHandlerFor( TileEventType type )
{ {
List<AETileEventHandler> list = this.getHandlerListFor( type ); List<AETileEventHandler> list = this.getHandlerListFor( type );
return list != null && !list.isEmpty();
return !list.isEmpty();
} }
@Override @Override
@ -331,45 +333,72 @@ public class AEBaseTile extends TileEntity implements IOrientable, ICommonTile,
return true; return true;
} }
protected List<AETileEventHandler> getHandlerListFor( TileEventType type ) @Nonnull
private List<AETileEventHandler> getHandlerListFor( TileEventType type )
{ {
Class clz = this.getClass(); final Map<TileEventType, List<AETileEventHandler>> eventToHandlers = this.getEventToHandlers();
EnumMap<TileEventType, List<AETileEventHandler>> handlerSet = HANDLERS.get( clz ); final List<AETileEventHandler> handlers = this.getHandlers( eventToHandlers, type );
if( handlerSet == null ) return handlers;
{
HANDLERS.put( clz, handlerSet = new EnumMap<TileEventType, List<AETileEventHandler>>( TileEventType.class ) );
for( Method m : clz.getMethods() )
{
TileEvent te = m.getAnnotation( TileEvent.class );
if( te != null )
{
this.addHandler( handlerSet, te.value(), m );
}
}
}
List<AETileEventHandler> list = handlerSet.get( type );
if( list == null )
{
handlerSet.put( type, list = new LinkedList<AETileEventHandler>() );
}
return list;
} }
private void addHandler( EnumMap<TileEventType, List<AETileEventHandler>> handlerSet, TileEventType value, Method m ) @Nonnull
private Map<TileEventType, List<AETileEventHandler>> getEventToHandlers()
{
final Class<? extends AEBaseTile> clazz = this.getClass();
final Map<TileEventType, List<AETileEventHandler>> storedHandlers = HANDLERS.get( clazz );
if ( storedHandlers == null )
{
final Map<TileEventType, List<AETileEventHandler>> newStoredHandlers = new EnumMap<TileEventType, List<AETileEventHandler>>( TileEventType.class );
HANDLERS.put( clazz, newStoredHandlers );
for( Method method : clazz.getMethods() )
{
TileEvent event = method.getAnnotation( TileEvent.class );
if( event != null )
{
this.addHandler( newStoredHandlers, event.value(), method );
}
}
return newStoredHandlers;
}
else
{
return storedHandlers;
}
}
@Nonnull
private List<AETileEventHandler> getHandlers( Map<TileEventType, List<AETileEventHandler>> eventToHandlers, TileEventType event ) {
final List<AETileEventHandler> oldHandlers = eventToHandlers.get( event );
if( oldHandlers == null )
{
final List<AETileEventHandler> newHandlers = new LinkedList<AETileEventHandler>();
eventToHandlers.put( event, newHandlers );
return newHandlers;
}
else
{
return oldHandlers;
}
}
private void addHandler( Map<TileEventType, List<AETileEventHandler>> handlerSet, TileEventType value, Method m )
{ {
List<AETileEventHandler> list = handlerSet.get( value ); List<AETileEventHandler> list = handlerSet.get( value );
if( list == null ) if( list == null )
{ {
handlerSet.put( value, list = new ArrayList<AETileEventHandler>() ); list = new ArrayList<AETileEventHandler>();
handlerSet.put( value, list );
} }
list.add( new AETileEventHandler( m, value ) ); list.add( new AETileEventHandler( m ) );
} }
@Override @Override

View file

@ -32,18 +32,18 @@ import cpw.mods.fml.relauncher.SideOnly;
import appeng.tile.AEBaseTile; import appeng.tile.AEBaseTile;
public class AETileEventHandler public final class AETileEventHandler
{ {
private final Method method; private final Method method;
public AETileEventHandler( Method m, TileEventType which ) public AETileEventHandler( Method method )
{ {
this.method = m; this.method = method;
} }
// TICK // TICK
public void Tick( AEBaseTile tile ) public void tick( AEBaseTile tile )
{ {
try try
{ {

View file

@ -25,5 +25,10 @@ public enum TileEventType
WORLD_NBT_READ, WORLD_NBT_WRITE, WORLD_NBT_READ, WORLD_NBT_WRITE,
NETWORK_READ, NETWORK_WRITE /**
* Methods annotated with this need to return a boolean
*/
NETWORK_READ,
NETWORK_WRITE
} }

View file

@ -36,21 +36,25 @@ import appeng.api.networking.ticking.TickingRequest;
import appeng.api.util.AECableType; import appeng.api.util.AECableType;
import appeng.api.util.DimensionalCoord; import appeng.api.util.DimensionalCoord;
import appeng.core.settings.TickRates; import appeng.core.settings.TickRates;
import appeng.helpers.Reflected;
import appeng.me.GridAccessException; import appeng.me.GridAccessException;
import appeng.tile.TileEvent; import appeng.tile.TileEvent;
import appeng.tile.events.TileEventType; import appeng.tile.events.TileEventType;
import appeng.tile.grid.AENetworkInvTile; import appeng.tile.grid.AENetworkInvTile;
import appeng.tile.inventory.AppEngInternalInventory; import appeng.tile.inventory.AppEngInternalInventory;
import appeng.tile.inventory.InvOperation; import appeng.tile.inventory.InvOperation;
import appeng.util.Platform;
public class TileVibrationChamber extends AENetworkInvTile implements IGridTickable public class TileVibrationChamber extends AENetworkInvTile implements IGridTickable
{ {
private static final int FUEL_SLOT_INDEX = 0;
final double powerPerTick = 5; private static final double POWER_PER_TICK = 5;
private static final int[] ACCESSIBLE_SLOTS = new int[] { FUEL_SLOT_INDEX };
final int[] sides = new int[] { 0 }; private static final int MAX_BURN_SPEED = 200;
final AppEngInternalInventory inv = new AppEngInternalInventory( this, 1 ); private static final double DILATION_SCALING = 100.0;
private static final int MIN_BURN_SPEED = 20;
private final IInventory inv = new AppEngInternalInventory( this, 1 );
public int burnSpeed = 100; public int burnSpeed = 100;
public double burnTime = 0; public double burnTime = 0;
@ -71,16 +75,20 @@ public class TileVibrationChamber extends AENetworkInvTile implements IGridTicka
return AECableType.COVERED; return AECableType.COVERED;
} }
@Reflected
@TileEvent( TileEventType.NETWORK_READ ) @TileEvent( TileEventType.NETWORK_READ )
public boolean readFromStream_TileVibrationChamber( ByteBuf data ) public boolean hasUpdate( ByteBuf data )
{ {
boolean wasOn = this.isOn; final boolean wasOn = this.isOn;
this.isOn = data.readBoolean(); this.isOn = data.readBoolean();
return wasOn != this.isOn; // TESR doesn't need updates! return wasOn != this.isOn; // TESR doesn't need updates!
} }
@Reflected
@TileEvent( TileEventType.NETWORK_WRITE ) @TileEvent( TileEventType.NETWORK_WRITE )
public void writeToStream_TileVibrationChamber( ByteBuf data ) public void writeToNetwork( ByteBuf data )
{ {
data.writeBoolean( this.burnTime > 0 ); data.writeBoolean( this.burnTime > 0 );
} }
@ -107,12 +115,6 @@ public class TileVibrationChamber extends AENetworkInvTile implements IGridTicka
return this.inv; return this.inv;
} }
@Override
public int getInventoryStackLimit()
{
return 64;
}
@Override @Override
public boolean isItemValidForSlot( int i, ItemStack itemstack ) public boolean isItemValidForSlot( int i, ItemStack itemstack )
{ {
@ -147,12 +149,12 @@ public class TileVibrationChamber extends AENetworkInvTile implements IGridTicka
@Override @Override
public int[] getAccessibleSlotsBySide( ForgeDirection side ) public int[] getAccessibleSlotsBySide( ForgeDirection side )
{ {
return this.sides; return ACCESSIBLE_SLOTS;
} }
private boolean canEatFuel() private boolean canEatFuel()
{ {
ItemStack is = this.getStackInSlot( 0 ); ItemStack is = this.getStackInSlot( FUEL_SLOT_INDEX );
if( is != null ) if( is != null )
{ {
int newBurnTime = TileEntityFurnace.getItemBurnTime( is ); int newBurnTime = TileEntityFurnace.getItemBurnTime( is );
@ -182,7 +184,7 @@ public class TileVibrationChamber extends AENetworkInvTile implements IGridTicka
} }
@Override @Override
public TickRateModulation tickingRequest( IGridNode node, int TicksSinceLastCall ) public TickRateModulation tickingRequest( IGridNode node, int ticksSinceLastCall )
{ {
if( this.burnTime <= 0 ) if( this.burnTime <= 0 )
{ {
@ -197,10 +199,10 @@ public class TileVibrationChamber extends AENetworkInvTile implements IGridTicka
return TickRateModulation.SLEEP; return TickRateModulation.SLEEP;
} }
this.burnSpeed = Math.max( 20, Math.min( this.burnSpeed, 200 ) ); this.burnSpeed = Math.max( MIN_BURN_SPEED, Math.min( this.burnSpeed, MAX_BURN_SPEED ) );
double dilation = this.burnSpeed / 100.0; double dilation = this.burnSpeed / DILATION_SCALING;
double timePassed = TicksSinceLastCall * dilation; double timePassed = ticksSinceLastCall * dilation;
this.burnTime -= timePassed; this.burnTime -= timePassed;
if( this.burnTime < 0 ) if( this.burnTime < 0 )
{ {
@ -211,7 +213,7 @@ public class TileVibrationChamber extends AENetworkInvTile implements IGridTicka
try try
{ {
IEnergyGrid grid = this.gridProxy.getEnergy(); IEnergyGrid grid = this.gridProxy.getEnergy();
double newPower = timePassed * this.powerPerTick; double newPower = timePassed * POWER_PER_TICK;
double overFlow = grid.injectPower( newPower, Actionable.SIMULATE ); double overFlow = grid.injectPower( newPower, Actionable.SIMULATE );
// burn the over flow. // burn the over flow.
@ -219,30 +221,30 @@ public class TileVibrationChamber extends AENetworkInvTile implements IGridTicka
if( overFlow > 0 ) if( overFlow > 0 )
{ {
this.burnSpeed -= TicksSinceLastCall; this.burnSpeed -= ticksSinceLastCall;
} }
else else
{ {
this.burnSpeed += TicksSinceLastCall; this.burnSpeed += ticksSinceLastCall;
} }
this.burnSpeed = Math.max( 20, Math.min( this.burnSpeed, 200 ) ); this.burnSpeed = Math.max( MIN_BURN_SPEED, Math.min( this.burnSpeed, MAX_BURN_SPEED ) );
return overFlow > 0 ? TickRateModulation.SLOWER : TickRateModulation.FASTER; return overFlow > 0 ? TickRateModulation.SLOWER : TickRateModulation.FASTER;
} }
catch( GridAccessException e ) catch( GridAccessException e )
{ {
this.burnSpeed -= TicksSinceLastCall; this.burnSpeed -= ticksSinceLastCall;
this.burnSpeed = Math.max( 20, Math.min( this.burnSpeed, 200 ) ); this.burnSpeed = Math.max( MIN_BURN_SPEED, Math.min( this.burnSpeed, MAX_BURN_SPEED ) );
return TickRateModulation.SLOWER; return TickRateModulation.SLOWER;
} }
} }
private void eatFuel() private void eatFuel()
{ {
ItemStack is = this.getStackInSlot( 0 ); final ItemStack is = this.getStackInSlot( FUEL_SLOT_INDEX );
if( is != null ) if( is != null )
{ {
int newBurnTime = TileEntityFurnace.getItemBurnTime( is ); final int newBurnTime = TileEntityFurnace.getItemBurnTime( is );
if( newBurnTime > 0 && is.stackSize > 0 ) if( newBurnTime > 0 && is.stackSize > 0 )
{ {
this.burnTime += newBurnTime; this.burnTime += newBurnTime;
@ -252,7 +254,7 @@ public class TileVibrationChamber extends AENetworkInvTile implements IGridTicka
{ {
ItemStack container = null; ItemStack container = null;
if( is.getItem().hasContainerItem( is ) ) if( is.getItem() != null && is.getItem().hasContainerItem( is ) )
{ {
container = is.getItem().getContainerItem( is ); container = is.getItem().getContainerItem( is );
} }
@ -263,6 +265,8 @@ public class TileVibrationChamber extends AENetworkInvTile implements IGridTicka
{ {
this.setInventorySlotContents( 0, is ); this.setInventorySlotContents( 0, is );
} }
this.markDirty();
} }
} }
@ -278,10 +282,16 @@ public class TileVibrationChamber extends AENetworkInvTile implements IGridTicka
} }
} }
// state change
if( ( !this.isOn && this.burnTime > 0 ) || ( this.isOn && this.burnTime <= 0 ) ) if( ( !this.isOn && this.burnTime > 0 ) || ( this.isOn && this.burnTime <= 0 ) )
{ {
this.isOn = this.burnTime > 0; this.isOn = this.burnTime > 0;
this.markForUpdate(); this.markForUpdate();
if ( this.hasWorldObj() )
{
Platform.notifyBlocksOfNeighbors( this.worldObj, this.xCoord, this.yCoord, this.zCoord );
}
} }
} }
} }

View file

@ -30,7 +30,6 @@ import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB; import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.Vec3; import net.minecraft.util.Vec3;
import net.minecraft.world.World; import net.minecraft.world.World;
@ -162,12 +161,6 @@ public class TileCableBus extends AEBaseTile implements AEMultiTile, ICustomColl
return this.cb.getCableConnectionType( side ); return this.cb.getCableConnectionType( side );
} }
@Override
public TileEntity getTile()
{
return this;
}
@Override @Override
public void onChunkUnload() public void onChunkUnload()
{ {