Partially implemented gui for cpu.

main
Astoria 7 months ago
parent 18f75240dd
commit b90fa0e343

@ -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(

@ -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);
}
}

@ -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);
}
}

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

@ -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");
}

@ -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");

Binary file not shown.

After

Width:  |  Height:  |  Size: 997 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 393 B

Loading…
Cancel
Save