From b90fa0e3435f67d78944be214b741cc0c4f63036 Mon Sep 17 00:00:00 2001 From: Astoria Date: Fri, 12 Apr 2024 13:20:13 -0500 Subject: [PATCH] Partially implemented gui for cpu. --- .../redcontrol/RedControlClient.java | 2 +- .../redcontrol/screen/CpuScreen.java | 63 +++++++++++++++++- .../redcontrol/screen/MonitorScreen.java | 9 +++ .../net/brokenmoon/redcontrol/RedControl.java | 28 ++++++++ .../redcontrol/RedControlNetworking.java | 3 + .../redcontrol/blockentities/CpuEntity.java | 24 +++++-- .../resources/assets/redcontrol/gui/cpu.ase | Bin 0 -> 803 bytes .../resources/assets/redcontrol/gui/cpu.png | Bin 0 -> 997 bytes .../assets/redcontrol/gui/lit_button.ase | Bin 0 -> 352 bytes .../assets/redcontrol/gui/lit_button.png | Bin 0 -> 393 bytes 10 files changed, 123 insertions(+), 6 deletions(-) create mode 100644 src/main/resources/assets/redcontrol/gui/cpu.ase create mode 100644 src/main/resources/assets/redcontrol/gui/cpu.png create mode 100644 src/main/resources/assets/redcontrol/gui/lit_button.ase create mode 100644 src/main/resources/assets/redcontrol/gui/lit_button.png diff --git a/src/client/java/net/brokenmoon/redcontrol/RedControlClient.java b/src/client/java/net/brokenmoon/redcontrol/RedControlClient.java index 382f398..5eef227 100644 --- a/src/client/java/net/brokenmoon/redcontrol/RedControlClient.java +++ b/src/client/java/net/brokenmoon/redcontrol/RedControlClient.java @@ -16,7 +16,7 @@ public class RedControlClient implements ClientModInitializer { RedControlNetworking.CPUGUI_PACKET_ID, (client, handler, buf, responseSender) -> { BlockPos blockPos = buf.readBlockPos(); client.execute(() -> { - client.setScreen(new CpuScreen(Text.literal("cpu"))); + client.setScreen(new CpuScreen(Text.literal("cpu"), blockPos)); }); }); ClientPlayNetworking.registerGlobalReceiver( diff --git a/src/client/java/net/brokenmoon/redcontrol/screen/CpuScreen.java b/src/client/java/net/brokenmoon/redcontrol/screen/CpuScreen.java index 4298cec..a1e88a4 100644 --- a/src/client/java/net/brokenmoon/redcontrol/screen/CpuScreen.java +++ b/src/client/java/net/brokenmoon/redcontrol/screen/CpuScreen.java @@ -1,17 +1,78 @@ package net.brokenmoon.redcontrol.screen; import net.brokenmoon.redcontrol.RedControl; +import net.brokenmoon.redcontrol.RedControlNetworking; +import net.brokenmoon.redcontrol.blockentities.CpuEntity; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; +import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.gui.widget.ButtonWidget; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; +import net.minecraft.util.Identifier; +import net.minecraft.util.math.BlockPos; public class CpuScreen extends Screen { - public CpuScreen(Text title) { + + private static final Identifier cpuTexture = new Identifier("redcontrol", "gui/cpu.png"); + private static final Identifier poweredButton = new Identifier("redcontrol", "gui/lit_button.png"); + + BlockPos pos; + CpuEntity cpu; + + public CpuScreen(Text title, BlockPos blockPos) { super(title); + this.pos = blockPos; } + public ButtonWidget startButton; + public ButtonWidget stopButton; + public ButtonWidget resetButton; + @Override protected void init() { super.init(); RedControl.LOGGER.info("Created CPU screen"); + cpu = (CpuEntity) client.world.getBlockEntity(pos); + + startButton = ButtonWidget.builder(Text.literal("Start"), button -> { + PacketByteBuf byteBuf = PacketByteBufs.create(); + byteBuf.writeBlockPos(pos); + ClientPlayNetworking.send(RedControlNetworking.CPU_START,byteBuf); + }).dimensions(width / 2 + 105, 20, 200, 20).build(); + + stopButton = ButtonWidget.builder(Text.literal("Stop"), button -> { + PacketByteBuf byteBuf = PacketByteBufs.create(); + byteBuf.writeBlockPos(pos); + ClientPlayNetworking.send(RedControlNetworking.CPU_STOP,byteBuf); + }).dimensions(width / 2 + 105, 50, 200, 20).build(); + + resetButton = ButtonWidget.builder(Text.literal("Reset"), button -> { + PacketByteBuf byteBuf = PacketByteBufs.create(); + byteBuf.writeBlockPos(pos); + ClientPlayNetworking.send(RedControlNetworking.CPU_RESET,byteBuf); + }).dimensions(width / 2 + 105, 80, 200, 20).build(); + + addDrawableChild(startButton); + addDrawableChild(stopButton); + addDrawableChild(resetButton); + } + + @Override + public void render(DrawContext context, int mouseX, int mouseY, float delta) { + super.render(context, mouseX, mouseY, delta); + int buttonPos = 7; + if(cpu.isRunning){ + buttonPos = 0; + } else if(cpu.isResetting || cpu.isReset){ + buttonPos = 14; + } + int x = (width - 176) / 2; + int y = (height - 166) / 2; + context.drawTexture(cpuTexture,x,y,0,0,176,166); + context.drawTexture(poweredButton, x + 27, y + 6 + buttonPos, 0, 0, 4, 4); } } diff --git a/src/client/java/net/brokenmoon/redcontrol/screen/MonitorScreen.java b/src/client/java/net/brokenmoon/redcontrol/screen/MonitorScreen.java index 0a95451..03fe270 100644 --- a/src/client/java/net/brokenmoon/redcontrol/screen/MonitorScreen.java +++ b/src/client/java/net/brokenmoon/redcontrol/screen/MonitorScreen.java @@ -1,10 +1,13 @@ package net.brokenmoon.redcontrol.screen; import net.brokenmoon.redcontrol.RedControl; +import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.screen.Screen; import net.minecraft.text.Text; +import net.minecraft.util.Identifier; public class MonitorScreen extends Screen { + Identifier screenTexture = new Identifier("redcontrol", "gui/display.png"); public MonitorScreen(Text title) { super(title); } @@ -14,4 +17,10 @@ public class MonitorScreen extends Screen { super.init(); RedControl.LOGGER.info("Opened Monitor Screen"); } + + @Override + public void render(DrawContext context, int mouseX, int mouseY, float delta) { + super.render(context, mouseX, mouseY, delta); + context.drawTexture(screenTexture,0,0,0,0,350,230); + } } diff --git a/src/main/java/net/brokenmoon/redcontrol/RedControl.java b/src/main/java/net/brokenmoon/redcontrol/RedControl.java index 7c72688..c080bba 100644 --- a/src/main/java/net/brokenmoon/redcontrol/RedControl.java +++ b/src/main/java/net/brokenmoon/redcontrol/RedControl.java @@ -9,6 +9,7 @@ import net.brokenmoon.redcontrol.blocks.MonitorBlock; 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; @@ -21,7 +22,9 @@ import net.minecraft.registry.Registries; import net.minecraft.resource.Resource; import net.minecraft.resource.ResourceManager; import net.minecraft.resource.ResourceType; +import net.minecraft.text.Text; import net.minecraft.util.Identifier; +import net.minecraft.util.math.BlockPos; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -84,5 +87,30 @@ public class RedControl implements ModInitializer { Registry.register(Registries.ITEM, new Identifier("redcontrol", "monitor"), new BlockItem(MONITOR, new FabricItemSettings())); Registry.register(Registries.ITEM, new Identifier("redcontrol", "disk_drive"), new BlockItem(DRIVE, new FabricItemSettings())); + //Packets + ServerPlayNetworking.registerGlobalReceiver(RedControlNetworking.CPU_START, ((server, player, handler, buf, responseSender) -> { + server.execute(() -> { + CpuEntity cpu = (CpuEntity) player.getWorld().getBlockEntity(buf.readBlockPos()); + cpu.start(); + LOGGER.info("Starting cpu at " + buf.readBlockPos()); + }); + })); + + ServerPlayNetworking.registerGlobalReceiver(RedControlNetworking.CPU_STOP, ((server, player, handler, buf, responseSender) -> { + server.execute(() -> { + CpuEntity cpu = (CpuEntity) player.getWorld().getBlockEntity(buf.readBlockPos()); + cpu.stop(); + LOGGER.info("Stopping cpu at " + buf.readBlockPos()); + }); + })); + + ServerPlayNetworking.registerGlobalReceiver(RedControlNetworking.CPU_RESET, ((server, player, handler, buf, responseSender) -> { + server.execute(() -> { + CpuEntity cpu = (CpuEntity) player.getWorld().getBlockEntity(buf.readBlockPos()); + cpu.reset(); + LOGGER.info("Resetting cpu at " + buf.readBlockPos()); + }); + })); + } } \ No newline at end of file diff --git a/src/main/java/net/brokenmoon/redcontrol/RedControlNetworking.java b/src/main/java/net/brokenmoon/redcontrol/RedControlNetworking.java index daaecc4..854eede 100644 --- a/src/main/java/net/brokenmoon/redcontrol/RedControlNetworking.java +++ b/src/main/java/net/brokenmoon/redcontrol/RedControlNetworking.java @@ -5,4 +5,7 @@ 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"); + public static final Identifier CPU_START = new Identifier("redcontrol", "start_cpu"); + public static final Identifier CPU_STOP = new Identifier("redcontrol", "stop_cpu"); + public static final Identifier CPU_RESET = new Identifier("redcontrol", "reset_cpu"); } diff --git a/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java b/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java index 0907f27..1a67bd6 100644 --- a/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java +++ b/src/main/java/net/brokenmoon/redcontrol/blockentities/CpuEntity.java @@ -23,9 +23,9 @@ public class CpuEntity extends Peripheral{ private int defaultMonitorId = 1; private int defaultDriveId = 2; - private boolean isRunning = true; - private boolean isResetting = false; - private boolean isReset = false; + public boolean isRunning = false; + public boolean isResetting = false; + public boolean isReset = false; private int resetTimer = 5; public CpuEntity(BlockPos pos, BlockState state) { @@ -76,6 +76,22 @@ public class CpuEntity extends Peripheral{ public void reset() { this.core.reset(this.defaultDriveId, this.defaultMonitorId); + isRunning = false; + isResetting = true; + isReset = false; + resetTimer = 5; + } + + public void stop() { + isRunning = false; + isResetting = false; + isReset = false; + } + + public void start() { + isRunning = true; + isResetting = false; + isReset = false; } public void step() { @@ -129,7 +145,7 @@ public class CpuEntity extends Peripheral{ @Override public void readNbt(NbtCompound nbt) { - this.isReset = nbt.getBoolean("isRunning"); + this.isRunning = nbt.getBoolean("isRunning"); this.isResetting = nbt.getBoolean("isResetting"); this.isReset = nbt.getBoolean("isReset"); this.resetTimer = nbt.getInt("resetTimer"); diff --git a/src/main/resources/assets/redcontrol/gui/cpu.ase b/src/main/resources/assets/redcontrol/gui/cpu.ase new file mode 100644 index 0000000000000000000000000000000000000000..e4f0bdbc649b8571eecceacedc2ce2d22c0bc168 GIT binary patch literal 803 zcmY#pW?*=*l#zjfk%3VGNH8#@05Jjp#Xy1#0zgO$u$YN~;nzntu&r!B3&en!T>)$* zl66@?HVepg3P6UEf{Ck!O`4@)f0*_CLbaO<(%v1O_4||A%%=fa`wYydBiL4yqlOV`r88ojujZDtU!ox zl`exK$W_z4xtI)j+H5Z`*zuhu&gIU|5><|QN3F{OIHpZ-pKP-9&>w+C+l+rsI(cN; zr_1mBS=LP2{)FYX-1qdAYt4(#E`EAp760?EMgOAMHXq%7FW;m%_P58}%~i>lB?5~R zzwK=QxuR~_zU=SPhOc*6RX_K5c4vZx?U}`CvZ3ku`3-5aN;mJHzxmZ(9af*)%4?rr zIQj9_jyvgjd(Uy!q}e}~+V*V0)2Wu`2F%ylFIgS6pCnma*mI}ug3so>$^KXVKj4t% z|NY4H^-jLJ!`D5o7oXXFd3D7eF=uP#%Ja1Xw;x-Yty~royH%#zD7f_6ytPMiS8l$( zyzPj>Mz_~8iPMs@vnN_r=dx~n H)7%RHTVfom literal 0 HcmV?d00001 diff --git a/src/main/resources/assets/redcontrol/gui/cpu.png b/src/main/resources/assets/redcontrol/gui/cpu.png new file mode 100644 index 0000000000000000000000000000000000000000..4597fa18a6292556bdf520638ac1b5e1cfcbfbf1 GIT binary patch literal 997 zcmeAS@N?(olHy`uVBq!ia0y~yU<5K5893O0R7}x|G!U;i$lZxy-8q?;3=GWkJzX3_ zD(1YsyD{&wg9O6^@1r4|j4EMbag9M{_nOw;&0wEk^--W_YC^`%ThAXbDpjzGUGpk= z&BeT{!1A5Uan5zJ_47q;Zk($j`Om)m`)|F?6D({h{%QBz`15-E&Dwf4iOH@9dTf6v zdotXb-MT^MPyYSAJNNr$CnQ- zemt4<|JSQc+xeNlWUnt%;{7v2;8JeQ7QywuKmL7wiFrr-RhEL1noB=k@Ao^cu=m@u zGP%6G=WFA+|Je8HHf-DfVRg>y+J?7UeT-9bgpOo#KRCJg@VWQQIoB8Q?9f(zu~B3N z569}z-3$lbX{$9{(%4^T^Jd;F35T^jGx+(IozI@y_+!_eFW{%#Da-Ndx{j zCI>zzGMq__B;+E$pZ7-}mh5;p=m^ z9rzV>mW}7(eNLyJB@au?5^`@9WZHj!c>1?rbV9B4SvDQj##^7K+-x-9>D6XhYbEy7 zm?hho?elBzxP2Bcwy<7X&ch+R42Z8qu*GaWc7K~Dv(x^lzs3P^$4>wLv9%~+`a!!? zhZp{@^&B#%K8T6?_xh4o09(tyHKuGA)E3OX#>>*S^z(k^_7B@;nJI0!|%E4+tEK=JRQe=M=aV$>KGO s<(SV0=W6eRM&DEvet;7DFam9l&ELc=&2D?Lhy~;;Pgg&ebxsLQ04o@8UH||9 literal 0 HcmV?d00001 diff --git a/src/main/resources/assets/redcontrol/gui/lit_button.ase b/src/main/resources/assets/redcontrol/gui/lit_button.ase new file mode 100644 index 0000000000000000000000000000000000000000..6e767eadcada46c55389857eb00d6ce407c418ed GIT binary patch literal 352 zcmYdbWMFu(6i6^KFe(5E28I+MMgX7~NRUAQ2x$Qx01f^1kp*lk3y>`a#Ow-SD}gK+ z$O1B0K(12&GL#fdTs3UcEDig^tnU}9-CU6N?(nSNpUh@H4anMOU_K?!J5xW%TgAx6 z(%!zrAfYKb?_u)n|M6#^oWA|%|NsB9*KTjAYtPKeiVO=XbLv`SWOOq;`Fi!P{^?#_ z`3CYJn-rLVBn5-U@S6buc4999L0)PE)j4RRU_L&cozmD&IAH?`%rsW%(y N*%`4hD6ng+1ORpFLAd|` literal 0 HcmV?d00001 diff --git a/src/main/resources/assets/redcontrol/gui/lit_button.png b/src/main/resources/assets/redcontrol/gui/lit_button.png new file mode 100644 index 0000000000000000000000000000000000000000..fde8f4e48abe641e06af74f0d1a7149bc61f08e5 GIT binary patch literal 393 zcmeAS@N?(olHy`uVBq!ia0y~yU<5K5893O0R7}x|G!U;i$lZxy-8q?;K(@1|i(^Q| zoVVvLaykSsuwL+X3sf`enZ&Poc!tN`*dL~i%jXy$%-oi`|C7P`VBrViw^St0Px`2{ vRsX>0Telb&%+vGiTe$u&(A1F%er%CGqR%NZOZlidC