Skip to content

Commit

Permalink
Port safe client access and tooltip key from tinkers
Browse files Browse the repository at this point in the history
  • Loading branch information
KnightMiner committed Jun 6, 2022
1 parent 220de4d commit 0edb280
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/main/java/slimeknights/mantle/client/SafeClientAccess.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package slimeknights.mantle.client;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.fml.loading.FMLEnvironment;

import javax.annotation.Nullable;

/** Class to add one level of static indirection to client only lookups */
public class SafeClientAccess {
/** Gets the currently pressed key for tooltips, returns UNKNOWN on a server */
public static TooltipKey getTooltipKey() {
if (FMLEnvironment.dist == Dist.CLIENT) {
return ClientOnly.getPressedKey();
}
return TooltipKey.UNKNOWN;
}

/** Gets the client player entity, or null on a server */
@Nullable
public static Player getPlayer() {
if (FMLEnvironment.dist == Dist.CLIENT) {
return ClientOnly.getClientPlayer();
}
return null;
}

/** Gets the client player entity, or null on a server */
@Nullable
public static Level getLevel() {
if (FMLEnvironment.dist == Dist.CLIENT) {
return ClientOnly.getClientLevel();
}
return null;
}

/** This class is only loaded on the client, so is safe to reference client only methods */
private static class ClientOnly {
/** Gets the currently pressed key modifier for tooltips */
public static TooltipKey getPressedKey() {
if (Screen.hasShiftDown()) {
return TooltipKey.SHIFT;
}
if (Screen.hasControlDown()) {
return TooltipKey.CONTROL;
}
if (Screen.hasAltDown()) {
return TooltipKey.ALT;
}
return TooltipKey.NORMAL;
}

/** Gets the client player instance */
@Nullable
public static Player getClientPlayer() {
return Minecraft.getInstance().player;
}

/** Gets the client level instance */
@Nullable
public static Level getClientLevel() {
return Minecraft.getInstance().level;
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/slimeknights/mantle/client/TooltipKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package slimeknights.mantle.client;

/** Options for which tooltip is being used on an item */
public enum TooltipKey {
/** Tooltip with neither shift nor control */
NORMAL,
/** Tooltip with shift held */
SHIFT,
/** Tooltip with control held */
CONTROL,
/** Tooltip with alt held */
ALT,
/** Tooltip key cannot be determined, typically caused by being on a server */
UNKNOWN;

/** Common operation is wanting to cancel when shift or unknown */
public boolean isShiftOrUnknown() {
return this == SHIFT || this == UNKNOWN;
}
}

0 comments on commit 0edb280

Please sign in to comment.