Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.tterrag.moonlightmobs;
- import javax.annotation.ParametersAreNonnullByDefault;
- import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
- import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
- import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
- import it.unimi.dsi.fastutil.ints.IntSet;
- import net.minecraft.entity.EnumCreatureType;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.util.text.Style;
- import net.minecraft.util.text.TextComponentString;
- import net.minecraft.util.text.TextFormatting;
- import net.minecraft.world.World;
- import net.minecraftforge.common.config.Config;
- import net.minecraftforge.event.world.WorldEvent.PotentialSpawns;
- import net.minecraftforge.fml.client.event.ConfigChangedEvent;
- import net.minecraftforge.fml.common.Mod;
- import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
- import net.minecraftforge.fml.common.Mod.EventHandler;
- import net.minecraftforge.fml.common.Mod.Instance;
- import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
- import net.minecraftforge.fml.common.event.FMLServerStartingEvent;
- import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
- import net.minecraftforge.fml.common.gameevent.PlayerEvent.PlayerChangedDimensionEvent;
- @Mod(modid = "moonlightmobs", name = "Moonlight Mobs", version = "@VERSION@", acceptableRemoteVersions = "*")
- @EventBusSubscriber
- @ParametersAreNonnullByDefault
- public class MoonlightMobs {
- @Config(modid = "moonlightmobs")
- public static class Configs {
- public static int[] validDimensions = new int[] { 0 };
- }
- enum WarningState {
- NONE,
- STARTING_DAY,
- STARTING_NIGHT,
- ENDING
- }
- private static Int2ObjectMap<WarningState> states = new Int2ObjectOpenHashMap<>();
- static {
- states.defaultReturnValue(WarningState.NONE);
- }
- @Instance
- private static MoonlightMobs instance;
- private IntSet validDimensions = new IntOpenHashSet();
- @SubscribeEvent
- public static void onConfigChanged(ConfigChangedEvent.PostConfigChangedEvent event) {
- if (event.getModID().equals("moonlightmobs")) {
- instance.validDimensions = new IntOpenHashSet(Configs.validDimensions);
- }
- }
- @EventHandler
- public void postInit(FMLPostInitializationEvent event) {
- instance.validDimensions = new IntOpenHashSet(Configs.validDimensions);
- }
- @EventHandler
- public void onServerStarting(FMLServerStartingEvent event) {
- states.clear();
- }
- private static final String NIGHT_END = "The curse of the full moon has lifted!";
- private static final String DAY_START = "Beware, the full moon is starting! Mobs will begin to spawn in dark places.";
- private static final String DAY_TO_NIGHT = "The full moon is now at its peak. Mobs will spawn on the surface!";
- private static final String NIGHT_START = "Beware, the full moon has started! Mobs will begin to spawn.";
- @SubscribeEvent
- public static void checkSpawn(PotentialSpawns event) {
- World world = event.getWorld();
- int dim = world.provider.getDimension();
- if (!world.isRemote && event.getType() == EnumCreatureType.MONSTER && instance.validDimensions.contains(dim)) {
- WarningState state = states.get(world.provider.getDimension());
- if (event.getWorld().getCurrentMoonPhaseFactor() < 1 - 1e-6) {
- if (state != WarningState.ENDING) {
- if (state != WarningState.NONE) {
- sendMessage(world, TextFormatting.GREEN, NIGHT_END);
- }
- states.put(dim, WarningState.ENDING);
- }
- event.getList().clear();
- } else {
- long time = event.getWorld().getWorldTime() % 24000;
- if (state != WarningState.STARTING_DAY && state != WarningState.STARTING_NIGHT && time < 12500) {
- sendMessage(world, TextFormatting.YELLOW, DAY_START);
- states.put(dim, WarningState.STARTING_DAY);
- } else if (time >= 12500) {
- if (state == WarningState.STARTING_DAY) {
- sendMessage(world, TextFormatting.RED, DAY_TO_NIGHT);
- } else if (state != WarningState.STARTING_NIGHT) {
- sendMessage(world, TextFormatting.RED, NIGHT_START);
- }
- states.put(dim, WarningState.STARTING_NIGHT);
- }
- }
- }
- }
- @SubscribeEvent
- public static void onPlayerChangeDimension(PlayerChangedDimensionEvent event) {
- if (!event.player.world.isRemote) {
- WarningState oldState = states.get(event.fromDim);
- WarningState newState = states.get(event.toDim);
- if (oldState != newState) {
- if ((newState == WarningState.ENDING && oldState != WarningState.NONE) || (newState == WarningState.NONE && oldState != WarningState.ENDING)) {
- sendMessage(event.player, TextFormatting.GREEN, NIGHT_END);
- } else if (newState == WarningState.STARTING_DAY) {
- sendMessage(event.player, TextFormatting.YELLOW, DAY_START);
- } else if (newState == WarningState.STARTING_NIGHT) {
- if (oldState != WarningState.STARTING_DAY) {
- sendMessage(event.player, TextFormatting.RED, NIGHT_START);
- } else {
- sendMessage(event.player, TextFormatting.RED, DAY_TO_NIGHT);
- }
- }
- }
- }
- }
- private static void sendMessage(World world, TextFormatting color, String text) {
- world.playerEntities.forEach(p -> sendMessage(p, color, text));
- }
- private static void sendMessage(EntityPlayer player, TextFormatting color, String text) {
- player.sendMessage(new TextComponentString(text).setStyle(new Style().setColor(color)));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment