From 8488bf569769730d041f6714f837b077330062eb Mon Sep 17 00:00:00 2001 From: bazke Date: Fri, 27 Oct 2023 10:56:14 +0200 Subject: [PATCH] hide things when disabled --- .../extras/command/WarpCommand.java | 10 +++- .../extras/item/TropicMapItem.java | 55 +++++++++++++++++-- 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/lovetropics/extras/command/WarpCommand.java b/src/main/java/com/lovetropics/extras/command/WarpCommand.java index 481e556f..a6a937d8 100644 --- a/src/main/java/com/lovetropics/extras/command/WarpCommand.java +++ b/src/main/java/com/lovetropics/extras/command/WarpCommand.java @@ -30,6 +30,7 @@ public class WarpCommand { private static final int COOLDOWN_TICKS = SharedConstants.TICKS_PER_SECOND * 30; private static final Duration COOLDOWN_DURATION = Duration.ofSeconds(COOLDOWN_TICKS / 20); private static final SimpleCommandExceptionType TOO_MUCH = new SimpleCommandExceptionType(Component.translatable("commands.warp.too_much")); + private static final SimpleCommandExceptionType GENERAL_ERROR = new SimpleCommandExceptionType(Component.translatable("commands.warp.general_error")); private static final Cache warpCache = CacheBuilder.newBuilder() .maximumSize(50) @@ -47,11 +48,15 @@ public static void register(CommandDispatcher dispatcher) { private static int warp(CommandContext ctx) throws CommandSyntaxException { final String targetName = ctx.getArgument(ARGUMENT_TARGET, String.class); - final Poi target = MapPoiManager.get(ctx.getSource().getServer()).getEnabledPoi(targetName); + final Poi target = MapPoiManager.get(ctx.getSource().getServer()).getPoi(targetName); final ServerPlayer player = ctx.getSource().getPlayerOrException(); final BlockPos blockPos = target.globalPos().pos(); final ServerLevel level = player.getServer().getLevel(target.globalPos().dimension()); + if (!target.enabled() && !player.canUseGameMasterBlocks()) { + throw GENERAL_ERROR.create(); + } + if (warpCache.getIfPresent(player.getUUID()) != null && !player.canUseGameMasterBlocks()) { throw TOO_MUCH.create(); } @@ -62,6 +67,7 @@ private static int warp(CommandContext ctx) throws CommandSy } public static void addTranslations(final RegistrateLangProvider provider) { - provider.add("commands.warp.too_much", "Too soon! Please wait %s seconds"); + provider.add("commands.warp.too_much", "Can't do that yet"); + provider.add("commands.warp.general_error", "Couldn't go there"); } } diff --git a/src/main/java/com/lovetropics/extras/item/TropicMapItem.java b/src/main/java/com/lovetropics/extras/item/TropicMapItem.java index fecc1ae2..28b37cf0 100644 --- a/src/main/java/com/lovetropics/extras/item/TropicMapItem.java +++ b/src/main/java/com/lovetropics/extras/item/TropicMapItem.java @@ -60,8 +60,7 @@ protected void init() { int y = (this.height / 2) - (256 / 2); for (Poi mapPoi : pois.values()) { - //TODO add ImageButton (Renderable) to some list, and removeWidget if it is unavailable? expensive to call on render? could do % check for not every tick. but also maybe doesnt matter? - addRenderableWidget(new ImageButton(x + mapPoi.x(), y + mapPoi.y(), ICON_SIZE, ICON_SIZE, 0, 0, 0, + addRenderableWidget(new PoiImageButton(mapPoi.name(), player.canUseGameMasterBlocks(), x + mapPoi.x(), y + mapPoi.y(), ICON_SIZE, ICON_SIZE, 0, 0, 0, mapPoi.resourceLocation(), ICON_SIZE, ICON_SIZE, b -> doWarp(mapPoi))); @@ -78,6 +77,9 @@ public void render(final GuiGraphics guiGraphics, final int pMouseX, final int p renderBackground(guiGraphics); super.render(guiGraphics, pMouseX, pMouseY, pPartialTick); + int x = (this.width / 2) - (256 / 2); + int y = (this.height / 2) - (256 / 2); + //Dont think this is a great way to scale... just wanted the text a bit smaller :) PoseStack pose = guiGraphics.pose(); pose.pushPose(); @@ -89,10 +91,9 @@ public void render(final GuiGraphics guiGraphics, final int pMouseX, final int p } else if (!mapPoi.enabled() && player.canUseGameMasterBlocks()) { mapText += " [DISABLED]"; } - int x = (this.width / 2) - (256 / 2); - int y = (this.height / 2) - (256 / 2); + guiGraphics.drawString(this.font, Component.literal(mapText), (x + mapPoi.x() + ICON_SIZE) * FONT_SCALING, - (y + mapPoi.y() + (this.font.lineHeight / 9)) * FONT_SCALING, mapPoi.color()); + (y + mapPoi.y() + (this.font.lineHeight / 4)) * FONT_SCALING, mapPoi.color()); } pose.popPose(); @@ -113,6 +114,50 @@ private void doWarp(Poi mapPoi) { } } + //There is probably a smarter solution for this. Not sure how much computer-juice it eats but since it only runs on the client when the map is open, how bad can it be? + private static 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 pois.get(poiName).enabled() || isGm; + } + + } + private static class InvisibleButton extends Button { protected InvisibleButton(final Builder builder) {