1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-24 06:03:28 +00:00

Replace WorldUtil methods with builtin alternatives

- Replace most 0 <= y < world.getHeight() checks with
  world.isBlockValid.
 - Remove several "in bounds" checks, as they'll be handled by later
   calls.
This commit is contained in:
SquidDev 2018-12-26 10:28:32 +00:00
parent a1c4a9fb58
commit 4d5c52bc63
14 changed files with 121 additions and 206 deletions

View File

@ -61,7 +61,6 @@
import dan200.computercraft.shared.util.CreativeTabMain;
import dan200.computercraft.shared.util.IDAssigner;
import dan200.computercraft.shared.util.InventoryUtil;
import dan200.computercraft.shared.util.WorldUtil;
import dan200.computercraft.shared.wired.CapabilityWiredElement;
import dan200.computercraft.shared.wired.WiredNode;
import io.netty.buffer.Unpooled;
@ -778,20 +777,12 @@ public static IWiredElement getWiredElementAt( IBlockAccess world, BlockPos pos,
public static int getDefaultBundledRedstoneOutput( World world, BlockPos pos, EnumFacing side )
{
if( WorldUtil.isBlockInWorld( world, pos ) )
{
return DefaultBundledRedstoneProvider.getDefaultBundledRedstoneOutput( world, pos, side );
}
return -1;
return world.isValid( pos ) ? DefaultBundledRedstoneProvider.getDefaultBundledRedstoneOutput( world, pos, side ) : -1;
}
public static int getBundledRedstoneOutput( World world, BlockPos pos, EnumFacing side )
{
int y = pos.getY();
if( y < 0 || y >= world.getHeight() )
{
return -1;
}
if( !world.isValid( pos ) ) return -1;
// Try the handlers in order:
int combinedSignal = -1;

View File

@ -12,7 +12,6 @@
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.shared.computer.blocks.TileCommandComputer;
import dan200.computercraft.shared.util.WorldUtil;
import net.minecraft.block.Block;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.state.IBlockState;
@ -215,7 +214,7 @@ public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull O
Math.max( miny, maxy ),
Math.max( minz, maxz )
);
if( !WorldUtil.isBlockInWorld( world, min ) || !WorldUtil.isBlockInWorld( world, max ) )
if( !world.isValid( min ) || !world.isValid( max ) )
{
throw new LuaException( "Co-ordinates out or range" );
}
@ -250,7 +249,7 @@ public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull O
// Get the details of the block
World world = m_computer.getWorld();
BlockPos position = new BlockPos( x, y, z );
if( WorldUtil.isBlockInWorld( world, position ) )
if( world.isValid( position ) )
{
return new Object[] { getBlockInfo( world, position ) };
}

View File

@ -326,7 +326,7 @@ private void updateSideInput( ServerComputer computer, EnumFacing dir, BlockPos
int localDir = remapLocalSide( DirectionUtil.toLocal( this, dir ) );
if( !isRedstoneBlockedOnSide( localDir ) )
{
computer.setRedstoneInput( localDir, RedstoneUtil.getRedstoneOutput( getWorld(), offset, offsetSide ) );
computer.setRedstoneInput( localDir, getWorld().getRedstonePower( offset, offsetSide ) );
computer.setBundledRedstoneInput( localDir, RedstoneUtil.getBundledRedstoneOutput( getWorld(), offset, offsetSide ) );
}
if( !isPeripheralBlockedOnSide( localDir ) )

View File

@ -420,29 +420,21 @@ public int getYIndex()
private TileMonitor getSimilarMonitorAt( BlockPos pos )
{
if( pos.equals( getPos() ) )
{
return this;
}
if( pos.equals( getPos() ) ) return this;
int y = pos.getY();
World world = getWorld();
if( world != null && y >= 0 && y < world.getHeight() )
if( world != null && world.isBlockLoaded( pos ) )
{
if( world.isBlockLoaded( pos ) )
TileEntity tile = world.getTileEntity( pos );
if( tile != null && tile instanceof TileMonitor )
{
TileEntity tile = world.getTileEntity( pos );
if( tile != null && tile instanceof TileMonitor )
TileMonitor monitor = (TileMonitor) tile;
if( monitor.getDir() == getDir() && monitor.m_advanced == m_advanced &&
!monitor.m_destroyed && !monitor.m_ignoreMe )
{
TileMonitor monitor = (TileMonitor) tile;
if( monitor.getDir() == getDir() && monitor.m_advanced == m_advanced &&
!monitor.m_destroyed && !monitor.m_ignoreMe )
{
return monitor;
}
return monitor;
}
}
}
return null;
}

View File

@ -9,7 +9,6 @@
import dan200.computercraft.api.turtle.ITurtleAccess;
import dan200.computercraft.api.turtle.ITurtleCommand;
import dan200.computercraft.api.turtle.TurtleCommandResult;
import dan200.computercraft.shared.util.WorldUtil;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.item.Item;
@ -48,62 +47,59 @@ public TurtleCommandResult execute( @Nonnull ITurtleAccess turtle )
BlockPos newPosition = oldPosition.offset( direction );
ItemStack lookAtStack = ItemStack.EMPTY;
if( WorldUtil.isBlockInWorld( world, newPosition ) )
if( !world.isAirBlock( newPosition ) )
{
if( !world.isAirBlock( newPosition ) )
IBlockState lookAtState = world.getBlockState( newPosition );
Block lookAtBlock = lookAtState.getBlock();
if( !lookAtBlock.isAir( lookAtState, world, newPosition ) )
{
IBlockState lookAtState = world.getBlockState( newPosition );
Block lookAtBlock = lookAtState.getBlock();
if( !lookAtBlock.isAir( lookAtState, world, newPosition ) )
// Try createStackedBlock first
if( !lookAtBlock.hasTileEntity( lookAtState ) )
{
// Try createStackedBlock first
if( !lookAtBlock.hasTileEntity( lookAtState ) )
try
{
try
{
Method method = ReflectionHelper.findMethod(
Block.class,
"func_180643_i", "getSilkTouchDrop",
IBlockState.class
);
lookAtStack = (ItemStack) method.invoke( lookAtBlock, lookAtState );
}
catch( Exception e )
{
}
Method method = ReflectionHelper.findMethod(
Block.class,
"func_180643_i", "getSilkTouchDrop",
IBlockState.class
);
lookAtStack = (ItemStack) method.invoke( lookAtBlock, lookAtState );
}
// See if the block drops anything with the same ID as itself
// (try 5 times to try and beat random number generators)
for( int i = 0; (i < 5) && lookAtStack.isEmpty(); i++ )
catch( Exception e )
{
NonNullList<ItemStack> drops = NonNullList.create();
lookAtBlock.getDrops( drops, world, newPosition, lookAtState, 0 );
if( drops.size() > 0 )
}
}
// See if the block drops anything with the same ID as itself
// (try 5 times to try and beat random number generators)
for( int i = 0; (i < 5) && lookAtStack.isEmpty(); i++ )
{
NonNullList<ItemStack> drops = NonNullList.create();
lookAtBlock.getDrops( drops, world, newPosition, lookAtState, 0 );
if( drops.size() > 0 )
{
for( ItemStack drop : drops )
{
for( ItemStack drop : drops )
if( drop.getItem() == Item.getItemFromBlock( lookAtBlock ) )
{
if( drop.getItem() == Item.getItemFromBlock( lookAtBlock ) )
{
lookAtStack = drop;
break;
}
lookAtStack = drop;
break;
}
}
}
}
// Last resort: roll our own (which will probably be wrong)
if( lookAtStack.isEmpty() )
// Last resort: roll our own (which will probably be wrong)
if( lookAtStack.isEmpty() )
{
Item item = Item.getItemFromBlock( lookAtBlock );
if( item != null && item.getHasSubtypes() )
{
Item item = Item.getItemFromBlock( lookAtBlock );
if( item != null && item.getHasSubtypes() )
{
lookAtStack = new ItemStack( item, 1, lookAtBlock.getMetaFromState( lookAtState ) );
}
else
{
lookAtStack = new ItemStack( item, 1, 0 );
}
lookAtStack = new ItemStack( item, 1, lookAtBlock.getMetaFromState( lookAtState ) );
}
else
{
lookAtStack = new ItemStack( item, 1, 0 );
}
}
}

View File

@ -37,13 +37,9 @@ public TurtleCommandResult execute( @Nonnull ITurtleAccess turtle )
BlockPos oldPosition = turtle.getPosition();
BlockPos newPosition = oldPosition.offset( direction );
if( WorldUtil.isBlockInWorld( world, newPosition ) )
if( !WorldUtil.isLiquidBlock( world, newPosition ) && !world.isAirBlock( newPosition ) )
{
if( !WorldUtil.isLiquidBlock( world, newPosition ) &&
!world.isAirBlock( newPosition ) )
{
return TurtleCommandResult.success();
}
return TurtleCommandResult.success();
}
return TurtleCommandResult.failure();

View File

@ -11,7 +11,6 @@
import dan200.computercraft.api.turtle.ITurtleCommand;
import dan200.computercraft.api.turtle.TurtleCommandResult;
import dan200.computercraft.api.turtle.event.TurtleBlockEvent;
import dan200.computercraft.shared.util.WorldUtil;
import net.minecraft.block.Block;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.state.IBlockState;
@ -26,7 +25,6 @@
public class TurtleInspectCommand implements ITurtleCommand
{
private static final boolean FAIL_ON_AIR = true;
private final InteractDirection m_direction;
public TurtleInspectCommand( InteractDirection direction )
@ -44,57 +42,44 @@ public TurtleCommandResult execute( @Nonnull ITurtleAccess turtle )
// Check if thing in front is air or not
World world = turtle.getWorld();
BlockPos oldPosition = turtle.getPosition();
BlockPos newPosition = WorldUtil.moveCoords( oldPosition, direction );
BlockPos newPosition = oldPosition.offset( direction );
if( WorldUtil.isBlockInWorld( world, newPosition ) )
IBlockState state = world.getBlockState( newPosition );
if( state.getBlock().isAir( state, world, newPosition ) )
{
IBlockState state = world.getBlockState( newPosition );
if( !FAIL_ON_AIR || !state.getBlock().isAir( state, world, newPosition ) )
return TurtleCommandResult.failure( "No block to inspect" );
}
Block block = state.getBlock();
String name = Block.REGISTRY.getNameForObject( block ).toString();
int metadata = block.getMetaFromState( state );
Map<String, Object> table = new HashMap<>();
table.put( "name", name );
table.put( "metadata", metadata );
Map<Object, Object> stateTable = new HashMap<>();
for( ImmutableMap.Entry<IProperty<?>, ?> entry : state.getActualState( world, newPosition ).getProperties().entrySet() )
{
String propertyName = entry.getKey().getName();
Object value = entry.getValue();
if( value instanceof String || value instanceof Number || value instanceof Boolean )
{
Block block = state.getBlock();
String name = Block.REGISTRY.getNameForObject( block ).toString();
int metadata = block.getMetaFromState( state );
Map<String, Object> table = new HashMap<>();
table.put( "name", name );
table.put( "metadata", metadata );
Map<Object, Object> stateTable = new HashMap<>();
for( ImmutableMap.Entry<IProperty<?>, ?> entry : state.getActualState( world, newPosition ).getProperties().entrySet() )
{
String propertyName = entry.getKey().getName();
Object value = entry.getValue();
if( value instanceof String || value instanceof Number || value instanceof Boolean )
{
stateTable.put( propertyName, value );
}
else
{
stateTable.put( propertyName, value.toString() );
}
}
table.put( "state", stateTable );
// Fire the event, exiting if it is cancelled
TurtlePlayer turtlePlayer = TurtlePlaceCommand.createPlayer( turtle, oldPosition, direction );
TurtleBlockEvent.Inspect event = new TurtleBlockEvent.Inspect( turtle, turtlePlayer, world, newPosition, state, table );
if( MinecraftForge.EVENT_BUS.post( event ) )
{
return TurtleCommandResult.failure( event.getFailureMessage() );
}
return TurtleCommandResult.success( new Object[] { table } );
stateTable.put( propertyName, value );
}
else
{
stateTable.put( propertyName, value.toString() );
}
}
table.put( "state", stateTable );
// Fire the event, exiting if it is cancelled
TurtlePlayer turtlePlayer = TurtlePlaceCommand.createPlayer( turtle, oldPosition, direction );
TurtleBlockEvent.Inspect event = new TurtleBlockEvent.Inspect( turtle, turtlePlayer, world, newPosition, state, table );
if( MinecraftForge.EVENT_BUS.post( event ) ) return TurtleCommandResult.failure( event.getFailureMessage() );
return TurtleCommandResult.success( new Object[] { table } );
if( !FAIL_ON_AIR )
{
Map<Object, Object> table = new HashMap<>();
table.put( "name", "minecraft:air" );
table.put( "metadata", 0 );
table.put( "state", new HashMap<>() );
return TurtleCommandResult.success( new Object[] { table } );
}
return TurtleCommandResult.failure( "No block to inspect" );
}
}

View File

@ -43,7 +43,7 @@ public TurtleCommandResult execute( @Nonnull ITurtleAccess turtle )
// Check if we can move
World oldWorld = turtle.getWorld();
BlockPos oldPosition = turtle.getPosition();
BlockPos newPosition = WorldUtil.moveCoords( oldPosition, direction );
BlockPos newPosition = oldPosition.offset( direction );
TurtlePlayer turtlePlayer = TurtlePlaceCommand.createPlayer( turtle, oldPosition, direction );
TurtleCommandResult canEnterResult = canEnter( turtlePlayer, oldWorld, newPosition );
@ -158,14 +158,15 @@ public TurtleCommandResult execute( @Nonnull ITurtleAccess turtle )
private TurtleCommandResult canEnter( TurtlePlayer turtlePlayer, World world, BlockPos position )
{
if( position.getY() < 0 )
if( world.isOutsideBuildHeight( position ) )
{
return TurtleCommandResult.failure( "Too low to move" );
return TurtleCommandResult.failure( position.getY() < 0 ? "Too low to move" : "Too high to move" );
}
else if( position.getY() > world.getHeight() - 1 )
else if( !world.isValid( position ) )
{
return TurtleCommandResult.failure( "Too high to move" );
return TurtleCommandResult.failure( "Cannot leave the world" );
}
if( ComputerCraft.turtlesObeyBlockProtection )
{
// Check spawn protection

View File

@ -64,10 +64,10 @@ public TurtleCommandResult execute( @Nonnull ITurtleAccess turtle )
// Remember old block
EnumFacing direction = m_direction.toWorldDir( turtle );
World world = turtle.getWorld();
BlockPos coordinates = WorldUtil.moveCoords( turtle.getPosition(), direction );
BlockPos coordinates = turtle.getPosition().offset( direction );
// Create a fake player, and orient it appropriately
BlockPos playerPosition = WorldUtil.moveCoords( turtle.getPosition(), direction );
BlockPos playerPosition = turtle.getPosition().offset( direction );
TurtlePlayer turtlePlayer = createPlayer( turtle, playerPosition, direction );
TurtleBlockEvent.Place place = new TurtleBlockEvent.Place( turtle, turtlePlayer, turtle.getWorld(), coordinates, stack );
@ -76,16 +76,6 @@ public TurtleCommandResult execute( @Nonnull ITurtleAccess turtle )
return TurtleCommandResult.failure( place.getFailureMessage() );
}
IBlockState previousState;
if( WorldUtil.isBlockInWorld( world, coordinates ) )
{
previousState = world.getBlockState( coordinates );
}
else
{
previousState = null;
}
// Do the deploying
String[] errorMessage = new String[1];
ItemStack remainder = deploy( stack, turtle, turtlePlayer, direction, m_extraArguments, errorMessage );
@ -119,7 +109,7 @@ else if( stack.getItem() instanceof ItemBlock )
public static ItemStack deploy( @Nonnull ItemStack stack, ITurtleAccess turtle, EnumFacing direction, Object[] extraArguments, String[] o_errorMessage )
{
// Create a fake player, and orient it appropriately
BlockPos playerPosition = WorldUtil.moveCoords( turtle.getPosition(), direction );
BlockPos playerPosition = turtle.getPosition().offset( direction );
TurtlePlayer turtlePlayer = createPlayer( turtle, playerPosition, direction );
return deploy( stack, turtle, turtlePlayer, direction, extraArguments, o_errorMessage );
@ -136,7 +126,7 @@ public static ItemStack deploy( @Nonnull ItemStack stack, ITurtleAccess turtle,
// Deploy on the block immediately in front
BlockPos position = turtle.getPosition();
BlockPos newPosition = WorldUtil.moveCoords( position, direction );
BlockPos newPosition = position.offset( direction );
remainder = deployOnBlock( stack, turtle, turtlePlayer, newPosition, direction.getOpposite(), extraArguments, true, o_errorMessage );
if( remainder != stack )
{
@ -144,7 +134,7 @@ public static ItemStack deploy( @Nonnull ItemStack stack, ITurtleAccess turtle,
}
// Deploy on the block one block away
remainder = deployOnBlock( stack, turtle, turtlePlayer, WorldUtil.moveCoords( newPosition, direction ), direction.getOpposite(), extraArguments, false, o_errorMessage );
remainder = deployOnBlock( stack, turtle, turtlePlayer, newPosition.offset( direction ), direction.getOpposite(), extraArguments, false, o_errorMessage );
if( remainder != stack )
{
return remainder;
@ -299,7 +289,7 @@ else if( !remainder.isEmpty() )
private static boolean canDeployOnBlock( @Nonnull ItemStack stack, ITurtleAccess turtle, TurtlePlayer player, BlockPos position, EnumFacing side, boolean allowReplaceable, String[] o_errorMessage )
{
World world = turtle.getWorld();
if( WorldUtil.isBlockInWorld( world, position ) &&
if( world.isValid( position ) &&
!world.isAirBlock( position ) &&
!(stack.getItem() instanceof ItemBlock && WorldUtil.isLiquidBlock( world, position )) )
{
@ -312,19 +302,9 @@ private static boolean canDeployOnBlock( @Nonnull ItemStack stack, ITurtleAccess
if( ComputerCraft.turtlesObeyBlockProtection )
{
// Check spawn protection
boolean editable = true;
if( replaceable )
{
editable = ComputerCraft.isBlockEditable( world, position, player );
}
else
{
BlockPos shiftedPos = WorldUtil.moveCoords( position, side );
if( WorldUtil.isBlockInWorld( world, shiftedPos ) )
{
editable = ComputerCraft.isBlockEditable( world, shiftedPos, player );
}
}
boolean editable = replaceable
? ComputerCraft.isBlockEditable( world, position, player )
: ComputerCraft.isBlockEditable( world, position.offset( side ), player );
if( !editable )
{
if( o_errorMessage != null )
@ -356,7 +336,7 @@ private static ItemStack deployOnBlock( @Nonnull ItemStack stack, ITurtleAccess
// Re-orient the fake player
EnumFacing playerDir = side.getOpposite();
BlockPos playerPosition = WorldUtil.moveCoords( position, side );
BlockPos playerPosition = position.offset( side );
orientPlayer( turtle, turtlePlayer, playerPosition, playerDir );
// Calculate where the turtle would hit the block
@ -421,7 +401,7 @@ else if( actionResult == null )
TileEntity tile = world.getTileEntity( position );
if( tile == null || tile == existingTile )
{
tile = world.getTileEntity( WorldUtil.moveCoords( position, side ) );
tile = world.getTileEntity( position.offset( side ) );
}
if( tile instanceof TileEntitySign )
{

View File

@ -12,7 +12,6 @@
import dan200.computercraft.api.turtle.TurtleCommandResult;
import dan200.computercraft.api.turtle.event.TurtleInventoryEvent;
import dan200.computercraft.shared.util.InventoryUtil;
import dan200.computercraft.shared.util.WorldUtil;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.ItemStack;
@ -54,7 +53,7 @@ public TurtleCommandResult execute( @Nonnull ITurtleAccess turtle )
// Get inventory for thing in front
World world = turtle.getWorld();
BlockPos oldPosition = turtle.getPosition();
BlockPos newPosition = WorldUtil.moveCoords( oldPosition, direction );
BlockPos newPosition = oldPosition.offset( direction );
EnumFacing side = direction.getOpposite();
IItemHandler inventory = InventoryUtil.getInventory( world, newPosition, side );

View File

@ -49,25 +49,21 @@ public static ItemStack copyItem( @Nonnull ItemStack a )
public static IItemHandler getInventory( World world, BlockPos pos, EnumFacing side )
{
// Look for tile with inventory
int y = pos.getY();
if( y >= 0 && y < world.getHeight() )
TileEntity tileEntity = world.getTileEntity( pos );
if( tileEntity != null )
{
TileEntity tileEntity = world.getTileEntity( pos );
if( tileEntity != null )
IItemHandler itemHandler = tileEntity.getCapability( CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side );
if( itemHandler != null )
{
IItemHandler itemHandler = tileEntity.getCapability( CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side );
if( itemHandler != null )
{
return itemHandler;
}
else if( side != null && tileEntity instanceof ISidedInventory )
{
return new SidedInvWrapper( (ISidedInventory) tileEntity, side );
}
else if( tileEntity instanceof IInventory )
{
return new InvWrapper( (IInventory) tileEntity );
}
return itemHandler;
}
else if( side != null && tileEntity instanceof ISidedInventory )
{
return new SidedInvWrapper( (ISidedInventory) tileEntity, side );
}
else if( tileEntity instanceof IInventory )
{
return new InvWrapper( (IInventory) tileEntity );
}
}

View File

@ -10,11 +10,6 @@ public class PeripheralUtil
{
public static IPeripheral getPeripheral( World world, BlockPos pos, EnumFacing side )
{
int y = pos.getY();
if( y >= 0 && y < world.getHeight() && !world.isRemote )
{
return ComputerCraft.getPeripheralAt( world, pos, side );
}
return null;
return world.isValid( pos ) && !world.isRemote ? ComputerCraft.getPeripheralAt( world, pos, side ) : null;
}
}

View File

@ -15,11 +15,6 @@
public class RedstoneUtil
{
public static int getRedstoneOutput( World world, BlockPos pos, EnumFacing side )
{
return world.getRedstonePower( pos, side );
}
public static int getBundledRedstoneOutput( World world, BlockPos pos, EnumFacing side )
{
int signal = ComputerCraft.getBundledRedstoneOutput( world, pos, side );

View File

@ -24,19 +24,9 @@
public class WorldUtil
{
public static boolean isBlockInWorld( World world, BlockPos pos )
{
return pos.getY() >= 0 && pos.getY() < world.getHeight();
}
public static boolean isLiquidBlock( World world, BlockPos pos )
{
return isBlockInWorld( world, pos ) && world.getBlockState( pos ).getMaterial().isLiquid();
}
public static BlockPos moveCoords( BlockPos pos, EnumFacing dir )
{
return pos.offset( dir );
return world.getBlockState( pos ).getMaterial().isLiquid();
}
public static Pair<Entity, Vec3d> rayTraceEntities( World world, Vec3d vecStart, Vec3d vecDir, double distance )