Skip to content

Commit

Permalink
2.2.0 refactor, add json based modifier registry
Browse files Browse the repository at this point in the history
  • Loading branch information
Kasualix committed Nov 14, 2023
1 parent 25d8196 commit 76e2f4b
Show file tree
Hide file tree
Showing 27 changed files with 425 additions and 201 deletions.
3 changes: 0 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@ loom {

repositories {
maven { url = "https://maven.theillusivec4.top/" }
maven { url = 'https://jitpack.io' }
}

dependencies {
modImplementation "com.github.MCTeamPotato:Potattributes:${project.potattributes_version}"
include "com.github.MCTeamPotato:Potattributes:${project.potattributes_version}"
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings "net.fabricmc:yarn:1.19.2+build.28:v2"
forge "net.minecraftforge:forge:${project.forge_version}"
Expand Down
8 changes: 7 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@
potattributes_version=1.19.2-0.0.5

# Mod Properties
mod_version=1.19.2-2.1.1
mod_version=1.19.2-2.2.0
maven_group=com.teampotato.modifiers
archives_base_name=remodifier
mod_id=modifiers
mod_author=TeamPotato

# Proxy
systemProp.http.proxyHost=localhost
systemProp.http.proxyPort=7890
systemProp.https.proxyHost=localhost
systemProp.https.proxyPort=7890
65 changes: 37 additions & 28 deletions src/main/java/com/teampotato/modifiers/ModifiersMod.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.teampotato.modifiers;

import com.teampotato.modifiers.common.config.*;
import com.teampotato.modifiers.common.config.toml.*;
import com.teampotato.modifiers.common.curios.ICurioProxy;
import com.teampotato.modifiers.common.events.ClientEvents;
import com.teampotato.modifiers.client.events.ClientEvents;
import com.teampotato.modifiers.common.events.CommonEvents;
import com.teampotato.modifiers.common.item.ItemModifierBook;
import com.teampotato.modifiers.common.modifier.Modifiers;
Expand Down Expand Up @@ -34,49 +34,58 @@ public class ModifiersMod {
public static final Logger LOGGER = LogManager.getLogger();
public static ICurioProxy CURIO_PROXY;
public static ItemGroup GROUP_BOOKS;

public ModifiersMod() {
final IEventBus eventBus = FMLJavaModLoadingContext.get().getModEventBus();
final IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
final IEventBus forgeBus = MinecraftForge.EVENT_BUS;
final ModLoadingContext ctx = ModLoadingContext.get();
final ModConfig.Type COMMON = ModConfig.Type.COMMON;
final ModConfig.Type common = ModConfig.Type.COMMON;
NetworkHandler.register();
ITEM_DEFERRED_REGISTER.register(eventBus);
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup);
MinecraftForge.EVENT_BUS.register(CommonEvents.class);
if (FMLLoader.getDist().isClient()) {
MinecraftForge.EVENT_BUS.register(ClientEvents.class);
}
ctx.registerConfig(COMMON, ReforgeConfig.CONFIG, "remodifier/reforge.toml");
ctx.registerConfig(COMMON, CurioNArmorConfig.CONFIG, "remodifier/armor-n-curio-modifiers.toml");
ctx.registerConfig(COMMON, ToolConfig.CONFIG, "remodifier/tool-modifiers.toml");
ctx.registerConfig(COMMON, BowConfig.CONFIG, "remodifier/bow-modifiers.toml");
ctx.registerConfig(COMMON, ShieldConfig.CONFIG, "remodifier/shield-modifiers.toml");
ITEM_DEFERRED_REGISTER.register(modEventBus);
modEventBus.addListener(this::setup);
forgeBus.register(CommonEvents.class);
if (FMLLoader.getDist().isClient()) forgeBus.register(ClientEvents.class);
ctx.registerConfig(common, ReforgeConfig.CONFIG, "remodifier/reforge.toml");
ctx.registerConfig(common, ArmorConfig.CONFIG, "remodifier/armor-modifiers.toml");
ctx.registerConfig(common, ToolConfig.CONFIG, "remodifier/tool-modifiers.toml");
ctx.registerConfig(common, BowConfig.CONFIG, "remodifier/bow-modifiers.toml");
ctx.registerConfig(common, ShieldConfig.CONFIG, "remodifier/shield-modifiers.toml");
ctx.registerConfig(common, CuriosConfig.CONFIG, "remodifier/curios-modifiers.toml");
}

static {
NetworkHandler.setProxy(new NetworkHandlerForge());

GROUP_BOOKS = new ItemGroup(-1, MOD_ID +"_books") {
static ItemStack icon;
@Override
public ItemStack createIcon() {
return MODIFIER_BOOK.get().getDefaultStack();
if (icon == null) icon = MODIFIER_BOOK.get().getDefaultStack();
return icon;
}
};
}

private static Boolean isCuriosLoaded = null;

public static boolean isCuriosLoaded() {
if (isCuriosLoaded == null) isCuriosLoaded = ModList.get().isLoaded("curios");
return isCuriosLoaded;
}

private void setup(final FMLCommonSetupEvent event) {
if (ModList.get().isLoaded("curios")) {
try {
CURIO_PROXY = (ICurioProxy) Class.forName("com.teampotato.modifiers.common.curios.CurioCompat").getDeclaredConstructor().newInstance();
MinecraftForge.EVENT_BUS.register(CURIO_PROXY);
} catch (Exception e) {
LOGGER.error("Remodifier failed to load Curios integration.");
LOGGER.error(e.getMessage());
event.enqueueWork(() -> {
if (isCuriosLoaded()) {
try {
CURIO_PROXY = (ICurioProxy) Class.forName("com.teampotato.modifiers.common.curios.CurioCompat").getDeclaredConstructor().newInstance();
MinecraftForge.EVENT_BUS.register(CURIO_PROXY);
} catch (Exception e) {
LOGGER.error("Remodifier failed to load Curios integration.", e);
}
}
}
if (CURIO_PROXY == null) {
CURIO_PROXY = new ICurioProxy() {};
}
Modifiers.init();
if (CURIO_PROXY == null) CURIO_PROXY = new ICurioProxy() {};
Modifiers.initialize();
});
}

static {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.teampotato.modifiers.client;

@SuppressWarnings("unused")
public interface SmithingScreenReforge {
void modifiers_init();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.Text;
Expand Down Expand Up @@ -38,9 +37,9 @@ public void setTextureUV(int i, int j, int k, int l, Identifier identifier) {
public void renderButton(MatrixStack matrixStack, int i, int j, float f) {

MinecraftClient minecraftClient = MinecraftClient.getInstance();
TextRenderer textRenderer = minecraftClient.textRenderer;
minecraftClient.getTextureManager().bindTexture(this.texture);
RenderSystem.disableDepthTest();

int u = this.u;
int v = this.v;
if (this.toggled) {
Expand All @@ -53,7 +52,7 @@ public void renderButton(MatrixStack matrixStack, int i, int j, float f) {

this.drawTexture(matrixStack, this.x, this.y, u, v, this.width, this.height);
int color = this.active ? 16777215 : 10526880;
drawCenteredText(matrixStack, textRenderer, this.getMessage(), this.x + this.width / 2, this.y + (this.height - 8) / 2, color | MathHelper.ceil(this.alpha * 255.0F) << 24);
drawCenteredText(matrixStack, minecraftClient.textRenderer, this.getMessage(), this.x + this.width / 2, this.y + (this.height - 8) / 2, color | MathHelper.ceil(this.alpha * 255.0F) << 24);

if (this.isHovered()) this.renderTooltip(matrixStack, i, j);
RenderSystem.enableDepthTest();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.teampotato.modifiers.common.events;
package com.teampotato.modifiers.client.events;

import com.teampotato.modifiers.common.modifier.Modifier;
import com.teampotato.modifiers.common.modifier.ModifierHandler;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package com.teampotato.modifiers.common.config.json;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.teampotato.modifiers.ModifiersMod;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.minecraftforge.fml.loading.FMLLoader;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

@SuppressWarnings("ResultOfMethodCallIgnored")
public class JsonConfigInitialier {

private static final Path ROOT = FMLLoader.getGamePath().resolve("config").resolve("remodifier");
private static final String NAME = "name", WEIGHT = "weight", ATTRIBUTES = "attributes", AMOUNTS = "amounts", OPERATION_ID = "operationId";

private static @NotNull FileWriter writeFile(File configFile) throws IOException {
JsonObject config = new JsonObject();
config.addProperty(NAME, "violent");
config.addProperty(WEIGHT, 100);
config.addProperty(ATTRIBUTES, "minecraft:generic.attack_speed");
config.addProperty(AMOUNTS, "0.04");
config.addProperty(OPERATION_ID, "2");
FileWriter fileWriter = new FileWriter(configFile);
fileWriter.write(config.toString());
return fileWriter;
}

static {
ROOT.toFile().mkdirs();
mkdirs("armor", "curios", "shield", "tool", "bow");
generateExampleFile();
}

private static void mkdirs(String @NotNull ... strings) {
for (String s : strings) ROOT.resolve(s).toFile().mkdirs();
}

private static void generateExampleFile() {
File violent = new File(ROOT.resolve("armor").toFile(), "violent.json");
if (!violent.exists()) {
try {
FileWriter writer = writeFile(violent);
writer.close();
} catch (Throwable throwable) {
ModifiersMod.LOGGER.error("Error occurs during example json file generation", throwable);
}
}
}

private static final Iterable<File> BOW_JSONS = readFiles(ROOT.resolve("bow")),
CURIOS_JSONS = readFiles(ROOT.resolve("curios")),
ARMOR_JSONS = readFiles(ROOT.resolve("armor")),
SHIELD_JSONS = readFiles(ROOT.resolve("shield")),
TOOL_JSONS = readFiles(ROOT.resolve("tool"));

public static final Iterable<? extends String> BOW_NAMES = getElements(BOW_JSONS, NAME),
BOW_WEIGHTS = getElements(BOW_JSONS, WEIGHT),
BOW_ATTRIBUTES = getElements(BOW_JSONS, ATTRIBUTES),
BOW_AMOUNTS = getElements(BOW_JSONS, AMOUNTS),
BOW_OPERATIONS_IDS = getElements(BOW_JSONS, OPERATION_ID);

public static final Iterable<String> CURIOS_NAMES = getElements(CURIOS_JSONS, NAME),
CURIOS_WEIGHTS = getElements(CURIOS_JSONS, WEIGHT),
CURIOS_ATTRIBUTES = getElements(CURIOS_JSONS, ATTRIBUTES),
CURIOS_AMOUNTS = getElements(CURIOS_JSONS, AMOUNTS),
CURIOS_OPERATIONS_IDS = getElements(CURIOS_JSONS, OPERATION_ID);

public static final Iterable<String> ARMOR_NAMES = getElements(ARMOR_JSONS, NAME),
ARMOR_WEIGHTS = getElements(ARMOR_JSONS, WEIGHT),
ARMOR_ATTRIBUTES = getElements(ARMOR_JSONS, ATTRIBUTES),
ARMOR_AMOUNTS = getElements(ARMOR_JSONS, AMOUNTS),
ARMOR_OPERATIONS_IDS = getElements(ARMOR_JSONS, OPERATION_ID);

public static final Iterable<String> SHIELD_NAMES = getElements(SHIELD_JSONS, NAME),
SHIELD_WEIGHTS = getElements(SHIELD_JSONS, WEIGHT),
SHIELD_ATTRIBUTES = getElements(SHIELD_JSONS, ATTRIBUTES),
SHIELD_AMOUNTS = getElements(SHIELD_JSONS, AMOUNTS),
SHIELD_OPERATIONS_IDS = getElements(SHIELD_JSONS, OPERATION_ID);

public static final Iterable<String> TOOL_NAMES = getElements(TOOL_JSONS, NAME),
TOOL_WEIGHTS = getElements(TOOL_JSONS, WEIGHT),
TOOL_ATTRIBUTES = getElements(TOOL_JSONS, ATTRIBUTES),
TOOL_AMOUNTS = getElements(TOOL_JSONS, AMOUNTS),
TOOL_OPERATIONS_IDS = getElements(TOOL_JSONS, OPERATION_ID);

private static @NotNull Iterable<String> getElements(@NotNull Iterable<File> files, String element) {
List<String> names = new LinkedList<>();
for (File file : files) {
try {
FileReader fileReader = new FileReader(file);
JsonObject configObject = JsonParser.parseReader(fileReader).getAsJsonObject();
names.add(configObject.get(element).getAsString());
} catch (Throwable throwable) {
ModifiersMod.LOGGER.error("Error occurs during " + file.getName() + " reading", throwable);
}
}
return names;
}

private static @NotNull Iterable<File> readFiles(@NotNull Path path) {
List<File> fileList = new ObjectArrayList<>();
File folder = path.toFile();
if (!folder.exists() || !folder.isDirectory()) return Collections.emptySet();
File[] files = folder.listFiles();
if (files == null) return Collections.emptySet();
for (File file : files) {
if (!file.isFile() || !file.getName().toLowerCase().endsWith(".json")) continue;
fileList.add(file);
}
return fileList;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.teampotato.modifiers.common.config;
package com.teampotato.modifiers.common.config.toml;

import com.google.common.collect.Lists;
import net.minecraftforge.common.ForgeConfigSpec;

import java.util.List;

public class CurioNArmorConfig {
public class ArmorConfig {
public static ForgeConfigSpec CONFIG;
public static ForgeConfigSpec.ConfigValue<List<? extends String>> NAMES;
public static ForgeConfigSpec.ConfigValue<List<? extends String>> WEIGHTS;
Expand All @@ -15,13 +15,13 @@ public class CurioNArmorConfig {

static {
ForgeConfigSpec.Builder BUILDER = new ForgeConfigSpec.Builder();
BUILDER.push("Modifiers for armors and curios");
BUILDER.push("Modifiers for armors");
BUILDER.comment("This configuration file is based on index. it means, 'half_hearted' -> '300' -> 'generic.max_health' -> '1.0' -> '0' is the first index.", "WARNING: You have to make a resource pack to save the customization-required translation key for the attributes if the mod author didn't do that, or your customization on other mods' attribute will crash the game!", "A hint on the translation key format: attribute.modxxx.attributexxx, e.g. attribute.minecraft.generic.attack_damage");
NAMES = BUILDER.comment("The name of the modifier").defineList("NAMES", Lists.newArrayList("half_hearted", "hearty", "hard", "guarding", "armored", "warding", "jagged", "spiked", "angry", "menacing", "brisk", "fleeting", "hasty", "quick", "wild", "rash", "intrepid", "violent"), o -> true);
WEIGHTS = BUILDER.comment("The weight of the modifier in the modifiers pool").defineList("WEIGHTS", Lists.newArrayList("300", "100", "300", "200", "100", "200", "200", "200", "100", "100", "200", "200", "100", "100", "200", "200", "100", "100"), o -> true);
ATTRIBUTES = BUILDER.comment("The attribute of the modifier has. One modifier can have multiple attributes. Use ';' to split different attributes").defineList("ATTRIBUTES", Lists.newArrayList("minecraft:generic.max_health", "minecraft:generic.max_health", "minecraft:generic.armor", "minecraft:generic.armor", "minecraft:generic.armor", "minecraft:generic.armor_toughness", "minecraft:generic.attack_damage", "minecraft:generic.attack_damage", "minecraft:generic.attack_damage", "minecraft:generic.attack_damage", "minecraft:generic.movement_speed", "minecraft:generic.movement_speed", "minecraft:generic.movement_speed", "minecraft:generic.movement_speed", "minecraft:generic.attack_speed", "minecraft:generic.attack_speed", "minecraft:generic.attack_speed", "minecraft:generic.attack_speed"), o -> true);
AMOUNTS = BUILDER.comment("The amount used to calculate the attribute effect. Also can be multiple. Use ';' to split").defineList("AMOUNTS", Lists.newArrayList("1.0", "2.0", "1.0", "1.5", "2.0", "1.0", "0.01", "0.02", "0.03", "0.04", "0.01", "0.02","0.03", "0.04", "0.01", "0.02", "0.03", "0.04"), o -> true);
OPERATIONS_IDS = BUILDER.comment("The operation ID of the attribute calculation. Can be three values: 0,1,2. 0 is ADDITION. 1 is MULTIPLY_BASE. 2 is MULTIPLY_TOTAL. you can refer to the calculation of the attributes already in the game").defineList("OPERATIONS_IDS", Lists.newArrayList("0", "0", "0", "0", "0", "0", "2", "2", "2", "2", "2", "2", "2","2", "2", "2", "2", "2"), o -> true);
NAMES = BUILDER.comment("The name of the modifier").defineList("NAMES", Lists.newArrayList("half_hearted", "hearty", "hard", "guarding", "armored", "warding", "jagged", "spiked", "angry", "menacing", "brisk", "fleeting", "hasty", "quick", "wild", "rash", "intrepid"), o -> true);
WEIGHTS = BUILDER.comment("The weight of the modifier in the modifiers pool").defineList("WEIGHTS", Lists.newArrayList("300", "100", "300", "200", "100", "200", "200", "200", "100", "100", "200", "200", "100", "100", "200", "200", "100"), o -> true);
ATTRIBUTES = BUILDER.comment("The attribute of the modifier has. One modifier can have multiple attributes. Use ';' to split different attributes").defineList("ATTRIBUTES", Lists.newArrayList("minecraft:generic.max_health", "minecraft:generic.max_health", "minecraft:generic.armor", "minecraft:generic.armor", "minecraft:generic.armor", "minecraft:generic.armor_toughness", "minecraft:generic.attack_damage", "minecraft:generic.attack_damage", "minecraft:generic.attack_damage", "minecraft:generic.attack_damage", "minecraft:generic.movement_speed", "minecraft:generic.movement_speed", "minecraft:generic.movement_speed", "minecraft:generic.movement_speed", "minecraft:generic.attack_speed", "minecraft:generic.attack_speed", "minecraft:generic.attack_speed"), o -> true);
AMOUNTS = BUILDER.comment("The amount used to calculate the attribute effect. Also can be multiple. Use ';' to split").defineList("AMOUNTS", Lists.newArrayList("1.0", "2.0", "1.0", "1.5", "2.0", "1.0", "0.01", "0.02", "0.03", "0.04", "0.01", "0.02","0.03", "0.04", "0.01", "0.02", "0.03"), o -> true);
OPERATIONS_IDS = BUILDER.comment("The operation ID of the attribute calculation. Can be three values: 0,1,2. 0 is ADDITION. 1 is MULTIPLY_BASE. 2 is MULTIPLY_TOTAL. you can refer to the calculation of the attributes already in the game").defineList("OPERATIONS_IDS", Lists.newArrayList("0", "0", "0", "0", "0", "0", "2", "2", "2", "2", "2", "2", "2","2", "2", "2", "2"), o -> true);
BUILDER.pop();
CONFIG = BUILDER.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.teampotato.modifiers.common.config;
package com.teampotato.modifiers.common.config.toml;

import com.google.common.collect.Lists;
import net.minecraftforge.common.ForgeConfigSpec;
Expand Down
Loading

0 comments on commit 76e2f4b

Please sign in to comment.