mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-01-05 23:10:30 +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:
parent
a1c4a9fb58
commit
4d5c52bc63
@ -61,7 +61,6 @@ import dan200.computercraft.shared.turtle.upgrades.*;
|
||||
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 class ComputerCraft
|
||||
|
||||
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;
|
||||
|
@ -12,7 +12,6 @@ import dan200.computercraft.api.lua.ILuaAPI;
|
||||
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 class CommandAPI implements ILuaAPI
|
||||
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 class CommandAPI implements ILuaAPI
|
||||
// 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 ) };
|
||||
}
|
||||
|
@ -326,7 +326,7 @@ public abstract class TileComputerBase extends TileGeneric
|
||||
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 ) )
|
||||
|
@ -420,16 +420,10 @@ public class TileMonitor extends TilePeripheralBase
|
||||
|
||||
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.isBlockLoaded( pos ) )
|
||||
if( world != null && world.isBlockLoaded( pos ) )
|
||||
{
|
||||
TileEntity tile = world.getTileEntity( pos );
|
||||
if( tile != null && tile instanceof TileMonitor )
|
||||
@ -442,8 +436,6 @@ public class TileMonitor extends TilePeripheralBase
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -9,7 +9,6 @@ package dan200.computercraft.shared.turtle.core;
|
||||
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,8 +47,6 @@ public class TurtleCompareCommand implements ITurtleCommand
|
||||
BlockPos newPosition = oldPosition.offset( direction );
|
||||
|
||||
ItemStack lookAtStack = ItemStack.EMPTY;
|
||||
if( WorldUtil.isBlockInWorld( world, newPosition ) )
|
||||
{
|
||||
if( !world.isAirBlock( newPosition ) )
|
||||
{
|
||||
IBlockState lookAtState = world.getBlockState( newPosition );
|
||||
@ -107,7 +104,6 @@ public class TurtleCompareCommand implements ITurtleCommand
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Compare them
|
||||
if( selectedStack.isEmpty() && lookAtStack.isEmpty() )
|
||||
|
@ -37,14 +37,10 @@ public class TurtleDetectCommand implements ITurtleCommand
|
||||
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.failure();
|
||||
}
|
||||
|
@ -11,7 +11,6 @@ import dan200.computercraft.api.turtle.ITurtleAccess;
|
||||
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 @@ import java.util.Map;
|
||||
|
||||
public class TurtleInspectCommand implements ITurtleCommand
|
||||
{
|
||||
private static final boolean FAIL_ON_AIR = true;
|
||||
private final InteractDirection m_direction;
|
||||
|
||||
public TurtleInspectCommand( InteractDirection direction )
|
||||
@ -44,13 +42,14 @@ public class TurtleInspectCommand implements ITurtleCommand
|
||||
// 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( !FAIL_ON_AIR || !state.getBlock().isAir( state, world, newPosition ) )
|
||||
if( 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 );
|
||||
@ -78,23 +77,9 @@ public class TurtleInspectCommand implements ITurtleCommand
|
||||
// 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() );
|
||||
}
|
||||
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" );
|
||||
}
|
||||
}
|
||||
|
@ -43,7 +43,7 @@ public class TurtleMoveCommand implements ITurtleCommand
|
||||
// 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 class TurtleMoveCommand implements ITurtleCommand
|
||||
|
||||
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
|
||||
|
@ -64,10 +64,10 @@ public class TurtlePlaceCommand implements ITurtleCommand
|
||||
// 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 class TurtlePlaceCommand implements ITurtleCommand
|
||||
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 @@ public class TurtlePlaceCommand implements ITurtleCommand
|
||||
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 class TurtlePlaceCommand implements ITurtleCommand
|
||||
|
||||
// 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 class TurtlePlaceCommand implements ITurtleCommand
|
||||
}
|
||||
|
||||
// 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 @@ public class TurtlePlaceCommand implements ITurtleCommand
|
||||
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 @@ public class TurtlePlaceCommand implements ITurtleCommand
|
||||
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 @@ public class TurtlePlaceCommand implements ITurtleCommand
|
||||
|
||||
// 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 @@ public class TurtlePlaceCommand implements ITurtleCommand
|
||||
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 )
|
||||
{
|
||||
|
@ -12,7 +12,6 @@ import dan200.computercraft.api.turtle.TurtleAnimation;
|
||||
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 class TurtleSuckCommand implements ITurtleCommand
|
||||
// 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 );
|
||||
|
@ -49,9 +49,6 @@ public class InventoryUtil
|
||||
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 )
|
||||
{
|
||||
@ -69,7 +66,6 @@ public class InventoryUtil
|
||||
return new InvWrapper( (IInventory) tileEntity );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Look for entity with inventory
|
||||
Vec3d vecStart = new Vec3d(
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -15,11 +15,6 @@ import net.minecraft.world.World;
|
||||
|
||||
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 );
|
||||
|
@ -24,19 +24,9 @@ import java.util.List;
|
||||
|
||||
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 )
|
||||
|
Loading…
Reference in New Issue
Block a user