diff --git a/src/main/java/dan200/computercraft/ComputerCraft.java b/src/main/java/dan200/computercraft/ComputerCraft.java index 2dc474c22..c79489d7d 100644 --- a/src/main/java/dan200/computercraft/ComputerCraft.java +++ b/src/main/java/dan200/computercraft/ComputerCraft.java @@ -44,7 +44,6 @@ import java.util.EnumSet; import java.util.List; import java.util.concurrent.TimeUnit; -import static dan200.computercraft.shared.ComputerCraftRegistry.ModBlocks; import static dan200.computercraft.shared.ComputerCraftRegistry.init; public final class ComputerCraft implements ModInitializer diff --git a/src/main/java/dan200/computercraft/client/render/CableHighlightRenderer.java b/src/main/java/dan200/computercraft/client/render/CableHighlightRenderer.java index 32965f478..ee0a1e93a 100644 --- a/src/main/java/dan200/computercraft/client/render/CableHighlightRenderer.java +++ b/src/main/java/dan200/computercraft/client/render/CableHighlightRenderer.java @@ -18,6 +18,7 @@ import net.minecraft.client.render.Camera; import net.minecraft.client.render.VertexConsumer; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.entity.Entity; +import net.minecraft.util.hit.HitResult; import net.minecraft.util.math.*; import net.minecraft.util.shape.VoxelShape; @@ -40,8 +41,12 @@ public final class CableHighlightRenderer return false; } + HitResult hitResult = MinecraftClient.getInstance().crosshairTarget; + + Vec3d hitPos = hitResult != null ? hitResult.getPos() : new Vec3d( d, e, f ); + VoxelShape shape = WorldUtil.isVecInside( CableShapes.getModemShape( state ), - new Vec3d( d, e, f ).subtract( pos.getX(), + hitPos.subtract( pos.getX(), pos.getY(), pos.getZ() ) ) ? CableShapes.getModemShape( state ) : CableShapes.getCableShape( state ); diff --git a/src/main/java/dan200/computercraft/fabric/mixin/MixinWorld.java b/src/main/java/dan200/computercraft/fabric/mixin/MixinWorld.java index ebe619a15..be1e1a8ba 100644 --- a/src/main/java/dan200/computercraft/fabric/mixin/MixinWorld.java +++ b/src/main/java/dan200/computercraft/fabric/mixin/MixinWorld.java @@ -7,6 +7,7 @@ package dan200.computercraft.fabric.mixin; import dan200.computercraft.shared.common.TileGeneric; import net.minecraft.block.entity.BlockEntity; +import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -26,12 +27,18 @@ import java.util.Collection; public class MixinWorld { @Shadow - protected boolean iteratingTickingBlockEntities; + private boolean iteratingTickingBlockEntities; + + @Shadow + public boolean isInBuildLimit( BlockPos pos ) + { + return false; + } @Inject( method = "addBlockEntity", at = @At( "HEAD" ) ) public void addBlockEntity( @Nullable BlockEntity entity, CallbackInfo info ) { - if( entity != null && !entity.isRemoved() && entity.getWorld() != null && entity.getWorld().isInBuildLimit( entity.getPos() ) && iteratingTickingBlockEntities ) + if( entity != null && !entity.isRemoved() && this.isInBuildLimit( entity.getPos() ) && iteratingTickingBlockEntities ) { setWorld( entity, this ); } @@ -41,7 +48,7 @@ public class MixinWorld { if( entity.getWorld() != world && entity instanceof TileGeneric ) { - entity.setWorld( (World) world ); //TODO why? + entity.setWorld( (World) world ); } } diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/BlockCable.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/BlockCable.java index 6388751e5..8cb28b302 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/BlockCable.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/BlockCable.java @@ -10,25 +10,26 @@ import com.google.common.collect.ImmutableMap; import dan200.computercraft.api.ComputerCraftAPI; import dan200.computercraft.shared.ComputerCraftRegistry; import dan200.computercraft.shared.common.BlockGeneric; +import dan200.computercraft.shared.util.WorldUtil; import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.block.ShapeContext; import net.minecraft.block.Waterloggable; import net.minecraft.block.entity.BlockEntity; import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.fluid.FluidState; import net.minecraft.item.ItemPlacementContext; import net.minecraft.item.ItemStack; import net.minecraft.state.StateManager; import net.minecraft.state.property.BooleanProperty; import net.minecraft.state.property.EnumProperty; +import net.minecraft.util.hit.BlockHitResult; +import net.minecraft.util.hit.HitResult; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; import net.minecraft.util.shape.VoxelShape; -import net.minecraft.world.BlockView; -import net.minecraft.world.World; -import net.minecraft.world.WorldAccess; -import net.minecraft.world.WorldView; +import net.minecraft.world.*; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -122,52 +123,50 @@ public class BlockCable extends BlockGeneric implements Waterloggable return getWaterloggedFluidState( state ); } - // @Override - // public boolean removedByPlayer( BlockState state, World world, BlockPos pos, PlayerEntity player, boolean willHarvest, FluidState fluid ) - // { - // if( state.get( CABLE ) && state.get( MODEM ).getFacing() != null ) - // { - // BlockHitResult hit = world.raycast( new RaycastContext( - // WorldUtil.getRayStart( player ), WorldUtil.getRayEnd( player ), - // RaycastContext.ShapeType.COLLIDER, RaycastContext.FluidHandling.NONE, player - // ) ); - // if( hit.getType() == HitResult.Type.BLOCK ) - // { - // BlockEntity tile = world.getBlockEntity( pos ); - // if( tile instanceof TileCable && tile.hasWorld() ) - // { - // TileCable cable = (TileCable) tile; - // - // ItemStack item; - // BlockState newState; - // - // if( WorldUtil.isVecInside( CableShapes.getModemShape( state ), hit.getPos().subtract( pos.getX(), pos.getY(), pos.getZ() ) ) ) - // { - // newState = state.with( MODEM, CableModemVariant.None ); - // item = new ItemStack( ComputerCraftRegistry.ModItems.WIRED_MODEM.get() ); - // } - // else - // { - // newState = state.with( CABLE, false ); - // item = new ItemStack( ComputerCraftRegistry.ModItems.CABLE.get() ); - // } - // - // world.setBlockState( pos, correctConnections( world, pos, newState ), 3 ); - // - // cable.modemChanged(); - // cable.connectionsChanged(); - // if( !world.isClient && !player.abilities.creativeMode ) - // { - // Block.dropStack( world, pos, item ); - // } - // - // return false; - // } - // } - // } - // - // return super.removedByPlayer( state, world, pos, player, willHarvest, fluid ); - // } + public boolean removedByPlayer( BlockState state, World world, BlockPos pos, PlayerEntity player, boolean willHarvest, FluidState fluid ) + { + if( state.get( CABLE ) && state.get( MODEM ).getFacing() != null ) + { + BlockHitResult hit = world.raycast( new RaycastContext( + WorldUtil.getRayStart( player ), WorldUtil.getRayEnd( player ), + RaycastContext.ShapeType.COLLIDER, RaycastContext.FluidHandling.NONE, player + ) ); + if( hit.getType() == HitResult.Type.BLOCK ) + { + BlockEntity tile = world.getBlockEntity( pos ); + if( tile instanceof TileCable cable && tile.hasWorld() ) + { + + ItemStack item; + BlockState newState; + + if( WorldUtil.isVecInside( CableShapes.getModemShape( state ), hit.getPos().subtract( pos.getX(), pos.getY(), pos.getZ() ) ) ) + { + newState = state.with( MODEM, CableModemVariant.None ); + item = new ItemStack( ComputerCraftRegistry.ModItems.WIRED_MODEM ); + } + else + { + newState = state.with( CABLE, false ); + item = new ItemStack( ComputerCraftRegistry.ModItems.CABLE ); + } + + world.setBlockState( pos, correctConnections( world, pos, newState ), 3 ); + + cable.modemChanged(); + cable.connectionsChanged(); + if( !world.isClient && !player.getAbilities().creativeMode ) + { + Block.dropStack( world, pos, item ); + } + + return false; + } + } + } + + return true; + } // TODO Re-implement, likely will need mixin // @Override @@ -232,9 +231,8 @@ public class BlockCable extends BlockGeneric implements Waterloggable public void onPlaced( World world, @Nonnull BlockPos pos, @Nonnull BlockState state, LivingEntity placer, @Nonnull ItemStack stack ) { BlockEntity tile = world.getBlockEntity( pos ); - if( tile instanceof TileCable ) + if( tile instanceof TileCable cable ) { - TileCable cable = (TileCable) tile; if( cable.hasCable() ) { cable.connectionsChanged(); diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/ItemBlockCable.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/ItemBlockCable.java index 8e6cc47e7..eb86b95f5 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/ItemBlockCable.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/ItemBlockCable.java @@ -56,9 +56,8 @@ public abstract class ItemBlockCable extends BlockItem world.playSound( null, pos, soundType.getPlaceSound(), SoundCategory.BLOCKS, (soundType.getVolume() + 1.0F) / 2.0F, soundType.getPitch() * 0.8F ); BlockEntity tile = world.getBlockEntity( pos ); - if( tile instanceof TileCable ) + if( tile instanceof TileCable cable ) { - TileCable cable = (TileCable) tile; cable.modemChanged(); cable.connectionsChanged(); } diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/TileCable.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/TileCable.java index 9795fa1ef..077486c8e 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/TileCable.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/TileCable.java @@ -46,7 +46,6 @@ public class TileCable extends TileGeneric implements IPeripheralTile private final IWiredNode node = cable.getNode(); private boolean peripheralAccessAllowed; private boolean destroyed = false; - private Direction modemDirection = Direction.NORTH; private final WiredModemPeripheral modem = new WiredModemPeripheral( new ModemState( () -> TickScheduler.schedule( this ) ), cable ) { @Nonnull @@ -60,7 +59,7 @@ public class TileCable extends TileGeneric implements IPeripheralTile @Override public Vec3d getPosition() { - BlockPos pos = getPos().offset( modemDirection ); + BlockPos pos = getPos().offset( getDirection() ); return new Vec3d( pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5 ); } @@ -71,7 +70,6 @@ public class TileCable extends TileGeneric implements IPeripheralTile return TileCable.this; } }; - private boolean hasModemDirection = false; private boolean connectionsFormed = false; public TileCable( BlockEntityType type, BlockPos pos, BlockState state ) @@ -176,8 +174,8 @@ public class TileCable extends TileGeneric implements IPeripheralTile @Nonnull private Direction getDirection() { - refreshDirection(); - return modemDirection == null ? Direction.NORTH : modemDirection; + Direction direction = getMaybeDirection(); + return direction == null ? Direction.NORTH : direction; } public boolean hasModem() @@ -312,8 +310,6 @@ public class TileCable extends TileGeneric implements IPeripheralTile return; } - refreshDirection(); - if( modem.getModemState() .pollChanged() ) { @@ -327,7 +323,7 @@ public class TileCable extends TileGeneric implements IPeripheralTile connectionsChanged(); if( peripheralAccessAllowed ) { - peripheral.attach( world, pos, modemDirection ); + peripheral.attach( world, pos, getDirection() ); updateConnectedPeripherals(); } } @@ -360,20 +356,7 @@ public class TileCable extends TileGeneric implements IPeripheralTile @Nullable private Direction getMaybeDirection() { - refreshDirection(); - return modemDirection; - } - - private void refreshDirection() - { - if( hasModemDirection ) - { - return; - } - - hasModemDirection = true; - modemDirection = getCachedState().get( BlockCable.MODEM ) - .getFacing(); + return getCachedState().get( BlockCable.MODEM ).getFacing(); } @Override @@ -412,7 +395,6 @@ public class TileCable extends TileGeneric implements IPeripheralTile { super.setCachedState( state ); if( state != null ) return; - hasModemDirection = false; if( !world.isClient ) { world.getBlockTickScheduler() diff --git a/src/main/java/dan200/computercraft/shared/proxy/ComputerCraftProxyCommon.java b/src/main/java/dan200/computercraft/shared/proxy/ComputerCraftProxyCommon.java index cf16b9904..cdbdc0c66 100644 --- a/src/main/java/dan200/computercraft/shared/proxy/ComputerCraftProxyCommon.java +++ b/src/main/java/dan200/computercraft/shared/proxy/ComputerCraftProxyCommon.java @@ -25,6 +25,7 @@ import dan200.computercraft.shared.media.items.RecordMedia; import dan200.computercraft.shared.network.NetworkHandler; import dan200.computercraft.shared.peripheral.commandblock.CommandBlockPeripheral; import dan200.computercraft.shared.peripheral.generic.methods.InventoryMethods; +import dan200.computercraft.shared.peripheral.modem.wired.BlockCable; import dan200.computercraft.shared.peripheral.modem.wireless.WirelessNetwork; import dan200.computercraft.shared.turtle.FurnaceRefuelHandler; import dan200.computercraft.shared.turtle.SignInspectHandler; @@ -34,6 +35,7 @@ import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerBlockEntityEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; +import net.fabricmc.fabric.api.event.player.PlayerBlockBreakEvents; import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.CommandBlockBlockEntity; import net.minecraft.item.Item; @@ -123,6 +125,11 @@ public final class ComputerCraftProxyCommon } } ); + PlayerBlockBreakEvents.BEFORE.register( ( world, player, pos, state, blockEntity ) -> { + if ( state.getBlock() instanceof BlockCable blockCable ) return blockCable.removedByPlayer( state, world, pos, player, false, null ); + return true; + } ); + // Config ServerLifecycleEvents.SERVER_STARTING.register( Config::serverStarting ); ServerLifecycleEvents.SERVER_STOPPING.register( Config::serverStopping );