Skip to content

Commit

Permalink
small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Fourmisain committed Oct 19, 2024
1 parent 9ba7ea3 commit b77c38f
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.util.Identifier;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import randommcsomethin.fallingleaves.init.Config;
Expand All @@ -11,12 +12,13 @@

@Environment(EnvType.CLIENT)
public class FallingLeavesClient implements ClientModInitializer {

/** The mod's unique identifier, used to avoid mod conflicts in the Registry and config files */
public static final String MOD_ID = "fallingleaves";

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

public static Identifier id(String path) {
return Identifier.of(MOD_ID, path);
}

@Override
public void onInitializeClient() {
Config.init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
import me.shedaniel.clothconfig2.impl.builders.BooleanToggleBuilder;
import me.shedaniel.clothconfig2.impl.builders.IntSliderBuilder;
import me.shedaniel.clothconfig2.impl.builders.SubCategoryBuilder;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.block.Block;
import net.minecraft.registry.Registries;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import randommcsomethin.fallingleaves.config.ConfigDefaults;
import randommcsomethin.fallingleaves.config.LeafSettingsEntry;
import randommcsomethin.fallingleaves.util.ModUtil;
import randommcsomethin.fallingleaves.util.TranslationComparator;

import java.lang.reflect.Field;
Expand All @@ -24,11 +25,14 @@
import java.util.Map;

import static randommcsomethin.fallingleaves.FallingLeavesClient.LOGGER;
import static randommcsomethin.fallingleaves.util.RegistryUtil.getBlock;

public class LeafSettingsGuiProvider implements GuiProvider {
private static final MutableText RESET_TEXT = Text.translatable("text.cloth-config.reset_value");

public static String getModName(String modId) {
return FabricLoader.getInstance().getModContainer(modId).map(c -> c.getMetadata().getName()).orElse(modId);
}

@SuppressWarnings({"rawtypes", "unchecked", "ConstantConditions"})
@Override
public List<AbstractConfigListEntry> get(String i13n, Field field, Object config, Object defaults, GuiRegistryAccess registry) {
Expand All @@ -38,20 +42,20 @@ public List<AbstractConfigListEntry> get(String i13n, Field field, Object config

// Insert per-leaf settings ordered by translation name
leafSettings.entrySet().stream()
.filter((e) -> getBlock(e.getKey()) != null) // Only insert registered blocks
.sorted((e1, e2) -> TranslationComparator.INST.compare(getBlock(e1.getKey()).getTranslationKey(), getBlock(e2.getKey()).getTranslationKey()))
.filter(e -> Registries.BLOCK.containsId(e.getKey())) // Only insert registered blocks
.sorted((e1, e2) -> TranslationComparator.INST.compare(Registries.BLOCK.get(e1.getKey()).getTranslationKey(), Registries.BLOCK.get(e2.getKey()).getTranslationKey()))
.forEachOrdered((e) -> {
Identifier blockId = e.getKey();
LeafSettingsEntry leafEntry = e.getValue();
Block block = getBlock(blockId);
Block block = Registries.BLOCK.get(blockId);

MutableText text = Text.translatable(block.getTranslationKey());
if (!leafEntry.isDefault(blockId)) {
text.append("*");
}

SubCategoryBuilder builder = new SubCategoryBuilder(RESET_TEXT, text)
.setTooltip(Text.of(ModUtil.getModName(block)));
.setTooltip(Text.of(getModName(blockId.getNamespace())));

builder.add(buildSpawnRateFactorSlider(blockId, leafEntry));
builder.add(buildIsConiferLeavesToggle(blockId, leafEntry));
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/randommcsomethin/fallingleaves/init/Leaves.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
import java.util.Map;

import static randommcsomethin.fallingleaves.FallingLeavesClient.LOGGER;
import static randommcsomethin.fallingleaves.FallingLeavesClient.id;
import static randommcsomethin.fallingleaves.init.Config.CONFIG;
import static randommcsomethin.fallingleaves.util.LeafUtil.getLeafSettingsEntry;
import static randommcsomethin.fallingleaves.util.RegistryUtil.makeId;

public class Leaves {
public static final ParticleType<BlockStateParticleEffect> FALLING_LEAF;
Expand All @@ -49,9 +49,9 @@ public class Leaves {
FALLING_SNOW = FabricParticleTypes.complex(true, BlockStateParticleEffect::createCodec, BlockStateParticleEffect::createPacketCodec);

LEAVES = Map.of(
FALLING_LEAF, makeId("falling_leaf"),
FALLING_CONIFER_LEAF, makeId("falling_leaf_conifer"),
FALLING_SNOW, makeId("falling_snow")
FALLING_LEAF, id("falling_leaf"),
FALLING_CONIFER_LEAF, id("falling_leaf_conifer"),
FALLING_SNOW, id("falling_snow")
);
}

Expand Down Expand Up @@ -90,7 +90,7 @@ public void reload(ResourceManager resourceManager) {

@Override
public Identifier getFabricId() {
return makeId("resource_reload_listener");
return id("resource_reload_listener");
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@

import static randommcsomethin.fallingleaves.FallingLeavesClient.LOGGER;
import static randommcsomethin.fallingleaves.init.Config.CONFIG;
import static randommcsomethin.fallingleaves.util.RegistryUtil.getBlockId;

public class LeafUtil {

Expand Down Expand Up @@ -259,7 +258,7 @@ public static boolean isLeafBlock(Block block, boolean useBlockTags) {

@Nullable
public static LeafSettingsEntry getLeafSettingsEntry(BlockState blockState) {
return CONFIG.leafSettings.get(getBlockId(blockState));
return CONFIG.leafSettings.get(Registries.BLOCK.getId(blockState.getBlock()));
}

public static int getMaximumDistance(Vec3i v1, Vec3i v2) {
Expand Down
40 changes: 0 additions & 40 deletions src/main/java/randommcsomethin/fallingleaves/util/ModUtil.java

This file was deleted.

This file was deleted.

0 comments on commit b77c38f

Please sign in to comment.