-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
bazke
committed
Oct 27, 2023
1 parent
1b32c91
commit cadd334
Showing
18 changed files
with
825 additions
and
3 deletions.
There are no files selected for viewing
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
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
6 changes: 6 additions & 0 deletions
6
src/generated/resources/assets/ltextras/models/item/tropical_map.json
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,6 @@ | ||
{ | ||
"parent": "minecraft:item/generated", | ||
"textures": { | ||
"layer0": "ltextras:item/tropical_map" | ||
} | ||
} |
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
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
30 changes: 30 additions & 0 deletions
30
src/main/java/com/lovetropics/extras/client/ClientMapPoiManager.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,30 @@ | ||
package com.lovetropics.extras.client; | ||
|
||
import com.lovetropics.extras.client.screen.map.TropicalMapScreen; | ||
import com.lovetropics.extras.data.poi.Poi; | ||
import net.minecraft.client.Minecraft; | ||
import net.minecraft.network.chat.Component; | ||
import net.minecraft.world.entity.player.Player; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class ClientMapPoiManager { | ||
private static final Map<String, Poi> POIS = new HashMap<>(); | ||
|
||
public static void updatePoi(final Poi poi, boolean delete) { | ||
if (delete) { | ||
POIS.remove(poi.name()); | ||
} else { | ||
POIS.put(poi.name(), poi); | ||
} | ||
} | ||
|
||
public static Map<String, Poi> getPois() { | ||
return POIS; | ||
} | ||
|
||
public static void openScreen(final Player player) { | ||
Minecraft.getInstance().setScreen(new TropicalMapScreen(Component.translatable("item.ltextras.tropical_map"), player)); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/lovetropics/extras/client/screen/map/InvisibleButton.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,15 @@ | ||
package com.lovetropics.extras.client.screen.map; | ||
|
||
import net.minecraft.client.gui.GuiGraphics; | ||
import net.minecraft.client.gui.components.Button; | ||
|
||
class InvisibleButton extends Button { | ||
protected InvisibleButton(final Builder builder) { | ||
super(builder); | ||
} | ||
|
||
@Override | ||
protected void renderWidget(final GuiGraphics guiGraphics, final int mouseX, final int mouseY, final float partialTick) { | ||
//Intentionally left blank | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/com/lovetropics/extras/client/screen/map/PoiImageButton.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,49 @@ | ||
package com.lovetropics.extras.client.screen.map; | ||
|
||
import com.lovetropics.extras.client.ClientMapPoiManager; | ||
import net.minecraft.client.gui.GuiGraphics; | ||
import net.minecraft.client.gui.components.ImageButton; | ||
import net.minecraft.resources.ResourceLocation; | ||
|
||
class PoiImageButton extends ImageButton { | ||
private final String poiName; | ||
private final boolean isGm; | ||
private boolean shouldRender; | ||
|
||
public PoiImageButton(String poiName, boolean isGm, int pX, int pY, int pWidth, int pHeight, int pXTexStart, int pYTexStart, int pYDiffTex, ResourceLocation pResourceLocation, int pTextureWidth, int pTextureHeight, OnPress pOnPress) { | ||
super(pX, pY, pWidth, pHeight, pXTexStart, pYTexStart, pYDiffTex, pResourceLocation, pTextureWidth, pTextureHeight, pOnPress); | ||
this.poiName = poiName; | ||
this.isGm = isGm; | ||
this.shouldRender = shouldRender(); | ||
} | ||
|
||
@Override | ||
public void render(GuiGraphics guiGraphics, int pMouseX, int pMouseY, float pPartialTick) { | ||
if (pPartialTick % 20 == 0) { | ||
shouldRender = shouldRender(); | ||
} | ||
|
||
if (shouldRender) { | ||
super.render(guiGraphics, pMouseX, pMouseY, pPartialTick); | ||
} | ||
} | ||
|
||
@Override | ||
public void renderWidget(GuiGraphics guiGraphics, int pMouseX, int pMouseY, float pPartialTick) { | ||
if (shouldRender) { | ||
super.renderWidget(guiGraphics, pMouseX, pMouseY, pPartialTick); | ||
} | ||
} | ||
|
||
@Override | ||
public void renderTexture(GuiGraphics guiGraphics, ResourceLocation pTexture, int pX, int pY, int pUOffset, int pVOffset, int p_283472_, int pWidth, int pHeight, int pTextureWidth, int pTextureHeight) { | ||
if (shouldRender) { | ||
super.renderTexture(guiGraphics, pTexture, pX, pY, pUOffset, pVOffset, p_283472_, pWidth, pHeight, pTextureWidth, pTextureHeight); | ||
} | ||
} | ||
|
||
private boolean shouldRender() { | ||
return ClientMapPoiManager.getPois().get(poiName).enabled() || isGm; | ||
} | ||
|
||
} |
111 changes: 111 additions & 0 deletions
111
src/main/java/com/lovetropics/extras/client/screen/map/TropicalMapScreen.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,111 @@ | ||
package com.lovetropics.extras.client.screen.map; | ||
|
||
import com.lovetropics.extras.client.ClientMapPoiManager; | ||
import com.lovetropics.extras.data.poi.MapPoiManager; | ||
import com.lovetropics.extras.data.poi.Poi; | ||
import com.mojang.blaze3d.vertex.PoseStack; | ||
import net.minecraft.client.gui.GuiGraphics; | ||
import net.minecraft.client.gui.components.Button; | ||
import net.minecraft.client.gui.screens.Screen; | ||
import net.minecraft.client.player.LocalPlayer; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.network.chat.Component; | ||
import net.minecraft.network.chat.MutableComponent; | ||
import net.minecraft.network.chat.TextColor; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.world.entity.player.Player; | ||
import org.apache.commons.lang3.tuple.Pair; | ||
|
||
public class TropicalMapScreen extends Screen { | ||
private static final int ICON_SIZE = 8; | ||
private static final int FONT_SCALING = 2; | ||
private static final int MAP_PNG_HEIGHT = 256; | ||
private static final int MAP_PNG_WIDTH = 256; | ||
private static final ResourceLocation MAP_LOCATION = new ResourceLocation("ltextras", "textures/map.png"); | ||
private final Player player; | ||
|
||
public TropicalMapScreen(final Component title, final Player player) { | ||
super(title); | ||
this.player = player; | ||
} | ||
|
||
@Override | ||
protected void init() { | ||
super.init(); | ||
int xOffset = (this.width / 2) - (MAP_PNG_WIDTH / 2); | ||
int yOffset = (this.height / 2) - (MAP_PNG_HEIGHT / 2); | ||
|
||
for (Poi mapPoi : ClientMapPoiManager.getPois().values()) { | ||
final BlockPos pos = mapPoi.globalPos().pos(); | ||
final Pair<Integer, Integer> poiPos = getPoiPos(pos); | ||
final int screenX = poiPos.getLeft() + xOffset; | ||
final int screenY = poiPos.getRight() + yOffset; | ||
|
||
addRenderableWidget(new PoiImageButton(mapPoi.name(), player.canUseGameMasterBlocks(), screenX, screenY, ICON_SIZE, ICON_SIZE, 0, 0, 0, | ||
mapPoi.resourceLocation(), ICON_SIZE, ICON_SIZE, | ||
b -> doWarp(mapPoi))); | ||
|
||
//This adds an invisible area where the text is so not just the icon is clickable | ||
int textWidthClickableArea = this.font.width(mapPoi.description()); | ||
addRenderableWidget(new InvisibleButton(Button.builder(mapPoi.description(), b -> doWarp(mapPoi)) | ||
.size(textWidthClickableArea + ICON_SIZE, ICON_SIZE) | ||
.pos(screenX, screenY))); | ||
} | ||
} | ||
|
||
private Pair<Integer, Integer> getPoiPos(final BlockPos blockPos) { | ||
final int mapWidth = MapPoiManager.MAP_BB.maxX() - MapPoiManager.MAP_BB.minX(); | ||
final int mapHeight = MapPoiManager.MAP_BB.maxZ() - MapPoiManager.MAP_BB.minZ(); | ||
final int screenX = (blockPos.getX() - MapPoiManager.MAP_BB.minX()) * MAP_PNG_WIDTH / mapWidth; | ||
final int screenY = (blockPos.getZ() - MapPoiManager.MAP_BB.minZ()) * MAP_PNG_HEIGHT / mapHeight; | ||
return Pair.of(screenX, screenY); | ||
} | ||
|
||
@Override | ||
public void render(final GuiGraphics guiGraphics, final int pMouseX, final int pMouseY, final float pPartialTick) { | ||
renderBackground(guiGraphics); | ||
super.render(guiGraphics, pMouseX, pMouseY, pPartialTick); | ||
|
||
final int xOffset = (this.width / 2) - (MAP_PNG_WIDTH / 2); | ||
final int yOffset = (this.height / 2) - (MAP_PNG_HEIGHT / 2); | ||
|
||
//Dont think this is a great way to scale... just wanted the text a bit smaller :) | ||
PoseStack pose = guiGraphics.pose(); | ||
pose.pushPose(); | ||
pose.scale(1.0f / FONT_SCALING, 1.0f / FONT_SCALING, 1.0f / FONT_SCALING); | ||
for (Poi mapPoi : ClientMapPoiManager.getPois().values()) { | ||
final BlockPos pos = mapPoi.globalPos().pos(); | ||
final Pair<Integer, Integer> poiPos = getPoiPos(pos); | ||
final int screenX = poiPos.getLeft() + xOffset; | ||
final int screenY = poiPos.getRight() + yOffset; | ||
|
||
Component mapComponent = mapPoi.description(); | ||
if (!mapPoi.enabled() && !player.canUseGameMasterBlocks()) { | ||
continue; | ||
} else if (!mapPoi.enabled() && player.canUseGameMasterBlocks() && mapComponent instanceof final MutableComponent mc) { | ||
mapComponent = mc.copy().append(" [DISABLED]"); | ||
} | ||
final TextColor textColor = mapComponent.getStyle().getColor(); | ||
final int color = textColor == null ? 0xFFFFFF : textColor.getValue(); | ||
|
||
guiGraphics.drawString(this.font, mapComponent, (screenX + ICON_SIZE) * FONT_SCALING, | ||
(screenY + (this.font.lineHeight / 4)) * FONT_SCALING, color); | ||
|
||
} | ||
pose.popPose(); | ||
} | ||
|
||
@Override | ||
public void renderBackground(final GuiGraphics guiGraphics) { | ||
int h = (this.height - MAP_PNG_HEIGHT) / 2; | ||
int w = (this.width - MAP_PNG_WIDTH) / 2; | ||
|
||
guiGraphics.blit(MAP_LOCATION, w, h, 0, 0.0F, 0.0F, MAP_PNG_WIDTH, MAP_PNG_HEIGHT, MAP_PNG_WIDTH, MAP_PNG_HEIGHT); | ||
} | ||
|
||
private void doWarp(Poi mapPoi) { | ||
if (player instanceof final LocalPlayer localPlayer) { | ||
localPlayer.connection.sendUnsignedCommand("warp " + mapPoi.name()); | ||
} | ||
} | ||
} |
Oops, something went wrong.