commit ef1cbff9f3ab46b7ca2ba5e51c54ac340a5dd92c Author: felden.r@grenoble.archi.fr Date: Mon Mar 3 14:53:45 2025 +0100 Le mods est fonctionnel en 1.20.1 sur Forge diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/domobspawningatnight.mcreator b/domobspawningatnight.mcreator new file mode 100644 index 0000000..7523a56 --- /dev/null +++ b/domobspawningatnight.mcreator @@ -0,0 +1,59 @@ +{ + "mod_elements": [ + { + "name": "DoMobSpawningAtNight", + "type": "gamerule", + "compiles": true, + "locked_code": false, + "registry_name": "do_mob_spawning_at_night", + "metadata": { + "files": [], + "type": "logic" + } + }, + { + "name": "SurfaceSpawnController", + "type": "code", + "compiles": true, + "locked_code": true, + "registry_name": "surface_spawn_controller", + "metadata": { + "files": [ + "src/main/java/net/mcreator/domobspawningatnight/SurfaceSpawnController.java" + ] + } + } + ], + "variable_elements": [], + "sound_elements": [], + "tag_elements": {}, + "tab_element_order": {}, + "language_map": { + "en_us": { + "gamerule.doMobSpawningAtNight.description": "Reduit fortement le spawn de mobs la nuit tombés ", + "gamerule.doMobSpawningAtNight": "doReduceMobSpawningAtNight" + } + }, + "foldersRoot": { + "name": "~", + "children": [] + }, + "workspaceSettings": { + "modid": "domobspawningatnight", + "modName": "do Mob Spawning At Night", + "version": "1.0.0", + "description": "Empeche les mobs de spawn a la surface via une gamerule est activé de base", + "author": "felden.r, MCreator", + "websiteURL": "https://mcreator.net", + "license": "Not specified", + "serverSideOnly": false, + "requiredMods": [], + "dependencies": [], + "dependants": [], + "mcreatorDependencies": [], + "currentGenerator": "forge-1.20.1", + "credits": "Libre de modification", + "modElementsPackage": "net.mcreator.domobspawningatnight" + }, + "mcreatorVersion": 202400452410 +} \ No newline at end of file diff --git a/elements/DoMobSpawningAtNight.mod.json b/elements/DoMobSpawningAtNight.mod.json new file mode 100644 index 0000000..6a6c1cc --- /dev/null +++ b/elements/DoMobSpawningAtNight.mod.json @@ -0,0 +1,12 @@ +{ + "_fv": 73, + "_type": "gamerule", + "definition": { + "type": "Logic", + "displayName": "doReduceMobSpawningAtNight", + "description": "Reduit fortement le spawn de mobs la nuit tombés ", + "category": "MOBS", + "defaultValueLogic": true, + "defaultValueNumber": 0 + } +} \ No newline at end of file diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..f237dcf --- /dev/null +++ b/gradle.properties @@ -0,0 +1,2 @@ +org.gradle.jvmargs=-Xmx3G +org.gradle.daemon=false \ No newline at end of file diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..93f1b80 --- /dev/null +++ b/settings.gradle @@ -0,0 +1,13 @@ +pluginManagement { + repositories { + gradlePluginPortal() + maven { + name = 'MinecraftForge' + url = 'https://maven.minecraftforge.net/' + } + } +} + +plugins { + id 'org.gradle.toolchains.foojay-resolver-convention' version '0.7.0' +} \ No newline at end of file diff --git a/src/main/java/net/mcreator/domobspawningatnight/DomobspawningatnightMod.java b/src/main/java/net/mcreator/domobspawningatnight/DomobspawningatnightMod.java new file mode 100644 index 0000000..8e1ea00 --- /dev/null +++ b/src/main/java/net/mcreator/domobspawningatnight/DomobspawningatnightMod.java @@ -0,0 +1,75 @@ +package net.mcreator.domobspawningatnight; + +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.LogManager; + +import net.minecraftforge.network.simple.SimpleChannel; +import net.minecraftforge.network.NetworkRegistry; +import net.minecraftforge.network.NetworkEvent; +import net.minecraftforge.fml.util.thread.SidedThreadGroups; +import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.eventbus.api.IEventBus; +import net.minecraftforge.event.TickEvent; +import net.minecraftforge.common.MinecraftForge; + +import net.minecraft.resources.ResourceLocation; +import net.minecraft.network.FriendlyByteBuf; + +import java.util.function.Supplier; +import java.util.function.Function; +import java.util.function.BiConsumer; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.List; +import java.util.Collection; +import java.util.ArrayList; +import java.util.AbstractMap; + +@Mod("domobspawningatnight") +public class DomobspawningatnightMod { + public static final Logger LOGGER = LogManager.getLogger(DomobspawningatnightMod.class); + public static final String MODID = "domobspawningatnight"; + + public DomobspawningatnightMod() { + // Start of user code block mod constructor + // End of user code block mod constructor + MinecraftForge.EVENT_BUS.register(this); + IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus(); + + // Start of user code block mod init + // End of user code block mod init + } + + // Start of user code block mod methods + // End of user code block mod methods + private static final String PROTOCOL_VERSION = "1"; + public static final SimpleChannel PACKET_HANDLER = NetworkRegistry.newSimpleChannel(new ResourceLocation(MODID, MODID), () -> PROTOCOL_VERSION, PROTOCOL_VERSION::equals, PROTOCOL_VERSION::equals); + private static int messageID = 0; + + public static void addNetworkMessage(Class messageType, BiConsumer encoder, Function decoder, BiConsumer> messageConsumer) { + PACKET_HANDLER.registerMessage(messageID, messageType, encoder, decoder, messageConsumer); + messageID++; + } + + private static final Collection> workQueue = new ConcurrentLinkedQueue<>(); + + public static void queueServerWork(int tick, Runnable action) { + if (Thread.currentThread().getThreadGroup() == SidedThreadGroups.SERVER) + workQueue.add(new AbstractMap.SimpleEntry<>(action, tick)); + } + + @SubscribeEvent + public void tick(TickEvent.ServerTickEvent event) { + if (event.phase == TickEvent.Phase.END) { + List> actions = new ArrayList<>(); + workQueue.forEach(work -> { + work.setValue(work.getValue() - 1); + if (work.getValue() == 0) + actions.add(work); + }); + actions.forEach(e -> e.getKey().run()); + workQueue.removeAll(actions); + } + } +} diff --git a/src/main/java/net/mcreator/domobspawningatnight/SurfaceSpawnController.java b/src/main/java/net/mcreator/domobspawningatnight/SurfaceSpawnController.java new file mode 100644 index 0000000..ce6c563 --- /dev/null +++ b/src/main/java/net/mcreator/domobspawningatnight/SurfaceSpawnController.java @@ -0,0 +1,119 @@ +package net.mcreator.domobspawningatnight.core; + +import net.mcreator.domobspawningatnight.init.DomobspawningatnightModGameRules; +import net.minecraft.ChatFormatting; +import net.minecraft.core.BlockPos; +import net.minecraft.network.chat.Component; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.entity.Mob; +import net.minecraft.world.entity.MobCategory; +import net.minecraft.world.entity.MobSpawnType; +import net.minecraft.world.level.LightLayer; +import net.minecraftforge.event.TickEvent; +import net.minecraftforge.event.entity.living.MobSpawnEvent; +import net.minecraftforge.eventbus.api.Event.Result; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.common.Mod; + +import java.util.HashMap; +import java.util.Map; + +@Mod.EventBusSubscriber +public class SurfaceSpawnController { + + private static final Map MOON_PHASE_TRACKER = new HashMap<>(); + private static final int MOON_CHECK_INTERVAL = 200; // Toutes les 10 secondes + + @SubscribeEvent + public static void onMobSpawnCheck(MobSpawnEvent.PositionCheck event) { + if (!(event.getLevel() instanceof ServerLevel level)) return; + if (!(event.getEntity() instanceof Mob)) return; + Mob mob = (Mob) event.getEntity(); + + if (!level.getGameRules().getBoolean(DomobspawningatnightModGameRules.DO_MOB_SPAWNING_AT_NIGHT)) return; + + BlockPos spawnPos = mob.blockPosition(); + SpawnContext context = new SpawnContext( + level.getDayTime() % 24000, + level.getMoonPhase(), + spawnPos, + level.getSeaLevel(), + level.canSeeSky(spawnPos), + level.getBrightness(LightLayer.SKY, spawnPos) + ); + + if (isValidHostileSpawn(mob, event.getSpawnType()) && shouldBlockSpawn(context)) { + event.setResult(Result.DENY); + } + } + + @SubscribeEvent + public static void onServerTick(TickEvent.ServerTickEvent event) { + if (event.phase == TickEvent.Phase.END && event.getServer().getTickCount() % MOON_CHECK_INTERVAL == 0) { + checkFullMoonTransition(event.getServer()); + } + } + + private static void checkFullMoonTransition(MinecraftServer server) { + for (ServerLevel level : server.getAllLevels()) { + int currentPhase = level.getMoonPhase(); + Integer lastPhase = MOON_PHASE_TRACKER.get(level); + + if (lastPhase == null || currentPhase != lastPhase) { + handleMoonPhaseChange(level, currentPhase, lastPhase); + MOON_PHASE_TRACKER.put(level, currentPhase); + } + } + } + + private static void handleMoonPhaseChange(ServerLevel level, int newPhase, Integer oldPhase) { + if (newPhase == 0 && (oldPhase == null || oldPhase != 0)) { + broadcastFullMoonWarning(level.getServer()); + } + } + + private static void broadcastFullMoonWarning(MinecraftServer server) { + Component warning = Component.literal("The full moon is shining! Hostile creatures roam free!") + .withStyle(ChatFormatting.DARK_RED, ChatFormatting.ITALIC); + + for (ServerPlayer player : server.getPlayerList().getPlayers()) { + player.sendSystemMessage(warning, true); + } + } + + private static boolean isValidHostileSpawn(Mob mob, MobSpawnType spawnType) { + return mob.getType().getCategory() == MobCategory.MONSTER && + spawnType == MobSpawnType.NATURAL; + } + + private static boolean shouldBlockSpawn(SpawnContext context) { + return isNightTime(context.time) && + isExposedSurface(context) && + !isFullMoon(context.moonPhase); + } + + private static boolean isNightTime(long worldTime) { + return worldTime >= 13000 && worldTime < 23000; + } + + private static boolean isFullMoon(int moonPhase) { + return moonPhase == 0; + } + + private static boolean isExposedSurface(SpawnContext context) { + return context.hasSkyAccess && + context.position.getY() >= context.seaLevel && + context.skyLight >= 10; + } + + private record SpawnContext( + long time, + int moonPhase, + BlockPos position, + int seaLevel, + boolean hasSkyAccess, + int skyLight + ) {} +} \ No newline at end of file diff --git a/src/main/java/net/mcreator/domobspawningatnight/init/DomobspawningatnightModGameRules.java b/src/main/java/net/mcreator/domobspawningatnight/init/DomobspawningatnightModGameRules.java new file mode 100644 index 0000000..95a86d2 --- /dev/null +++ b/src/main/java/net/mcreator/domobspawningatnight/init/DomobspawningatnightModGameRules.java @@ -0,0 +1,14 @@ + +/* + * MCreator note: This file will be REGENERATED on each build. + */ +package net.mcreator.domobspawningatnight.init; + +import net.minecraftforge.fml.common.Mod; + +import net.minecraft.world.level.GameRules; + +@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD) +public class DomobspawningatnightModGameRules { + public static final GameRules.Key DO_MOB_SPAWNING_AT_NIGHT = GameRules.register("doMobSpawningAtNight", GameRules.Category.MOBS, GameRules.BooleanValue.create(true)); +} diff --git a/src/main/resources/META-INF/mods.toml b/src/main/resources/META-INF/mods.toml new file mode 100644 index 0000000..ee34f3b --- /dev/null +++ b/src/main/resources/META-INF/mods.toml @@ -0,0 +1,28 @@ +modLoader="javafml" +loaderVersion="[47,)" +license="Not specified" + +[[mods]] +modId="domobspawningatnight" +version="1.0.0" +displayName="do Mob Spawning At Night" +displayURL="https://mcreator.net" +credits="Libre de modification" +authors="felden.r, MCreator" +description="Empeche les mobs de spawn a la surface via une gamerule est activ\u00E9 de base" + +# Start of user code block mod configuration +# End of user code block mod configuration + +[[dependencies.domobspawningatnight]] + modId="minecraft" + mandatory=true + versionRange="[1.20.1]" + ordering="AFTER" + side="BOTH" + + + + +# Start of user code block dependencies configuration +# End of user code block dependencies configuration \ No newline at end of file diff --git a/src/main/resources/assets/domobspawningatnight/lang/en_us.json b/src/main/resources/assets/domobspawningatnight/lang/en_us.json new file mode 100644 index 0000000..b489e18 --- /dev/null +++ b/src/main/resources/assets/domobspawningatnight/lang/en_us.json @@ -0,0 +1,4 @@ +{ + "gamerule.doMobSpawningAtNight.description": "Reduit fortement le spawn de mobs la nuit tombés ", + "gamerule.doMobSpawningAtNight": "doReduceMobSpawningAtNight" +} \ No newline at end of file diff --git a/src/main/resources/pack.mcmeta b/src/main/resources/pack.mcmeta new file mode 100644 index 0000000..ff3ee81 --- /dev/null +++ b/src/main/resources/pack.mcmeta @@ -0,0 +1,6 @@ +{ + "pack": { + "pack_format": 15, + "description": "Empeche les mobs de spawn a la surface via une gamerule est activé de base" + } +} \ No newline at end of file