From da1c4aee35e62f344a4d8e14b0e2b6c8eabc6926 Mon Sep 17 00:00:00 2001 From: Pieter Date: Fri, 7 Aug 2026 13:08:16 +0200 Subject: [PATCH] Port more utils; Bump version to 2.0.1 --- gradle.properties | 2 +- .../utilize/client/utils/ClientUtils.java | 27 ++++++++++++++++ .../utilize/client/utils/NetworkUtils.java | 29 +++++++++++++++++ .../utilize/client/utils/SoundUtils.java | 29 +++++++++++++++++ .../utilize/client/utils/TextUtils.java | 32 +++++++++++++++++++ .../utilize/client/utils/WorldUtils.java | 31 ++++++++++++++++++ 6 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 src/client/java/nl/devpieter/utilize/client/utils/NetworkUtils.java create mode 100644 src/client/java/nl/devpieter/utilize/client/utils/SoundUtils.java create mode 100644 src/client/java/nl/devpieter/utilize/client/utils/TextUtils.java create mode 100644 src/client/java/nl/devpieter/utilize/client/utils/WorldUtils.java diff --git a/gradle.properties b/gradle.properties index d12076d..2f86d7c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,6 +6,6 @@ org.gradle.jvmargs=-Xmx1G loader_version=0.19.3 # Mod Properties -mod_version=2.0.0 +mod_version=2.0.1 maven_group=nl.devpieter archives_base_name=utilize \ No newline at end of file diff --git a/src/client/java/nl/devpieter/utilize/client/utils/ClientUtils.java b/src/client/java/nl/devpieter/utilize/client/utils/ClientUtils.java index 7d32ef4..ee68a45 100644 --- a/src/client/java/nl/devpieter/utilize/client/utils/ClientUtils.java +++ b/src/client/java/nl/devpieter/utilize/client/utils/ClientUtils.java @@ -2,7 +2,10 @@ import net.fabricmc.loader.api.FabricLoader; import net.minecraft.client.Minecraft; +import net.minecraft.client.multiplayer.ClientLevel; +import net.minecraft.client.multiplayer.ClientPacketListener; import net.minecraft.client.player.LocalPlayer; +import net.minecraft.client.sounds.SoundManager; public final class ClientUtils { @@ -24,4 +27,28 @@ public static boolean hasPlayer() { public static LocalPlayer getPlayer() { return getClient().player; } + + public static boolean hasLevel() { + return getClient().level != null; + } + + public static ClientLevel getLevel() { + return getClient().level; + } + + public static boolean hasConnection() { + return getClient().getConnection() != null; + } + + public static ClientPacketListener getConnection() { + return getClient().getConnection(); + } + + public static boolean hasSoundManager() { + return getClient().getSoundManager() != null; + } + + public static SoundManager getSoundManager() { + return getClient().getSoundManager(); + } } diff --git a/src/client/java/nl/devpieter/utilize/client/utils/NetworkUtils.java b/src/client/java/nl/devpieter/utilize/client/utils/NetworkUtils.java new file mode 100644 index 0000000..4cb027e --- /dev/null +++ b/src/client/java/nl/devpieter/utilize/client/utils/NetworkUtils.java @@ -0,0 +1,29 @@ +package nl.devpieter.utilize.client.utils; + +import net.minecraft.network.protocol.Packet; +import org.jetbrains.annotations.NotNull; + +public final class NetworkUtils { + + private NetworkUtils() { + } + + public static void sendPacket(@NotNull Packet packet) { + if (!ClientUtils.hasConnection()) return; + ClientUtils.getConnection().send(packet); + } + + public static void sendChatMessage(@NotNull String message) { + if (!ClientUtils.hasConnection()) return; + + if (message.startsWith("/")) sendChatCommand(message); + else ClientUtils.getConnection().sendChat(message); + } + + public static void sendChatCommand(@NotNull String command) { + if (!ClientUtils.hasConnection()) return; + if (command.startsWith("/")) command = command.substring(1); + + ClientUtils.getConnection().sendCommand(command); + } +} diff --git a/src/client/java/nl/devpieter/utilize/client/utils/SoundUtils.java b/src/client/java/nl/devpieter/utilize/client/utils/SoundUtils.java new file mode 100644 index 0000000..9f3de31 --- /dev/null +++ b/src/client/java/nl/devpieter/utilize/client/utils/SoundUtils.java @@ -0,0 +1,29 @@ +package nl.devpieter.utilize.client.utils; + +import net.minecraft.client.resources.sounds.SimpleSoundInstance; +import net.minecraft.client.resources.sounds.SoundInstance; +import net.minecraft.sounds.SoundEvent; +import org.jetbrains.annotations.NotNull; + +public final class SoundUtils { + + private SoundUtils() { + } + + public static void playUi(@NotNull SoundEvent soundEvent) { + playUi(soundEvent, 1.0F, 1.0F); + } + + public static void playUi(@NotNull SoundEvent soundEvent, float pitch) { + playUi(soundEvent, pitch, 1.0F); + } + + public static void playUi(@NotNull SoundEvent soundEvent, float pitch, float volume) { + play(SimpleSoundInstance.forUI(soundEvent, pitch, volume)); + } + + public static void play(@NotNull SoundInstance soundInstance) { + if (!ClientUtils.hasSoundManager()) return; + ClientUtils.getSoundManager().play(soundInstance); + } +} diff --git a/src/client/java/nl/devpieter/utilize/client/utils/TextUtils.java b/src/client/java/nl/devpieter/utilize/client/utils/TextUtils.java new file mode 100644 index 0000000..fd5a66f --- /dev/null +++ b/src/client/java/nl/devpieter/utilize/client/utils/TextUtils.java @@ -0,0 +1,32 @@ +package nl.devpieter.utilize.client.utils; + +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.MutableComponent; +import net.minecraft.network.chat.Style; +import org.jetbrains.annotations.NotNull; + +public final class TextUtils { + + private TextUtils() { + } + + public static MutableComponent currentOrDefaultStyle(@NotNull Component text, @NotNull Style defaultStyle) { + return Component.literal("").copy().setStyle(defaultStyle).append(text); + } + + public static MutableComponent withStyle(@NotNull String text, @NotNull Style style) { + return withStyle(Component.literal(text), style); + } + + public static MutableComponent withStyle(@NotNull Component text, @NotNull Style style) { + return text.copy().setStyle(style); + } + + public static MutableComponent clearStyle(@NotNull String text) { + return clearStyle(Component.literal(text)); + } + + public static MutableComponent clearStyle(@NotNull Component text) { + return text.copy().setStyle(Style.EMPTY); + } +} \ No newline at end of file diff --git a/src/client/java/nl/devpieter/utilize/client/utils/WorldUtils.java b/src/client/java/nl/devpieter/utilize/client/utils/WorldUtils.java new file mode 100644 index 0000000..4e17a4f --- /dev/null +++ b/src/client/java/nl/devpieter/utilize/client/utils/WorldUtils.java @@ -0,0 +1,31 @@ +package nl.devpieter.utilize.client.utils; + +import net.minecraft.core.BlockPos; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.state.BlockState; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public final class WorldUtils { + + private WorldUtils() { + } + + public static @Nullable BlockState getStateAt(@NotNull BlockPos pos) { + if (!ClientUtils.hasLevel()) return null; + return ClientUtils.getLevel().getBlockState(pos); + } + + public static @Nullable Block getBlockAt(@NotNull BlockPos pos) { + BlockState state = getStateAt(pos); + if (state == null) return null; + + return state.getBlock(); + } + + public static @Nullable Entity getEntity(int id) { + if (!ClientUtils.hasLevel()) return null; + return ClientUtils.getLevel().getEntity(id); + } +}