extended entity IDs

This commit is contained in:
FalsePattern committed 2023-09-05 09:59:57 +02:00
1 parent 484a9175e7
commit c1d8e273fc
8 files changed
+212 -10

No files matched your search

@@ -38,6 +38,9 @@ public class GeneralConfig {
@Config.Comment("Extend Potion IDs. Vanilla limit is 32, new limit is 65536.")
@Config.DefaultBoolean(true)
public static boolean extendPotion;
@Config.Comment("Extend Entity IDs. Vanilla limit is 256, new limit is 32768.")
@Config.DefaultBoolean(true)
public static boolean extendEntity;
@Config.Comment("Improves the speed of the block registry by multiple orders of magnitudes. (not thoroughly tested yet)\n" +
"It's highly recommended to leave this enabled, especially when using a lot of IDs.\n" +
@@ -16,6 +16,9 @@ public class ExtendedConstants {
public static final int maximumPotionIDs = 65536;
public static final int maximumEnchantmentIDs = 32768;
//Align with max item metadata for spawn eggs. Will be treated as short in netcode
public static final int bitsPerEntity = 15;
//BlockItemIDs
public static final int nibblesPerID = bitsPerID / 4;
public static final int bytesPerID = (nibblesPerID + 1) / 2;
@@ -44,4 +47,9 @@ public class ExtendedConstants {
public static final int watchableCount = 1 << watchableBits;
public static final int maxWatchableID = watchableCount - 1;
public static final int watchableMask = (0x7 << watchableBits) | maxWatchableID;
//EntityIDs
public static final int entityIDCount = 1 << bitsPerEntity;
public static final int entityIDMask = entityIDCount - 1;
public static final int maxEntityID = entityIDMask;
}
@@ -10,6 +10,8 @@ public class VanillaConstants {
public static final int watchableBits = 5;
public static final int bitsPerBiome = 8;
public static final int bitsPerEntity = 8;
//BlockItemIDs
public static final int nibblesPerID = bitsPerID / 4;
public static final int bytesPerID = (nibblesPerID + 1) / 2;
@@ -38,4 +40,8 @@ public class VanillaConstants {
public static final int watchableCount = 1 << watchableBits;
public static final int maxWatchableID = watchableCount - 1;
public static final int watchableMask = (0x7 << watchableBits) | maxWatchableID;
//EntityIDs
public static final int entityIDCount = 1 << bitsPerEntity;
public static final int maxEntityID = entityIDCount - 1;
}
@@ -0,0 +1,49 @@
package com.falsepattern.endlessids.mixin.mixins.common.entity.vanilla;
import com.falsepattern.endlessids.constants.ExtendedConstants;
import com.falsepattern.endlessids.constants.VanillaConstants;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Constant;
import org.spongepowered.asm.mixin.injection.ModifyConstant;
import org.spongepowered.asm.mixin.injection.Redirect;
import net.minecraft.entity.EntityList;
@Mixin(EntityList.class)
public abstract class EntityListMixin {
@Shadow
public static void addMapping(Class p_75618_0_, String p_75618_1_, int p_75618_2_) {
}
@Shadow
public static void addMapping(Class p_75614_0_, String p_75614_1_, int p_75614_2_, int p_75614_3_, int p_75614_4_) {
}
@ModifyConstant(method = "addMapping(Ljava/lang/Class;Ljava/lang/String;I)V",
constant = @Constant(intValue = VanillaConstants.maxEntityID),
require = 1)
private static int extendRange(int constant) {
return ExtendedConstants.maxEntityID;
}
@Redirect(method = "<clinit>",
at = @At(value = "INVOKE",
target = "Lnet/minecraft/entity/EntityList;addMapping(Ljava/lang/Class;Ljava/lang/String;I)V"),
require = 1)
private static void messUpMappings1(Class p_75618_0_, String p_75618_1_, int p_75618_2_) {
addMapping(p_75618_0_, p_75618_1_, p_75618_2_ * 100);
}
@Redirect(method = "<clinit>",
at = @At(value = "INVOKE",
target = "Lnet/minecraft/entity/EntityList;addMapping(Ljava/lang/Class;Ljava/lang/String;III)V"),
require = 1)
private static void messUpMappings2(Class p_75614_0_, String p_75614_1_, int p_75614_2_, int p_75614_3_, int p_75614_4_) {
addMapping(p_75614_0_, p_75614_1_, p_75614_2_ * 100, p_75614_3_, p_75614_4_);
}
}
@@ -0,0 +1,66 @@
package com.falsepattern.endlessids.mixin.mixins.common.entity.vanilla;
import com.falsepattern.endlessids.constants.ExtendedConstants;
import com.falsepattern.endlessids.constants.VanillaConstants;
import org.apache.logging.log4j.Level;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.Constant;
import org.spongepowered.asm.mixin.injection.ModifyConstant;
import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.registry.EntityRegistry;
import java.util.BitSet;
@Mixin(value = EntityRegistry.class,
remap = false)
public abstract class EntityRegistryMixin {
@Shadow private BitSet availableIndicies;
@ModifyConstant(method = "<init>",
constant = @Constant(intValue = VanillaConstants.entityIDCount),
require = 1)
private int extendIDs1(int constant) {
return ExtendedConstants.entityIDCount;
}
@ModifyConstant(method = "<init>",
constant = @Constant(intValue = VanillaConstants.maxEntityID),
require = 1)
private int extendIDs2(int constant) {
return ExtendedConstants.maxEntityID;
}
/**
* @author FalsePattern
* @reason Extended IDs
*/
@Overwrite
private int validateAndClaimId(int id) {
//Keeping the legacy sub-0 id workaround here...
// workaround for broken ML
int realId = id;
if (id < Byte.MIN_VALUE) {
realId += 3000;
FMLLog.warning("Compensating for modloader out of range compensation by mod : entityId %d for mod %s is now %d", id, Loader.instance().activeModContainer().getModId(), realId);
}
if (realId < 0) {
realId += Byte.MAX_VALUE;
}
if ((realId & ~ExtendedConstants.entityIDMask) != 0) {
FMLLog.log(Level.ERROR, "The entity ID %d for mod %s is not in the range 1-" + ExtendedConstants.entityIDMask + " and may not work", id, Loader.instance().activeModContainer().getModId());
}
if (!availableIndicies.get(realId)) {
FMLLog.severe("The mod %s has attempted to register an entity ID %d which is already reserved. This could cause severe problems", Loader.instance().activeModContainer().getModId(), id);
}
availableIndicies.clear(realId);
return realId;
}
}
@@ -0,0 +1,63 @@
package com.falsepattern.endlessids.mixin.mixins.common.entity.vanilla;
import com.falsepattern.endlessids.constants.ExtendedConstants;
import io.netty.buffer.ByteBuf;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import net.minecraft.entity.EntityList;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.network.PacketBuffer;
import net.minecraft.network.play.server.S0FPacketSpawnMob;
@Mixin(S0FPacketSpawnMob.class)
public abstract class S0FPacketSpawnMobMixin {
@Shadow private int field_149040_b;
@Inject(method = "<init>(Lnet/minecraft/entity/EntityLivingBase;)V",
at = @At("RETURN"),
require = 1)
private void unClampType(EntityLivingBase entity, CallbackInfo ci) {
field_149040_b = EntityList.getEntityID(entity);
}
@Redirect(method = "readPacketData",
at = @At(value = "INVOKE",
target = "Lnet/minecraft/network/PacketBuffer;readByte()B",
ordinal = 0),
require = 1)
private byte noReadByte(PacketBuffer data) {
return 0;
}
@Inject(method = "readPacketData",
at = @At(value = "INVOKE",
target = "Lnet/minecraft/network/PacketBuffer;readByte()B",
ordinal = 0),
require = 1)
private void readType(PacketBuffer data, CallbackInfo ci) {
field_149040_b = data.readShort() & ExtendedConstants.entityIDMask;
}
@Redirect(method = "writePacketData",
at = @At(value = "INVOKE",
target = "Lnet/minecraft/network/PacketBuffer;writeByte(I)Lio/netty/buffer/ByteBuf;",
ordinal = 0),
require = 1)
private ByteBuf noWriteByte(PacketBuffer data, int theByte) {
return null;
}
@Inject(method = "writePacketData",
at = @At(value = "INVOKE",
target = "Lnet/minecraft/network/PacketBuffer;writeByte(I)Lio/netty/buffer/ByteBuf;",
ordinal = 0),
require = 1)
private void writeType(PacketBuffer data, CallbackInfo ci) {
data.writeShort(field_149040_b & ExtendedConstants.entityIDMask);
}
}
@@ -215,6 +215,13 @@ public enum Mixin implements IMixin {
S1EPacketRemoveEntityEffectMixin(Side.COMMON, condition(() -> GeneralConfig.extendPotion), "potion.vanilla.network.S1EPacketRemoveEntityEffectMixin"),
//endregion vanilla
//endregion potion
//region entity
//region vanilla
EntityListMixin(Side.COMMON, condition(() -> GeneralConfig.extendEntity), "entity.vanilla.EntityListMixin"),
EntityRegistryMixin(Side.COMMON, condition(() -> GeneralConfig.extendEntity), "entity.vanilla.EntityRegistryMixin"),
S0FPacketSpawnMobMixin(Side.COMMON, condition(() -> GeneralConfig.extendEntity), "entity.vanilla.S0FPacketSpawnMobMixin"),
//endregion vanilla
//endregion entity
//endregion common
//region client