Skip to content

Commit

Permalink
Fix taterzens being in the listing twice
Browse files Browse the repository at this point in the history
  • Loading branch information
samolego committed Jun 12, 2022
1 parent 484bb53 commit 91818ff
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -164,17 +165,13 @@ private static int listTaterzens(CommandContext<CommandSourceStack> 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)
Expand All @@ -189,18 +186,17 @@ private static int listTaterzens(CommandContext<CommandSourceStack> 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<String> getAvailableTaterzenIndices() {
return IntStream.range(0, TATERZEN_NPCS.size())
.mapToObj(i -> String.valueOf(i + 1))
.toList();
}

private static int selectTaterzenById(CommandContext<CommandSourceStack> 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];
Expand All @@ -226,20 +222,13 @@ private static int selectTaterzenById(CommandContext<CommandSourceStack> 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<String> 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<CommandSourceStack> context) throws CommandSyntaxException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/**
Expand All @@ -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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ public TaterzenNPC(EntityType<? extends PathfinderMob> entityType, Level world)
this.npcData.deathSounds = new ArrayList<>(config.defaults.deathSounds);
}

TATERZEN_NPCS.put(this.getUUID(), this);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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();*/
}
}
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 91818ff

Please sign in to comment.