diff --git a/src/client/java/net/brokenmoon/redcontrol/RedControlClient.java b/src/client/java/net/brokenmoon/redcontrol/RedControlClient.java index b060ea2..382f398 100644 --- a/src/client/java/net/brokenmoon/redcontrol/RedControlClient.java +++ b/src/client/java/net/brokenmoon/redcontrol/RedControlClient.java @@ -1,10 +1,30 @@ package net.brokenmoon.redcontrol; +import net.brokenmoon.redcontrol.screen.CpuScreen; +import net.brokenmoon.redcontrol.screen.MonitorScreen; import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; +import net.minecraft.client.MinecraftClient; +import net.minecraft.text.Text; +import net.minecraft.util.math.BlockPos; public class RedControlClient implements ClientModInitializer { @Override public void onInitializeClient() { // This entrypoint is suitable for setting up client-specific logic, such as rendering. + ClientPlayNetworking.registerGlobalReceiver( + RedControlNetworking.CPUGUI_PACKET_ID, (client, handler, buf, responseSender) -> { + BlockPos blockPos = buf.readBlockPos(); + client.execute(() -> { + client.setScreen(new CpuScreen(Text.literal("cpu"))); + }); + }); + ClientPlayNetworking.registerGlobalReceiver( + RedControlNetworking.MONITOR_PACKET_ID, (client, handler, buf, responseSender) -> { + BlockPos blockPos = buf.readBlockPos(); + client.execute(() -> { + client.setScreen(new MonitorScreen(Text.literal("monitor"))); + }); + }); } } \ No newline at end of file diff --git a/src/client/java/net/brokenmoon/redcontrol/screen/CpuScreen.java b/src/client/java/net/brokenmoon/redcontrol/screen/CpuScreen.java new file mode 100644 index 0000000..4298cec --- /dev/null +++ b/src/client/java/net/brokenmoon/redcontrol/screen/CpuScreen.java @@ -0,0 +1,17 @@ +package net.brokenmoon.redcontrol.screen; + +import net.brokenmoon.redcontrol.RedControl; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.text.Text; + +public class CpuScreen extends Screen { + public CpuScreen(Text title) { + super(title); + } + + @Override + protected void init() { + super.init(); + RedControl.LOGGER.info("Created CPU screen"); + } +} diff --git a/src/client/java/net/brokenmoon/redcontrol/screen/MonitorScreen.java b/src/client/java/net/brokenmoon/redcontrol/screen/MonitorScreen.java new file mode 100644 index 0000000..0a95451 --- /dev/null +++ b/src/client/java/net/brokenmoon/redcontrol/screen/MonitorScreen.java @@ -0,0 +1,17 @@ +package net.brokenmoon.redcontrol.screen; + +import net.brokenmoon.redcontrol.RedControl; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.text.Text; + +public class MonitorScreen extends Screen { + public MonitorScreen(Text title) { + super(title); + } + + @Override + protected void init() { + super.init(); + RedControl.LOGGER.info("Opened Monitor Screen"); + } +} diff --git a/src/main/java/net/brokenmoon/redcontrol/RedControlNetworking.java b/src/main/java/net/brokenmoon/redcontrol/RedControlNetworking.java new file mode 100644 index 0000000..daaecc4 --- /dev/null +++ b/src/main/java/net/brokenmoon/redcontrol/RedControlNetworking.java @@ -0,0 +1,8 @@ +package net.brokenmoon.redcontrol; + +import net.minecraft.util.Identifier; + +public class RedControlNetworking { + public static final Identifier CPUGUI_PACKET_ID = new Identifier("redcontrol", "open_cpu_gui"); + public static final Identifier MONITOR_PACKET_ID = new Identifier("redcontrol", "open_monitor"); +} diff --git a/src/main/java/net/brokenmoon/redcontrol/api/Emulator.java b/src/main/java/net/brokenmoon/redcontrol/api/Emulator.java index 4ca583e..b36289d 100644 --- a/src/main/java/net/brokenmoon/redcontrol/api/Emulator.java +++ b/src/main/java/net/brokenmoon/redcontrol/api/Emulator.java @@ -5,9 +5,11 @@ import com.simon816.j65el02.Cpu; import com.simon816.j65el02.CpuState; import com.simon816.j65el02.device.Memory; import com.simon816.j65el02.device.RedBus; +import com.simon816.j65el02.device.RedBusState; import net.brokenmoon.redcontrol.RedControl; import net.brokenmoon.redcontrol.mixin.BusAccessor; import net.brokenmoon.redcontrol.mixin.CpuAccessor; +import net.brokenmoon.redcontrol.mixin.MemoryAccessor; import net.minecraft.nbt.NbtCompound; import java.io.IOException; @@ -105,14 +107,15 @@ public class Emulator { nbt.putLong("stepCounter", cpu.getCpuState().stepCounter); //Redbus state write nbt - nbt.putInt("deviceId", cpu.redBusState.activeDeviceId); - nbt.putInt("offset", cpu.redBusState.offset); - nbt.putInt("memoryWindow", cpu.redBusState.memoryWindow); - nbt.putBoolean("enabled", cpu.redBusState.enabled); - nbt.putBoolean("enableWindow", cpu.redBusState.enableWindow); + RedBusState redBusState = ((CpuAccessor)cpu).getRedBusState(); + nbt.putInt("deviceId", redBusState.activeDeviceId); + nbt.putInt("offset", redBusState.offset); + nbt.putInt("memoryWindow", redBusState.memoryWindow); + nbt.putBoolean("enabled", redBusState.enabled); + nbt.putBoolean("enableWindow", redBusState.enableWindow); //Ram write nbt. - nbt.putByteArray("ramValues", this.ram.getMem()); + nbt.putByteArray("ramValues", ((MemoryAccessor)this.ram).getMem()); } public void readNbt(NbtCompound nbt) { @@ -157,13 +160,14 @@ public class Emulator { cpu.getCpuState().stepCounter = nbt.getLong("stepCounter"); //Redbus state write nbt - cpu.redBusState.activeDeviceId = nbt.getInt("deviceId"); - cpu.redBusState.offset = nbt.getInt("offset"); - cpu.redBusState.memoryWindow = nbt.getInt("memoryWindow"); - cpu.redBusState.enabled = nbt.getBoolean("enabled"); - cpu.redBusState.enableWindow = nbt.getBoolean("enableWindow"); + RedBusState redBusState = ((CpuAccessor)cpu).getRedBusState(); + redBusState.activeDeviceId = nbt.getInt("deviceId"); + redBusState.offset = nbt.getInt("offset"); + redBusState.memoryWindow = nbt.getInt("memoryWindow"); + redBusState.enabled = nbt.getBoolean("enabled"); + redBusState.enableWindow = nbt.getBoolean("enableWindow"); //Ram write nbt. - ram.setMem(nbt.getByteArray("ramValues")); + ((MemoryAccessor)ram).setMem(nbt.getByteArray("ramValues")); } } diff --git a/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java b/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java index 4faee09..0907f27 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java +++ b/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java @@ -110,6 +110,11 @@ public class CpuEntity extends Peripheral{ return BlockEntityUpdateS2CPacket.create(this); } + @Override + public NbtCompound toInitialChunkDataNbt() { + return createNbt(); + } + @Override protected void writeNbt(NbtCompound nbt) { nbt.putBoolean("isRunning", this.isRunning); @@ -133,4 +138,5 @@ public class CpuEntity extends Peripheral{ core.readNbt(nbt); super.readNbt(nbt); } + } diff --git a/src/main/java/net/brokenmoon/redcontrol/blockentities/MonitorEntity.java b/src/main/java/net/brokenmoon/redcontrol/blockentities/MonitorEntity.java index 9da1972..a4d39c2 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blockentities/MonitorEntity.java +++ b/src/main/java/net/brokenmoon/redcontrol/blockentities/MonitorEntity.java @@ -3,7 +3,11 @@ package net.brokenmoon.redcontrol.blockentities; import net.brokenmoon.redcontrol.RedControl; import net.minecraft.block.BlockState; 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.util.math.BlockPos; +import org.jetbrains.annotations.Nullable; public class MonitorEntity extends Peripheral { @@ -258,4 +262,15 @@ public class MonitorEntity extends Peripheral { super.readNbt(nbt); } + + @Nullable + @Override + public Packet toUpdatePacket() { + return BlockEntityUpdateS2CPacket.create(this); + } + + @Override + public NbtCompound toInitialChunkDataNbt() { + return createNbt(); + } } diff --git a/src/main/java/net/brokenmoon/redcontrol/blocks/CpuBlock.java b/src/main/java/net/brokenmoon/redcontrol/blocks/CpuBlock.java index 1563007..75d5d07 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blocks/CpuBlock.java +++ b/src/main/java/net/brokenmoon/redcontrol/blocks/CpuBlock.java @@ -3,14 +3,19 @@ package net.brokenmoon.redcontrol.blocks; import com.mojang.serialization.MapCodec; import com.simon816.j65el02.Cpu; import net.brokenmoon.redcontrol.RedControl; +import net.brokenmoon.redcontrol.RedControlNetworking; import net.brokenmoon.redcontrol.blockentities.CpuEntity; import net.brokenmoon.redcontrol.blockentities.MonitorEntity; import net.brokenmoon.redcontrol.blockentities.Peripheral; +import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; 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.player.PlayerEntity; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; import net.minecraft.util.ActionResult; import net.minecraft.util.Hand; @@ -50,6 +55,9 @@ public class CpuBlock extends NetworkCarrier { 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); + PacketByteBuf byteBuf = PacketByteBufs.create(); + byteBuf.writeBlockPos(pos); + ServerPlayNetworking.send((ServerPlayerEntity) player, RedControlNetworking.CPUGUI_PACKET_ID,byteBuf); } return ActionResult.SUCCESS; diff --git a/src/main/java/net/brokenmoon/redcontrol/blocks/MonitorBlock.java b/src/main/java/net/brokenmoon/redcontrol/blocks/MonitorBlock.java index 1449689..477daa0 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blocks/MonitorBlock.java +++ b/src/main/java/net/brokenmoon/redcontrol/blocks/MonitorBlock.java @@ -1,13 +1,18 @@ package net.brokenmoon.redcontrol.blocks; import com.mojang.serialization.MapCodec; +import net.brokenmoon.redcontrol.RedControlNetworking; import net.brokenmoon.redcontrol.blockentities.MonitorEntity; +import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; import net.minecraft.block.Block; import net.minecraft.block.BlockEntityProvider; import net.minecraft.block.BlockState; import net.minecraft.block.BlockWithEntity; import net.minecraft.block.entity.BlockEntity; import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; import net.minecraft.util.ActionResult; import net.minecraft.util.Hand; @@ -39,9 +44,12 @@ public class MonitorBlock extends NetworkCarrier { MonitorEntity monitor = (MonitorEntity) world.getBlockEntity(pos); player.sendMessage(Text.literal(String.valueOf(monitor.getBus().hashCode())), false); String[] text = monitor.getText(); - for(int i = 0; i < text.length; i++){ - player.sendMessage(Text.literal(text[i]), false); + for (String s : text) { + player.sendMessage(Text.literal(s), false); } + PacketByteBuf byteBuf = PacketByteBufs.create(); + byteBuf.writeBlockPos(pos); + ServerPlayNetworking.send((ServerPlayerEntity) player, RedControlNetworking.MONITOR_PACKET_ID,byteBuf); } return ActionResult.SUCCESS; diff --git a/src/main/java/net/brokenmoon/redcontrol/mixin/MemoryAccessor.java b/src/main/java/net/brokenmoon/redcontrol/mixin/MemoryAccessor.java new file mode 100644 index 0000000..e5893be --- /dev/null +++ b/src/main/java/net/brokenmoon/redcontrol/mixin/MemoryAccessor.java @@ -0,0 +1,13 @@ +package net.brokenmoon.redcontrol.mixin; + +import com.simon816.j65el02.device.Memory; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +@Mixin(Memory.class) +public interface MemoryAccessor { + @Accessor("mem") + byte[] getMem(); + @Accessor("mem") + void setMem(byte[] mem); +} diff --git a/src/main/resources/redcontrol.mixins.json b/src/main/resources/redcontrol.mixins.json index abe32fb..6b561e9 100644 --- a/src/main/resources/redcontrol.mixins.json +++ b/src/main/resources/redcontrol.mixins.json @@ -6,6 +6,7 @@ "BusAccessor", "CpuAccessor", "ExampleMixin", + "MemoryAccessor", "RedBusAccessor" ], "injectors": {