Skip to content

Commit

Permalink
Calculate screen position from blockpos, implement a simpler /poi add
Browse files Browse the repository at this point in the history
  • Loading branch information
bazke committed Oct 27, 2023
1 parent fbd4d85 commit cc8c827
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 66 deletions.
46 changes: 17 additions & 29 deletions src/main/java/com/lovetropics/extras/command/PoiCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
.then(literal("add")
.then(argument("name", word())
.then(argument("description", textComponent())
.then(argument("x", integer())
.then(argument("y", integer())
.then(argument("icon", string())
.executes(PoiCommand::addWithDefaults)
.then(argument("blockpos", blockPos())
.then(argument("enabled", bool())
.executes(PoiCommand::add)
))))))))); //lol
)))))));

dispatcher.register(literal(COMMAND_BASE)
.requires(source -> source.hasPermission(Commands.LEVEL_GAMEMASTERS))
Expand Down Expand Up @@ -80,18 +79,14 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
.suggests((ctx, builder) -> suggest(suggestName(ctx), builder))
.then(argument("description", textComponent())
.suggests((ctx, builder) -> suggest(suggestDescription(ctx), builder))
.then(argument("x", integer())
.suggests((ctx, builder) -> suggest(suggestX(ctx), builder))
.then(argument("y", integer())
.suggests((ctx, builder) -> suggest(suggestY(ctx), builder))
.then(argument("icon", string())
.suggests((ctx, builder) -> suggest(suggestIcon(ctx), builder))
.then(argument("blockpos", blockPos())
.suggests((ctx, builder) -> suggest(suggestGlobalPos(ctx), builder)) //TODO how to suggest blockpos?
.then(argument("enabled", bool())
.suggests((ctx, builder) -> suggest(suggestEnabled(ctx), builder))
.executes(PoiCommand::edit)
)))))))));
)))))));

dispatcher.register(literal(COMMAND_BASE)
.requires(source -> source.hasPermission(Commands.LEVEL_GAMEMASTERS))
Expand Down Expand Up @@ -151,6 +146,19 @@ private static int add(CommandContext<CommandSourceStack> ctx) {
return Command.SINGLE_SUCCESS;
}

private static int addWithDefaults(CommandContext<CommandSourceStack> ctx) {
final String name = StringArgumentType.getString(ctx, "name");
final Component description = ComponentArgument.getComponent(ctx, "description");
final String icon = ctx.getArgument("icon", String.class);
final GlobalPos globalPos = GlobalPos.of(ctx.getSource().getLevel().dimension(), ctx.getSource().getPlayer().getOnPos().above());
final boolean enabled = false;

final Poi newPoi = new Poi(name, description, new ResourceLocation(icon), globalPos, enabled);
MapPoiManager.get(ctx.getSource().getServer()).add(newPoi);
ctx.getSource().sendSuccess(() -> Component.literal("Added new disabled POI " + newPoi.name() + " at your current position"), false);
return Command.SINGLE_SUCCESS;
}

private static int enable(CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {
final String name = ctx.getArgument("name", String.class);
if (!MapPoiManager.get(ctx.getSource().getServer()).enable(name)) {
Expand Down Expand Up @@ -222,14 +230,12 @@ private static Stream<String> suggestEnabled(CommandContext<CommandSourceStack>
private static Poi createPoiFromCtx(CommandContext<CommandSourceStack> ctx) {
final String name = StringArgumentType.getString(ctx, "name");
final Component description = ComponentArgument.getComponent(ctx, "description");
final Integer x = ctx.getArgument("x", Integer.class);
final Integer y = ctx.getArgument("y", Integer.class);
final String icon = ctx.getArgument("icon", String.class);
final WorldCoordinates worldCoordinates = ctx.getArgument("blockpos", WorldCoordinates.class);
final GlobalPos globalPos = GlobalPos.of(ctx.getSource().getLevel().dimension(), worldCoordinates.getBlockPos(ctx.getSource()));
final Boolean enabled = ctx.getArgument("enabled", Boolean.class);

return new Poi(name, description, x, y, new ResourceLocation(icon), globalPos, enabled);
return new Poi(name, description, new ResourceLocation(icon), globalPos, enabled);
}

private static Stream<String> suggestGlobalPos(CommandContext<CommandSourceStack> ctx) {
Expand All @@ -249,22 +255,4 @@ private static Stream<String> suggestIcon(CommandContext<CommandSourceStack> ctx
}
return Stream.empty();
}

private static Stream<String> suggestY(CommandContext<CommandSourceStack> ctx) {
final String name = ctx.getArgument("name", String.class);
Poi poi = MapPoiManager.get(ctx.getSource().getServer()).getPoi(name);
if (poi != null) {
return Stream.of(String.valueOf(poi.screenY()));
}
return Stream.empty();
}

private static Stream<String> suggestX(CommandContext<CommandSourceStack> ctx) {
final String name = ctx.getArgument("name", String.class);
Poi poi = MapPoiManager.get(ctx.getSource().getServer()).getPoi(name);
if (poi != null) {
return Stream.of(String.valueOf(poi.screenX()));
}
return Stream.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.minecraft.nbt.NbtOps;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.levelgen.structure.BoundingBox;
import net.minecraft.world.level.saveddata.SavedData;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
Expand All @@ -26,6 +27,8 @@
@Mod.EventBusSubscriber(modid = LTExtras.MODID)
public class MapPoiManager extends SavedData {

//This is used to calculate where to draw POIs on the map item itself. Update when map.png is updated.
public static final BoundingBox MAP_BB = new BoundingBox(2013, 0, 1883, 2910, 0, 2799);
private static final Codec<Map<String, Poi>> CODEC = Codec.unboundedMap(Codec.STRING, Poi.CODEC);
private static final String STORAGE_ID = LTExtras.MODID + "_map_poi";
private final Map<String, Poi> pois;
Expand Down Expand Up @@ -127,6 +130,7 @@ private void updateClients(final Poi poi) {

public void remove(String name) {
final Poi poi = pois.remove(name);
setDirty();
LTExtrasNetwork.CHANNEL.send(PacketDistributor.ALL.noArg(), new ClientboundPoiPacket(poi, true));
//TODO: remove+do something with networking to remove from clients
}
Expand Down
18 changes: 0 additions & 18 deletions src/main/java/com/lovetropics/extras/data/poi/Poi.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,25 @@ public final class Poi {
public static final Codec<Poi> CODEC = RecordCodecBuilder.create(i -> i.group(
Codec.STRING.fieldOf("name").forGetter(Poi::name),
ExtraCodecs.COMPONENT.fieldOf("description").forGetter(Poi::description),
Codec.INT.fieldOf("x").forGetter(Poi::screenX),
Codec.INT.fieldOf("y").forGetter(Poi::screenY),
ResourceLocation.CODEC.fieldOf("resourceLocation").forGetter(Poi::resourceLocation),
GlobalPos.CODEC.fieldOf("blockPos").forGetter(Poi::globalPos),
Codec.BOOL.fieldOf("enabled").forGetter(Poi::enabled)
).apply(i, Poi::new));

private final String name;
private final Component description;
private final int screenX;
private final int screenY;
private final ResourceLocation resourceLocation;
private final GlobalPos globalPos;
private boolean enabled;

public Poi(
String name,
Component description,
int screenX,
int screenY,
ResourceLocation resourceLocation,
GlobalPos globalPos,
boolean enabled) {
this.name = name;
this.description = description;
this.screenX = screenX;
this.screenY = screenY;
this.resourceLocation = resourceLocation;
this.globalPos = globalPos;
this.enabled = enabled;
Expand Down Expand Up @@ -67,14 +59,6 @@ public Component description() {
return description;
}

public int screenX() {
return screenX;
}

public int screenY() {
return screenY;
}

public ResourceLocation resourceLocation() {
return resourceLocation;
}
Expand All @@ -92,8 +76,6 @@ public String toString() {
return "Poi[" +
"name=" + name + ", " +
"description=" + description + ", " +
"x=" + screenX + ", " +
"y=" + screenY + ", " +
"resourceLocation=" + resourceLocation + ", " +
"globalPos=" + globalPos + ", " +
"enabled=" + enabled + ']';
Expand Down
52 changes: 37 additions & 15 deletions src/main/java/com/lovetropics/extras/item/TropicMapItem.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.lovetropics.extras.item;

import com.lovetropics.extras.data.poi.MapPoiManager;
import com.lovetropics.extras.data.poi.Poi;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.Minecraft;
Expand All @@ -8,6 +9,7 @@
import net.minecraft.client.gui.components.ImageButton;
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;
Expand All @@ -18,6 +20,7 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.MapItem;
import net.minecraft.world.level.Level;
import org.apache.commons.lang3.tuple.Pair;

import java.util.*;

Expand All @@ -28,6 +31,8 @@ public TropicMapItem(Properties pProperties) {

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;
public static Map<String, Poi> pois = new HashMap<>();

public static void updatePoi(final Poi poi, boolean delete) {
Expand All @@ -52,66 +57,83 @@ public static class TropicalMapScreen extends Screen {

protected TropicalMapScreen(final Component title, final Player player) {
super(title);
this.height = 256;
this.width = 256;
this.height = MAP_PNG_HEIGHT;
this.width = MAP_PNG_WIDTH;
this.player = player;
}

@Override
protected void init() {
super.init();

int x = (this.width / 2) - (256 / 2); //This is for a 256x256 png
int y = (this.height / 2) - (256 / 2);
int xOffset = (this.width / 2) - (MAP_PNG_WIDTH / 2);
int yOffset = (this.height / 2) - (MAP_PNG_HEIGHT / 2);

for (Poi mapPoi : pois.values()) {
addRenderableWidget(new PoiImageButton(mapPoi.name(), player.canUseGameMasterBlocks(), x + mapPoi.screenX(), y + mapPoi.screenY(), ICON_SIZE, ICON_SIZE, 0, 0, 0,
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(x + mapPoi.screenX(), y + mapPoi.screenY())));
.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);

int x = (this.width / 2) - (256 / 2);
int y = (this.height / 2) - (256 / 2);
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 : pois.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, (x + mapPoi.screenX() + ICON_SIZE) * FONT_SCALING,
(y + mapPoi.screenY() + (this.font.lineHeight / 4)) * FONT_SCALING, color);

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 - 256) / 2;
int w = (this.width - 256) / 2;
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, 256, 256, 256, 256);
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ public record ClientboundPoiPacket(Poi poi, boolean delete) {
public ClientboundPoiPacket(final FriendlyByteBuf input) {
this(new Poi(input.readUtf(50),
input.readComponent(),
input.readVarInt(),
input.readVarInt(),
new ResourceLocation(input.readUtf(50)),
input.readGlobalPos(),
input.readBoolean()),
Expand All @@ -23,8 +21,6 @@ public ClientboundPoiPacket(final FriendlyByteBuf input) {
public void write(final FriendlyByteBuf output) {
output.writeUtf(poi.name(), 50);
output.writeComponent(poi.description());
output.writeVarInt(poi.screenX());
output.writeVarInt(poi.screenY());
output.writeUtf(poi.resourceLocation().toString(), 50);
output.writeGlobalPos(poi.globalPos());
output.writeBoolean(poi.enabled());
Expand Down
Binary file removed src/main/resources/assets/ltextras/textures/map.png
Binary file not shown.

0 comments on commit cc8c827

Please sign in to comment.