Skip to content

Commit

Permalink
Optimize LightLevel0 config option, tweaks
Browse files Browse the repository at this point in the history
Tweak config comments
Add (untested) EndlessIDs AND NotEnoughIDs compat
Add slightly more utils to ModsList for more precise version checking
  • Loading branch information
Roadhog360 committed Feb 12, 2024
1 parent 6c8d916 commit ba6f8c0
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/main/java/ganymedes01/etfuturum/EtFuturum.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/ganymedes01/etfuturum/api/BeePlantRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/ganymedes01/etfuturum/api/StrippedLogRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -11,7 +12,7 @@

public class StrippedLogRegistry {

private static final Map<RegistryMapping<Block>, RegistryMapping<Block>> strippedLogs = new HashMap<RegistryMapping<Block>, RegistryMapping<Block>>();
private static final Map<RegistryMapping<Block>, RegistryMapping<Block>> strippedLogs = new HashMap<>();

/**
* Adds a specified log and its metadata to be converted to another specified log and its metadata.
Expand All @@ -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<Block> from, RegistryMapping<Block> 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);
}

Expand All @@ -37,7 +41,7 @@ private static void addLog(RegistryMapping<Block> from, RegistryMapping<Block> 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<Block> map) {
Expand All @@ -52,7 +56,7 @@ private static boolean hasLog(RegistryMapping<Block> map) {
* the block instance and the meta data it should be replaced with.
*/
public static RegistryMapping<Block> getLog(Block block, int meta) {
return getLog(new RegistryMapping<>(block, meta % 4));
return getLog(RegistryMapping.getKeyFor(block, meta & 3));
}

private static RegistryMapping<Block> getLog(RegistryMapping<Block> map) {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/ganymedes01/etfuturum/compat/ModsList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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");
Expand All @@ -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+.");
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<Class, List<EnumCreatureType>> 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<EnumCreatureType> getSpawnTypes(Entity entity) {
List<EnumCreatureType> 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<BiomeGenBase.SpawnListEntry> 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);
Expand Down Expand Up @@ -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;
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/ganymedes01/etfuturum/core/utils/Utils.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit ba6f8c0

Please sign in to comment.