From 230190d4ee52e4f32c949fad43b073f85479c765 Mon Sep 17 00:00:00 2001 From: Walker Fowlkes Date: Sat, 13 Apr 2024 12:57:35 -0700 Subject: [PATCH 1/4] flood fill --- .../net/brokenmoon/redcontrol/RedControl.java | 26 +++++++++++++++++++ .../brokenmoon/redcontrol/util/FloodFill.kt | 25 ++++++++++++++++++ .../tags/blocks/bus_conductive.json | 1 + src/main/resources/redcontrol.mixins.json | 3 ++- 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 src/main/java/net/brokenmoon/redcontrol/util/FloodFill.kt diff --git a/src/main/java/net/brokenmoon/redcontrol/RedControl.java b/src/main/java/net/brokenmoon/redcontrol/RedControl.java index b543673..4fa4686 100644 --- a/src/main/java/net/brokenmoon/redcontrol/RedControl.java +++ b/src/main/java/net/brokenmoon/redcontrol/RedControl.java @@ -1,5 +1,6 @@ package net.brokenmoon.redcontrol; +import net.brokenmoon.redcontrol.api.events.BlockModificationEvents; import net.brokenmoon.redcontrol.blockentities.CpuEntity; import net.brokenmoon.redcontrol.blockentities.DriveEntity; import net.brokenmoon.redcontrol.blocks.CpuBlock; @@ -7,6 +8,7 @@ import net.brokenmoon.redcontrol.blocks.DriveBlock; import net.brokenmoon.redcontrol.blocks.TerminalBlock; import net.brokenmoon.redcontrol.blocks.TerminalEntity; import net.brokenmoon.redcontrol.item.FloppyDisk; +import net.brokenmoon.redcontrol.util.FloodFill; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.item.v1.FabricItemSettings; @@ -16,6 +18,7 @@ import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder; import net.fabricmc.fabric.api.resource.ResourceManagerHelper; import net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener; +import net.minecraft.block.Block; import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntityType; import net.minecraft.item.BlockItem; @@ -23,14 +26,19 @@ import net.minecraft.item.Item; import net.minecraft.item.ItemGroup; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NbtCompound; +import net.minecraft.particle.ParticleTypes; import net.minecraft.registry.Registry; import net.minecraft.registry.Registries; +import net.minecraft.registry.RegistryKeys; +import net.minecraft.registry.tag.TagKey; import net.minecraft.resource.Resource; import net.minecraft.resource.ResourceManager; import net.minecraft.resource.ResourceType; +import net.minecraft.server.world.ServerWorld; import net.minecraft.text.Text; import net.minecraft.util.Identifier; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Vec3d; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -43,8 +51,13 @@ public class RedControl implements ModInitializer { // It is considered best practice to use your mod id as the logger's name. // That way, it's clear which mod wrote info, warnings, and errors. public static final Logger LOGGER = LoggerFactory.getLogger("redcontrol"); + + //A hashmap of files contained in images public static final HashMap images = new HashMap<>(); + //Tags + public static final TagKey TAG_BUSABLE = TagKey.of(RegistryKeys.BLOCK, modloc("bus_conductive")); + //Blocks public static final CpuBlock CPU = new CpuBlock(FabricBlockSettings.create().strength(4.0f)); public static final TerminalBlock TERMINAL = new TerminalBlock(FabricBlockSettings.create().strength(4.0f)); @@ -169,6 +182,19 @@ public class RedControl implements ModInitializer { te.pushKey(key); } }))); + + BlockModificationEvents.ON_CHANGE.register((world,blockPos,oldState,newState) -> { + if (oldState.isIn(TAG_BUSABLE) ^ newState.isIn(TAG_BUSABLE)) { + Vec3d center = blockPos.toCenterPos(); + ((ServerWorld)world).spawnParticles(ParticleTypes.CRIT,center.x,center.y,center.z,30,0.1,0.1,0.1,0.1); + if (newState.isIn(TAG_BUSABLE)) { + //a bus-block was placed + FloodFill.INSTANCE.blockFloodFiller((ServerWorld) world,blockPos); + } else { + //a bus block was removed + } + } + }); } static Identifier modloc(String path) {return new Identifier("redcontrol",path);} diff --git a/src/main/java/net/brokenmoon/redcontrol/util/FloodFill.kt b/src/main/java/net/brokenmoon/redcontrol/util/FloodFill.kt new file mode 100644 index 0000000..a7fe00c --- /dev/null +++ b/src/main/java/net/brokenmoon/redcontrol/util/FloodFill.kt @@ -0,0 +1,25 @@ +package net.brokenmoon.redcontrol.util + +import net.brokenmoon.redcontrol.RedControl +import net.minecraft.particle.ParticleTypes +import net.minecraft.server.world.ServerWorld +import net.minecraft.util.math.BlockPos +import net.minecraft.util.math.Direction + +object FloodFill { + fun blockFloodFiller(world: ServerWorld, seed: BlockPos) { + val checkable = mutableListOf(seed) + val seen = mutableListOf() + while (checkable.isNotEmpty()) { + val pos = checkable.removeFirst() + if (pos in seen) {continue} + if (!world.getBlockState(pos).isIn(RedControl.TAG_BUSABLE)){continue} + val cpos = pos.toCenterPos() + world.spawnParticles(ParticleTypes.CHERRY_LEAVES,cpos.x,cpos.y,cpos.z,20,0.2,0.2,0.2,0.2) + Direction.entries.forEach { + checkable.add(pos.add(it.offsetX,it.offsetY,it.offsetZ)) + } + seen.add(pos) + } + } +} \ No newline at end of file diff --git a/src/main/resources/data/redcontrol/tags/blocks/bus_conductive.json b/src/main/resources/data/redcontrol/tags/blocks/bus_conductive.json index dc0dbf0..ffe184a 100644 --- a/src/main/resources/data/redcontrol/tags/blocks/bus_conductive.json +++ b/src/main/resources/data/redcontrol/tags/blocks/bus_conductive.json @@ -1,3 +1,4 @@ { + "replace": false, "values": ["minecraft:glass"] } \ No newline at end of file diff --git a/src/main/resources/redcontrol.mixins.json b/src/main/resources/redcontrol.mixins.json index 6b561e9..fc14a4e 100644 --- a/src/main/resources/redcontrol.mixins.json +++ b/src/main/resources/redcontrol.mixins.json @@ -7,7 +7,8 @@ "CpuAccessor", "ExampleMixin", "MemoryAccessor", - "RedBusAccessor" + "RedBusAccessor", + "WorldMixin" ], "injectors": { "defaultRequire": 1 From cb753ec3b68247d8b4847505bcc87e0aaa5dcfe4 Mon Sep 17 00:00:00 2001 From: Walker Fowlkes Date: Sat, 13 Apr 2024 14:08:37 -0700 Subject: [PATCH 2/4] flood fill --- .../net/brokenmoon/redcontrol/RedControl.java | 3 +- .../brokenmoon/redcontrol/api/RCWorldBus.java | 3 +- .../redcontrol/blockentities/CpuEntity.java | 9 ++-- .../redcontrol/blocks/CpuBlock.java | 4 +- .../redcontrol/blocks/NetworkCarrier.java | 30 +++++------- .../brokenmoon/redcontrol/blocks/Terminal.kt | 9 ++-- .../brokenmoon/redcontrol/util/FloodFill.kt | 49 +++++++++++++++++-- .../tags/blocks/bus_conductive.json | 7 ++- 8 files changed, 79 insertions(+), 35 deletions(-) diff --git a/src/main/java/net/brokenmoon/redcontrol/RedControl.java b/src/main/java/net/brokenmoon/redcontrol/RedControl.java index 4fa4686..a82dafa 100644 --- a/src/main/java/net/brokenmoon/redcontrol/RedControl.java +++ b/src/main/java/net/brokenmoon/redcontrol/RedControl.java @@ -189,9 +189,10 @@ public class RedControl implements ModInitializer { ((ServerWorld)world).spawnParticles(ParticleTypes.CRIT,center.x,center.y,center.z,30,0.1,0.1,0.1,0.1); if (newState.isIn(TAG_BUSABLE)) { //a bus-block was placed - FloodFill.INSTANCE.blockFloodFiller((ServerWorld) world,blockPos); + FloodFill.INSTANCE.blockFloodFiller((ServerWorld) world, blockPos); } else { //a bus block was removed + FloodFill.INSTANCE.blockBreakFloodFiller((ServerWorld) world, blockPos); } } }); diff --git a/src/main/java/net/brokenmoon/redcontrol/api/RCWorldBus.java b/src/main/java/net/brokenmoon/redcontrol/api/RCWorldBus.java index b1efaf5..4ab6f03 100644 --- a/src/main/java/net/brokenmoon/redcontrol/api/RCWorldBus.java +++ b/src/main/java/net/brokenmoon/redcontrol/api/RCWorldBus.java @@ -2,6 +2,7 @@ package net.brokenmoon.redcontrol.api; import com.simon816.j65el02.device.RedBus; import net.brokenmoon.redcontrol.blocks.NetworkCarrier; +import net.minecraft.server.world.ServerWorld; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; @@ -21,7 +22,7 @@ public class RCWorldBus { } - public void generateBus(World world, BlockPos pos){ + public void generateBus(ServerWorld world, BlockPos pos){ ((NetworkCarrier)(world.getBlockState(pos).getBlock())).generateBus(world, pos); } diff --git a/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java b/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java index 42574c4..f629cc0 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java +++ b/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java @@ -11,6 +11,7 @@ import net.minecraft.nbt.NbtCompound; import net.minecraft.network.listener.ClientPlayPacketListener; import net.minecraft.network.packet.Packet; import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket; +import net.minecraft.server.world.ServerWorld; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import org.jetbrains.annotations.Nullable; @@ -33,11 +34,11 @@ public class CpuEntity extends Peripheral{ super(RedControl.CPU_BLOCK_ENTITY, pos, state, 0); } - public static void tick(World world, BlockPos pos, BlockState state, CpuEntity be) { - if(be.worldBus == null || !be.worldBus.getValid()){ - ((NetworkCarrier)state.getBlock()).generateBus(world, pos); - } + public static void tick(World world, BlockPos pos, BlockState state, CpuEntity beo) { if(!world.isClient) { + if(be.worldBus == null || !be.worldBus.getValid()){ + ((NetworkCarrier)state.getBlock()).generateBus((ServerWorld) world, pos); + } if (be.notTicked) { be.notTicked = false; } diff --git a/src/main/java/net/brokenmoon/redcontrol/blocks/CpuBlock.java b/src/main/java/net/brokenmoon/redcontrol/blocks/CpuBlock.java index 858a54c..3d342cd 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blocks/CpuBlock.java +++ b/src/main/java/net/brokenmoon/redcontrol/blocks/CpuBlock.java @@ -49,11 +49,9 @@ public class CpuBlock extends NetworkCarrier { super.onUse(state,world,pos,player,hand,hit); CpuEntity peripheral = (CpuEntity) world.getBlockEntity(pos); player.sendMessage(Text.literal("Cpu Debug: "), false); - player.sendMessage(Text.literal(" " + Integer.toHexString(peripheral.getBus().hashCode())), false); - player.sendMessage(Text.literal(" " + Integer.toHexString(peripheral.core.cpu.getBus().getRedBus().hashCode())), false); player.sendMessage(Text.literal(" " + Integer.toHexString(peripheral.i)), false); player.sendMessage(Text.literal(" " + peripheral.core.isWaitingOnInterrupt()), false); - player.sendMessage(Text.literal(" " + peripheral.core.cpu.getCpuState().toTraceEvent())); + player.sendMessage(Text.literal(" " + peripheral.core.cpu.getCpuState().toTraceEvent())); } PacketByteBuf byteBuf = PacketByteBufs.create(); byteBuf.writeBlockPos(pos); diff --git a/src/main/java/net/brokenmoon/redcontrol/blocks/NetworkCarrier.java b/src/main/java/net/brokenmoon/redcontrol/blocks/NetworkCarrier.java index 7e0656e..ab753e3 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blocks/NetworkCarrier.java +++ b/src/main/java/net/brokenmoon/redcontrol/blocks/NetworkCarrier.java @@ -3,13 +3,19 @@ package net.brokenmoon.redcontrol.blocks; import com.simon816.j65el02.device.RedBus; import net.brokenmoon.redcontrol.RedControl; import net.brokenmoon.redcontrol.api.RCWorldBus; +import net.brokenmoon.redcontrol.blockentities.CpuEntity; import net.brokenmoon.redcontrol.blockentities.Peripheral; import net.brokenmoon.redcontrol.mixin.RedBusAccessor; +import net.brokenmoon.redcontrol.util.FloodFill; import net.minecraft.block.*; +import net.minecraft.block.entity.BlockEntity; +import net.minecraft.block.entity.BlockEntityTicker; +import net.minecraft.block.entity.BlockEntityType; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemPlacementContext; import net.minecraft.item.ItemStack; +import net.minecraft.server.world.ServerWorld; import net.minecraft.state.StateManager; import net.minecraft.state.property.Properties; import net.minecraft.text.Text; @@ -35,11 +41,12 @@ public abstract class NetworkCarrier extends BlockWithEntity implements BlockEnt @Override public void onPlaced(World world, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack itemStack) { - generateBus(world, pos); + if (!world.isClient) { + generateBus((ServerWorld) world, pos); + } } @Override - @SuppressWarnings("uses or overrides a deprecated API.") public void onStateReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean moved) { if(state.hasBlockEntity() && world.getBlockEntity(pos) instanceof Peripheral){ if(((Peripheral) world.getBlockEntity(pos)).getBus() != null) @@ -57,7 +64,7 @@ public abstract class NetworkCarrier extends BlockWithEntity implements BlockEnt } - public void generateBus(World world, BlockPos pos){ + public void generateBus(ServerWorld world, BlockPos pos){ Block worldBlock = world.getBlockState(pos).getBlock(); if(world.getBlockEntity(pos) instanceof Peripheral){ Peripheral entityBlock = (Peripheral) world.getBlockEntity(pos); @@ -69,20 +76,9 @@ public abstract class NetworkCarrier extends BlockWithEntity implements BlockEnt } - private void floodBus(RCWorldBus bus, World world, BlockPos pos) { - replaceBus(bus, world, pos.north()); - replaceBus(bus, world, pos.south()); - replaceBus(bus, world, pos.east()); - replaceBus(bus, world, pos.west()); - replaceBus(bus, world, pos.up()); - replaceBus(bus, world, pos.down()); - } - - private void replaceBus(RCWorldBus bus, World world, BlockPos pos) { - if(world.getBlockEntity(pos) instanceof Peripheral && ((Peripheral) world.getBlockEntity(pos)).getBus() != bus){ - ((Peripheral) world.getBlockEntity(pos)).setBus(bus); - floodBus(bus, world, pos); - } + private void floodBus(RCWorldBus bus, ServerWorld world, BlockPos pos) { + this.setBus(world,pos,bus); + FloodFill.INSTANCE.blockFloodFiller(world,pos); } @Override diff --git a/src/main/java/net/brokenmoon/redcontrol/blocks/Terminal.kt b/src/main/java/net/brokenmoon/redcontrol/blocks/Terminal.kt index 8615de2..55699ab 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blocks/Terminal.kt +++ b/src/main/java/net/brokenmoon/redcontrol/blocks/Terminal.kt @@ -36,11 +36,10 @@ class TerminalBlock(settings: Settings) : NetworkCarrier(settings) { if (!world.isClient) { if (player.getStackInHand(hand).item === RedControl.SQUEAKY_HAMMER) { super.onUse(state, world, pos, player, hand, hit) - player.sendMessage(Text.literal(java.lang.String.valueOf(monitor.bus.hashCode())), false) - val text: Array = monitor.getText() - for (s in text) { - player.sendMessage(Text.literal(s), false) - } + //val text: Array = monitor.getText() + //for (s in text) { + // player.sendMessage(Text.literal(s), false) + //} } val bbuf = PacketByteBufs.create() bbuf.writeBlockPos(pos) diff --git a/src/main/java/net/brokenmoon/redcontrol/util/FloodFill.kt b/src/main/java/net/brokenmoon/redcontrol/util/FloodFill.kt index a7fe00c..dd4a8ea 100644 --- a/src/main/java/net/brokenmoon/redcontrol/util/FloodFill.kt +++ b/src/main/java/net/brokenmoon/redcontrol/util/FloodFill.kt @@ -1,6 +1,8 @@ package net.brokenmoon.redcontrol.util import net.brokenmoon.redcontrol.RedControl +import net.brokenmoon.redcontrol.api.RCWorldBus +import net.brokenmoon.redcontrol.blocks.NetworkCarrier import net.minecraft.particle.ParticleTypes import net.minecraft.server.world.ServerWorld import net.minecraft.util.math.BlockPos @@ -10,16 +12,57 @@ object FloodFill { fun blockFloodFiller(world: ServerWorld, seed: BlockPos) { val checkable = mutableListOf(seed) val seen = mutableListOf() + var bus: RCWorldBus? = null while (checkable.isNotEmpty()) { val pos = checkable.removeFirst() if (pos in seen) {continue} - if (!world.getBlockState(pos).isIn(RedControl.TAG_BUSABLE)){continue} - val cpos = pos.toCenterPos() - world.spawnParticles(ParticleTypes.CHERRY_LEAVES,cpos.x,cpos.y,cpos.z,20,0.2,0.2,0.2,0.2) + val state = world.getBlockState(pos) + if (!state.isIn(RedControl.TAG_BUSABLE)){continue} + val net = state.block as? NetworkCarrier + if (net != null) { + val cpos = pos.toCenterPos() + world.spawnParticles(ParticleTypes.WAX_ON,cpos.x,cpos.y,cpos.z,20,0.6,0.6,0.6,0.2) + if (bus == null) { + bus = net.getBus(world,pos) + } else { + net.setBus(world,pos,bus) + } + } + //val cpos = pos.toCenterPos() + //world.spawnParticles(ParticleTypes.WAX_ON,cpos.x,cpos.y,cpos.z,20,0.2,0.2,0.2,0.2) Direction.entries.forEach { checkable.add(pos.add(it.offsetX,it.offsetY,it.offsetZ)) } seen.add(pos) } } + fun blockBreakFloodFiller(world: ServerWorld, seed: BlockPos) { + val checkable = mutableListOf() + Direction.entries.forEach { + checkable.add(seed.add(it.offsetX,it.offsetY,it.offsetZ)) + } + val seen = mutableListOf() + while (checkable.isNotEmpty()) { + val pos = checkable.removeFirst() + if (pos in seen) { + continue + } + val state = world.getBlockState(pos) + if (!state.isIn(RedControl.TAG_BUSABLE)) { + continue + } + Direction.entries.forEach { + checkable.add(pos.add(it.offsetX, it.offsetY, it.offsetZ)) + } + seen.add(pos) + val nc = (state.block as? NetworkCarrier) + if (nc != null) { + nc.getBus(world,pos).valid = false + val cpos = pos.toCenterPos() + world.spawnParticles(ParticleTypes.WAX_OFF,cpos.x,cpos.y,cpos.z,20,0.6,0.6,0.6,0.2) + } + //val cpos = pos.toCenterPos() + //world.spawnParticles(ParticleTypes.WAX_OFF, cpos.x, cpos.y, cpos.z, 20, 0.2, 0.2, 0.2, 0.2) + } + } } \ No newline at end of file diff --git a/src/main/resources/data/redcontrol/tags/blocks/bus_conductive.json b/src/main/resources/data/redcontrol/tags/blocks/bus_conductive.json index ffe184a..c92d53b 100644 --- a/src/main/resources/data/redcontrol/tags/blocks/bus_conductive.json +++ b/src/main/resources/data/redcontrol/tags/blocks/bus_conductive.json @@ -1,4 +1,9 @@ { "replace": false, - "values": ["minecraft:glass"] + "values": [ + "minecraft:glass", + "redcontrol:cpu", + "redcontrol:disk_drive", + "redcontrol:monitor" + ] } \ No newline at end of file From 5905b4924bda40f7363f4b61dabc72bc0013c200 Mon Sep 17 00:00:00 2001 From: Walker Fowlkes Date: Sat, 13 Apr 2024 14:08:57 -0700 Subject: [PATCH 3/4] flood fill --- .../java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java b/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java index f629cc0..558f959 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java +++ b/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java @@ -34,7 +34,7 @@ public class CpuEntity extends Peripheral{ super(RedControl.CPU_BLOCK_ENTITY, pos, state, 0); } - public static void tick(World world, BlockPos pos, BlockState state, CpuEntity beo) { + public static void tick(World world, BlockPos pos, BlockState state, CpuEntity be) { if(!world.isClient) { if(be.worldBus == null || !be.worldBus.getValid()){ ((NetworkCarrier)state.getBlock()).generateBus((ServerWorld) world, pos); From 6d7e3b8f392d6f5c0e4518d80f7c202c69e16adb Mon Sep 17 00:00:00 2001 From: Walker Fowlkes Date: Sat, 13 Apr 2024 14:15:43 -0700 Subject: [PATCH 4/4] after alot of jank. I am pleased to present. GLass cables --- .../net/brokenmoon/redcontrol/blockentities/Peripheral.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/net/brokenmoon/redcontrol/blockentities/Peripheral.java b/src/main/java/net/brokenmoon/redcontrol/blockentities/Peripheral.java index b53c86c..4a3786a 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blockentities/Peripheral.java +++ b/src/main/java/net/brokenmoon/redcontrol/blockentities/Peripheral.java @@ -21,6 +21,9 @@ public abstract class Peripheral extends BlockEntity implements RedBus.Periphera } public RCWorldBus getBus(){ + if (this.worldBus == null || !this.worldBus.getValid()) { + this.worldBus = new RCWorldBus(new RedBus(),world,getPos()); + } return this.worldBus; }