flood fill

main
Walker Fowlkes 7 months ago
parent 27543642a9
commit 230190d4ee

@ -1,5 +1,6 @@
package net.brokenmoon.redcontrol; package net.brokenmoon.redcontrol;
import net.brokenmoon.redcontrol.api.events.BlockModificationEvents;
import net.brokenmoon.redcontrol.blockentities.CpuEntity; import net.brokenmoon.redcontrol.blockentities.CpuEntity;
import net.brokenmoon.redcontrol.blockentities.DriveEntity; import net.brokenmoon.redcontrol.blockentities.DriveEntity;
import net.brokenmoon.redcontrol.blocks.CpuBlock; 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.TerminalBlock;
import net.brokenmoon.redcontrol.blocks.TerminalEntity; import net.brokenmoon.redcontrol.blocks.TerminalEntity;
import net.brokenmoon.redcontrol.item.FloppyDisk; import net.brokenmoon.redcontrol.item.FloppyDisk;
import net.brokenmoon.redcontrol.util.FloodFill;
import net.fabricmc.api.ModInitializer; import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.item.v1.FabricItemSettings; 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.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper; import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener; import net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener;
import net.minecraft.block.Block;
import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType; import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.item.BlockItem; import net.minecraft.item.BlockItem;
@ -23,14 +26,19 @@ import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup; import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.NbtCompound;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.registry.Registry; import net.minecraft.registry.Registry;
import net.minecraft.registry.Registries; 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.Resource;
import net.minecraft.resource.ResourceManager; import net.minecraft.resource.ResourceManager;
import net.minecraft.resource.ResourceType; import net.minecraft.resource.ResourceType;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 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. // 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. // That way, it's clear which mod wrote info, warnings, and errors.
public static final Logger LOGGER = LoggerFactory.getLogger("redcontrol"); public static final Logger LOGGER = LoggerFactory.getLogger("redcontrol");
//A hashmap of files contained in images
public static final HashMap<String,byte[]> images = new HashMap<>(); public static final HashMap<String,byte[]> images = new HashMap<>();
//Tags
public static final TagKey<Block> TAG_BUSABLE = TagKey.of(RegistryKeys.BLOCK, modloc("bus_conductive"));
//Blocks //Blocks
public static final CpuBlock CPU = new CpuBlock(FabricBlockSettings.create().strength(4.0f)); public static final CpuBlock CPU = new CpuBlock(FabricBlockSettings.create().strength(4.0f));
public static final TerminalBlock TERMINAL = new TerminalBlock(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); 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);} static Identifier modloc(String path) {return new Identifier("redcontrol",path);}

@ -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<BlockPos>()
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)
}
}
}

@ -1,3 +1,4 @@
{ {
"replace": false,
"values": ["minecraft:glass"] "values": ["minecraft:glass"]
} }

@ -7,7 +7,8 @@
"CpuAccessor", "CpuAccessor",
"ExampleMixin", "ExampleMixin",
"MemoryAccessor", "MemoryAccessor",
"RedBusAccessor" "RedBusAccessor",
"WorldMixin"
], ],
"injectors": { "injectors": {
"defaultRequire": 1 "defaultRequire": 1

Loading…
Cancel
Save