Skip to content

Commit

Permalink
Port to 1.21
Browse files Browse the repository at this point in the history
  • Loading branch information
NertzhulDEV committed Jul 21, 2024
1 parent ed49f84 commit 44430d2
Show file tree
Hide file tree
Showing 24 changed files with 294 additions and 192 deletions.
7 changes: 7 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -211,5 +211,12 @@ publishing {
}


}
}

idea {
module {
isDownloadSources = true
isDownloadJavadoc = true
}
}
14 changes: 7 additions & 7 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
org.gradle.jvmargs=-Xmx4G

# Gradle Plugins
loom_version=1.1-SNAPSHOT
loom_version=1.7-SNAPSHOT
grgit_version=5.0.0
cursegradle_version=1.4.0
modrinth_version=2.7.3
Expand All @@ -13,16 +13,16 @@ mod_version=2.3+1.20
maven_group=io.github.lucaargolo

# Fabric Properties
minecraft_version=1.20.1
yarn_mappings=1.20.1+build.10
loader_version=0.14.22
minecraft_version=1.21
yarn_mappings=1.21+build.9
loader_version=0.15.11

#Fabric api
fabric_version=0.86.1+1.20.1
fabric_version=0.100.7+1.21

#Libraries
modmenu_version=7.2.1
rei_version=12.0.645
modmenu_version=11.0.1
rei_version=16.0.729

#Publishing
curseforge_id=413523
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
80 changes: 41 additions & 39 deletions src/main/java/io/github/lucaargolo/seasons/FabricSeasons.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@
import com.google.gson.JsonParser;
import io.github.lucaargolo.seasons.commands.SeasonCommand;
import io.github.lucaargolo.seasons.mixed.BiomeMixed;
import io.github.lucaargolo.seasons.payload.ConfigSyncPacket;
import io.github.lucaargolo.seasons.payload.UpdateCropsPaycket;
import io.github.lucaargolo.seasons.resources.CropConfigs;
import io.github.lucaargolo.seasons.utils.*;
import io.github.lucaargolo.seasons.utils.GreenhouseCache;
import io.github.lucaargolo.seasons.utils.ModConfig;
import io.github.lucaargolo.seasons.utils.PlacedMeltablesState;
import io.github.lucaargolo.seasons.utils.ReplacedMeltablesState;
import io.github.lucaargolo.seasons.utils.Season;
import io.github.lucaargolo.seasons.utils.SeasonalFertilizable;
import it.unimi.dsi.fastutil.longs.LongArraySet;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.fabricmc.loader.api.FabricLoader;
Expand All @@ -23,7 +30,6 @@
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.registry.Registries;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
Expand Down Expand Up @@ -62,78 +68,74 @@ public class FabricSeasons implements ModInitializer {

public static HashMap<Item, Block> SEEDS_MAP = new HashMap<>();

public static Identifier ASK_FOR_CONFIG = new ModIdentifier("ask_for_config");
public static Identifier ANSWER_CONFIG = new ModIdentifier("anwer_config");

public static Identifier UPDATE_CROPS = new ModIdentifier("update_crops");


@Override
public void onInitialize() {

Path configPath = FabricLoader.getInstance().getConfigDir();
File configFile = new File(configPath + File.separator + "seasons.json");

LOGGER.info("["+MOD_NAME+"] Trying to read config file...");
LOGGER.info("[" + MOD_NAME + "] Trying to read config file...");
try {
if (configFile.createNewFile()) {
LOGGER.info("["+MOD_NAME+"] No config file found, creating a new one...");
LOGGER.info("[" + MOD_NAME + "] No config file found, creating a new one...");
String json = GSON.toJson(JsonParser.parseString(GSON.toJson(new ModConfig())));
try (PrintWriter out = new PrintWriter(configFile)) {
out.println(json);
}
CONFIG = new ModConfig();
LOGGER.info("["+MOD_NAME+"] Successfully created default config file.");
LOGGER.info("[" + MOD_NAME + "] Successfully created default config file.");
} else {
LOGGER.info("["+MOD_NAME+"] A config file was found, loading it..");
LOGGER.info("[" + MOD_NAME + "] A config file was found, loading it..");
CONFIG = GSON.fromJson(new String(Files.readAllBytes(configFile.toPath())), ModConfig.class);
if(CONFIG == null) {
throw new NullPointerException("["+MOD_NAME+"] The config file was empty.");
}else{
LOGGER.info("["+MOD_NAME+"] Successfully loaded config file.");
if (CONFIG == null) {
throw new NullPointerException("[" + MOD_NAME + "] The config file was empty.");
} else {
LOGGER.info("[" + MOD_NAME + "] Successfully loaded config file.");
}
}
}catch (Exception exception) {
LOGGER.error("["+MOD_NAME+"] There was an error creating/loading the config file!", exception);
} catch (Exception exception) {
LOGGER.error("[" + MOD_NAME + "] There was an error creating/loading the config file!", exception);
CONFIG = new ModConfig();
LOGGER.warn("["+MOD_NAME+"] Defaulting to original config.");
LOGGER.warn("[" + MOD_NAME + "] Defaulting to original config.");
}

CommandRegistrationCallback.EVENT.register((dispatcher, dedicated, ignored) -> SeasonCommand.register(dispatcher));

ServerLifecycleEvents.SERVER_STARTED.register(server -> {
SEEDS_MAP.clear();
Registries.ITEM.forEach(item -> {
if(item instanceof BlockItem) {
if (item instanceof BlockItem) {
Block block = ((BlockItem) item).getBlock();
if(block instanceof SeasonalFertilizable) {
if (block instanceof SeasonalFertilizable) {
FabricSeasons.SEEDS_MAP.put(item, ((BlockItem) item).getBlock());
}
}
});
});


PayloadTypeRegistry.playS2C().register(UpdateCropsPaycket.ID, UpdateCropsPaycket.CODEC);
ServerLifecycleEvents.SYNC_DATA_PACK_CONTENTS.register((player, joined) -> {
PacketByteBuf buf = PacketByteBufs.create();
CropConfigs.getDefaultCropConfig().toBuf(buf);
CropConfigs.toBuf(buf);
ServerPlayNetworking.send(player, UPDATE_CROPS, buf);
ServerPlayNetworking.send(player, UpdateCropsPaycket.fromConfig(CropConfigs.getDefaultCropConfig(), CropConfigs.getCropConfigMap()));
});

ServerTickEvents.END_SERVER_TICK.register(server -> {
GreenhouseCache.tick(server);
temporaryMeltableCache.clear();
});

ServerPlayNetworking.registerGlobalReceiver(ASK_FOR_CONFIG, (server, player, handler, buf, responseSender) -> {
PayloadTypeRegistry.playS2C().register(ConfigSyncPacket.ID, ConfigSyncPacket.CODEC);
PayloadTypeRegistry.playC2S().register(ConfigSyncPacket.ID, ConfigSyncPacket.CODEC);

ServerPlayNetworking.registerGlobalReceiver(ConfigSyncPacket.ID, (payload, context) -> {
String configJson = GSON.toJson(JsonParser.parseString(GSON.toJson(CONFIG)));
PacketByteBuf configBuf = PacketByteBufs.create();
configBuf.writeString(configJson);
ServerPlayNetworking.send(player, ANSWER_CONFIG, configBuf);
ServerPlayNetworking.send(context.player(), new ConfigSyncPacket(configJson));
});

ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(new CropConfigs());
}

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

public static void setMeltable(BlockPos blockPos) {
temporaryMeltableCache.add(blockPos.asLong());
Expand All @@ -144,11 +146,11 @@ public static boolean isMeltable(BlockPos blockPos) {
}

public static PlacedMeltablesState getPlacedMeltablesState(ServerWorld world) {
return world.getPersistentStateManager().getOrCreate(PlacedMeltablesState::createFromNbt, PlacedMeltablesState::new, "seasons_placed_meltables");
return world.getPersistentStateManager().getOrCreate(PlacedMeltablesState.getPersistentStateType(), "seasons_placed_meltables");
}

public static ReplacedMeltablesState getReplacedMeltablesState(ServerWorld world) {
return world.getPersistentStateManager().getOrCreate(ReplacedMeltablesState::createFromNbt, ReplacedMeltablesState::new, "seasons_replaced_meltables");
return world.getPersistentStateManager().getOrCreate(ReplacedMeltablesState.getPersistentStateType(), "seasons_replaced_meltables");
}

public static long getTimeToNextSeason(World world) {
Expand Down Expand Up @@ -337,8 +339,8 @@ else if (m == 12 && d > 20)
return season;
}

private static final TagKey<Biome> IGNORED_CATEGORIES_TAG = TagKey.of(RegistryKeys.BIOME, new Identifier(FabricSeasons.MOD_ID, "ignored"));
private static final TagKey<Biome> JUNGLE_LIKE_TAG = TagKey.of(RegistryKeys.BIOME, new Identifier(FabricSeasons.MOD_ID, "jungle_like"));
private static final TagKey<Biome> IGNORED_CATEGORIES_TAG = TagKey.of(RegistryKeys.BIOME, FabricSeasons.identifier("ignored"));
private static final TagKey<Biome> JUNGLE_LIKE_TAG = TagKey.of(RegistryKeys.BIOME, FabricSeasons.identifier("jungle_like"));

public static void injectBiomeTemperature(RegistryEntry<Biome> entry, World world) {
if(entry.isIn(IGNORED_CATEGORIES_TAG))
Expand Down
73 changes: 39 additions & 34 deletions src/main/java/io/github/lucaargolo/seasons/FabricSeasonsClient.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package io.github.lucaargolo.seasons;

import io.github.lucaargolo.seasons.commands.SeasonDebugCommand;
import io.github.lucaargolo.seasons.payload.ConfigSyncPacket;
import io.github.lucaargolo.seasons.payload.UpdateCropsPaycket;
import io.github.lucaargolo.seasons.resources.CropConfigs;
import io.github.lucaargolo.seasons.resources.FoliageSeasonColors;
import io.github.lucaargolo.seasons.resources.GrassSeasonColors;
import io.github.lucaargolo.seasons.utils.*;
import io.github.lucaargolo.seasons.utils.CompatWarnState;
import io.github.lucaargolo.seasons.utils.CropConfig;
import io.github.lucaargolo.seasons.utils.ModConfig;
import io.github.lucaargolo.seasons.utils.Season;
import io.github.lucaargolo.seasons.utils.SeasonalFertilizable;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.fabricmc.fabric.api.resource.ResourcePackActivationType;
import net.fabricmc.loader.api.FabricLoader;
Expand All @@ -29,7 +34,9 @@
import java.util.HashMap;
import java.util.Map;

import static io.github.lucaargolo.seasons.FabricSeasons.*;
import static io.github.lucaargolo.seasons.FabricSeasons.CONFIG;
import static io.github.lucaargolo.seasons.FabricSeasons.MOD_ID;
import static io.github.lucaargolo.seasons.FabricSeasons.MOD_NAME;

public class FabricSeasonsClient implements ClientModInitializer {

Expand All @@ -44,72 +51,70 @@ public void onInitializeClient() {
clientConfig = FabricSeasons.CONFIG;
ResourceManagerHelper.get(ResourceType.CLIENT_RESOURCES).registerReloadListener(new GrassSeasonColors());
ResourceManagerHelper.get(ResourceType.CLIENT_RESOURCES).registerReloadListener(new FoliageSeasonColors());

ClientLifecycleEvents.CLIENT_STARTED.register(client -> {
FabricSeasons.SEEDS_MAP.clear();
Registries.ITEM.forEach(item -> {
if(item instanceof BlockItem) {
if (item instanceof BlockItem) {
Block block = ((BlockItem) item).getBlock();
if(block instanceof SeasonalFertilizable) {
if (block instanceof SeasonalFertilizable) {
FabricSeasons.SEEDS_MAP.put(item, ((BlockItem) item).getBlock());
}
}
});
});

ClientTickEvents.END_WORLD_TICK.register((clientWorld) -> {
if(FabricSeasons.getCurrentSeason(clientWorld) != lastRenderedSeasonMap.get(clientWorld.getRegistryKey())) {
if (FabricSeasons.getCurrentSeason(clientWorld) != lastRenderedSeasonMap.get(clientWorld.getRegistryKey())) {
lastRenderedSeasonMap.put(clientWorld.getRegistryKey(), FabricSeasons.getCurrentSeason(clientWorld));
MinecraftClient.getInstance().worldRenderer.reload();
}
});

ClientPlayNetworking.registerGlobalReceiver(FabricSeasons.ANSWER_CONFIG, (client, handler, buf, responseSender) -> {
String configJson = buf.readString();
client.execute(() -> {
ClientPlayNetworking.registerGlobalReceiver(ConfigSyncPacket.ID, (payload, handler) -> {
String configJson = payload.config();
handler.client().execute(() -> {
FabricSeasons.CONFIG = FabricSeasons.GSON.fromJson(configJson, ModConfig.class);
isServerConfig = true;
FabricSeasons.LOGGER.info("["+MOD_NAME+"] Received dedicated server config.");
FabricSeasons.LOGGER.info("[" + MOD_NAME + "] Received dedicated server config.");
});

});

ClientPlayNetworking.registerGlobalReceiver(FabricSeasons.UPDATE_CROPS, (client, handler, buf, responseSender) -> {
CropConfig receivedConfig = CropConfig.fromBuf(buf);
HashMap<Identifier, CropConfig> receivedMap = CropConfigs.fromBuf(buf);
client.execute(() -> {

ClientPlayNetworking.registerGlobalReceiver(UpdateCropsPaycket.ID, (payload, context) -> {
CropConfig receivedConfig = payload.cropConfig();
HashMap<Identifier, CropConfig> receivedMap = payload.cropConfigMap();

context.client().execute(() -> {
CropConfigs.receiveConfig(receivedConfig, receivedMap);
FabricSeasons.LOGGER.info("["+MOD_NAME+"] Received dedicated server crops.");
FabricSeasons.LOGGER.info("[" + MOD_NAME + "] Received dedicated server crops.");
});

});



ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> {
if(CONFIG.shouldNotifyCompat()) {
if (CONFIG.shouldNotifyCompat()) {
CompatWarnState.join(client);
}
if(!client.isIntegratedServerRunning()) {
FabricSeasons.LOGGER.info("["+MOD_NAME+"] Joined dedicated server, asking for config.");
ClientPlayNetworking.send(ASK_FOR_CONFIG, PacketByteBufs.empty());
if (!client.isIntegratedServerRunning()) {
FabricSeasons.LOGGER.info("[" + MOD_NAME + "] Joined dedicated server, asking for config.");
ClientPlayNetworking.send(new ConfigSyncPacket("request"));
}
});

ClientPlayConnectionEvents.DISCONNECT.register(((handler, client) -> {
CropConfigs.clear();
if(isServerConfig && clientConfig != null) {
FabricSeasons.LOGGER.info("["+MOD_NAME+"] Left dedicated server, restoring config.");
if (isServerConfig && clientConfig != null) {
FabricSeasons.LOGGER.info("[" + MOD_NAME + "] Left dedicated server, restoring config.");
FabricSeasons.CONFIG = clientConfig;
isServerConfig = false;
}
}));

if(FabricLoader.getInstance().isDevelopmentEnvironment() || CONFIG.isDebugCommandEnabled()) {
if (FabricLoader.getInstance().isDevelopmentEnvironment() || CONFIG.isDebugCommandEnabled()) {
ClientCommandRegistrationCallback.EVENT.register((SeasonDebugCommand::register));
}

FabricLoader.getInstance().getModContainer(MOD_ID).ifPresent((container) -> {
ResourceManagerHelper.registerBuiltinResourcePack(new ModIdentifier("seasonal_lush_caves"), container, Text.literal("Seasonal Lush Caves"), ResourcePackActivationType.DEFAULT_ENABLED);
ResourceManagerHelper.registerBuiltinResourcePack(FabricSeasons.identifier("seasonal_lush_caves"), container, Text.literal("Seasonal Lush Caves"), ResourcePackActivationType.DEFAULT_ENABLED);
});
}
}
Loading

0 comments on commit 44430d2

Please sign in to comment.