Skip to content

Commit b77c38f

Browse files
committed
small refactor
1 parent 9ba7ea3 commit b77c38f

File tree

6 files changed

+21
-84
lines changed

6 files changed

+21
-84
lines changed

src/main/java/randommcsomethin/fallingleaves/FallingLeavesClient.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import net.fabricmc.api.ClientModInitializer;
44
import net.fabricmc.api.EnvType;
55
import net.fabricmc.api.Environment;
6+
import net.minecraft.util.Identifier;
67
import org.apache.logging.log4j.LogManager;
78
import org.apache.logging.log4j.Logger;
89
import randommcsomethin.fallingleaves.init.Config;
@@ -11,12 +12,13 @@
1112

1213
@Environment(EnvType.CLIENT)
1314
public class FallingLeavesClient implements ClientModInitializer {
14-
15-
/** The mod's unique identifier, used to avoid mod conflicts in the Registry and config files */
1615
public static final String MOD_ID = "fallingleaves";
17-
1816
public static final Logger LOGGER = LogManager.getLogger(MOD_ID);
1917

18+
public static Identifier id(String path) {
19+
return Identifier.of(MOD_ID, path);
20+
}
21+
2022
@Override
2123
public void onInitializeClient() {
2224
Config.init();

src/main/java/randommcsomethin/fallingleaves/config/gui/LeafSettingsGuiProvider.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
import me.shedaniel.clothconfig2.impl.builders.BooleanToggleBuilder;
99
import me.shedaniel.clothconfig2.impl.builders.IntSliderBuilder;
1010
import me.shedaniel.clothconfig2.impl.builders.SubCategoryBuilder;
11+
import net.fabricmc.loader.api.FabricLoader;
1112
import net.minecraft.block.Block;
13+
import net.minecraft.registry.Registries;
1214
import net.minecraft.text.MutableText;
1315
import net.minecraft.text.Text;
1416
import net.minecraft.util.Identifier;
1517
import randommcsomethin.fallingleaves.config.ConfigDefaults;
1618
import randommcsomethin.fallingleaves.config.LeafSettingsEntry;
17-
import randommcsomethin.fallingleaves.util.ModUtil;
1819
import randommcsomethin.fallingleaves.util.TranslationComparator;
1920

2021
import java.lang.reflect.Field;
@@ -24,11 +25,14 @@
2425
import java.util.Map;
2526

2627
import static randommcsomethin.fallingleaves.FallingLeavesClient.LOGGER;
27-
import static randommcsomethin.fallingleaves.util.RegistryUtil.getBlock;
2828

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

32+
public static String getModName(String modId) {
33+
return FabricLoader.getInstance().getModContainer(modId).map(c -> c.getMetadata().getName()).orElse(modId);
34+
}
35+
3236
@SuppressWarnings({"rawtypes", "unchecked", "ConstantConditions"})
3337
@Override
3438
public List<AbstractConfigListEntry> get(String i13n, Field field, Object config, Object defaults, GuiRegistryAccess registry) {
@@ -38,20 +42,20 @@ public List<AbstractConfigListEntry> get(String i13n, Field field, Object config
3842

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

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

5357
SubCategoryBuilder builder = new SubCategoryBuilder(RESET_TEXT, text)
54-
.setTooltip(Text.of(ModUtil.getModName(block)));
58+
.setTooltip(Text.of(getModName(blockId.getNamespace())));
5559

5660
builder.add(buildSpawnRateFactorSlider(blockId, leafEntry));
5761
builder.add(buildIsConiferLeavesToggle(blockId, leafEntry));

src/main/java/randommcsomethin/fallingleaves/init/Leaves.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
import java.util.Map;
3030

3131
import static randommcsomethin.fallingleaves.FallingLeavesClient.LOGGER;
32+
import static randommcsomethin.fallingleaves.FallingLeavesClient.id;
3233
import static randommcsomethin.fallingleaves.init.Config.CONFIG;
3334
import static randommcsomethin.fallingleaves.util.LeafUtil.getLeafSettingsEntry;
34-
import static randommcsomethin.fallingleaves.util.RegistryUtil.makeId;
3535

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

5151
LEAVES = Map.of(
52-
FALLING_LEAF, makeId("falling_leaf"),
53-
FALLING_CONIFER_LEAF, makeId("falling_leaf_conifer"),
54-
FALLING_SNOW, makeId("falling_snow")
52+
FALLING_LEAF, id("falling_leaf"),
53+
FALLING_CONIFER_LEAF, id("falling_leaf_conifer"),
54+
FALLING_SNOW, id("falling_snow")
5555
);
5656
}
5757

@@ -90,7 +90,7 @@ public void reload(ResourceManager resourceManager) {
9090

9191
@Override
9292
public Identifier getFabricId() {
93-
return makeId("resource_reload_listener");
93+
return id("resource_reload_listener");
9494
}
9595
});
9696
}

src/main/java/randommcsomethin/fallingleaves/util/LeafUtil.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838

3939
import static randommcsomethin.fallingleaves.FallingLeavesClient.LOGGER;
4040
import static randommcsomethin.fallingleaves.init.Config.CONFIG;
41-
import static randommcsomethin.fallingleaves.util.RegistryUtil.getBlockId;
4241

4342
public class LeafUtil {
4443

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

260259
@Nullable
261260
public static LeafSettingsEntry getLeafSettingsEntry(BlockState blockState) {
262-
return CONFIG.leafSettings.get(getBlockId(blockState));
261+
return CONFIG.leafSettings.get(Registries.BLOCK.getId(blockState.getBlock()));
263262
}
264263

265264
public static int getMaximumDistance(Vec3i v1, Vec3i v2) {

src/main/java/randommcsomethin/fallingleaves/util/ModUtil.java

-40
This file was deleted.

src/main/java/randommcsomethin/fallingleaves/util/RegistryUtil.java

-28
This file was deleted.

0 commit comments

Comments
 (0)