Skip to content

Commit

Permalink
Added map item, /poi, and /warp
Browse files Browse the repository at this point in the history
  • Loading branch information
bazke committed Oct 27, 2023
1 parent 1b32c91 commit cadd334
Show file tree
Hide file tree
Showing 18 changed files with 825 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/generated/resources/assets/ltextras/lang/en_ud.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@
"commands.tpa.request_sent": "ʇuǝs ʇsǝnbǝᴚ",
"commands.tpa.too_much": "ɥɔnɯ ooʇ ʇɐɥʇ buıop ǝɹɐ noʎ",
"commands.tpa.tp_accepted": "uɹnʇǝɹ oʇ ʞɔɐq/ ǝs∩ ˙ʇsǝnbǝɹ ʇɹodǝןǝʇ ɹnoʎ pǝʇdǝɔɔɐ sɐɥ %s",
"commands.warp.general_error": "ǝɹǝɥʇ ob ʇ,upןnoƆ",
"commands.warp.not_found": "punoɟ ʇou uoıʇɐuıʇsǝᗡ",
"commands.warp.success": "%s oʇ pǝdɹɐM",
"effect.ltextras.fish_eye": "ǝʎƎ ɥsıℲ",
"entity.ltextras.collectible": "ǝןqıʇɔǝןןoƆ",
"entity.ltextras.hologram": "ɯɐɹboןoH",
Expand All @@ -143,6 +146,7 @@
"item.ltextras.entity_wand": "puɐM ʎʇıʇuƎ",
"item.ltextras.image": "ǝbɐɯI",
"item.ltextras.invite": "ǝʇıʌuI",
"item.ltextras.tropical_map": "dɐW ןɐɔıdoɹ⟘",
"item.ltextras.tropicoin": "uıoƆıdoɹ⟘",
"itemGroup.ltextras.ltextras": "sɐɹʇxƎ⟘Ꞁ",
"lt.collectibles.unicode.milestone": "",
Expand Down
4 changes: 4 additions & 0 deletions src/generated/resources/assets/ltextras/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@
"commands.tpa.request_sent": "Request sent",
"commands.tpa.too_much": "You are doing that too much",
"commands.tpa.tp_accepted": "%s has accepted your teleport request. Use /back to return",
"commands.warp.general_error": "Couldn't go there",
"commands.warp.not_found": "Destination not found",
"commands.warp.success": "Warped to %s",
"effect.ltextras.fish_eye": "Fish Eye",
"entity.ltextras.collectible": "Collectible",
"entity.ltextras.hologram": "Hologram",
Expand All @@ -143,6 +146,7 @@
"item.ltextras.entity_wand": "Entity Wand",
"item.ltextras.image": "Image",
"item.ltextras.invite": "Invite",
"item.ltextras.tropical_map": "Tropical Map",
"item.ltextras.tropicoin": "TropiCoin",
"itemGroup.ltextras.ltextras": "LTExtras",
"lt.collectibles.unicode.milestone": "",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/generated",
"textures": {
"layer0": "ltextras:item/tropical_map"
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/lovetropics/extras/ExtraItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public class ExtraItems {
})
.register();

public static final ItemEntry<TropicMapItem> TROPICAL_MAP = REGISTRATE.item("tropical_map", TropicMapItem::new)
.initialProperties(() -> new Item.Properties().stacksTo(1))
.defaultModel()
.register();

public static void init() {
}
}
7 changes: 4 additions & 3 deletions src/main/java/com/lovetropics/extras/LTExtras.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import com.lovetropics.extras.client.particle.ExtraParticles;
import com.lovetropics.extras.collectible.CollectibleCommand;
import com.lovetropics.extras.collectible.CollectibleStore;
import com.lovetropics.extras.command.GenerateCommand;
import com.lovetropics.extras.command.SetMaxPlayersCommand;
import com.lovetropics.extras.command.*;
import com.lovetropics.extras.data.spawnitems.SpawnItemsCommand;
import com.lovetropics.extras.data.spawnitems.SpawnItemsStore;
import com.lovetropics.extras.command.TpCommand;
import com.lovetropics.extras.effect.ExtraEffects;
import com.lovetropics.extras.entity.ExtraEntities;
import com.lovetropics.extras.network.LTExtrasNetwork;
Expand Down Expand Up @@ -101,6 +99,7 @@ public LTExtras() {
p.add("lt.collectibles.unicode.purchased", "\uE010");

TpCommand.addTranslations(p);
WarpCommand.addTranslations(p);
})
.generic(TAB_ID.getPath(), Registries.CREATIVE_MODE_TAB, () -> CreativeModeTab.builder()
.title(registrate().addLang("itemGroup", TAB_ID, "LTExtras"))
Expand Down Expand Up @@ -150,6 +149,8 @@ private void onRegisterCommands(RegisterCommandsEvent event) {
SpawnItemsCommand.register(dispatcher);
TpCommand.register(dispatcher);
WorldEffectCommand.register(dispatcher);
WarpCommand.register(dispatcher);
PoiCommand.register(dispatcher);
}

@OnlyIn(Dist.CLIENT)
Expand Down
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));
}
}
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
}
}
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;
}

}
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());
}
}
}
Loading

0 comments on commit cadd334

Please sign in to comment.