Created
April 3, 2024 10:30
-
-
Save Densamisten/714046a00b1d6b0fec3b06621083e9e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.densamisten.gui; | |
import com.mojang.blaze3d.platform.ClipboardManager; | |
import io.github.densamisten.Forgient; | |
import net.minecraft.client.gui.GuiGraphics; | |
import net.minecraft.client.gui.components.Button; | |
import net.minecraft.client.gui.components.EditBox; | |
import net.minecraft.client.gui.components.Tooltip; | |
import net.minecraft.client.gui.screens.Screen; | |
import net.minecraft.network.chat.Component; | |
import net.minecraft.resources.ResourceLocation; | |
import net.minecraft.world.level.Level; | |
import org.jetbrains.annotations.NotNull; | |
import org.lwjgl.glfw.GLFW; | |
import java.nio.ByteBuffer; | |
import java.nio.charset.StandardCharsets; | |
import java.util.Random; | |
import java.util.UUID; | |
public class ForgientScreen extends Screen { | |
private static final Component TITLE = | |
Component.translatable("gui." + Forgient.MOD_ID + ".forgient_screen"); | |
private static final Component UUID_V1_BUTTON = | |
Component.translatable("gui." + Forgient.MOD_ID + ".uuid_v1_button"); | |
private static final Component UUID_V2_BUTTON = | |
Component.translatable("gui." + Forgient.MOD_ID + ".uuid_v2_button"); | |
private static final Component UUID_V3_BUTTON = | |
Component.translatable("gui." + Forgient.MOD_ID + ".uuid_v3_button"); | |
private static final Component UUID_V5_BUTTON = | |
Component.translatable("gui." + Forgient.MOD_ID + ".uuid_v5_button"); | |
private static final ResourceLocation TEXTURE = | |
new ResourceLocation(Forgient.MOD_ID, "textures/gui/uuid_screen.png"); | |
private static final ClipboardManager clipboardManager = new ClipboardManager(); | |
private final int imageWidth, imageHeight; | |
private int leftPos, topPos; | |
private Button uuidV1button; | |
private Button uuidV2Button; | |
private Button uuidV3Button; | |
private Button uuidV5Button; | |
private EditBox editBox; // Add an EditBox field | |
private EditBox editBox2; // Add an EditBox field | |
public ForgientScreen() { | |
super(TITLE); | |
this.imageWidth = 176; | |
this.imageHeight = 166; | |
} | |
@Override | |
protected void init() { | |
super.init(); | |
this.leftPos = (this.width - this.imageWidth) / 2; | |
this.topPos = (this.height - this.imageHeight) / 2; | |
if(this.minecraft == null) return; | |
Level level = this.minecraft.level; | |
if(level == null) return; | |
// Add the button for UUID v1 | |
this.uuidV1button = addRenderableWidget( | |
Button.builder( | |
UUID_V1_BUTTON, | |
this::handleUuidV1Button) | |
.bounds(this.leftPos + 40, this.topPos + 50, 80, 20) // Adjusted y coordinate | |
.tooltip(Tooltip.create(UUID_V1_BUTTON)) | |
.build()); | |
// Add the button for UUID v2 | |
this.uuidV2Button = addRenderableWidget( | |
Button.builder( | |
UUID_V2_BUTTON, | |
this::handleUuidV2Button) | |
.bounds(this.leftPos + 40, this.topPos + 100, 80, 20) // Adjusted y coordinate | |
.tooltip(Tooltip.create(UUID_V1_BUTTON)) | |
.build()); | |
// Add the button for UUID v3 | |
this.uuidV3Button = addRenderableWidget( | |
Button.builder( | |
UUID_V3_BUTTON, | |
this::handleUuidV3Button) | |
.bounds(this.leftPos + 40, this.topPos + 150, 80, 20) // Adjusted y coordinate | |
.tooltip(Tooltip.create(UUID_V1_BUTTON)) | |
.build()); | |
// Add the button for UUID v5 | |
this.uuidV5Button = addRenderableWidget( | |
Button.builder( | |
UUID_V5_BUTTON, | |
this::handleUuidV5Button) | |
.bounds(this.leftPos + 40, this.topPos + 200, 80, 20) // Adjusted y coordinate | |
.tooltip(Tooltip.create(UUID_V5_BUTTON)) | |
.build()); | |
// Initialize the EditBox | |
this.editBox = new EditBox(this.font, this.leftPos + 8, this.topPos + 40, 100, 20, Component.literal("Input text")); | |
this.editBox.setMaxLength(36); | |
this.editBox.setResponder((p_98305_) -> { | |
System.out.println("Hewo"); | |
}); | |
this.addWidget(this.editBox); | |
// Initialize the second EditBox | |
this.editBox2 = new EditBox(this.font, this.leftPos + 8, this.topPos + 70, 100, 20, Component.literal("Input shift")); | |
this.editBox2.setMaxLength(36); | |
this.editBox2.setResponder((p_98305_) -> { | |
System.out.println("Hewo"); | |
}); | |
this.addWidget(this.editBox2); | |
} | |
@Override | |
public void render(@NotNull GuiGraphics graphics, int mouseX, int mouseY, float partialTicks) { | |
renderTransparentBackground(graphics); | |
graphics.blit(TEXTURE, this.leftPos, this.topPos, 0, 0, this.imageWidth, this.imageHeight); | |
super.render(graphics, mouseX, mouseY, partialTicks); | |
graphics.drawString(this.font, | |
TITLE, | |
this.leftPos + 32, | |
this.topPos + 8, | |
0x404040, | |
false); | |
graphics.drawString(this.font, | |
"Generate UUID v1", | |
this.leftPos + 100, | |
this.topPos + 8, | |
0x404040, | |
false); | |
// Render the EditBox | |
this.editBox.render(graphics, mouseX, mouseY, partialTicks); | |
this.editBox2.render(graphics, mouseX, mouseY, partialTicks); | |
} | |
private void handleUuidV1Button(Button button) { | |
// Generate a Version 1 UUID | |
UUID generatedUuid = generateUuidV1(); | |
// Copy the output to the clipboard | |
clipboardManager.setClipboard(GLFW.glfwGetCurrentContext(), generatedUuid.toString()); | |
} | |
private UUID generateUuidV1() { | |
long most64SigBits = get64MostSignificantBitsForVersion1(); | |
long least64SigBits = get64LeastSignificantBitsForVersion1(); | |
return new UUID(most64SigBits, least64SigBits); | |
} | |
private long get64MostSignificantBitsForVersion1() { | |
long currentTimeMillis = System.currentTimeMillis(); | |
long timeLow = (currentTimeMillis & 4294967295L) << 32; | |
long timeMid = ((currentTimeMillis >> 32) & 65535L) << 16; | |
long version = 1L << 12; | |
long timeHi = (currentTimeMillis >> 48) & 4095L; | |
return timeLow | timeMid | version | timeHi; | |
} | |
private long get64LeastSignificantBitsForVersion1() { | |
Random random = new Random(); | |
long random63BitLong = random.nextLong() & 4611686018427387903L; | |
long variant3BitFlag = -9223372036854775808L; | |
return random63BitLong | variant3BitFlag; | |
} | |
private void handleUuidV2Button(Button button) { | |
// Generate a Version 2 UUID | |
UUID generatedUuid = generateUuidV2(); | |
// Copy the output to the clipboard | |
clipboardManager.setClipboard(GLFW.glfwGetCurrentContext(), generatedUuid.toString()); | |
} | |
private UUID generateUuidV2() { | |
Random random = new Random(); | |
long mostSignificantBits = random.nextLong() & 0xFFFFFFFFFFFF0FFFL | 0x0000000000002000L; // Set the version to 2 | |
long leastSignificantBits = random.nextLong(); | |
return new UUID(mostSignificantBits, leastSignificantBits); | |
} | |
private void handleUuidV3Button(Button button) { | |
// Get the input text from the first EditBox (input text) | |
String inputText = this.editBox.getValue(); | |
// Get the namespace text from the second EditBox (namespace) | |
String namespaceText = this.editBox2.getValue(); | |
// Generate a Version 3 UUID based on the input text and namespace | |
UUID generatedUuid = generateUuidV3(inputText, namespaceText); | |
// Copy the output to the clipboard | |
clipboardManager.setClipboard(GLFW.glfwGetCurrentContext(), generatedUuid.toString()); | |
java.awt.Toolkit.getDefaultToolkit().beep(); | |
} | |
private UUID generateUuidV3(String inputText, String namespaceText) { | |
// Convert namespace string to UUID | |
UUID namespaceUUID = UUID.fromString(namespaceText); | |
// Convert input text to bytes | |
byte[] inputBytes = inputText.getBytes(StandardCharsets.UTF_8); | |
// Generate the UUID v3 based on the input text and the namespace UUID | |
return UUID.nameUUIDFromBytes(concatenateByteArrays(namespaceUUID, inputBytes)); | |
} | |
private byte[] concatenateByteArrays(UUID uuid, byte[] bytes) { | |
// Concatenate the UUID's bytes with the input bytes | |
ByteBuffer buffer = ByteBuffer.wrap(new byte[uuid.toString().getBytes(StandardCharsets.UTF_8).length + bytes.length]); | |
buffer.put(uuid.toString().getBytes(StandardCharsets.UTF_8)); | |
buffer.put(bytes); | |
return buffer.array(); | |
} | |
private void handleUuidV5Button(Button button) { | |
// Get the input text from the first EditBox (input text) | |
String inputText = this.editBox.getValue(); | |
// Get the namespace text from the second EditBox (namespace) | |
String namespaceText = this.editBox2.getValue(); | |
// Generate a Version 5 UUID based on the input text and namespace | |
UUID generatedUuid = generateUuidV5(inputText, namespaceText); | |
// Copy the output to the clipboard | |
clipboardManager.setClipboard(GLFW.glfwGetCurrentContext(), generatedUuid.toString()); | |
} | |
private UUID generateUuidV5(String inputText, String namespaceText) { | |
// Convert namespace string to UUID | |
UUID namespaceUUID = UUID.fromString(namespaceText); | |
// Convert input text to bytes | |
byte[] inputBytes = inputText.getBytes(StandardCharsets.UTF_8); | |
// Generate the UUID v5 based on the input text and the namespace UUID | |
return UUID.nameUUIDFromBytes(concatenateByteArrays(namespaceUUID, inputBytes)); | |
} | |
@Override | |
public boolean isPauseScreen() { | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment