Spawn restriction changes

This commit is contained in:
paulevsGitch 2020-12-26 20:50:39 +03:00
parent 52e5600bb8
commit 8b53764e92
3 changed files with 15 additions and 36 deletions

View file

@ -1,53 +1,22 @@
package ru.betterend.util;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import net.fabricmc.fabric.mixin.object.builder.SpawnRestrictionAccessor;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.SpawnRestriction;
import net.minecraft.entity.SpawnRestriction.Location;
import net.minecraft.entity.SpawnRestriction.SpawnPredicate;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.world.Heightmap.Type;
import ru.betterend.BetterEnd;
public class SpawnHelper {
private static Method regRestriction;
public static <T extends MobEntity> void restriction(EntityType<T> entity, Location location, Type heughtmapType, SpawnPredicate<T> predicate) {
if (regRestriction != null) {
try {
regRestriction.invoke(null, entity, location, heughtmapType, predicate);
}
catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
BetterEnd.LOGGER.error(e.getMessage());
}
}
else {
BetterEnd.LOGGER.error("Unable to register spawn restriction, variable is not handled");
}
public static <T extends MobEntity> void restrictionAir(EntityType<T> entity, SpawnPredicate<T> predicate) {
SpawnRestrictionAccessor.callRegister(entity, Location.NO_RESTRICTIONS, Type.MOTION_BLOCKING, predicate);
}
public static <T extends MobEntity> void restrictionLand(EntityType<T> entity, SpawnPredicate<T> predicate) {
restriction(entity, Location.ON_GROUND, Type.MOTION_BLOCKING, predicate);
SpawnRestrictionAccessor.callRegister(entity, Location.ON_GROUND, Type.MOTION_BLOCKING, predicate);
}
public static <T extends MobEntity> void restrictionWater(EntityType<T> entity, SpawnPredicate<T> predicate) {
restriction(entity, Location.IN_WATER, Type.MOTION_BLOCKING, predicate);
}
static {
try {
for (Method method: SpawnRestriction.class.getDeclaredMethods()) {
if (method.getParameterCount() == 4) {
regRestriction = method;
regRestriction.setAccessible(true);
break;
}
}
}
catch (SecurityException e) {
BetterEnd.LOGGER.error(e.getMessage());
}
SpawnRestrictionAccessor.callRegister(entity, Location.IN_WATER, Type.MOTION_BLOCKING, predicate);
}
}