Skip to content

Instantly share code, notes, and snippets.

@AlexiyOrlov
Last active October 15, 2019 16:46
Show Gist options
  • Save AlexiyOrlov/e50dc38ebfd5573534f7413cc75fc7e9 to your computer and use it in GitHub Desktop.
Save AlexiyOrlov/e50dc38ebfd5573534f7413cc75fc7e9 to your computer and use it in GitHub Desktop.
Access to registration of net.minecraft.entity.SpawnRestriction.Entry
package net.fabricmc.fabric.api.entity;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.SpawnRestriction;
import net.minecraft.world.Heightmap;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* Helps to set spawning settings for given entity type; see
* {@linkplain SpawnRestriction}
*/
public class FabricSpawnRestriction<T extends Entity>
{
@SuppressWarnings("unchecked")
public static final List<FabricSpawnRestriction> SPAWN_RESTRICTIONS = new ArrayList()
{
@Override
public boolean remove(Object o)
{
throw new UnsupportedOperationException();
}
@Override
public Object remove(int index)
{
throw new UnsupportedOperationException();
}
};
//Defaults
public Heightmap.Type heightMapType = Heightmap.Type.MOTION_BLOCKING_NO_LEAVES;
public SpawnRestriction.Location location = SpawnRestriction.Location.ON_GROUND;
public SpawnRestriction.class_4306<T> predicate = (entityType, world, spawnType, blockPosition, random) -> true;
public final EntityType<T> entityType;
public FabricSpawnRestriction(EntityType<T> entityType)
{
this.entityType = entityType;
Objects.requireNonNull(entityType);
}
public Builder<T> builder()
{
return new Builder<>(this);
}
public class Builder<E extends Entity>
{
private FabricSpawnRestriction<E> spawnRestriction;
private Heightmap.Type heightMapType;
private SpawnRestriction.Location location;
private SpawnRestriction.class_4306<E> predicate;
private Builder(FabricSpawnRestriction<E> fabricSpawnRestriction)
{
spawnRestriction = fabricSpawnRestriction;
}
public Builder<E> location(SpawnRestriction.Location spawnLocation)
{
this.location = spawnLocation;
return this;
}
public Builder<E> heightMapType(Heightmap.Type heightMapType)
{
this.heightMapType = heightMapType;
return this;
}
public Builder<E> restrictionPredicate(SpawnRestriction.class_4306<E> predicate)
{
this.predicate = predicate;
return this;
}
/**
* Validates and adds the restriction
*/
public void build()
{
spawnRestriction.heightMapType = this.heightMapType;
spawnRestriction.location = this.location;
spawnRestriction.predicate = this.predicate;
Objects.requireNonNull(heightMapType);
Objects.requireNonNull(location);
Objects.requireNonNull(predicate);
SPAWN_RESTRICTIONS.add(spawnRestriction);
}
}
}
package net.fabricmc.fabric.mixin.builders;
import net.fabricmc.fabric.api.entity.FabricSpawnRestriction;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.SpawnRestriction;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.world.Heightmap;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
@Mixin(SpawnRestriction.class)
public abstract class MixinSpawnRestriction
{
/**
* Represents net.minecraft.entity.SpawnRestriction#setRestrictions
*/
@Shadow
private static <T extends MobEntity> void method_20637(EntityType<T> entityType, SpawnRestriction.Location spawnLocation, Heightmap.Type heightMapType, SpawnRestriction.class_4306<T> restrictionPredicate)
{
}
//Registers all FabricSpawnRestriction instances
static
{
FabricSpawnRestriction.SPAWN_RESTRICTIONS.forEach(fabricSpawnRestrictionBuilder -> method_20637(fabricSpawnRestrictionBuilder.entityType, fabricSpawnRestrictionBuilder.location, fabricSpawnRestrictionBuilder.heightMapType, fabricSpawnRestrictionBuilder.predicate));
}
}
package test;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.entity.FabricEntityTypeBuilder;
import net.fabricmc.fabric.api.entity.FabricSpawnRestriction;
import net.minecraft.entity.*;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.Heightmap;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.biome.Biomes;
/**
* Example
*/
public class TestRestriction implements ModInitializer
{
@Override
public void onInitialize()
{
EntityType<Entity> fabricEntityType = FabricEntityTypeBuilder.create(EntityCategory.MONSTER, (EntityType.EntityFactory<Entity>) TestEntity::new).size(new EntityDimensions(1, 2, false)).build();
Registry.register(Registry.ENTITY_TYPE, new Identifier("fabric", "creeper"), fabricEntityType);
new FabricSpawnRestriction<>(fabricEntityType).builder().heightMapType(Heightmap.Type.MOTION_BLOCKING_NO_LEAVES).location(SpawnRestriction.Location.ON_GROUND).restrictionPredicate((var1, var2, var3, var4, var5) -> true).build();
Biomes.PLAINS.getEntitySpawnList(EntityCategory.MONSTER).add(new Biome.SpawnEntry(fabricEntityType, 100, 2, 2));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment