Skip to content

Commit

Permalink
hide things when disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
bazke committed Oct 27, 2023
1 parent d8d6d82 commit 8488bf5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
10 changes: 8 additions & 2 deletions src/main/java/com/lovetropics/extras/command/WarpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<UUID, Long> warpCache = CacheBuilder.newBuilder()
.maximumSize(50)
Expand All @@ -47,11 +48,15 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {

private static int warp(CommandContext<CommandSourceStack> 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();
}
Expand All @@ -62,6 +67,7 @@ private static int warp(CommandContext<CommandSourceStack> 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");
}
}
55 changes: 50 additions & 5 deletions src/main/java/com/lovetropics/extras/item/TropicMapItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)));

Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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) {
Expand Down

0 comments on commit 8488bf5

Please sign in to comment.