From 91818ff9881dcc1cba5c8c476fb3d15556afb0b6 Mon Sep 17 00:00:00 2001 From: samolego <34912839+samolego@users.noreply.github.com> Date: Sun, 12 Jun 2022 13:29:28 +0200 Subject: [PATCH] Fix taterzens being in the listing twice --- .../taterzens/commands/NpcCommand.java | 45 +++++++------------ .../mixin/ChunkMapMixin_TaterzenList.java | 9 ++-- .../samo_lego/taterzens/npc/TaterzenNPC.java | 1 - .../forge/platform/ForgePlatform.java | 9 +--- gradle.properties | 6 +-- 5 files changed, 28 insertions(+), 42 deletions(-) diff --git a/common/src/main/java/org/samo_lego/taterzens/commands/NpcCommand.java b/common/src/main/java/org/samo_lego/taterzens/commands/NpcCommand.java index af8f6da78..b9839a27e 100644 --- a/common/src/main/java/org/samo_lego/taterzens/commands/NpcCommand.java +++ b/common/src/main/java/org/samo_lego/taterzens/commands/NpcCommand.java @@ -32,6 +32,7 @@ import java.util.List; import java.util.UUID; import java.util.function.Consumer; +import java.util.stream.IntStream; import static net.minecraft.commands.Commands.argument; import static net.minecraft.commands.Commands.literal; @@ -164,17 +165,13 @@ private static int listTaterzens(CommandContext context) thr boolean sel = taterzenNPC == npc; - response - .append( + response.append( Component.literal("\n" + i + "-> " + name) .withStyle(sel ? ChatFormatting.BOLD : ChatFormatting.RESET) .withStyle(sel ? ChatFormatting.GREEN : (i % 2 == 0 ? ChatFormatting.YELLOW : ChatFormatting.GOLD)) .withStyle(style -> style - .withClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/npc select uuid" + taterzenNPC.getUUID().toString())) - .withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, translate(sel ? "taterzens.tooltip.current_selection" : "taterzens.tooltip.new_selection", name)) - ) - ) - ) + .withClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/npc select uuid " + taterzenNPC.getUUID().toString())) + .withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, translate(sel ? "taterzens.tooltip.current_selection" : "taterzens.tooltip.new_selection", name))))) .append( Component.literal(" (" + (console ? taterzenNPC.getStringUUID() : "uuid") + ")") .withStyle(ChatFormatting.GRAY) @@ -189,18 +186,17 @@ private static int listTaterzens(CommandContext context) thr source.sendSuccess(response, false); return 1; } - private static String[] getAvailableTaterzenIndices() { - String[] availableIDs = new String[TATERZEN_NPCS.size()]; - for (int i = 0; i < TATERZEN_NPCS.size(); i++) { - availableIDs[i] = Integer.toString(i + 1); - } - return availableIDs; + + private static List getAvailableTaterzenIndices() { + return IntStream.range(0, TATERZEN_NPCS.size()) + .mapToObj(i -> String.valueOf(i + 1)) + .toList(); } private static int selectTaterzenById(CommandContext context) throws CommandSyntaxException { int id = IntegerArgumentType.getInteger(context, "id"); CommandSourceStack source = context.getSource(); - if(id > TATERZEN_NPCS.size()) { + if (id > TATERZEN_NPCS.size()) { source.sendFailure(errorText("taterzens.error.404.id", String.valueOf(id))); } else { TaterzenNPC taterzen = (TaterzenNPC) TATERZEN_NPCS.values().toArray()[id - 1]; @@ -226,20 +222,13 @@ private static int selectTaterzenById(CommandContext context return 1; } - private static String[] getAvailableTaterzenNames() { - String[] availableNames = new String[TATERZEN_NPCS.size()]; - - int i = 0; - for (var taterzen : TATERZEN_NPCS.values()) { - availableNames[i] = taterzen.getName().getString(); - availableNames[i] = "\"" + availableNames[i] + "\""; // Adds quotation marks to the suggested name, such that - // Names containing a whitespace character (ex. the - // name is 'Foo Bar') can be completed and correctly - // used without the user having to enclose the argument - // name with quotation marks themselves. - ++i; - } - return availableNames; + private static List getAvailableTaterzenNames() { + // Adds quotation marks to the suggested name, such that + // Names containing a whitespace character (ex. the + // name is 'Foo Bar') can be completed and correctly + // used without the user having to enclose the argument + // name with quotation marks themselves. + return TATERZEN_NPCS.values().stream().map(npc -> "\"" + npc.getName().getString() + "\"").toList(); } private static int selectTaterzenByName(CommandContext context) throws CommandSyntaxException { diff --git a/common/src/main/java/org/samo_lego/taterzens/mixin/ChunkMapMixin_TaterzenList.java b/common/src/main/java/org/samo_lego/taterzens/mixin/ChunkMapMixin_TaterzenList.java index a4e4c8b8a..b46a873f8 100644 --- a/common/src/main/java/org/samo_lego/taterzens/mixin/ChunkMapMixin_TaterzenList.java +++ b/common/src/main/java/org/samo_lego/taterzens/mixin/ChunkMapMixin_TaterzenList.java @@ -19,8 +19,10 @@ public class ChunkMapMixin_TaterzenList { */ @Inject(method = "addEntity(Lnet/minecraft/world/entity/Entity;)V", at = @At("TAIL")) private void onEntityAdded(Entity entity, CallbackInfo ci) { - if(entity instanceof TaterzenNPC) + if (entity instanceof TaterzenNPC && !TATERZEN_NPCS.containsKey(entity.getUUID())) { + System.out.println("Adding " + entity.getName() + " to Taterzen NPCs " + entity.getUUID()); TATERZEN_NPCS.put(entity.getUUID(), (TaterzenNPC) entity); + } } /** @@ -29,7 +31,8 @@ private void onEntityAdded(Entity entity, CallbackInfo ci) { */ @Inject(method = "removeEntity(Lnet/minecraft/world/entity/Entity;)V", at = @At("TAIL")) private void onEntityRemoved(Entity entity, CallbackInfo ci) { - if(entity instanceof TaterzenNPC) - TATERZEN_NPCS.remove(entity); + if (entity instanceof TaterzenNPC) { + TATERZEN_NPCS.remove(entity.getUUID()); + } } } diff --git a/common/src/main/java/org/samo_lego/taterzens/npc/TaterzenNPC.java b/common/src/main/java/org/samo_lego/taterzens/npc/TaterzenNPC.java index 16739e42d..9940c4794 100644 --- a/common/src/main/java/org/samo_lego/taterzens/npc/TaterzenNPC.java +++ b/common/src/main/java/org/samo_lego/taterzens/npc/TaterzenNPC.java @@ -210,7 +210,6 @@ public TaterzenNPC(EntityType entityType, Level world) this.npcData.deathSounds = new ArrayList<>(config.defaults.deathSounds); } - TATERZEN_NPCS.put(this.getUUID(), this); } /** diff --git a/forge/src/main/java/org/samo_lego/taterzens/forge/platform/ForgePlatform.java b/forge/src/main/java/org/samo_lego/taterzens/forge/platform/ForgePlatform.java index 065ccc23e..75fcc4ce8 100644 --- a/forge/src/main/java/org/samo_lego/taterzens/forge/platform/ForgePlatform.java +++ b/forge/src/main/java/org/samo_lego/taterzens/forge/platform/ForgePlatform.java @@ -1,9 +1,7 @@ package org.samo_lego.taterzens.forge.platform; -import eu.pb4.sgui.api.gui.SimpleGui; import net.minecraft.commands.CommandSourceStack; import net.minecraft.resources.ResourceLocation; -import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.MobCategory; import net.minecraft.world.entity.player.Player; @@ -17,13 +15,10 @@ import org.samo_lego.taterzens.platform.Platform; import java.nio.file.Path; -import java.util.Collections; import static org.samo_lego.taterzens.Taterzens.MOD_ID; import static org.samo_lego.taterzens.Taterzens.NPC_ID; import static org.samo_lego.taterzens.Taterzens.TATERZEN_TYPE; -import static org.samo_lego.taterzens.commands.NpcCommand.npcNode; -import static org.samo_lego.taterzens.gui.EditorGUI.createCommandGui; public class ForgePlatform extends Platform { @@ -65,7 +60,7 @@ public void registerTaterzenType() { @Override public void openEditorGui(Player player) { - SimpleGui editorGUI = createCommandGui((ServerPlayer) player, null, npcNode, Collections.singletonList("npc"), false); - editorGUI.open(); + /*SimpleGui editorGUI = createCommandGui((ServerPlayer) player, null, npcNode, Collections.singletonList("npc"), false); + editorGUI.open();*/ } } diff --git a/gradle.properties b/gradle.properties index 138d80b46..df7482eba 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,12 +8,12 @@ loader_version=0.14.6 fabric_version=0.55.2+1.19 #Forge forge_version=41.0.1 -enable_forge=false +enable_forge=true # Mod Properties -mod_version=1.10.1 +mod_version=1.10.2 maven_group=org.samo_lego archives_base_name=taterzens # Dependencies disguiselib_version=1.2.1 -c2b_version=4c3b0be618 +c2b_version=1.1.4 sgui_version=1.1.0+1.19