-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port safe client access and tooltip key from tinkers
- Loading branch information
1 parent
220de4d
commit 0edb280
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
src/main/java/slimeknights/mantle/client/SafeClientAccess.java
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
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; | ||
} | ||
} | ||
} |
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
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; | ||
} | ||
} |