Skip to content

Commit

Permalink
ID config and Multiplayer
Browse files Browse the repository at this point in the history
  • Loading branch information
Helinos committed Feb 3, 2025
1 parent 35f21a8 commit bef7190
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 32 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ loader_version=0.15.6-bta.7

# Other Mods
mod_menu_version=3.0.0
halplibe_version=5.1.1
halplibe_version=5.1.2

# Mod
mod_version=0
mod_version=0.1
mod_group=net.helinos
mod_name=moresnow
37 changes: 26 additions & 11 deletions src/main/java/net/helinos/moresnow/MoreSnow.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,48 @@

import net.fabricmc.api.ModInitializer;
import net.helinos.moresnow.block.MSBlocks;
import net.minecraft.core.block.Block;
import turniplabs.halplibe.util.TomlConfigHandler;
import turniplabs.halplibe.util.toml.Toml;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.io.File;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MoreSnow implements ModInitializer, BlockInitEntrypoint {
public static final String MOD_ID = "moresnow";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
public static final TomlConfigHandler config = new TomlConfigHandler(MOD_ID, new Toml("BTA + NFC configuration file."), false);

public static final List<Field> BLOCK_FIELDS = Arrays.stream(MSBlocks.class.getDeclaredFields()).filter(field -> Block.class.isAssignableFrom(field.getType())).collect(Collectors.toList());
public static final TomlConfigHandler CONFIG = new TomlConfigHandler(MOD_ID, new Toml("More Snow configuration file."), false);

static {
File configFile = CONFIG.getConfigFile();
if (configFile.exists()) {
CONFIG.loadConfig();
CONFIG.setDefaults(CONFIG.getRawParsed());
} else {
Toml defaultConfig = new Toml("More Snow configuration file.");
defaultConfig.addCategory("BlockIDs");

CONFIG.setDefaults(defaultConfig);

try {
configFile.getParentFile().mkdirs();
configFile.createNewFile();
CONFIG.writeConfig();
CONFIG.loadConfig();
} catch (IOException e) {
throw new RuntimeException("Failed to generate configuration file!", e);
}
}
}

@Override
public void onInitialize() {
LOGGER.info("More Snow initialized.");
}


@Override
public void afterBlockInit() {
MSBlocks.init(1050);
MSBlocks.init();
}
}
76 changes: 61 additions & 15 deletions src/main/java/net/helinos/moresnow/block/MSBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.llamalad7.mixinextras.lib.apache.commons.ArrayUtils;

import turniplabs.halplibe.helper.BlockBuilder;
import turniplabs.halplibe.util.toml.Toml;

public class MSBlocks {
public static Block<BlockLogicSnowyPlant<?, BlockLogicFlower>> SNOWY_PLANT;
Expand All @@ -45,9 +46,14 @@ public class MSBlocks {
public static int[] solidIds;
public static int[] blockIds;

public static void init(int minimumID) {
private static Toml rawConfig;
private static boolean configChanged = false;

public static void init() {
MoreSnow.LOGGER.info("Initializing Blocks.");

rawConfig = MoreSnow.CONFIG.getRawParsed();

ArrayList<Integer> excludedPlantIDs = new ArrayList<>();
for (Block<?> block : Blocks.blocksList) {
if (
Expand All @@ -62,56 +68,62 @@ public static void init(int minimumID) {
}
}

String key = "snowy_plant";
SNOWY_PLANT = new BlockBuilder(MoreSnow.MOD_ID)
.setBlockSound(BlockSounds.CLOTH)
.setHardness(0.1f)
.setUseInternalLight()
.setVisualUpdateOnMetadata()
.setTags(BlockTags.BROKEN_BY_FLUIDS, BlockTags.MINEABLE_BY_SHOVEL, BlockTags.OVERRIDE_STEPSOUND, BlockTags.NOT_IN_CREATIVE_MENU)
.build("snowy_plant", minimumID++, block -> new BlockLogicSnowyPlant<>(block, BlockLogicFlower.class, excludedPlantIDs));
.build(key, getID(key), block -> new BlockLogicSnowyPlant<>(block, BlockLogicFlower.class, excludedPlantIDs));

for (Block<?> flower : Blocks.blocksList) {
if (flower != null && flower.getLogic() instanceof BlockLogicFlowerStackable) {
String[] flowerKeys = flower.getKey().split("\\.");
key = "snowy_flower_stackable_" + flowerKeys[flowerKeys.length - 1];
Block<?> snowyFlowerStackable = new BlockBuilder(MoreSnow.MOD_ID)
.setBlockSound(BlockSounds.CLOTH)
.setHardness(0.1f)
.setUseInternalLight()
.setVisualUpdateOnMetadata()
.setTags(BlockTags.BROKEN_BY_FLUIDS, BlockTags.MINEABLE_BY_SHOVEL, BlockTags.OVERRIDE_STEPSOUND, BlockTags.NOT_IN_CREATIVE_MENU)
.build("snowy_" + flower.getKey(), minimumID++, block -> new BlockLogicSnowyFlowerStackable<>(block, flower.id()));
.build(key, getID(key), block -> new BlockLogicSnowyFlowerStackable<>(block, flower.id()));
SNOWY_FLOWER_STACKABLES.add(snowyFlowerStackable);
}
}

key = "snowy_slab";
SNOWY_SLAB = new BlockBuilder(MoreSnow.MOD_ID)
.setBlockSound(BlockSounds.CLOTH)
.setHardness(0.1f)
.setUseInternalLight()
.setLightOpacity(1)
.setVisualUpdateOnMetadata()
.setTags(BlockTags.MINEABLE_BY_SHOVEL, BlockTags.NOT_IN_CREATIVE_MENU)
.build("snowy_slab", minimumID++, block -> new BlockLogicSnowySlab<>(block, BlockLogicSlab.class, Collections.singletonList(Blocks.SLAB_PLANKS_PAINTED.id())));
.build(key, getID(key), block -> new BlockLogicSnowySlab<>(block, BlockLogicSlab.class, Collections.singletonList(Blocks.SLAB_PLANKS_PAINTED.id())));

key = "snowy_slab_painted";
SNOWY_SLAB_PAINTED = new BlockBuilder(MoreSnow.MOD_ID)
.setBlockSound(BlockSounds.CLOTH)
.setHardness(0.1f)
.setUseInternalLight()
.setLightOpacity(1)
.setVisualUpdateOnMetadata()
.setTags(BlockTags.MINEABLE_BY_SHOVEL, BlockTags.NOT_IN_CREATIVE_MENU)
.build("snowy_slab_painted", minimumID++, block -> new BlockLogicSnowySlabPainted<>(block, BlockLogicSlabPainted.class));
.build(key, getID(key), block -> new BlockLogicSnowySlabPainted<>(block, BlockLogicSlabPainted.class));

List<Integer> usedStairIDs = new ArrayList<>();
usedStairIDs.add(Blocks.STAIRS_PLANKS_PAINTED.id());
for(int index = 1; true; index++) {
key = "snowy_stairs_" + index;
Block<?> snowyStairs = new BlockBuilder(MoreSnow.MOD_ID)
.setBlockSound(BlockSounds.CLOTH)
.setHardness(0.1f)
.setUseInternalLight()
.setLightOpacity(15)
.setVisualUpdateOnMetadata()
.setTags(BlockTags.MINEABLE_BY_SHOVEL, BlockTags.NOT_IN_CREATIVE_MENU)
.build("snowy_stairs_" + index, minimumID++, block -> new BlockLogicSnowyStairs<>(block, BlockLogicStairs.class, usedStairIDs));
.build(key, getID(key), block -> new BlockLogicSnowyStairs<>(block, BlockLogicStairs.class, usedStairIDs));
SNOWY_STAIRS.add(snowyStairs);

if (((BlockLogicSnowyStairs<?, ?>) snowyStairs.getLogic()).USED_IDS.size() >= 16) {
Expand All @@ -121,64 +133,70 @@ public static void init(int minimumID) {
}
}

key = "snowy_stairs_painted";
SNOWY_STAIRS_PAINTED = new BlockBuilder(MoreSnow.MOD_ID)
.setBlockSound(BlockSounds.CLOTH)
.setHardness(0.1f)
.setUseInternalLight()
.setLightOpacity(15)
.setVisualUpdateOnMetadata()
.setTags(BlockTags.MINEABLE_BY_SHOVEL, BlockTags.NOT_IN_CREATIVE_MENU)
.build("snowy.stairs.painted", minimumID++, block -> new BlockLogicSnowyStairsPainted<>(block, BlockLogicStairsPainted.class));
.build(key, getID(key), block -> new BlockLogicSnowyStairsPainted<>(block, BlockLogicStairsPainted.class));

key = "snowy_partial";
SNOWY_PARTIAL = new BlockBuilder(MoreSnow.MOD_ID)
.setBlockSound(BlockSounds.CLOTH)
.setHardness(0.1f)
.setUseInternalLight()
.setVisualUpdateOnMetadata()
.setTags(BlockTags.MINEABLE_BY_SHOVEL, BlockTags.OVERRIDE_STEPSOUND, BlockTags.BROKEN_BY_FLUIDS, BlockTags.PLACE_OVERWRITES, BlockTags.NOT_IN_CREATIVE_MENU)
.build("snowy.partial", minimumID++, block -> new BlockLogicSnowyPartial<>(block));
.build(key, getID(key), block -> new BlockLogicSnowyPartial<>(block));

key = "snowy_fence";
SNOWY_FENCE = new BlockBuilder(MoreSnow.MOD_ID)
.setBlockSound(BlockSounds.CLOTH)
.setHardness(0.1f)
.setUseInternalLight()
.setVisualUpdateOnMetadata()
.setTags(BlockTags.MINEABLE_BY_SHOVEL, BlockTags.OVERRIDE_STEPSOUND, BlockTags.FENCES_CONNECT, BlockTags.NOT_IN_CREATIVE_MENU)
.build("snowy.fence", minimumID++, block -> new BlockLogicSnowyFence<>(block, BlockLogicFence.class, Collections.singletonList(Blocks.FENCE_PLANKS_OAK_PAINTED.id())));
.build(key, getID(key), block -> new BlockLogicSnowyFence<>(block, BlockLogicFence.class, Collections.singletonList(Blocks.FENCE_PLANKS_OAK_PAINTED.id())));

key = "snowy_fence_painted";
SNOWY_FENCE_PAINTED = new BlockBuilder(MoreSnow.MOD_ID)
.setBlockSound(BlockSounds.CLOTH)
.setHardness(0.1f)
.setUseInternalLight()
.setVisualUpdateOnMetadata()
.setTags(BlockTags.MINEABLE_BY_SHOVEL, BlockTags.OVERRIDE_STEPSOUND, BlockTags.FENCES_CONNECT, BlockTags.NOT_IN_CREATIVE_MENU)
.build("snowy.fence.painted", minimumID++, block -> new BlockLogicSnowyFencePainted<>(block, BlockLogicFencePainted.class));
.build(key, getID(key), block -> new BlockLogicSnowyFencePainted<>(block, BlockLogicFencePainted.class));

key = "snowy_fence_wallpaper";
SNOWY_FENCE_WALLPAPER = new BlockBuilder(MoreSnow.MOD_ID)
.setBlockSound(BlockSounds.CLOTH)
.setHardness(0.1f)
.setUseInternalLight()
.setVisualUpdateOnMetadata()
.setTags(BlockTags.MINEABLE_BY_SHOVEL, BlockTags.OVERRIDE_STEPSOUND, BlockTags.FENCES_CONNECT, BlockTags.NOT_IN_CREATIVE_MENU)
.build("snowy.fence.wall_paper", minimumID++, BlockLogicSnowyFenceWallPaper::new);
.build(key, getID(key), BlockLogicSnowyFenceWallPaper::new);

key = "snowy_fence_steel";
SNOWY_FENCE_STEEL = new BlockBuilder(MoreSnow.MOD_ID)
.setBlockSound(BlockSounds.CLOTH)
.setHardness(0.1f)
.setUseInternalLight()
.setVisualUpdateOnMetadata()
.setTags(BlockTags.MINEABLE_BY_SHOVEL, BlockTags.OVERRIDE_STEPSOUND, BlockTags.CHAINLINK_FENCES_CONNECT, BlockTags.NOT_IN_CREATIVE_MENU)
.build("snowy.fence.steel", minimumID++, BlockLogicSnowyFenceSteel::new);
.build(key, getID(key), BlockLogicSnowyFenceSteel::new);

key = "snowy_fence_chainlink";
SNOWY_FENCE_CHAINLINK = new BlockBuilder(MoreSnow.MOD_ID)
.setBlockSound(BlockSounds.CLOTH)
.setBlockSound(BlockSounds.CLOTH)
.setHardness(0.1f)
.setUseInternalLight()
.setVisualUpdateOnMetadata()
.setTags(BlockTags.MINEABLE_BY_SHOVEL, BlockTags.OVERRIDE_STEPSOUND, BlockTags.CHAINLINK_FENCES_CONNECT, BlockTags.NOT_IN_CREATIVE_MENU)
.build("snowy.fence.chainlink", minimumID++, BlockLogicSnowyFenceChainlink::new);

.build(key, getID(key), BlockLogicSnowyFenceChainlink::new);

blockIds = new int[] {
SNOWY_PLANT.id(),
Expand All @@ -201,6 +219,12 @@ public static void init(int minimumID) {
SNOWY_STAIRS.stream().mapToInt(block -> block.id()).toArray()
);

if (configChanged) {
MoreSnow.CONFIG.setDefaults(rawConfig);
MoreSnow.CONFIG.writeConfig();
MoreSnow.CONFIG.loadConfig();
}

MoreSnow.LOGGER.info("Initialized Blocks.");
}

Expand All @@ -224,7 +248,6 @@ public static boolean tryMakeSnowy(World world, int id, int x, int y, int z) {
return placed;
}


public static boolean tryMakeSnowy(Chunk chunk, int id, int x, int y, int z) {
boolean placed = false;
for (int whichId : blockIds) {
Expand All @@ -235,4 +258,27 @@ public static boolean tryMakeSnowy(Chunk chunk, int id, int x, int y, int z) {
}
return placed;
}

private static int nextBlockID = 0;

public static int getID(String key) {
boolean containsBlock;
String category = "BlockIDs.";
try {
containsBlock = rawConfig.contains(category + key);
} catch (NullPointerException e) {
containsBlock = false;
}

if (containsBlock) {
return MoreSnow.CONFIG.getInt(category + key);
}

while (Blocks.blocksList[++nextBlockID] != null) {}

rawConfig.addEntry(category + key, nextBlockID);
configChanged = true;

return nextBlockID;
}
}
18 changes: 18 additions & 0 deletions src/main/java/net/helinos/moresnow/mixin/MinecraftServerMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package net.helinos.moresnow.mixin;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import net.fabricmc.loader.api.FabricLoader;
import net.helinos.moresnow.BlockInitEntrypoint;
import net.minecraft.server.MinecraftServer;

@Mixin(value = MinecraftServer.class, remap = false)
public class MinecraftServerMixin {
@Inject(method = "startServer", at = @At(value = "INVOKE", target = "Lnet/minecraft/core/block/Blocks;init()V", shift = At.Shift.AFTER))
public void afterBlockInitEntrypoint(CallbackInfoReturnable<?> callbackInfoReturnable) {
FabricLoader.getInstance().getEntrypoints("afterBlockInit", BlockInitEntrypoint.class).forEach(BlockInitEntrypoint::afterBlockInit);;
}
}
3 changes: 2 additions & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
],
"depends": {
"minecraft": "^7.3",
"fabricloader": ">=0.15.5"
"fabricloader": ">=0.15.5",
"halplibe": ">=5.1.2"
},
"suggests": {}
}
10 changes: 7 additions & 3 deletions src/main/resources/moresnow.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
"package": "net.helinos.moresnow.mixin",
"compatibilityLevel": "JAVA_8",
"mixins": [
"BlockModelGrassMixin",
"BlockLogicLayerSnowMixin",
"ItemBlockLayerMixin",
"WeatherMixin",
"WeatherSnowMixin",
"WeatherSnowMixin"
],
"client": [
"BlockModelGrassMixin",
"MinecraftMixin"
],
"client": [],
"server": [
"MinecraftServerMixin"
],
"injectors": {
"defaultRequire": 1
}
Expand Down

0 comments on commit bef7190

Please sign in to comment.