You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
129 lines
6.4 KiB
Java
129 lines
6.4 KiB
Java
package net.brokenmoon.redcontrol;
|
|
|
|
import net.brokenmoon.redcontrol.blockentities.CpuEntity;
|
|
import net.brokenmoon.redcontrol.blockentities.DriveEntity;
|
|
import net.brokenmoon.redcontrol.blocks.CpuBlock;
|
|
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.fabricmc.api.ModInitializer;
|
|
|
|
import net.fabricmc.fabric.api.item.v1.FabricItemSettings;
|
|
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
|
|
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.entity.BlockEntity;
|
|
import net.minecraft.block.entity.BlockEntityType;
|
|
import net.minecraft.item.BlockItem;
|
|
import net.minecraft.item.Item;
|
|
import net.minecraft.registry.Registry;
|
|
import net.minecraft.registry.Registries;
|
|
import net.minecraft.resource.Resource;
|
|
import net.minecraft.resource.ResourceManager;
|
|
import net.minecraft.resource.ResourceType;
|
|
import net.minecraft.util.Identifier;
|
|
import net.minecraft.util.math.BlockPos;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import java.io.InputStream;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
public class RedControl implements ModInitializer {
|
|
// This logger is used to write text to the console and the log file.
|
|
// 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");
|
|
public static final HashMap<String,byte[]> images = new HashMap<>();
|
|
|
|
//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));
|
|
public static final DriveBlock DRIVE = new DriveBlock(FabricBlockSettings.create().strength(4.0f));
|
|
|
|
//Items
|
|
public static final FloppyDisk FLOPPY_ITEM = new FloppyDisk(new FabricItemSettings().maxCount(1));
|
|
|
|
//Block Entities
|
|
public static final BlockEntityType<CpuEntity> CPU_BLOCK_ENTITY = Registry.register(Registries.BLOCK_ENTITY_TYPE, modloc("cpu_block_entity"), FabricBlockEntityTypeBuilder.create(CpuEntity::new, CPU).build());
|
|
public static final BlockEntityType<TerminalEntity> TERMINAL_BLOCK_ENTITY = Registry.register(Registries.BLOCK_ENTITY_TYPE, modloc("monitor_block_entity"), FabricBlockEntityTypeBuilder.create(TerminalEntity::new, TERMINAL).build());
|
|
public static final BlockEntityType<DriveEntity> DRIVE_BLOCK_ENTITY = Registry.register(Registries.BLOCK_ENTITY_TYPE, modloc("drive_block_entity"), FabricBlockEntityTypeBuilder.create(DriveEntity::new, DRIVE).build());
|
|
|
|
public static final Item SQUEAKY_HAMMER = new Item(new FabricItemSettings());
|
|
|
|
@Override
|
|
public void onInitialize() {
|
|
// This code runs as soon as Minecraft is in a mod-load-ready state.
|
|
// However, some things (like resources) may still be uninitialized.
|
|
// Proceed with mild caution.
|
|
|
|
ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(new SimpleSynchronousResourceReloadListener() {
|
|
@Override
|
|
public Identifier getFabricId() {
|
|
return Identifier.of("redcontrol","image");
|
|
}
|
|
|
|
@Override
|
|
public void reload(ResourceManager manager) {
|
|
images.clear(); //remove all previous images
|
|
for(Map.Entry<Identifier,Resource> entry : manager.findResources("image", path -> true).entrySet()) {
|
|
try(InputStream stream = entry.getValue().getInputStream()) {
|
|
String[] split = entry.getKey().getPath().split("/");
|
|
String file = split[split.length-1];
|
|
LOGGER.info("Found image {}",file);
|
|
images.put(file,stream.readAllBytes());
|
|
} catch(Exception e) {
|
|
LOGGER.error("Error occurred while loading resource binary {}", entry.getKey().toString(), e);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
LOGGER.info("Initializing RedControl!");
|
|
Registry.register(Registries.BLOCK, modloc("cpu"), CPU);
|
|
Registry.register(Registries.BLOCK, modloc("monitor"), TERMINAL);
|
|
Registry.register(Registries.BLOCK, modloc("disk_drive"), DRIVE);
|
|
Registry.register(Registries.ITEM, modloc("cpu"), new BlockItem(CPU, new FabricItemSettings()));
|
|
Registry.register(Registries.ITEM, modloc("monitor"), new BlockItem(TERMINAL, new FabricItemSettings()));
|
|
Registry.register(Registries.ITEM, modloc("disk_drive"), new BlockItem(DRIVE, new FabricItemSettings()));
|
|
Registry.register(Registries.ITEM, modloc("squeaky_hammer"), SQUEAKY_HAMMER);
|
|
Registry.register(Registries.ITEM, modloc("floppy_disk"), FLOPPY_ITEM);
|
|
|
|
//Packets
|
|
ServerPlayNetworking.registerGlobalReceiver(RedControlNetworking.CPU_START, ((server, player, handler, buf, responseSender) -> server.execute(() -> {
|
|
BlockPos blockPos = buf.readBlockPos();
|
|
CpuEntity cpu = (CpuEntity) player.getWorld().getBlockEntity(blockPos);
|
|
cpu.start();
|
|
LOGGER.info("Starting cpu at {}",blockPos);
|
|
})));
|
|
|
|
ServerPlayNetworking.registerGlobalReceiver(RedControlNetworking.CPU_STOP, ((server, player, handler, buf, responseSender) -> server.execute(() -> {
|
|
BlockPos blockPos = buf.readBlockPos();
|
|
CpuEntity cpu = (CpuEntity) player.getWorld().getBlockEntity(blockPos);
|
|
cpu.stop();
|
|
LOGGER.info("Stopping cpu at {}",blockPos);
|
|
})));
|
|
|
|
ServerPlayNetworking.registerGlobalReceiver(RedControlNetworking.CPU_RESET, ((server, player, handler, buf, responseSender) -> server.execute(() -> {
|
|
BlockPos blockPos = buf.readBlockPos();
|
|
CpuEntity cpu = (CpuEntity) player.getWorld().getBlockEntity(blockPos);
|
|
cpu.reset();
|
|
LOGGER.info("Resetting cpu at {}", blockPos);
|
|
})));
|
|
|
|
ServerPlayNetworking.registerGlobalReceiver(RedControlNetworking.KEY_PRESS, ((server,player,handler,buf,responseSender) -> server.execute(() -> {
|
|
BlockPos blockPos = buf.readBlockPos();
|
|
byte key = buf.readByte();
|
|
BlockEntity term = player.getWorld().getBlockEntity(blockPos);
|
|
if (term instanceof TerminalEntity te) {
|
|
te.pushKey(key);
|
|
}
|
|
})));
|
|
}
|
|
|
|
static Identifier modloc(String path) {return new Identifier("redcontrol",path);}
|
|
} |