Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite naming logic #22

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import io.github.overlordsiii.villagernames.util.NamesLoader;
import io.github.overlordsiii.villagernames.util.VillagerUtil;
import me.sargunvohra.mcmods.autoconfig1u.AutoConfig;
import me.sargunvohra.mcmods.autoconfig1u.ConfigManager;
import me.sargunvohra.mcmods.autoconfig1u.ConfigHolder;
import me.sargunvohra.mcmods.autoconfig1u.serializer.GsonConfigSerializer;
import me.sargunvohra.mcmods.autoconfig1u.serializer.PartitioningSerializer;
import net.fabricmc.api.ModInitializer;
Expand All @@ -18,39 +18,36 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@SuppressWarnings({"UnstableApiUsage", "unused"})
public class VillagerNames implements ModInitializer {
public static final ConfigManager CONFIG_MANAGER;
public static final ConfigHolder<VillagerConfig> CONFIG_HOLDER;
public static final VillagerConfig CONFIG;

public static final Logger LOGGER = LogManager.getLogger(VillagerNames.class);

static {
CONFIG_MANAGER = (ConfigManager) AutoConfig.register(VillagerConfig.class, PartitioningSerializer.wrap(GsonConfigSerializer::new));
CONFIG = AutoConfig.getConfigHolder(VillagerConfig.class).getConfig();
CONFIG_HOLDER = AutoConfig.register(VillagerConfig.class, PartitioningSerializer.wrap(GsonConfigSerializer::new));
CONFIG = CONFIG_HOLDER.getConfig();
}

@Override
public void onInitialize() {
NamesLoader.load();

CommandRegistrationCallback.EVENT.register((commandDispatcher, dedicated) -> {
VillagerNameCommand.register(commandDispatcher);
if (!dedicated){
if (!dedicated) {
StopCommand.register(commandDispatcher);
}
});

ServerEntityEvents.ENTITY_LOAD.register((entity, serverWorld) -> {
if (entity instanceof VillagerEntity){
VillagerUtil.createVillagerNames((VillagerEntity)entity);
VillagerUtil.generalVillagerUpdate((VillagerEntity)entity);
}
else if (entity instanceof IronGolemEntity){
VillagerUtil.loadGolemNames((IronGolemEntity)entity);
VillagerUtil.updateGolemNames((IronGolemEntity)entity);
} else if (entity instanceof WanderingTraderEntity){
VillagerUtil.createWanderingTraderNames((WanderingTraderEntity)entity);
VillagerUtil.updateWanderingTraderNames((WanderingTraderEntity)entity);
if (entity instanceof VillagerEntity) {
VillagerUtil.updateVillagerName((VillagerEntity)entity);
} else if (entity instanceof IronGolemEntity) {
VillagerUtil.updateGolemName((IronGolemEntity)entity);
} else if (entity instanceof WanderingTraderEntity) {
VillagerUtil.updateWanderingTraderName((WanderingTraderEntity)entity);
}
});

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private static int executeSetFormatting(CommandContext<ServerCommandSource> ctx,
new LiteralText(FormattingDummy.fromFormatting(newFormatting)
.toString()).formatted(newFormatting))))
, true);
VillagerNames.CONFIG_MANAGER.save();
VillagerNames.CONFIG_HOLDER.save();
try {
broadCastConfigChangeToOps(ctx, ConfigChange.SET, VillagerGeneralConfig.class.getDeclaredField("villagerTextFormatting"), ctx.getSource().getPlayer(), null);
} catch (Exception e) {
Expand All @@ -134,7 +134,7 @@ private static int executeSetString(CommandContext<ServerCommandSource> ctx, Str
"/villagername set " + literal + " " + newvalue)))
.withClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND
, "/villagername set " + literal))), true);
VillagerNames.CONFIG_MANAGER.save();
VillagerNames.CONFIG_HOLDER.save();
try {
broadCastConfigChangeToOps(ctx, ConfigChange.SET, VillagerGeneralConfig.class.getDeclaredField(literal), ctx.getSource().getPlayer(), null);
} catch (Exception e) {
Expand Down Expand Up @@ -163,7 +163,7 @@ private static int executeToggle(CommandContext<ServerCommandSource> ctx, String
, "/villagername toggle " + literal))
.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT
, new LiteralText("Toggle the " + literal + " rule")))), true);
VillagerNames.CONFIG_MANAGER.save();
VillagerNames.CONFIG_HOLDER.save();
try {
broadCastConfigChangeToOps(ctx, ConfigChange.TOGGLE, VillagerGeneralConfig.class.getDeclaredField(literal), ctx.getSource().getPlayer(), null);
} catch (Exception e) {
Expand Down Expand Up @@ -200,7 +200,7 @@ private static int executeAdd(CommandContext<ServerCommandSource> ctx, List<Stri
} catch (Exception e) {
logError(ctx, e);
}
VillagerNames.CONFIG_MANAGER.save();
VillagerNames.CONFIG_HOLDER.save();
return 1;
}

Expand Down Expand Up @@ -230,7 +230,7 @@ private static int executeRemove(CommandContext<ServerCommandSource> ctx, List<S
} catch (Exception e) {
logError(ctx, e);
}
VillagerNames.CONFIG_MANAGER.save();
VillagerNames.CONFIG_HOLDER.save();
return 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import io.github.overlordsiii.villagernames.config.FormattingDummy;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.util.Formatting;

import java.util.ArrayList;
import java.util.concurrent.CompletableFuture;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
public class ModMenuIntegration implements ModMenuApi {
@Override
public ConfigScreenFactory<?> getModConfigScreenFactory() {
return screen -> AutoConfig.getConfigScreen(VillagerConfig.class, screen).get();
return screen -> AutoConfig.getConfigScreen(VillagerConfig.class, screen).get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.Map;

@SuppressWarnings("UnstableApiUsage")
@Mixin(ConfigBuilderImpl.class)
@Mixin(value = ConfigBuilderImpl.class, remap = false)
public interface ConfigBuilderImplAccessor {
@Accessor("categoryMap")
Map<Text, ConfigCategory> getCategoryMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.function.Supplier;

@Environment(EnvType.CLIENT)
@Mixin(value = SelectionListEntry.class)
@Mixin(value = SelectionListEntry.class, remap = false)
public abstract class SelectionListEntryMixin<T> {
@Shadow private Function<T, Text> nameProvider;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,8 @@

@Mixin(VillagerBreedTask.class)
public abstract class VillagerBreedTaskMixin {
@Inject(method = "createChild",
at = @At("TAIL"),
locals = LocalCapture.CAPTURE_FAILHARD)
private void onCreateChild(ServerWorld serverWorld, VillagerEntity villagerEntity, VillagerEntity villagerEntity2, CallbackInfoReturnable<Optional> cir, VillagerEntity villagerEntity3) {
VillagerUtil.addLastNameFromBreeding(villagerEntity3, villagerEntity);
if (VillagerNames.CONFIG.villagerGeneralConfig.surNames) {
System.out.println("villager name for villagerEntity (parent) = " + valueOf(villagerEntity.getCustomName()));
System.out.println("villager name for villagerEntity3 (child) = " + valueOf(villagerEntity.getCustomName()));
}
@Inject(method = "createChild", at = @At("TAIL"), locals = LocalCapture.CAPTURE_FAILHARD)
private void onCreateChild(ServerWorld serverWorld, VillagerEntity villagerEntity, VillagerEntity villagerEntity2, CallbackInfoReturnable<Optional<VillagerEntity>> cir, VillagerEntity villagerEntity3) {
VillagerUtil.inheritSurname(villagerEntity3, villagerEntity);
}

private String valueOf(Text object) {
return object == null ? "null" : object.asString();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import net.minecraft.entity.InteractionObserver;
import net.minecraft.entity.passive.MerchantEntity;
import net.minecraft.entity.passive.VillagerEntity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.village.VillagerData;
import net.minecraft.village.VillagerDataContainer;
import net.minecraft.village.VillagerProfession;
import net.minecraft.world.World;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -22,42 +24,43 @@
public abstract class VillagerEntityMixin extends MerchantEntity implements InteractionObserver, VillagerDataContainer {
@Shadow public abstract VillagerData getVillagerData();

// TODO: may or may not be used, probably not though
// public String forename;
// @Nullable public String surname;

public VillagerEntityMixin(EntityType<? extends MerchantEntity> entityType, World world) {
super(entityType, world);
}
@Inject(method = "setVillagerData", at = @At("TAIL"))
private void changeText(VillagerData villagerData, CallbackInfo ci){
if (villagerData.getProfession() != VillagerProfession.NONE && this.hasCustomName()) {
VillagerUtil.updateVillagerNames((VillagerEntity) (Object) this);
}
if (this.hasCustomName() && villagerData.getProfession() == VillagerProfession.NONE){
VillagerUtil.updateLostVillagerProfessionName((VillagerEntity)(Object)this);
}

// @Inject(method = "writeCustomDataToTag", at = @At("RETURN"))
// public void writeCustomDataToTag(CompoundTag tag, CallbackInfo ci) {
// tag.putString("forename", forename);
// tag.putString("surname", surname);
// }
//
// @Inject(method = "readCustomDataFromTag", at = @At("RETURN"))
// public void fromTag(CompoundTag tag, CallbackInfo ci) {
// forename = tag.getString("forename");
// surname = tag.getString("surname");
// }

@Inject(method = "setVillagerData", at = @At("RETURN"))
private void setVillagerData(VillagerData villagerData, CallbackInfo ci) {
VillagerUtil.updateVillagerName((VillagerEntity) (Object) this);
}
@SuppressWarnings("ALL")
@Redirect(method = "onDeath", at = @At(value = "INVOKE", target = "Lorg/apache/logging/log4j/Logger;info(Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;)V"))
private void redirect(Logger logger, String message, Object p0, Object p1){
if (VillagerNames.CONFIG.villagerGeneralConfig.turnOffVillagerConsoleSpam) {
String lol = "ha lol gottem";
}
else{

@Redirect(method = "onDeath", at = @At(value = "INVOKE", target = "Lorg/apache/logging/log4j/Logger;info(Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;)V"), remap = false)
private void redirect(Logger logger, String message, Object p0, Object p1) {
// ha lol gottem
if (!VillagerNames.CONFIG.villagerGeneralConfig.turnOffVillagerConsoleSpam) {
logger.info(message, p0, p1);
}
}
@SuppressWarnings("ALL")
@Inject(method = "onGrowUp", at = @At("TAIL"))
private void updateBabyText(CallbackInfo ci){
VillagerUtil.updateGrownUpVillagerName((VillagerEntity)(Object)(this));
}
/*
@Inject(method = "createChild",
at = @At(value = "INVOKE",
target = "Lnet/minecraft/entity/passive/VillagerEntity;initialize(Lnet/minecraft/world/ServerWorldAccess;Lnet/minecraft/world/LocalDifficulty;Lnet/minecraft/entity/SpawnReason;Lnet/minecraft/entity/EntityData;Lnet/minecraft/nbt/CompoundTag;)Lnet/minecraft/entity/EntityData;")
, locals = LocalCapture.CAPTURE_FAILHARD)
private void onCreateChild(ServerWorld serverWorld, PassiveEntity passiveEntity, CallbackInfoReturnable<VillagerEntity> cir, VillagerType villagerType3, VillagerEntity villagerEntity) {
VillagerUtil.addLastNameFromBreeding(villagerEntity, (VillagerEntity)(Object)this);

@Inject(method = "onGrowUp", at = @At("RETURN"))
private void onGrowUp(CallbackInfo ci) {
VillagerUtil.updateVillagerName((VillagerEntity) (Object) this);
}
*/
}


Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ protected ZombieEntityMixin(EntityType<? extends HostileEntity> entityType, Worl
}

@Inject(method = "onKilledOther", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/mob/ZombieVillagerEntity;initialize(Lnet/minecraft/world/ServerWorldAccess;Lnet/minecraft/world/LocalDifficulty;Lnet/minecraft/entity/SpawnReason;Lnet/minecraft/entity/EntityData;Lnet/minecraft/nbt/CompoundTag;)Lnet/minecraft/entity/EntityData;"), locals = LocalCapture.CAPTURE_FAILEXCEPTION)
private void addZombieNameTag(ServerWorld serverWorld, LivingEntity livingEntity, CallbackInfo ci, VillagerEntity villagerEntity, ZombieVillagerEntity zombieVillagerEntity){
VillagerUtil.addZombieVillagerName(villagerEntity, zombieVillagerEntity);
private void addZombieNameTag(ServerWorld serverWorld, LivingEntity livingEntity, CallbackInfo ci, VillagerEntity villagerEntity, ZombieVillagerEntity zombieVillagerEntity) {
VillagerUtil.setZombieVillagerName(zombieVillagerEntity, villagerEntity);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public ZombieVillagerEntityMixin(EntityType<? extends ZombieEntity> entityType,
}

@Inject(method = "finishConversion", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/passive/VillagerEntity;initialize(Lnet/minecraft/world/ServerWorldAccess;Lnet/minecraft/world/LocalDifficulty;Lnet/minecraft/entity/SpawnReason;Lnet/minecraft/entity/EntityData;Lnet/minecraft/nbt/CompoundTag;)Lnet/minecraft/entity/EntityData;"), locals = LocalCapture.CAPTURE_FAILEXCEPTION)
private void returnOriginalVillagerName(ServerWorld world, CallbackInfo ci, VillagerEntity villagerEntity, EquipmentSlot[] var3, int var4, int var5){
VillagerUtil.removeZombieVillagerName(villagerEntity, (ZombieVillagerEntity)(Object)this);
private void returnOriginalVillagerName(ServerWorld world, CallbackInfo ci, VillagerEntity villagerEntity, EquipmentSlot[] var3, int var4, int var5) {
VillagerUtil.transferZombieVillagerName((ZombieVillagerEntity) (Object) this, villagerEntity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@
public class NamesLoader {
public static void load() {
if (!VillagerNames.CONFIG.villagerGeneralConfig.hasRead){
VillagerNames.CONFIG.villagerNamesConfig.villagerNames = loadJson("villagerNames.json");
VillagerNames.CONFIG.golemNamesConfig.golemNames = loadJson("golemNames.json");
VillagerNames.CONFIG.sureNamesConfig.sureNames = loadJson("surnameNames.json");
VillagerNames.CONFIG.villagerGeneralConfig.hasRead = true;
//noinspection UnstableApiUsage
VillagerNames.CONFIG_MANAGER.save();
} if (VillagerNames.CONFIG.sureNamesConfig.sureNames.isEmpty()) {
VillagerNames.CONFIG.villagerNamesConfig.villagerNames = loadJson("villagerNames.json");
VillagerNames.CONFIG.golemNamesConfig.golemNames = loadJson("golemNames.json");
VillagerNames.CONFIG.sureNamesConfig.sureNames = loadJson("surnameNames.json");
VillagerNames.CONFIG.villagerGeneralConfig.hasRead = true;
VillagerNames.CONFIG_HOLDER.save();
} if (VillagerNames.CONFIG.sureNamesConfig.sureNames.isEmpty()) {
VillagerNames.CONFIG.sureNamesConfig.sureNames = loadJson("surnameNames.json");
} if (!VillagerNames.CONFIG.golemNamesConfig.golemNames.contains("Oracle")) {
loadJson("golemNames.json").forEach(s -> {
Expand Down Expand Up @@ -56,14 +55,14 @@ private static void jsonifyTxtFile(String file) throws IOException {
Path configPath = Paths.get(FabricLoader.getInstance().getConfigDir() + "/VillagerNames.json");
System.out.println(configPath);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Files.createDirectories(configPath.getParent());
BufferedWriter writer = Files.newBufferedWriter(configPath);
Files.createDirectories(configPath.getParent());
try (BufferedWriter writer = Files.newBufferedWriter(configPath)) {
JsonArray array = new JsonArray();
strings.forEach(array::add);
JsonObject object = new JsonObject();
object.add("villagerNames", array);
gson.toJson(object, writer);
writer.close();
}
}

}
Expand Down
Loading