Mod Ding
Mod Ding
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@Mod(MysticAppleMod.MODID)
public class MysticAppleMod {
public static final String MODID = "mysticapple";
private static final Logger LOGGER = LogManager.getLogger();
public MysticAppleMod() {
IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus();
bus.addListener(this::setup);
bus.addListener(this::doClientStuff);
}
private void setup(final FMLCommonSetupEvent event) {
LOGGER.info("HELLO FROM PREINIT");
}
import net.minecraft.item.Item;
import net.minecraft.item.Food;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.Item.Properties;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.registries.ObjectHolder;
@SubscribeEvent
public static void registerItems(final RegistryEvent.Register<Item> event) {
event.getRegistry().register(new Item(new Properties()
.group(ItemGroup.FOOD)
.food(new Food.Builder().hunger(4).saturation(1.2f).effect(() -> new
EffectInstance(Effects.SPEED, 600, 1), 1.0f).build()))
.setRegistryName("mystic_apple"));
}
}
4. Distribution
• Ensure you comply with licensing for distribution.
• You can share your .jar file or upload it to a modding site like CurseForge.
Overview
1. Zombies Break Wooden Doors:
• On Normal difficulty, zombies will break wooden doors.
2. Difficulty Timer:
• Start the game in Peaceful.
• After 1 Minecraft day (20 minutes), set the difficulty to Easy.
• After another 5 Minecraft days (100 minutes), set the difficulty to Normal.
Step-by-Step Implementation
1. Setup the Modding Environment
Follow the instructions to set up Minecraft Forge and your modding environment from the previous
answer. You should already have the JDK, Forge MDK, and an IDE.
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.event.server.FMLServerStartingEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
@Mod(ZombieMod.MODID)
public class ZombieMod {
public static final String MODID = "zombiemod";
public ZombieMod() {
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup);
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::doClientStuff);
MinecraftForge.EVENT_BUS.register(this);
MinecraftForge.EVENT_BUS.register(new DifficultyChanger());
}
private void setup(final FMLCommonSetupEvent event) {
}
@SubscribeEvent
public void onServerStarting(FMLServerStartingEvent event) {
event.getServer().getWorldData().setDifficulty(net.minecraft.world.Difficulty.PEACE
FUL);
}
}
2. Zombie Behavior: Modify zombie behavior to break wooden doors on Normal difficulty.
java
Copy code
// com.example.zombiemod.ZombieDoorBreaking.java
package com.example.zombiemod;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.monster.ZombieEntity;
import net.minecraft.world.Difficulty;
import net.minecraft.world.World;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
@SubscribeEvent
public static void onEntityJoinWorld(EntityJoinWorldEvent event) {
if (event.getEntity() instanceof ZombieEntity) {
ZombieEntity zombie = (ZombieEntity) event.getEntity();
World world = event.getWorld();
if (world.getDifficulty() == Difficulty.NORMAL) {
zombie.goalSelector.addGoal(1, new
net.minecraft.entity.ai.goal.BreakDoorGoal(zombie,
net.minecraft.block.Blocks.OAK_DOOR));
}
}
}
}
3. Difficulty Changer: Create a class to handle difficulty changes based on in-game time.
java
Copy code
// com.example.zombiemod.DifficultyChanger.java
package com.example.zombiemod;
import net.minecraft.world.Difficulty;
import net.minecraft.world.World;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
@SubscribeEvent
public void onWorldTick(TickEvent.WorldTickEvent event) {
if (event.phase == TickEvent.Phase.START) {
return;
}
4. Add Mod Metadata: Modify mods.toml file to include your mod's information.
toml
Copy code
modLoader="javafml"
loaderVersion="[36,)"
license="MIT"
[[mods]]
modId="zombiemod"
version="1.0"
displayName="Zombie Mod"
description='Zombies break wooden doors on normal difficulty and game difficulty
changes over time.'
2. Test: Copy the generated .jar file from build/libs to your Minecraft mods folder and test
it in-game.
Summary of Classes
• ZombieMod.java: Main class for the mod.
• ZombieDoorBreaking.java: Handles zombie door-breaking behavior.
• DifficultyChanger.java: Changes game difficulty based on the in-game time.
Additional Notes
• Ensure your mod is placed in the src/main/java directory following the package structure.
• Textures and additional resources are not needed for this specific mod.
• Testing in a development environment can be done by running the runClient task in your
IDE.
Resources
• Forge Documentation: Forge Docs
• Minecraft Block API: Minecraft Block Documentation
Feel free to enhance or modify the mod further based on your needs!
4o
does this take into account sleeping through nights
ChatGPT
The initial implementation does not account for players sleeping through the night, which affects the
in-game time progression. We need to adjust the DifficultyChanger to correctly handle time
progression when players sleep.
Modified DifficultyChanger.java
We'll adjust the DifficultyChanger to check the actual in-game time instead of relying solely on
tick counting. We'll also reset the tick counter when players sleep through the night to ensure accurate
timing.
Here's the updated code:
java
Copy code
// com.example.zombiemod.DifficultyChanger.java
package com.example.zombiemod;
import net.minecraft.world.Difficulty;
import net.minecraft.world.server.ServerWorld;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.world.SleepFinishedTimeEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
@SubscribeEvent
public static void onWorldTick(TickEvent.WorldTickEvent event) {
if (event.phase == TickEvent.Phase.START || !(event.world instanceof
ServerWorld)) {
return;
}
@SubscribeEvent
public static void onSleepFinished(SleepFinishedTimeEvent event) {
long time = event.getNewTime();
long day = time / DAY_TICKS;
long easyDay = EASY_TICKS / DAY_TICKS;
long normalDay = NORMAL_TICKS / DAY_TICKS;
event.getWorld().getServer().getWorldData().setDifficulty(Difficulty.EASY);
} else if (day >= normalDay) {
event.getWorld().getServer().getWorldData().setDifficulty(Difficulty.NORMAL);
}
}
}
Explanation
1. Tick Counting: We use world.getDayTime() to get the current in-game time in ticks.
This method accounts for time changes due to sleeping.
2. Difficulty Change Timing:
• EASY_TICKS defines when to change to Easy difficulty.
• NORMAL_TICKS defines when to change to Normal difficulty.
• The game starts at 0 ticks and transitions to Easy after 1 day (24000 ticks), and to
Normal after 6 days (144000 ticks).
3. Handling Sleeping: The onSleepFinished method resets the world time when players
finish sleeping, adjusting the difficulty based on the new time. This ensures that sleeping does
not disrupt the difficulty progression schedule.
@Mod(ZombieMod.MODID)
public class ZombieMod {
public static final String MODID = "zombiemod";
public ZombieMod() {
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup);
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::doClientStuff);
MinecraftForge.EVENT_BUS.register(this);
MinecraftForge.EVENT_BUS.register(new DifficultyChanger());
MinecraftForge.EVENT_BUS.register(new ZombieDoorBreaking());
}
@SubscribeEvent
public void onServerStarting(FMLServerStartingEvent event) {
event.getServer().getWorldData().setDifficulty(net.minecraft.world.Difficulty.PEACE
FUL);
}
}
import net.minecraft.entity.EntityType;
import net.minecraft.entity.monster.ZombieEntity;
import net.minecraft.world.Difficulty;
import net.minecraft.world.World;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
@SubscribeEvent
public static void onEntityJoinWorld(EntityJoinWorldEvent event) {
if (event.getEntity() instanceof ZombieEntity) {
ZombieEntity zombie = (ZombieEntity) event.getEntity();
World world = event.getWorld();
if (world.getDifficulty() == Difficulty.NORMAL) {
zombie.goalSelector.addGoal(1, new
net.minecraft.entity.ai.goal.BreakDoorGoal(zombie,
net.minecraft.block.Blocks.OAK_DOOR));
}
}
}
}
import net.minecraft.world.Difficulty;
import net.minecraft.world.server.ServerWorld;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.world.SleepFinishedTimeEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
@SubscribeEvent
public static void onWorldTick(TickEvent.WorldTickEvent event) {
if (event.phase == TickEvent.Phase.START || !(event.world instanceof
ServerWorld)) {
return;
}
@SubscribeEvent
public static void onSleepFinished(SleepFinishedTimeEvent event) {
long time = event.getNewTime();
long day = time / DAY_TICKS;
long easyDay = EASY_TICKS / DAY_TICKS;
long normalDay = NORMAL_TICKS / DAY_TICKS;
event.getWorld().getServer().getWorldData().setDifficulty(Difficulty.EASY);
} else if (day >= normalDay) {
event.getWorld().getServer().getWorldData().setDifficulty(Difficulty.NORMAL);
}
}
}
Additional Notes
• Testing: After building and placing your mod .jar file in the mods folder, you can test it in
Minecraft. Ensure you test by changing the time manually and sleeping to verify the difficulty
changes correctly.
• Debugging: Use log statements if needed to check the time and difficulty transitions.
Feel free to further customize the mod or add more features as you see fit!
4o