1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-01-15 11:45:42 +00:00

Cache direction of modems within the tile

This ensures the world is not accessed from another thread.

Closes #410
This commit is contained in:
SquidDev 2018-01-16 14:23:07 +00:00
parent c3454a195d
commit c9b0894f26
4 changed files with 102 additions and 9 deletions

View File

@ -23,7 +23,7 @@ public abstract class TilePeripheralBase extends TileGeneric
{ {
// Statics // Statics
private EnumFacing m_dir; protected EnumFacing m_dir;
private int m_anim; private int m_anim;
private boolean m_changed; private boolean m_changed;
@ -97,6 +97,11 @@ public abstract class TilePeripheralBase extends TileGeneric
return m_dir; return m_dir;
} }
public EnumFacing getCachedDirection()
{
return m_dir;
}
@Override @Override
public void setDirection( EnumFacing dir ) public void setDirection( EnumFacing dir )
{ {

View File

@ -8,8 +8,8 @@ package dan200.computercraft.shared.peripheral.modem;
import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.peripheral.IPeripheral;
import net.minecraft.block.state.IBlockState; import net.minecraft.block.state.IBlockState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World; import net.minecraft.world.World;
@ -40,7 +40,7 @@ public class TileAdvancedModem extends TileModemBase
@Override @Override
public Vec3d getPosition() public Vec3d getPosition()
{ {
BlockPos pos = m_entity.getPos().offset( m_entity.getDirection() ); BlockPos pos = m_entity.getPos().offset( m_entity.getCachedDirection() );
return new Vec3d( pos.getX(), pos.getY(), pos.getZ() ); return new Vec3d( pos.getX(), pos.getY(), pos.getZ() );
} }
@ -57,9 +57,40 @@ public class TileAdvancedModem extends TileModemBase
} }
// Members // Members
private boolean m_hasDirection = false;
public TileAdvancedModem() public TileAdvancedModem()
{ {
m_dir = EnumFacing.DOWN;
}
@Override
public void onLoad()
{
super.onLoad();
updateDirection();
}
@Override
public void updateContainingBlockInfo()
{
m_hasDirection = false;
}
@Override
public void update()
{
super.update();
updateDirection();
}
private void updateDirection()
{
if( !m_hasDirection )
{
m_hasDirection = true;
m_dir = getDirection();
}
} }
@Override @Override

View File

@ -99,7 +99,7 @@ public class TileCable extends TileModemBase
@Override @Override
public Vec3d getPosition() public Vec3d getPosition()
{ {
EnumFacing direction = m_entity.getDirection(); EnumFacing direction = m_entity.getCachedDirection();
BlockPos pos = m_entity.getPos().offset( direction ); BlockPos pos = m_entity.getPos().offset( direction );
return new Vec3d( pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5 ); return new Vec3d( pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5 );
} }
@ -244,6 +244,8 @@ public class TileCable extends TileModemBase
private boolean m_destroyed; private boolean m_destroyed;
private int m_lastSearchID; private int m_lastSearchID;
private boolean m_hasDirection = false;
public TileCable() public TileCable()
{ {
@ -272,6 +274,28 @@ public class TileCable extends TileModemBase
super.destroy(); super.destroy();
} }
@Override
public void onLoad()
{
super.onLoad();
updateDirection();
}
@Override
public void updateContainingBlockInfo()
{
m_hasDirection = false;
}
private void updateDirection()
{
if( !m_hasDirection )
{
m_hasDirection = true;
m_dir = getDirection();
}
}
@Override @Override
public EnumFacing getDirection() public EnumFacing getDirection()
{ {
@ -551,6 +575,7 @@ public class TileCable extends TileModemBase
public void update() public void update()
{ {
super.update(); super.update();
updateDirection();
if( !getWorld().isRemote ) if( !getWorld().isRemote )
{ {
synchronized( m_peripheralsByName ) synchronized( m_peripheralsByName )

View File

@ -12,8 +12,8 @@ import dan200.computercraft.shared.peripheral.PeripheralType;
import dan200.computercraft.shared.peripheral.common.BlockPeripheral; import dan200.computercraft.shared.peripheral.common.BlockPeripheral;
import dan200.computercraft.shared.peripheral.common.BlockPeripheralVariant; import dan200.computercraft.shared.peripheral.common.BlockPeripheralVariant;
import net.minecraft.block.state.IBlockState; import net.minecraft.block.state.IBlockState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World; import net.minecraft.world.World;
@ -26,7 +26,7 @@ public class TileWirelessModem extends TileModemBase
private static class Peripheral extends WirelessModemPeripheral private static class Peripheral extends WirelessModemPeripheral
{ {
private TileModemBase m_entity; private TileModemBase m_entity;
public Peripheral( TileModemBase entity ) public Peripheral( TileModemBase entity )
{ {
super( false ); super( false );
@ -39,12 +39,12 @@ public class TileWirelessModem extends TileModemBase
{ {
return m_entity.getWorld(); return m_entity.getWorld();
} }
@Nonnull @Nonnull
@Override @Override
public Vec3d getPosition() public Vec3d getPosition()
{ {
BlockPos pos = m_entity.getPos().offset( m_entity.getDirection() ); BlockPos pos = m_entity.getPos().offset( m_entity.getCachedDirection() );
return new Vec3d( pos.getX(), pos.getY(), pos.getZ() ); return new Vec3d( pos.getX(), pos.getY(), pos.getZ() );
} }
@ -53,7 +53,7 @@ public class TileWirelessModem extends TileModemBase
{ {
if( other instanceof Peripheral ) if( other instanceof Peripheral )
{ {
Peripheral otherModem = (Peripheral)other; Peripheral otherModem = (Peripheral) other;
return otherModem.m_entity == m_entity; return otherModem.m_entity == m_entity;
} }
return false; return false;
@ -62,8 +62,40 @@ public class TileWirelessModem extends TileModemBase
// Members // Members
private boolean m_hasDirection = false;
public TileWirelessModem() public TileWirelessModem()
{ {
m_dir = EnumFacing.DOWN;
}
@Override
public void onLoad()
{
super.onLoad();
updateDirection();
}
@Override
public void updateContainingBlockInfo()
{
m_hasDirection = false;
}
@Override
public void update()
{
super.update();
updateDirection();
}
private void updateDirection()
{
if( !m_hasDirection )
{
m_hasDirection = true;
m_dir = getDirection();
}
} }
@Override @Override