From ba6f8c03f60860d9dea8550cea92b158ed6314a7 Mon Sep 17 00:00:00 2001 From: Roadhog360 Date: Mon, 12 Feb 2024 00:20:06 -0600 Subject: [PATCH] Optimize LightLevel0 config option, tweaks Tweak config comments Add (untested) EndlessIDs AND NotEnoughIDs compat Add slightly more utils to ModsList for more precise version checking --- .../java/ganymedes01/etfuturum/EtFuturum.java | 2 +- .../etfuturum/api/BeePlantRegistry.java | 6 +- .../etfuturum/api/DeepslateOreRegistry.java | 4 +- .../etfuturum/api/StrippedLogRegistry.java | 12 ++-- .../etfuturum/compat/ModsList.java | 11 +++ .../configuration/configs/ConfigEntities.java | 8 +-- .../core/handlers/ServerEventHandler.java | 68 ++++++++++++++----- .../etfuturum/core/utils/Utils.java | 44 ++++++++++++ 8 files changed, 124 insertions(+), 31 deletions(-) diff --git a/src/main/java/ganymedes01/etfuturum/EtFuturum.java b/src/main/java/ganymedes01/etfuturum/EtFuturum.java index 04c93647e..3794413f7 100644 --- a/src/main/java/ganymedes01/etfuturum/EtFuturum.java +++ b/src/main/java/ganymedes01/etfuturum/EtFuturum.java @@ -261,7 +261,7 @@ public void init(FMLInitializationEvent event) { // int test = ModsList.MULTIPART.compareVersion("1.4.1"); // String test2 = ModsList.MULTIPART.getVersion(); // } - if (ModsList.MULTIPART.isLoaded() && ModsList.MULTIPART.isVersionNewer("1.4.1")) { + if (ModsList.MULTIPART.isLoaded() && ModsList.MULTIPART.isVersionNewerOrEqual("1.4.2")) { try { Class button = ReflectionHelper.getClass(getClass().getClassLoader(), "codechicken.multipart.minecraft.ButtonPart"); diff --git a/src/main/java/ganymedes01/etfuturum/api/BeePlantRegistry.java b/src/main/java/ganymedes01/etfuturum/api/BeePlantRegistry.java index b6aac8b16..071092139 100644 --- a/src/main/java/ganymedes01/etfuturum/api/BeePlantRegistry.java +++ b/src/main/java/ganymedes01/etfuturum/api/BeePlantRegistry.java @@ -25,11 +25,11 @@ public class BeePlantRegistry { * @param block */ public static void addFlower(Block block, int meta) { - if (block instanceof BlockDoublePlant && meta > 7 && meta != OreDictionary.WILDCARD_VALUE) { - throw new IllegalArgumentException("BlockDoublePlant can't have meta greater than 7, 8 and above are used by the top half. Bees will go to the top half if the bottom meta is valid."); + if (block instanceof BlockDoublePlant && BlockDoublePlant.func_149887_c(meta) && meta != OreDictionary.WILDCARD_VALUE) { + throw new IllegalArgumentException("BlockDoublePlant can't have meta using bit 8, it is for the top half. Bees will go to the top half if the bottom meta is valid."); } if (ModRecipes.validateItems(block)) { - BEE_FLOWERS.add(RegistryMapping.getKeyFor(block, meta)); + BEE_FLOWERS.add(new RegistryMapping<>(block, meta)); } } diff --git a/src/main/java/ganymedes01/etfuturum/api/DeepslateOreRegistry.java b/src/main/java/ganymedes01/etfuturum/api/DeepslateOreRegistry.java index 4e8b8131f..396bc05e0 100644 --- a/src/main/java/ganymedes01/etfuturum/api/DeepslateOreRegistry.java +++ b/src/main/java/ganymedes01/etfuturum/api/DeepslateOreRegistry.java @@ -60,8 +60,8 @@ public static void addOre(Block from, int fromMeta, Block to, int toMeta) { if (from instanceof ITileEntityProvider || to instanceof ITileEntityProvider) { throw new IllegalArgumentException("Block Entities are not supported for the deepslate ore registry!"); } - if (((fromMeta < 0 || fromMeta > 15) && fromMeta != OreDictionary.WILDCARD_VALUE) || ((toMeta < 0 || toMeta > 15) && toMeta != OreDictionary.WILDCARD_VALUE)) { - throw new IllegalArgumentException("Meta must be between 0 and 15 (inclusive), you may be trying to add an ItemStack-only meta value (maybe used for Block Entity data?"); + if (!Utils.isMetaInBlockBoundsIgnoreWildcard(fromMeta) || !Utils.isMetaInBlockBoundsIgnoreWildcard(toMeta)) { + throw new IllegalArgumentException("Meta must be between " + Utils.getMinMetadata() + " and " + Utils.getMaxMetadata() + " (inclusive)."); } deepslateOres.put(new RegistryMapping<>(from, fromMeta), new RegistryMapping<>(to, toMeta)); } diff --git a/src/main/java/ganymedes01/etfuturum/api/StrippedLogRegistry.java b/src/main/java/ganymedes01/etfuturum/api/StrippedLogRegistry.java index 7f8296537..d2b0b2ea2 100644 --- a/src/main/java/ganymedes01/etfuturum/api/StrippedLogRegistry.java +++ b/src/main/java/ganymedes01/etfuturum/api/StrippedLogRegistry.java @@ -3,6 +3,7 @@ import ganymedes01.etfuturum.ModBlocks; import ganymedes01.etfuturum.api.mappings.RegistryMapping; import ganymedes01.etfuturum.configuration.configs.ConfigBlocksItems; +import ganymedes01.etfuturum.core.utils.Utils; import net.minecraft.block.Block; import net.minecraft.init.Blocks; @@ -11,7 +12,7 @@ public class StrippedLogRegistry { - private static final Map, RegistryMapping> strippedLogs = new HashMap, RegistryMapping>(); + private static final Map, RegistryMapping> strippedLogs = new HashMap<>(); /** * Adds a specified log and its metadata to be converted to another specified log and its metadata. @@ -24,10 +25,13 @@ public class StrippedLogRegistry { * @param toMeta Wrapped by 4 */ public static void addLog(Block from, int fromMeta, Block to, int toMeta) { - addLog(new RegistryMapping<>(from, fromMeta % 4), new RegistryMapping<>(to, toMeta % 4)); + addLog(new RegistryMapping<>(from, fromMeta & 3), new RegistryMapping<>(to, toMeta & 3)); } private static void addLog(RegistryMapping from, RegistryMapping to) { + if (!Utils.isMetaInBlockBoundsIgnoreWildcard(from.getMeta()) || !Utils.isMetaInBlockBoundsIgnoreWildcard(to.getMeta())) { + throw new IllegalArgumentException("Meta must be between " + Utils.getMinMetadata() + " and " + Utils.getMaxMetadata() + " (inclusive)."); + } strippedLogs.put(from, to); } @@ -37,7 +41,7 @@ private static void addLog(RegistryMapping from, RegistryMapping t * @return True if this log and its metadata has a stripped variant. */ public static boolean hasLog(Block block, int meta) { - return hasLog(new RegistryMapping<>(block, meta % 4)); + return hasLog(RegistryMapping.getKeyFor(block, meta & 3)); } private static boolean hasLog(RegistryMapping map) { @@ -52,7 +56,7 @@ private static boolean hasLog(RegistryMapping map) { * the block instance and the meta data it should be replaced with. */ public static RegistryMapping getLog(Block block, int meta) { - return getLog(new RegistryMapping<>(block, meta % 4)); + return getLog(RegistryMapping.getKeyFor(block, meta & 3)); } private static RegistryMapping getLog(RegistryMapping map) { diff --git a/src/main/java/ganymedes01/etfuturum/compat/ModsList.java b/src/main/java/ganymedes01/etfuturum/compat/ModsList.java index eb5da3211..09ed5f106 100644 --- a/src/main/java/ganymedes01/etfuturum/compat/ModsList.java +++ b/src/main/java/ganymedes01/etfuturum/compat/ModsList.java @@ -35,6 +35,9 @@ public enum ModsList { ARS_MAGICA_2("arsmagica2"), MULTIPART("McMultipart"), DRACONIC_EVOLUTION("DraconicEvolution"), + + NOT_ENOUGH_IDS("neid"), + ENDLESS_IDS("endlessids"), ; private final String modID; @@ -71,10 +74,18 @@ public boolean isVersionNewer(String compareTo) { return compareVersion(compareTo) > 0; } + public boolean isVersionNewerOrEqual(String compareTo) { + return compareVersion(compareTo) >= 0; + } + public boolean isVersionEqual(String compareTo) { return compareVersion(compareTo) == 0; } + public boolean isVersionOlderOrEqual(String compareTo) { + return compareVersion(compareTo) <= 0; + } + public boolean isVersionOlder(String compareTo) { return compareVersion(compareTo) < 0; } diff --git a/src/main/java/ganymedes01/etfuturum/configuration/configs/ConfigEntities.java b/src/main/java/ganymedes01/etfuturum/configuration/configs/ConfigEntities.java index 30aea262f..f3ade9813 100644 --- a/src/main/java/ganymedes01/etfuturum/configuration/configs/ConfigEntities.java +++ b/src/main/java/ganymedes01/etfuturum/configuration/configs/ConfigEntities.java @@ -33,7 +33,7 @@ public ConfigEntities(File file) { setCategoryComment(catNeutral, "Neutral entities."); setCategoryComment(catPassive, "Passive entities."); setCategoryComment(catPlayer, "These settings affect the player directly."); - setCategoryComment(catMisc, "Entity settings that don't fit into any other category, typically entity settings."); + setCategoryComment(catMisc, "Entity settings that don't fit into any other category."); configCats.add(getCategory(catHostile)); configCats.add(getCategory(catNeutral)); @@ -46,10 +46,10 @@ public ConfigEntities(File file) { protected void syncConfigOptions() { //passive enableRabbit = getBoolean("enableRabbits", catPassive, true, ""); - enableBrownMooshroom = getBoolean("enableBrownMooshroom", catPassive, true, "Brown mooshroom variant."); + enableBrownMooshroom = getBoolean("enableBrownMooshroom", catPassive, true, "Brown mooshroom variant, the red mooshrooms turn into then when they are hit by lightning."); //neutral - enableBees = getBoolean("enableBees", catNeutral, true, "Bees, hives, and honey"); + enableBees = getBoolean("enableBees", catNeutral, true, ""); //hostile enableEndermite = getBoolean("enableEndermite", catHostile, true, "Rarely spawns when the player lands from Ender Pearl throws"); @@ -64,7 +64,7 @@ protected void syncConfigOptions() { enableVillagerTurnsIntoWitch = getBoolean("enableVillagerTurnsIntoWitch", catMisc, true, "Villagers turn into Witches when struck by lightning"); enableDragonRespawn = getBoolean("enableDragonRespawn", catMisc, true, "Crude implementation of respawning the dragon using four End crystals."); enableNetherEndermen = getBoolean("enableNetherEndermen", catMisc, true, "Allow endermen to rarely spawn in the Nether"); - enableLightLevel0 = getBoolean("enableLightLevel0", catMisc, false, "This config reduces the required light level for mobs to spawn to light level 0, like in Minecraft 1.18+."); + enableLightLevel0 = getBoolean("enableLightLevel0", catMisc, true, "Reduces the required light level for hostile mobs to spawn to light level 0, like in Minecraft 1.18+."); } } diff --git a/src/main/java/ganymedes01/etfuturum/core/handlers/ServerEventHandler.java b/src/main/java/ganymedes01/etfuturum/core/handlers/ServerEventHandler.java index 697649de7..1e7485598 100644 --- a/src/main/java/ganymedes01/etfuturum/core/handlers/ServerEventHandler.java +++ b/src/main/java/ganymedes01/etfuturum/core/handlers/ServerEventHandler.java @@ -1,6 +1,7 @@ package ganymedes01.etfuturum.core.handlers; import com.google.common.collect.Lists; +import com.google.common.collect.Maps; import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.eventhandler.Event.Result; import cpw.mods.fml.common.eventhandler.EventPriority; @@ -75,6 +76,7 @@ import net.minecraft.world.World; import net.minecraft.world.WorldProviderHell; import net.minecraft.world.WorldServer; +import net.minecraft.world.biome.BiomeGenBase; import net.minecraft.world.chunk.Chunk; import net.minecraft.world.chunk.IChunkProvider; import net.minecraft.world.gen.ChunkProviderServer; @@ -1348,21 +1350,66 @@ public void naturalSpawnEvent(LivingPackSizeEvent event) { } @SubscribeEvent + /** + * NOTE: This event for some reason uses event.setResult(Result.DENY) to cancel instead of event.setCanceled... Why does the "result" even exist? + */ public void spawnEvent(LivingSpawnEvent.CheckSpawn event) { - if (event.world.provider instanceof WorldProviderHell) { + if (event.getResult() != Result.DENY) { + World world = event.world; int x = MathHelper.floor_double(event.x); int y = MathHelper.floor_double(event.y); int z = MathHelper.floor_double(event.z); - World world = event.world; - if (world.getBlock(x, y - 1, z) == ModBlocks.NETHER_WART.get() && world.getBlockMetadata(x, y - 1, z) == 0) { - if (!(event.entity instanceof EntityFlying)) { + if (ConfigEntities.enableLightLevel0 && (event.entityLiving instanceof IMob || getSpawnTypes(event.entityLiving).contains(EnumCreatureType.monster))) { + if (event.entityLiving.worldObj.getBlockLightValue(x, y, z) > 0) { event.setResult(Result.DENY); + return; + } + } + + if (event.world.provider instanceof WorldProviderHell) { + if (world.getBlock(x, y - 1, z) == ModBlocks.NETHER_WART.get() && world.getBlockMetadata(x, y - 1, z) == 0) { + if (!(event.entity instanceof EntityFlying)) { + event.setResult(Result.DENY); + } } } } } + private static final Map> typesMap = Maps.newHashMap(); + + @SuppressWarnings("unchecked") + /** + * Checks the spawn types the mob comes from. Used by the light level 0 monster spawning in case the monster isn't instance of IMob but is still being spawned as one. + * Derived from CoreTweaks + * @author makamys + */ + private static List getSpawnTypes(Entity entity) { + List list = typesMap.get(entity.getClass()); + if (list == null) { + list = Lists.newArrayList(); + for (BiomeGenBase biome : BiomeGenBase.getBiomeGenArray()) { + if (biome != null) { + for (EnumCreatureType type : EnumCreatureType.values()) { + if (list.contains(type)) continue; + List spawnableList = biome.getSpawnableList(type); + if (spawnableList != null) { + for (BiomeGenBase.SpawnListEntry entry : spawnableList) { + if (entry.entityClass == entity.getClass()) { + list.add(type); + break; + } + } + } + } + } + } + typesMap.put(entity.getClass(), list); + } + return list; + } + @SubscribeEvent public void spawnEvent(EntityJoinWorldEvent event) { int x = MathHelper.floor_double(event.entity.posX); @@ -1955,19 +2002,6 @@ public void onWorldLoad(WorldEvent.Load e) { e.world.getGameRules().addGameRule("disableElytraMovementCheck", "false"); } - @SubscribeEvent - public void onEntitySpawn(LivingSpawnEvent.CheckSpawn event) { - if (ConfigEntities.enableLightLevel0 && event.entityLiving instanceof IMob) { - int x = MathHelper.floor_double(event.entityLiving.posX); - int y = MathHelper.floor_double(event.entityLiving.boundingBox.minY); - int z = MathHelper.floor_double(event.entityLiving.posZ); - - if (event.entityLiving.worldObj.getBlockLightValue(x, y, z) > 0) { - event.setResult(Result.DENY); - } - } - } - static MovingObjectPosition getMovingObjectPositionFromPlayer(World worldIn, EntityPlayer playerIn, boolean useLiquids) { float f = 1.0F; float f1 = playerIn.prevRotationPitch + (playerIn.rotationPitch - playerIn.prevRotationPitch) * f; diff --git a/src/main/java/ganymedes01/etfuturum/core/utils/Utils.java b/src/main/java/ganymedes01/etfuturum/core/utils/Utils.java index 523501078..0ac67499f 100644 --- a/src/main/java/ganymedes01/etfuturum/core/utils/Utils.java +++ b/src/main/java/ganymedes01/etfuturum/core/utils/Utils.java @@ -1,7 +1,9 @@ package ganymedes01.etfuturum.core.utils; import cpw.mods.fml.common.Loader; +import cpw.mods.fml.relauncher.ReflectionHelper; import ganymedes01.etfuturum.client.sound.ModSounds; +import ganymedes01.etfuturum.compat.ModsList; import ganymedes01.etfuturum.configuration.configs.ConfigSounds; import ganymedes01.etfuturum.lib.Reference; import net.minecraft.block.Block; @@ -448,6 +450,48 @@ public static BiomeGenBase[] excludeBiomesFromTypes(BiomeGenBase[] list, BiomeDi return list; } + private static Integer maxMeta; + private static Integer minMeta; + + public static int getMaxMetadata() { + if (maxMeta == null) { + if (ModsList.NOT_ENOUGH_IDS.isLoaded() && ModsList.NOT_ENOUGH_IDS.isVersionNewerOrEqual("2.0.0")) { + maxMeta = (int) Short.MAX_VALUE; + } else if (ModsList.ENDLESS_IDS.isLoaded() && ModsList.ENDLESS_IDS.isVersionNewerOrEqual("1.5-alpha0001")) { + try { + if (ReflectionHelper.findField(Class.forName("com.falsepattern.endlessids.config.GeneralConfig"), "extendBlockItem").getBoolean(null)) { + maxMeta = 65536; + } + } catch (Exception e) { + maxMeta = 15; + throw new RuntimeException(e.getMessage()); + } + } else { + maxMeta = 15; + } + } + return maxMeta; + } + + public static int getMinMetadata() { + if (minMeta == null) { + if (ModsList.NOT_ENOUGH_IDS.isLoaded() && ModsList.NOT_ENOUGH_IDS.isVersionNewerOrEqual("2.0.0")) { + minMeta = (int) Short.MIN_VALUE; + } else { //EIDs has min meta 0 too, so we don't need to check for it + minMeta = 0; + } + } + return minMeta; + } + + public static boolean isMetaInBlockBounds(int meta) { + return meta <= getMaxMetadata() && meta >= getMinMetadata(); + } + + public static boolean isMetaInBlockBoundsIgnoreWildcard(int meta) { + return meta == OreDictionary.WILDCARD_VALUE || isMetaInBlockBounds(meta); + } + public static void copyAttribs(Block to, Block from) { to.setHardness(from.blockHardness); to.setResistance(from.blockResistance);