tterrag1098

Untitled

May 24th, 2020
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. package com.tterrag.moonlightmobs;
  2.  
  3. import javax.annotation.ParametersAreNonnullByDefault;
  4.  
  5. import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
  6. import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
  7. import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
  8. import it.unimi.dsi.fastutil.ints.IntSet;
  9. import net.minecraft.entity.EnumCreatureType;
  10. import net.minecraft.entity.player.EntityPlayer;
  11. import net.minecraft.util.text.Style;
  12. import net.minecraft.util.text.TextComponentString;
  13. import net.minecraft.util.text.TextFormatting;
  14. import net.minecraft.world.World;
  15. import net.minecraftforge.common.config.Config;
  16. import net.minecraftforge.event.world.WorldEvent.PotentialSpawns;
  17. import net.minecraftforge.fml.client.event.ConfigChangedEvent;
  18. import net.minecraftforge.fml.common.Mod;
  19. import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
  20. import net.minecraftforge.fml.common.Mod.EventHandler;
  21. import net.minecraftforge.fml.common.Mod.Instance;
  22. import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
  23. import net.minecraftforge.fml.common.event.FMLServerStartingEvent;
  24. import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
  25. import net.minecraftforge.fml.common.gameevent.PlayerEvent.PlayerChangedDimensionEvent;
  26.  
  27. @Mod(modid = "moonlightmobs", name = "Moonlight Mobs", version = "@VERSION@", acceptableRemoteVersions = "*")
  28. @EventBusSubscriber
  29. @ParametersAreNonnullByDefault
  30. public class MoonlightMobs {
  31.  
  32. @Config(modid = "moonlightmobs")
  33. public static class Configs {
  34.  
  35. public static int[] validDimensions = new int[] { 0 };
  36. }
  37.  
  38. enum WarningState {
  39. NONE,
  40. STARTING_DAY,
  41. STARTING_NIGHT,
  42. ENDING
  43. }
  44.  
  45. private static Int2ObjectMap<WarningState> states = new Int2ObjectOpenHashMap<>();
  46. static {
  47. states.defaultReturnValue(WarningState.NONE);
  48. }
  49.  
  50. @Instance
  51. private static MoonlightMobs instance;
  52.  
  53. private IntSet validDimensions = new IntOpenHashSet();
  54.  
  55. @SubscribeEvent
  56. public static void onConfigChanged(ConfigChangedEvent.PostConfigChangedEvent event) {
  57. if (event.getModID().equals("moonlightmobs")) {
  58. instance.validDimensions = new IntOpenHashSet(Configs.validDimensions);
  59. }
  60. }
  61.  
  62. @EventHandler
  63. public void postInit(FMLPostInitializationEvent event) {
  64. instance.validDimensions = new IntOpenHashSet(Configs.validDimensions);
  65. }
  66.  
  67. @EventHandler
  68. public void onServerStarting(FMLServerStartingEvent event) {
  69. states.clear();
  70. }
  71.  
  72. private static final String NIGHT_END = "The curse of the full moon has lifted!";
  73. private static final String DAY_START = "Beware, the full moon is starting! Mobs will begin to spawn in dark places.";
  74. private static final String DAY_TO_NIGHT = "The full moon is now at its peak. Mobs will spawn on the surface!";
  75. private static final String NIGHT_START = "Beware, the full moon has started! Mobs will begin to spawn.";
  76.  
  77. @SubscribeEvent
  78. public static void checkSpawn(PotentialSpawns event) {
  79. World world = event.getWorld();
  80. int dim = world.provider.getDimension();
  81. if (!world.isRemote && event.getType() == EnumCreatureType.MONSTER && instance.validDimensions.contains(dim)) {
  82. WarningState state = states.get(world.provider.getDimension());
  83. if (event.getWorld().getCurrentMoonPhaseFactor() < 1 - 1e-6) {
  84. if (state != WarningState.ENDING) {
  85. if (state != WarningState.NONE) {
  86. sendMessage(world, TextFormatting.GREEN, NIGHT_END);
  87. }
  88. states.put(dim, WarningState.ENDING);
  89. }
  90. event.getList().clear();
  91. } else {
  92. long time = event.getWorld().getWorldTime() % 24000;
  93. if (state != WarningState.STARTING_DAY && state != WarningState.STARTING_NIGHT && time < 12500) {
  94. sendMessage(world, TextFormatting.YELLOW, DAY_START);
  95. states.put(dim, WarningState.STARTING_DAY);
  96. } else if (time >= 12500) {
  97. if (state == WarningState.STARTING_DAY) {
  98. sendMessage(world, TextFormatting.RED, DAY_TO_NIGHT);
  99. } else if (state != WarningState.STARTING_NIGHT) {
  100. sendMessage(world, TextFormatting.RED, NIGHT_START);
  101. }
  102. states.put(dim, WarningState.STARTING_NIGHT);
  103. }
  104. }
  105. }
  106. }
  107.  
  108. @SubscribeEvent
  109. public static void onPlayerChangeDimension(PlayerChangedDimensionEvent event) {
  110. if (!event.player.world.isRemote) {
  111. WarningState oldState = states.get(event.fromDim);
  112. WarningState newState = states.get(event.toDim);
  113. if (oldState != newState) {
  114. if ((newState == WarningState.ENDING && oldState != WarningState.NONE) || (newState == WarningState.NONE && oldState != WarningState.ENDING)) {
  115. sendMessage(event.player, TextFormatting.GREEN, NIGHT_END);
  116. } else if (newState == WarningState.STARTING_DAY) {
  117. sendMessage(event.player, TextFormatting.YELLOW, DAY_START);
  118. } else if (newState == WarningState.STARTING_NIGHT) {
  119. if (oldState != WarningState.STARTING_DAY) {
  120. sendMessage(event.player, TextFormatting.RED, NIGHT_START);
  121. } else {
  122. sendMessage(event.player, TextFormatting.RED, DAY_TO_NIGHT);
  123. }
  124. }
  125. }
  126. }
  127. }
  128.  
  129. private static void sendMessage(World world, TextFormatting color, String text) {
  130. world.playerEntities.forEach(p -> sendMessage(p, color, text));
  131. }
  132.  
  133. private static void sendMessage(EntityPlayer player, TextFormatting color, String text) {
  134. player.sendMessage(new TextComponentString(text).setStyle(new Style().setColor(color)));
  135. }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment