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>.
|
|
|
|
*/
|
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
package appeng.tile;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
import java.lang.ref.WeakReference;
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.EnumMap;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2015-05-01 09:49:00 +02:00
|
|
|
import java.util.Map;
|
2015-05-09 13:06:09 +02:00
|
|
|
|
2015-05-01 09:49:00 +02:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
import javax.annotation.Nullable;
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-12-29 21:59:05 +01:00
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
import io.netty.buffer.Unpooled;
|
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.inventory.IInventory;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.network.NetworkManager;
|
|
|
|
import net.minecraft.network.Packet;
|
|
|
|
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
2014-12-29 21:59:05 +01:00
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
import appeng.api.implementations.tiles.ISegmentedInventory;
|
|
|
|
import appeng.api.util.ICommonTile;
|
|
|
|
import appeng.api.util.IConfigManager;
|
|
|
|
import appeng.api.util.IConfigurableObject;
|
|
|
|
import appeng.api.util.IOrientable;
|
|
|
|
import appeng.core.AELog;
|
2015-07-10 00:07:45 +02:00
|
|
|
import appeng.core.features.IStackSrc;
|
2014-09-24 02:26:27 +02:00
|
|
|
import appeng.helpers.ICustomNameObject;
|
|
|
|
import appeng.helpers.IPriorityHost;
|
|
|
|
import appeng.tile.events.AETileEventHandler;
|
|
|
|
import appeng.tile.events.TileEventType;
|
|
|
|
import appeng.tile.inventory.AppEngInternalAEInventory;
|
|
|
|
import appeng.util.Platform;
|
|
|
|
import appeng.util.SettingsFrom;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
public class AEBaseTile extends TileEntity implements IOrientable, ICommonTile, ICustomNameObject
|
|
|
|
{
|
|
|
|
|
2015-10-08 15:42:42 +02:00
|
|
|
private static final ThreadLocal<WeakReference<AEBaseTile>> DROP_NO_ITEMS = new ThreadLocal<WeakReference<AEBaseTile>>();
|
2015-05-01 09:49:00 +02:00
|
|
|
private static final Map<Class<? extends AEBaseTile>, Map<TileEventType, List<AETileEventHandler>>> HANDLERS = new HashMap<Class<? extends AEBaseTile>, Map<TileEventType, List<AETileEventHandler>>>();
|
2015-07-10 00:07:45 +02:00
|
|
|
private static final Map<Class<? extends TileEntity>, IStackSrc> ITEM_STACKS = new HashMap<Class<? extends TileEntity>, IStackSrc>();
|
2015-05-01 09:49:00 +02:00
|
|
|
private int renderFragment = 0;
|
|
|
|
@Nullable
|
2015-10-08 15:42:42 +02:00
|
|
|
private String customName;
|
2014-09-24 02:26:27 +02:00
|
|
|
private ForgeDirection forward = ForgeDirection.UNKNOWN;
|
|
|
|
private ForgeDirection up = ForgeDirection.UNKNOWN;
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
public static void registerTileItem( final Class<? extends TileEntity> c, final IStackSrc wat )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
ITEM_STACKS.put( c, wat );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean dropItems()
|
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final WeakReference<AEBaseTile> what = DROP_NO_ITEMS.get();
|
2014-09-24 02:26:27 +02:00
|
|
|
return what == null || what.get() != this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean notLoaded()
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
return !this.worldObj.blockExists( this.xCoord, this.yCoord, this.zCoord );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2015-05-01 09:49:00 +02:00
|
|
|
@Nonnull
|
2014-09-24 02:26:27 +02:00
|
|
|
public TileEntity getTile()
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2015-05-01 09:49:00 +02:00
|
|
|
@Nullable
|
2015-09-25 23:10:56 +02:00
|
|
|
protected ItemStack getItemFromTile( final Object obj )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final IStackSrc src = ITEM_STACKS.get( obj.getClass() );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( src == null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-09-24 02:26:27 +02:00
|
|
|
return null;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
return src.stack( 1 );
|
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
/**
|
|
|
|
* for dormant chunk cache.
|
|
|
|
*/
|
|
|
|
public void onChunkLoad()
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.isInvalid() )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
this.validate();
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-04-03 08:54:31 +02:00
|
|
|
// NOTE: WAS FINAL, changed for Immibis
|
2015-09-25 23:10:56 +02:00
|
|
|
public final void readFromNBT( final NBTTagCompound data )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
super.readFromNBT( data );
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( data.hasKey( "customName" ) )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
this.customName = data.getString( "customName" );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
else
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
this.customName = null;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
if( this.canBeRotated() )
|
|
|
|
{
|
|
|
|
this.forward = ForgeDirection.valueOf( data.getString( "orientation_forward" ) );
|
|
|
|
this.up = ForgeDirection.valueOf( data.getString( "orientation_up" ) );
|
|
|
|
}
|
|
|
|
}
|
2015-09-25 23:10:56 +02:00
|
|
|
catch( final IllegalArgumentException ignored )
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
for( final AETileEventHandler h : this.getHandlerListFor( TileEventType.WORLD_NBT_READ ) )
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
|
|
|
h.readFromNBT( this, data );
|
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
// NOTE: WAS FINAL, changed for Immibis
|
2015-09-25 23:10:56 +02:00
|
|
|
public final void writeToNBT( final NBTTagCompound data )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
super.writeToNBT( data );
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.canBeRotated() )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
data.setString( "orientation_forward", this.forward.name() );
|
|
|
|
data.setString( "orientation_up", this.up.name() );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.customName != null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
data.setString( "customName", this.customName );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
for( final AETileEventHandler h : this.getHandlerListFor( TileEventType.WORLD_NBT_WRITE ) )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-09-24 02:26:27 +02:00
|
|
|
h.writeToNBT( this, data );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-04-06 00:35:42 +02:00
|
|
|
public final void updateEntity()
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
for( final AETileEventHandler h : this.getHandlerListFor( TileEventType.TICK ) )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-05-01 09:49:00 +02:00
|
|
|
h.tick( this );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
@Override
|
|
|
|
public Packet getDescriptionPacket()
|
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final NBTTagCompound data = new NBTTagCompound();
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
final ByteBuf stream = Unpooled.buffer();
|
2014-09-24 02:26:27 +02:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
this.writeToStream( stream );
|
|
|
|
if( stream.readableBytes() == 0 )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return null;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
2015-09-25 23:10:56 +02:00
|
|
|
catch( final Throwable t )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-11-22 15:54:29 +01:00
|
|
|
AELog.debug( t );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
stream.capacity( stream.readableBytes() );
|
|
|
|
data.setByteArray( "X", stream.array() );
|
|
|
|
return new S35PacketUpdateTileEntity( this.xCoord, this.yCoord, this.zCoord, 64, data );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
@Override
|
2015-04-06 00:35:42 +02:00
|
|
|
public final boolean canUpdate()
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return this.hasHandlerFor( TileEventType.TICK );
|
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
private boolean hasHandlerFor( final TileEventType type )
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final List<AETileEventHandler> list = this.getHandlerListFor( type );
|
2015-05-01 09:49:00 +02:00
|
|
|
|
|
|
|
return !list.isEmpty();
|
2015-04-03 08:54:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public void onDataPacket( final NetworkManager net, final S35PacketUpdateTileEntity pkt )
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
|
|
|
// / pkt.actionType
|
|
|
|
if( pkt.func_148853_f() == 64 )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final ByteBuf stream = Unpooled.copiedBuffer( pkt.func_148857_g().getByteArray( "X" ) );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.readFromStream( stream ) )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
this.markForUpdate();
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
@Override
|
|
|
|
public void onChunkUnload()
|
|
|
|
{
|
|
|
|
if( !this.isInvalid() )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
this.invalidate();
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
}
|
|
|
|
|
2015-10-08 15:42:42 +02:00
|
|
|
private final boolean readFromStream( final ByteBuf data )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
boolean output = false;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.canBeRotated() )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final ForgeDirection old_Forward = this.forward;
|
|
|
|
final ForgeDirection old_Up = this.up;
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
final byte orientation = data.readByte();
|
2014-12-29 15:13:47 +01:00
|
|
|
this.forward = ForgeDirection.getOrientation( orientation & 0x7 );
|
|
|
|
this.up = ForgeDirection.getOrientation( orientation >> 3 );
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-01-01 21:16:04 +01:00
|
|
|
output = this.forward != old_Forward || this.up != old_Up;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
this.renderFragment = 100;
|
2015-09-25 23:10:56 +02:00
|
|
|
for( final AETileEventHandler h : this.getHandlerListFor( TileEventType.NETWORK_READ ) )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( h.readFromStream( this, data ) )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-09-24 02:26:27 +02:00
|
|
|
output = true;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( ( this.renderFragment & 1 ) == 1 )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-09-24 02:26:27 +02:00
|
|
|
output = true;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-12-29 15:13:47 +01:00
|
|
|
this.renderFragment = 0;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
2015-09-25 23:10:56 +02:00
|
|
|
catch( final Throwable t )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-11-22 15:54:29 +01:00
|
|
|
AELog.debug( t );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
public void markForUpdate()
|
|
|
|
{
|
|
|
|
if( this.renderFragment > 0 )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
this.renderFragment |= 1;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// TODO: Optimize Network Load
|
|
|
|
if( this.worldObj != null )
|
|
|
|
{
|
|
|
|
AELog.blockUpdate( this.xCoord, this.yCoord, this.zCoord, this );
|
|
|
|
this.worldObj.markBlockForUpdate( this.xCoord, this.yCoord, this.zCoord );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-08 15:42:42 +02:00
|
|
|
private final void writeToStream( final ByteBuf data )
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if( this.canBeRotated() )
|
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final byte orientation = (byte) ( ( this.up.ordinal() << 3 ) | this.forward.ordinal() );
|
2015-04-03 08:54:31 +02:00
|
|
|
data.writeByte( orientation );
|
|
|
|
}
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
for( final AETileEventHandler h : this.getHandlerListFor( TileEventType.NETWORK_WRITE ) )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
h.writeToStream( this, data );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
}
|
2015-09-25 23:10:56 +02:00
|
|
|
catch( final Throwable t )
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
2015-11-22 15:54:29 +01:00
|
|
|
AELog.debug( t );
|
2015-04-03 08:54:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
/**
|
|
|
|
* By default all blocks can have orientation, this handles saving, and loading, as well as synchronization.
|
2015-02-03 12:04:13 +01:00
|
|
|
*
|
2014-09-27 23:17:47 +02:00
|
|
|
* @return true if tile can be rotated
|
2014-09-24 02:26:27 +02:00
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public boolean canBeRotated()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-01 09:49:00 +02:00
|
|
|
@Nonnull
|
2015-09-25 23:10:56 +02:00
|
|
|
private List<AETileEventHandler> getHandlerListFor( final TileEventType type )
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
2015-05-01 09:49:00 +02:00
|
|
|
final Map<TileEventType, List<AETileEventHandler>> eventToHandlers = this.getEventToHandlers();
|
|
|
|
final List<AETileEventHandler> handlers = this.getHandlers( eventToHandlers, type );
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2015-05-01 09:49:00 +02:00
|
|
|
return handlers;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
private Map<TileEventType, List<AETileEventHandler>> getEventToHandlers()
|
|
|
|
{
|
|
|
|
final Class<? extends AEBaseTile> clazz = this.getClass();
|
|
|
|
final Map<TileEventType, List<AETileEventHandler>> storedHandlers = HANDLERS.get( clazz );
|
|
|
|
|
2015-08-06 18:49:57 +02:00
|
|
|
if( storedHandlers == null )
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
2015-05-01 09:49:00 +02:00
|
|
|
final Map<TileEventType, List<AETileEventHandler>> newStoredHandlers = new EnumMap<TileEventType, List<AETileEventHandler>>( TileEventType.class );
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2015-05-01 09:49:00 +02:00
|
|
|
HANDLERS.put( clazz, newStoredHandlers );
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
for( final Method method : clazz.getMethods() )
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final TileEvent event = method.getAnnotation( TileEvent.class );
|
2015-05-01 09:49:00 +02:00
|
|
|
if( event != null )
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
2015-05-01 09:49:00 +02:00
|
|
|
this.addHandler( newStoredHandlers, event.value(), method );
|
2015-04-03 08:54:31 +02:00
|
|
|
}
|
|
|
|
}
|
2015-05-01 09:49:00 +02:00
|
|
|
|
|
|
|
return newStoredHandlers;
|
2015-04-03 08:54:31 +02:00
|
|
|
}
|
2015-05-01 09:49:00 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return storedHandlers;
|
|
|
|
}
|
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2015-05-01 09:49:00 +02:00
|
|
|
@Nonnull
|
2015-09-25 23:10:56 +02:00
|
|
|
private List<AETileEventHandler> getHandlers( final Map<TileEventType, List<AETileEventHandler>> eventToHandlers, final TileEventType event )
|
2015-08-06 18:49:57 +02:00
|
|
|
{
|
2015-05-01 09:49:00 +02:00
|
|
|
final List<AETileEventHandler> oldHandlers = eventToHandlers.get( event );
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2015-05-01 09:49:00 +02:00
|
|
|
if( oldHandlers == null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-05-01 09:49:00 +02:00
|
|
|
final List<AETileEventHandler> newHandlers = new LinkedList<AETileEventHandler>();
|
|
|
|
eventToHandlers.put( event, newHandlers );
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2015-05-01 09:49:00 +02:00
|
|
|
return newHandlers;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return oldHandlers;
|
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
}
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
private void addHandler( final Map<TileEventType, List<AETileEventHandler>> handlerSet, final TileEventType value, final Method m )
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
|
|
|
List<AETileEventHandler> list = handlerSet.get( value );
|
|
|
|
|
|
|
|
if( list == null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-05-01 09:49:00 +02:00
|
|
|
list = new ArrayList<AETileEventHandler>();
|
|
|
|
handlerSet.put( value, list );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2015-05-01 09:49:00 +02:00
|
|
|
list.add( new AETileEventHandler( m ) );
|
2015-04-03 08:54:31 +02:00
|
|
|
}
|
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
@Override
|
|
|
|
public ForgeDirection getForward()
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.forward;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ForgeDirection getUp()
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.up;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public void setOrientation( final ForgeDirection inForward, final ForgeDirection inUp )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.forward = inForward;
|
|
|
|
this.up = inUp;
|
|
|
|
this.markForUpdate();
|
|
|
|
Platform.notifyBlocksOfNeighbors( this.worldObj, this.xCoord, this.yCoord, this.zCoord );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
public void onPlacement( final ItemStack stack, final EntityPlayer player, final int side )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( stack.hasTagCompound() )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.uploadSettings( SettingsFrom.DISMANTLE_ITEM, stack.getTagCompound() );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
/**
|
|
|
|
* depending on the from, different settings will be accepted, don't call this with null
|
|
|
|
*
|
2015-08-06 18:49:57 +02:00
|
|
|
* @param from source of settings
|
2015-04-03 08:54:31 +02:00
|
|
|
* @param compound compound of source
|
|
|
|
*/
|
2015-09-25 23:10:56 +02:00
|
|
|
public void uploadSettings( final SettingsFrom from, final NBTTagCompound compound )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( compound != null && this instanceof IConfigurableObject )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final IConfigManager cm = ( (IConfigurableObject) this ).getConfigManager();
|
2015-04-03 08:54:31 +02:00
|
|
|
if( cm != null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
cm.readFromNBT( compound );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this instanceof IPriorityHost )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final IPriorityHost pHost = (IPriorityHost) this;
|
2015-04-03 08:54:31 +02:00
|
|
|
pHost.setPriority( compound.getInteger( "priority" ) );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this instanceof ISegmentedInventory )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final IInventory inv = ( (ISegmentedInventory) this ).getInventoryByName( "config" );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( inv instanceof AppEngInternalAEInventory )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final AppEngInternalAEInventory target = (AppEngInternalAEInventory) inv;
|
|
|
|
final AppEngInternalAEInventory tmp = new AppEngInternalAEInventory( null, target.getSizeInventory() );
|
2015-04-03 08:54:31 +02:00
|
|
|
tmp.readFromNBT( compound, "config" );
|
|
|
|
for( int x = 0; x < tmp.getSizeInventory(); x++ )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
target.setInventorySlotContents( x, tmp.getStackInSlot( x ) );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns the contents of the tile entity, into the world, defaults to dropping everything in the inventory.
|
2015-02-03 12:04:13 +01:00
|
|
|
*
|
2015-08-06 18:49:57 +02:00
|
|
|
* @param w world
|
|
|
|
* @param x x pos of tile entity
|
|
|
|
* @param y y pos of tile entity
|
|
|
|
* @param z z pos of tile entity
|
2014-09-27 23:17:47 +02:00
|
|
|
* @param drops drops of tile entity
|
2014-09-24 02:26:27 +02:00
|
|
|
*/
|
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public void getDrops( final World w, final int x, final int y, final int z, final List<ItemStack> drops )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this instanceof IInventory )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final IInventory inv = (IInventory) this;
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
for( int l = 0; l < inv.getSizeInventory(); l++ )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final ItemStack is = inv.getStackInSlot( l );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( is != null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-09-24 02:26:27 +02:00
|
|
|
drops.add( is );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
public void getNoDrops( final World w, final int x, final int y, final int z, final List<ItemStack> drops )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onReady()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* null means nothing to store...
|
2015-02-03 12:04:13 +01:00
|
|
|
*
|
2014-09-27 23:17:47 +02:00
|
|
|
* @param from source of settings
|
2015-04-03 08:54:31 +02:00
|
|
|
*
|
2014-09-27 23:17:47 +02:00
|
|
|
* @return compound of source
|
2014-09-24 02:26:27 +02:00
|
|
|
*/
|
2015-09-25 23:10:56 +02:00
|
|
|
public NBTTagCompound downloadSettings( final SettingsFrom from )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final NBTTagCompound output = new NBTTagCompound();
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.hasCustomName() )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final NBTTagCompound dsp = new NBTTagCompound();
|
2014-12-29 15:13:47 +01:00
|
|
|
dsp.setString( "Name", this.getCustomName() );
|
2014-09-24 02:26:27 +02:00
|
|
|
output.setTag( "display", dsp );
|
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this instanceof IConfigurableObject )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final IConfigManager cm = ( (IConfigurableObject) this ).getConfigManager();
|
2015-04-03 08:54:31 +02:00
|
|
|
if( cm != null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-09-24 02:26:27 +02:00
|
|
|
cm.writeToNBT( output );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this instanceof IPriorityHost )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final IPriorityHost pHost = (IPriorityHost) this;
|
2014-09-24 02:26:27 +02:00
|
|
|
output.setInteger( "priority", pHost.getPriority() );
|
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this instanceof ISegmentedInventory )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final IInventory inv = ( (ISegmentedInventory) this ).getInventoryByName( "config" );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( inv instanceof AppEngInternalAEInventory )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
( (AppEngInternalAEInventory) inv ).writeToNBT( output, "config" );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return output.hasNoTags() ? null : output;
|
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
@Override
|
|
|
|
public String getCustomName()
|
|
|
|
{
|
|
|
|
return this.hasCustomName() ? this.customName : this.getClass().getSimpleName();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasCustomName()
|
|
|
|
{
|
|
|
|
return this.customName != null && this.customName.length() > 0;
|
|
|
|
}
|
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
public void securityBreak()
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.worldObj.func_147480_a( this.xCoord, this.yCoord, this.zCoord, true );
|
|
|
|
this.disableDrops();
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
public void disableDrops()
|
|
|
|
{
|
|
|
|
DROP_NO_ITEMS.set( new WeakReference<AEBaseTile>( this ) );
|
|
|
|
}
|
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
public void saveChanges()
|
|
|
|
{
|
|
|
|
super.markDirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean requiresTESR()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
public void setName( final String name )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
this.customName = name;
|
|
|
|
}
|
|
|
|
}
|