From 400bd30251c5524262b7d6ea9f6550f3520ba574 Mon Sep 17 00:00:00 2001 From: raoulvdberge Date: Sat, 10 Aug 2024 13:34:09 +0200 Subject: [PATCH] refactor: move security toast helper to client helper Rename PlatformUtil to ClientPlatformUtil as that's what it does. --- .../common/autocrafting/PatternItem.java | 4 ++-- .../common/autocrafting/PatternRendering.java | 4 ++-- .../SmithingTablePatternGridRenderer.java | 4 ++-- .../StonecutterPatternGridRenderer.java | 6 +++--- .../support/packet/s2c/NoPermissionPacket.java | 4 ++-- ...ityToastUtil.java => ClientPlatformUtil.java} | 16 +++++++++++++--- .../refinedstorage/common/util/PlatformUtil.java | 16 ---------------- .../portablegrid/PortableGridBakedModel.java | 4 ++-- 8 files changed, 26 insertions(+), 32 deletions(-) rename refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/util/{SecurityToastUtil.java => ClientPlatformUtil.java} (57%) delete mode 100644 refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/util/PlatformUtil.java diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/PatternItem.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/PatternItem.java index 522a54053..dbb5295d8 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/PatternItem.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/PatternItem.java @@ -10,7 +10,7 @@ import com.refinedmods.refinedstorage.common.content.Items; import com.refinedmods.refinedstorage.common.support.RecipeMatrixContainer; import com.refinedmods.refinedstorage.common.support.resource.ItemResource; -import com.refinedmods.refinedstorage.common.util.PlatformUtil; +import com.refinedmods.refinedstorage.common.util.ClientPlatformUtil; import java.util.ArrayList; import java.util.Arrays; @@ -86,7 +86,7 @@ public Optional getTooltipImage(final ItemStack stack) { if (state == null) { return Optional.of(new HelpTooltipComponent(HELP)); } - final Level level = PlatformUtil.getClientLevel(); + final Level level = ClientPlatformUtil.getClientLevel(); return switch (state.type()) { case CRAFTING -> { final CraftingPatternState craftingState = stack.get(DataComponents.INSTANCE.getCraftingPatternState()); diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/PatternRendering.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/PatternRendering.java index 3c8108885..8f4ccc83e 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/PatternRendering.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/PatternRendering.java @@ -3,7 +3,7 @@ import com.refinedmods.refinedstorage.common.api.RefinedStorageApi; import com.refinedmods.refinedstorage.common.api.autocrafting.PatternProviderItem; import com.refinedmods.refinedstorage.common.support.resource.ItemResource; -import com.refinedmods.refinedstorage.common.util.PlatformUtil; +import com.refinedmods.refinedstorage.common.util.ClientPlatformUtil; import java.util.Optional; @@ -32,7 +32,7 @@ public static boolean canDisplayOutput(final ItemStack stack) { } public static Optional getOutput(final ItemStack stack) { - final Level level = PlatformUtil.getClientLevel(); + final Level level = ClientPlatformUtil.getClientLevel(); if (level == null) { return Optional.empty(); } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/SmithingTablePatternGridRenderer.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/SmithingTablePatternGridRenderer.java index 9628d7568..9008c9790 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/SmithingTablePatternGridRenderer.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/SmithingTablePatternGridRenderer.java @@ -1,6 +1,6 @@ package com.refinedmods.refinedstorage.common.autocrafting; -import com.refinedmods.refinedstorage.common.util.PlatformUtil; +import com.refinedmods.refinedstorage.common.util.ClientPlatformUtil; import java.util.List; import java.util.Optional; @@ -62,7 +62,7 @@ class SmithingTablePatternGridRenderer implements PatternGridRenderer { @Override public void addWidgets(final Consumer widgets, final Consumer renderables) { - final Level level = PlatformUtil.getClientLevel(); + final Level level = ClientPlatformUtil.getClientLevel(); if (level == null) { return; } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/StonecutterPatternGridRenderer.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/StonecutterPatternGridRenderer.java index cb7551b3c..b7b5beb04 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/StonecutterPatternGridRenderer.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/autocrafting/StonecutterPatternGridRenderer.java @@ -1,7 +1,7 @@ package com.refinedmods.refinedstorage.common.autocrafting; import com.refinedmods.refinedstorage.common.support.widget.ScrollbarWidget; -import com.refinedmods.refinedstorage.common.util.PlatformUtil; +import com.refinedmods.refinedstorage.common.util.ClientPlatformUtil; import java.util.function.Consumer; import javax.annotation.Nullable; @@ -125,7 +125,7 @@ public void renderBackground(final GuiGraphics graphics, } graphics.blitSprite(buttonSprite, xx, yy, 16, 18); graphics.renderItem( - recipe.value().getResultItem(requireNonNull(PlatformUtil.getClientLevel()).registryAccess()), + recipe.value().getResultItem(requireNonNull(ClientPlatformUtil.getClientLevel()).registryAccess()), xx, yy + 1 ); @@ -145,7 +145,7 @@ public void renderTooltip(final Font font, for (int i = 0; i < menu.getStonecutterRecipes().size(); ++i) { final RecipeHolder recipe = menu.getStonecutterRecipes().get(i); final ItemStack result = recipe.value().getResultItem( - requireNonNull(PlatformUtil.getClientLevel()).registryAccess() + requireNonNull(ClientPlatformUtil.getClientLevel()).registryAccess() ); final int xx = getRecipeX(x, i); final int row = i / VanillaConstants.STONECUTTER_RECIPES_PER_ROW; diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/s2c/NoPermissionPacket.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/s2c/NoPermissionPacket.java index 0e2908da0..3c63a0f49 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/s2c/NoPermissionPacket.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/support/packet/s2c/NoPermissionPacket.java @@ -1,6 +1,6 @@ package com.refinedmods.refinedstorage.common.support.packet.s2c; -import com.refinedmods.refinedstorage.common.util.SecurityToastUtil; +import com.refinedmods.refinedstorage.common.util.ClientPlatformUtil; import net.minecraft.network.RegistryFriendlyByteBuf; import net.minecraft.network.chat.Component; @@ -18,7 +18,7 @@ public record NoPermissionPacket(Component component) implements CustomPacketPay ); public static void handle(final NoPermissionPacket packet) { - SecurityToastUtil.addNoPermissionToast(packet.component); + ClientPlatformUtil.addNoPermissionToast(packet.component); } @Override diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/util/SecurityToastUtil.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/util/ClientPlatformUtil.java similarity index 57% rename from refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/util/SecurityToastUtil.java rename to refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/util/ClientPlatformUtil.java index a92ef1557..72bf0d0dd 100644 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/util/SecurityToastUtil.java +++ b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/util/ClientPlatformUtil.java @@ -1,22 +1,32 @@ package com.refinedmods.refinedstorage.common.util; +import javax.annotation.Nullable; + import net.minecraft.client.Minecraft; import net.minecraft.client.gui.components.toasts.SystemToast; import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.MutableComponent; +import net.minecraft.world.level.Level; import static com.refinedmods.refinedstorage.common.util.IdentifierUtil.createTranslation; -public final class SecurityToastUtil { +public final class ClientPlatformUtil { private static final SystemToast.SystemToastId NO_PERMISSION_TOAST_ID = new SystemToast.SystemToastId(); + private static final MutableComponent NO_PERMISSION = createTranslation("misc", "no_permission"); + + private ClientPlatformUtil() { + } - private SecurityToastUtil() { + @Nullable + public static Level getClientLevel() { // avoids classloading issues + return Minecraft.getInstance().level; } public static void addNoPermissionToast(final Component message) { SystemToast.add( Minecraft.getInstance().getToasts(), NO_PERMISSION_TOAST_ID, - createTranslation("misc", "no_permission"), + NO_PERMISSION, message ); } diff --git a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/util/PlatformUtil.java b/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/util/PlatformUtil.java deleted file mode 100644 index 24072c78e..000000000 --- a/refinedstorage-common/src/main/java/com/refinedmods/refinedstorage/common/util/PlatformUtil.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.refinedmods.refinedstorage.common.util; - -import javax.annotation.Nullable; - -import net.minecraft.client.Minecraft; -import net.minecraft.world.level.Level; - -public final class PlatformUtil { - private PlatformUtil() { - } - - @Nullable - public static Level getClientLevel() { // avoids classloading issues - return Minecraft.getInstance().level; - } -} diff --git a/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/storage/portablegrid/PortableGridBakedModel.java b/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/storage/portablegrid/PortableGridBakedModel.java index 2c490a217..e0e3658d9 100644 --- a/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/storage/portablegrid/PortableGridBakedModel.java +++ b/refinedstorage-fabric/src/main/java/com/refinedmods/refinedstorage/fabric/storage/portablegrid/PortableGridBakedModel.java @@ -6,7 +6,7 @@ import com.refinedmods.refinedstorage.common.storage.portablegrid.PortableGridBlockItem; import com.refinedmods.refinedstorage.common.storage.portablegrid.PortableGridBlockItemRenderInfo; import com.refinedmods.refinedstorage.common.support.direction.BiDirection; -import com.refinedmods.refinedstorage.common.util.PlatformUtil; +import com.refinedmods.refinedstorage.common.util.ClientPlatformUtil; import com.refinedmods.refinedstorage.fabric.support.render.QuadRotators; import com.refinedmods.refinedstorage.fabric.support.render.QuadTranslator; @@ -56,7 +56,7 @@ public boolean isVanillaAdapter() { public void emitItemQuads(final ItemStack stack, final Supplier randomSupplier, final RenderContext context) { - final Level level = PlatformUtil.getClientLevel(); + final Level level = ClientPlatformUtil.getClientLevel(); if (level == null) { return; }