Skip to content

Commit

Permalink
refactor: move security toast helper to client helper
Browse files Browse the repository at this point in the history
Rename PlatformUtil to ClientPlatformUtil
as that's what it does.
  • Loading branch information
raoulvdberge committed Aug 10, 2024
1 parent a78224c commit 400bd30
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -86,7 +86,7 @@ public Optional<TooltipComponent> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -32,7 +32,7 @@ public static boolean canDisplayOutput(final ItemStack stack) {
}

public static Optional<ItemStack> getOutput(final ItemStack stack) {
final Level level = PlatformUtil.getClientLevel();
final Level level = ClientPlatformUtil.getClientLevel();
if (level == null) {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -62,7 +62,7 @@ class SmithingTablePatternGridRenderer implements PatternGridRenderer {
@Override
public void addWidgets(final Consumer<AbstractWidget> widgets,
final Consumer<AbstractWidget> renderables) {
final Level level = PlatformUtil.getClientLevel();
final Level level = ClientPlatformUtil.getClientLevel();
if (level == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
);
Expand All @@ -145,7 +145,7 @@ public void renderTooltip(final Font font,
for (int i = 0; i < menu.getStonecutterRecipes().size(); ++i) {
final RecipeHolder<StonecutterRecipe> 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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -56,7 +56,7 @@ public boolean isVanillaAdapter() {
public void emitItemQuads(final ItemStack stack,
final Supplier<RandomSource> randomSupplier,
final RenderContext context) {
final Level level = PlatformUtil.getClientLevel();
final Level level = ClientPlatformUtil.getClientLevel();
if (level == null) {
return;
}
Expand Down

0 comments on commit 400bd30

Please sign in to comment.