mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 16:25:36 +00:00
iris:spawn: EntitySpawns provider; entity, item and attribute Bukkit behaviour moved out of the model classes
This commit is contained in:
1 parent
e94d8e00f7
commit
910eab1f79
14 files changed
+453
-1036
No files matched your search
@@ -17,3 +17,4 @@ art/arcane/iris/engine/framework/PlacedObject.java
|
||||
art/arcane/iris/engine/framework/PlacedStructurePiece.java
|
||||
art/arcane/iris/builtin/**
|
||||
art/arcane/iris/engine/loot/**
|
||||
art/arcane/iris/engine/spawn/**
|
||||
@@ -23,25 +23,12 @@ import art.arcane.iris.engine.object.annotations.MaxNumber;
|
||||
import art.arcane.iris.engine.object.annotations.MinNumber;
|
||||
import art.arcane.iris.engine.object.annotations.Required;
|
||||
import art.arcane.iris.engine.object.annotations.Snippet;
|
||||
import art.arcane.iris.spi.IrisLogging;
|
||||
import art.arcane.volmlib.util.math.RNG;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.Registry;
|
||||
import org.bukkit.attribute.Attributable;
|
||||
import org.bukkit.attribute.Attribute;
|
||||
import org.bukkit.attribute.AttributeModifier;
|
||||
import org.bukkit.attribute.AttributeModifier.Operation;
|
||||
import org.bukkit.inventory.EquipmentSlotGroup;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@Snippet("attribute-modifier")
|
||||
@Accessors(chain = true)
|
||||
@@ -50,8 +37,6 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
@Desc("Represents an attribute modifier for an item or an entity. This allows you to create modifications to basic game attributes such as MAX_HEALTH or ARMOR_VALUE.")
|
||||
@Data
|
||||
public class IrisAttributeModifier {
|
||||
private static final Set<String> WARNED_ATTRIBUTES = ConcurrentHashMap.newKeySet();
|
||||
|
||||
@Required
|
||||
@Desc("The Attribute type key such as max_health or minecraft:armor. This type is pulled from the game attributes. Zombie & Horse attributes will not work on non-zombie/horse entities.\nUsing an attribute on an item will have affects when held, or worn. There is no way to specify further granularity as the game picks this depending on the item type.")
|
||||
private String attribute = null;
|
||||
@@ -75,80 +60,7 @@ public class IrisAttributeModifier {
|
||||
@Desc("The chance that this attribute is applied (0 to 1). If the chance is 0.5 (50%), then Iris will only apply this attribute 50% of the time.")
|
||||
private double chance = 1;
|
||||
|
||||
public void apply(RNG rng, ItemMeta meta) {
|
||||
if (rng.nextDouble() < getChance()) {
|
||||
Attribute resolved = resolveAttribute();
|
||||
if (resolved == null) {
|
||||
return;
|
||||
}
|
||||
meta.addAttributeModifier(resolved, createModifier(rng));
|
||||
}
|
||||
}
|
||||
|
||||
public void apply(RNG rng, Attributable meta) {
|
||||
if (rng.nextDouble() < getChance()) {
|
||||
Attribute resolved = resolveAttribute();
|
||||
if (resolved == null) {
|
||||
return;
|
||||
}
|
||||
meta.getAttribute(resolved).addModifier(createModifier(rng));
|
||||
}
|
||||
}
|
||||
|
||||
public double getAmount(RNG rng) {
|
||||
return rng.d(getMinAmount(), getMaxAmount());
|
||||
}
|
||||
|
||||
private Attribute resolveAttribute() {
|
||||
if (attribute == null || attribute.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
String value = attribute.trim().toLowerCase(Locale.ROOT);
|
||||
Attribute resolved;
|
||||
if (value.contains(":")) {
|
||||
NamespacedKey key = NamespacedKey.fromString(value);
|
||||
resolved = key == null ? null : Registry.ATTRIBUTE.get(key);
|
||||
} else {
|
||||
resolved = Registry.ATTRIBUTE.get(NamespacedKey.minecraft(value));
|
||||
if (resolved == null && value.startsWith("generic_")) {
|
||||
resolved = Registry.ATTRIBUTE.get(NamespacedKey.minecraft(value.substring("generic_".length())));
|
||||
}
|
||||
if (resolved == null && value.startsWith("generic.")) {
|
||||
resolved = Registry.ATTRIBUTE.get(NamespacedKey.minecraft(value.substring("generic.".length())));
|
||||
}
|
||||
}
|
||||
if (resolved == null && WARNED_ATTRIBUTES.add(value)) {
|
||||
IrisLogging.warn("Unknown attribute '" + attribute + "' in attribute-modifier '" + name + "'");
|
||||
}
|
||||
return resolved;
|
||||
}
|
||||
|
||||
private Operation resolveOperation() {
|
||||
if (operation == null || operation.isBlank()) {
|
||||
return Operation.ADD_NUMBER;
|
||||
}
|
||||
try {
|
||||
return Operation.valueOf(operation.trim().toUpperCase(Locale.ROOT));
|
||||
} catch (IllegalArgumentException e) {
|
||||
return Operation.ADD_NUMBER;
|
||||
}
|
||||
}
|
||||
|
||||
private AttributeModifier createModifier(RNG rng) {
|
||||
NamespacedKey key = NamespacedKey.minecraft(generateModifierKey());
|
||||
return new AttributeModifier(key, getAmount(rng), resolveOperation(), EquipmentSlotGroup.ANY);
|
||||
}
|
||||
|
||||
private String generateModifierKey() {
|
||||
String source = getName() == null ? "modifier" : getName();
|
||||
String normalized = source.toLowerCase(Locale.ROOT).replaceAll("[^a-z0-9/._-]", "_");
|
||||
if (normalized.isBlank()) {
|
||||
normalized = "modifier";
|
||||
}
|
||||
if (normalized.length() > 32) {
|
||||
normalized = normalized.substring(0, 32);
|
||||
}
|
||||
String random = UUID.randomUUID().toString().replace("-", "");
|
||||
return normalized + "_" + random;
|
||||
}
|
||||
}
|
||||
@@ -285,39 +285,4 @@ public class IrisEffect {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void apply(Entity p) {
|
||||
if (!shouldApplyNow()) {
|
||||
return;
|
||||
}
|
||||
|
||||
J.runEntity(p, () -> {
|
||||
Sound soundType = getSound();
|
||||
Particle particleType = getParticleEffect();
|
||||
if (soundType != null) {
|
||||
Location part = p.getLocation().clone().add(RNG.r.i(-soundDistance, soundDistance), RNG.r.i(-soundDistance, soundDistance), RNG.r.i(-soundDistance, soundDistance));
|
||||
p.getWorld().playSound(part, soundType, (float) volume, (float) RNG.r.d(minPitch, maxPitch));
|
||||
}
|
||||
|
||||
if (particleType != null) {
|
||||
Location part = p.getLocation().clone().add(0, 0.25, 0).add(new Vector(1, 1, 1).multiply(RNG.r.d())).subtract(new Vector(1, 1, 1).multiply(RNG.r.d()));
|
||||
part.add(RNG.r.d(), 0, RNG.r.d());
|
||||
if (extra != 0) {
|
||||
p.getWorld().spawnParticle(particleType, part.getX(), part.getY() + RNG.r.i(particleOffset),
|
||||
part.getZ(),
|
||||
particleCount,
|
||||
randomAltX ? RNG.r.d(-particleAltX, particleAltX) : particleAltX,
|
||||
randomAltY ? RNG.r.d(-particleAltY, particleAltY) : particleAltY,
|
||||
randomAltZ ? RNG.r.d(-particleAltZ, particleAltZ) : particleAltZ,
|
||||
extra);
|
||||
} else {
|
||||
p.getWorld().spawnParticle(particleType, part.getX(), part.getY() + RNG.r.i(particleOffset), part.getZ(),
|
||||
particleCount,
|
||||
randomAltX ? RNG.r.d(-particleAltX, particleAltX) : particleAltX,
|
||||
randomAltY ? RNG.r.d(-particleAltY, particleAltY) : particleAltY,
|
||||
randomAltZ ? RNG.r.d(-particleAltZ, particleAltZ) : particleAltZ);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -19,70 +19,20 @@
|
||||
package art.arcane.iris.engine.object.entity;
|
||||
|
||||
import art.arcane.iris.engine.object.entity.spawn.IrisSurface;
|
||||
import art.arcane.iris.engine.object.loot.InventorySlotType;
|
||||
import art.arcane.iris.engine.object.loot.IrisLoot;
|
||||
import art.arcane.iris.engine.object.loot.IrisLootReference;
|
||||
import art.arcane.iris.engine.object.loot.IrisLootTable;
|
||||
import art.arcane.iris.engine.object.marker.IrisCommand;
|
||||
import art.arcane.iris.spi.IrisLogging;
|
||||
import art.arcane.iris.spi.IrisServices;
|
||||
import art.arcane.iris.core.IrisSettings;
|
||||
import art.arcane.iris.core.link.Identifier;
|
||||
import art.arcane.iris.core.loader.IrisRegistrant;
|
||||
import art.arcane.iris.core.service.EntityRiseSVC;
|
||||
import art.arcane.iris.core.service.ExternalDataSVC;
|
||||
import art.arcane.iris.engine.framework.Engine;
|
||||
import art.arcane.iris.engine.framework.LootResolver;
|
||||
import art.arcane.iris.engine.platform.EngineBukkitOps;
|
||||
import art.arcane.iris.engine.object.annotations.ArrayType;
|
||||
import art.arcane.iris.engine.object.annotations.Desc;
|
||||
import art.arcane.iris.engine.object.annotations.RegistryListEntityType;
|
||||
import art.arcane.iris.engine.object.annotations.RegistryListSpecialEntity;
|
||||
import art.arcane.iris.engine.object.annotations.Required;
|
||||
import art.arcane.iris.platform.bukkit.BukkitPlatform;
|
||||
import art.arcane.iris.util.common.data.registry.RegistryUtil;
|
||||
import art.arcane.iris.platform.bukkit.BukkitWorld;
|
||||
import art.arcane.iris.spi.PlatformWorld;
|
||||
import art.arcane.volmlib.util.collection.KList;
|
||||
import art.arcane.iris.util.common.format.C;
|
||||
import art.arcane.volmlib.util.math.M;
|
||||
import art.arcane.volmlib.util.math.RNG;
|
||||
import art.arcane.iris.util.common.plugin.Chunks;
|
||||
import art.arcane.iris.util.common.scheduling.J;
|
||||
import art.arcane.volmlib.util.scheduling.PrecisionStopwatch;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.attribute.Attributable;
|
||||
import org.bukkit.entity.Ageable;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.LivingEntity;
|
||||
import org.bukkit.entity.Mob;
|
||||
import org.bukkit.entity.Panda;
|
||||
import org.bukkit.entity.Villager;
|
||||
import org.bukkit.entity.Panda.Gene;
|
||||
import org.bukkit.event.entity.CreatureSpawnEvent;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.loot.LootContext;
|
||||
import org.bukkit.loot.LootTable;
|
||||
import org.bukkit.loot.Lootable;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Locale;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static art.arcane.iris.util.common.data.registry.Particles.ITEM;
|
||||
|
||||
@SuppressWarnings("ALL")
|
||||
@Accessors(chain = true)
|
||||
@@ -194,355 +144,10 @@ public class IrisEntity extends IrisRegistrant {
|
||||
@Desc("Run raw commands when this entity is spawned. Use {x}, {y}, and {z} for location. /summon pig {x} {y} {z}")
|
||||
private KList<IrisCommand> rawCommands = new KList<>();
|
||||
|
||||
public EntityType getBukkitType() {
|
||||
if (type == null || type.isBlank()) {
|
||||
return EntityType.UNKNOWN;
|
||||
}
|
||||
|
||||
NamespacedKey key = NamespacedKey.fromString(type.trim().toLowerCase(Locale.ROOT));
|
||||
|
||||
if (key == null) {
|
||||
return EntityType.UNKNOWN;
|
||||
}
|
||||
|
||||
EntityType entityType = RegistryUtil.lookup(EntityType.class).get(key);
|
||||
return entityType == null ? EntityType.UNKNOWN : entityType;
|
||||
}
|
||||
|
||||
public CreatureSpawnEvent.SpawnReason getBukkitReason() {
|
||||
if (reason == null || reason.isBlank()) {
|
||||
return CreatureSpawnEvent.SpawnReason.NATURAL;
|
||||
}
|
||||
|
||||
try {
|
||||
return CreatureSpawnEvent.SpawnReason.valueOf(reason.trim().toUpperCase(Locale.ROOT));
|
||||
} catch (IllegalArgumentException e) {
|
||||
return CreatureSpawnEvent.SpawnReason.NATURAL;
|
||||
}
|
||||
}
|
||||
|
||||
public Gene getBukkitPandaMainGene() {
|
||||
return resolveGene(pandaMainGene);
|
||||
}
|
||||
|
||||
public Gene getBukkitPandaHiddenGene() {
|
||||
return resolveGene(pandaHiddenGene);
|
||||
}
|
||||
|
||||
private static Gene resolveGene(String gene) {
|
||||
if (gene == null || gene.isBlank()) {
|
||||
return Gene.NORMAL;
|
||||
}
|
||||
|
||||
try {
|
||||
return Gene.valueOf(gene.trim().toUpperCase(Locale.ROOT));
|
||||
} catch (IllegalArgumentException e) {
|
||||
return Gene.NORMAL;
|
||||
}
|
||||
}
|
||||
|
||||
public Entity spawn(Engine gen, Location at) {
|
||||
return spawn(gen, at, new RNG(at.hashCode()));
|
||||
}
|
||||
|
||||
public Entity spawn(Engine gen, Location at, RNG rng) {
|
||||
if (!Chunks.isSafe(at)) {
|
||||
return null;
|
||||
}
|
||||
if (isSpawnEffectRiseOutOfGround()) {
|
||||
AtomicReference<Location> f = new AtomicReference<>(at);
|
||||
try {
|
||||
J.sfut(() -> {
|
||||
if (Chunks.hasPlayersNearby(f.get())) {
|
||||
Location b = f.get().clone();
|
||||
Location start = new Location(b.getWorld(), b.getX(), b.getY() - 5, b.getZ());
|
||||
f.set(start);
|
||||
}
|
||||
}).get();
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
IrisLogging.reportError("Iris entity spawn preparation was interrupted.", e);
|
||||
} catch (ExecutionException e) {
|
||||
IrisLogging.reportError("Iris entity spawn preparation failed.", e);
|
||||
}
|
||||
at = f.get();
|
||||
}
|
||||
|
||||
Entity ee = doSpawn(at);
|
||||
|
||||
if (ee == null && !Chunks.isSafe(at)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (isSpecialType() && !applySettingsToCustomMobAnyways) {
|
||||
return ee;
|
||||
}
|
||||
|
||||
if (ee == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Entity e = ee;
|
||||
e.setCustomName(getCustomName() != null ? C.translateAlternateColorCodes('&', getCustomName()) : null);
|
||||
e.setCustomNameVisible(isCustomNameVisible());
|
||||
e.setGlowing(isGlowing());
|
||||
e.setGravity(isGravity());
|
||||
e.setInvulnerable(isInvulnerable());
|
||||
e.setSilent(isSilent());
|
||||
e.setPersistent(isKeepEntity() || IrisSettings.get().getWorld().isForcePersistEntities());
|
||||
|
||||
int gg = 0;
|
||||
for (IrisEntity i : passengers) {
|
||||
Entity passenger = i.spawn(gen, at, rng.nextParallelRNG(234858 + gg++));
|
||||
if (passenger == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (Bukkit.isPrimaryThread()) {
|
||||
e.addPassenger(passenger);
|
||||
} else {
|
||||
J.runEntity(e, () -> e.addPassenger(passenger));
|
||||
}
|
||||
}
|
||||
|
||||
if (e instanceof Attributable) {
|
||||
Attributable a = (Attributable) e;
|
||||
|
||||
for (IrisAttributeModifier i : getAttributes()) {
|
||||
i.apply(rng, a);
|
||||
}
|
||||
}
|
||||
|
||||
if (e instanceof Lootable) {
|
||||
Lootable l = (Lootable) e;
|
||||
|
||||
if (getLoot().getTables().isNotEmpty()) {
|
||||
BukkitOps.bindLoot(this, gen, l, at, rng);
|
||||
}
|
||||
}
|
||||
|
||||
if (e instanceof LivingEntity) {
|
||||
LivingEntity l = (LivingEntity) e;
|
||||
l.setAI(isAi());
|
||||
l.setCanPickupItems(isPickupItems());
|
||||
|
||||
if (getLeashHolder() != null) {
|
||||
l.setLeashHolder(getLeashHolder().spawn(gen, at, rng.nextParallelRNG(234548)));
|
||||
}
|
||||
|
||||
l.setRemoveWhenFarAway(isRemovable());
|
||||
|
||||
if (getHelmet() != null && LootResolver.oneIn(rng, getHelmet().getRarity())) {
|
||||
l.getEquipment().setHelmet(getHelmet().get(gen.isStudio(), rng));
|
||||
}
|
||||
|
||||
if (getChestplate() != null && LootResolver.oneIn(rng, getChestplate().getRarity())) {
|
||||
l.getEquipment().setChestplate(getChestplate().get(gen.isStudio(), rng));
|
||||
}
|
||||
|
||||
if (getLeggings() != null && LootResolver.oneIn(rng, getLeggings().getRarity())) {
|
||||
l.getEquipment().setLeggings(getLeggings().get(gen.isStudio(), rng));
|
||||
}
|
||||
|
||||
if (getBoots() != null && LootResolver.oneIn(rng, getBoots().getRarity())) {
|
||||
l.getEquipment().setBoots(getBoots().get(gen.isStudio(), rng));
|
||||
}
|
||||
|
||||
if (getMainHand() != null && LootResolver.oneIn(rng, getMainHand().getRarity())) {
|
||||
l.getEquipment().setItemInMainHand(getMainHand().get(gen.isStudio(), rng));
|
||||
}
|
||||
|
||||
if (getOffHand() != null && LootResolver.oneIn(rng, getOffHand().getRarity())) {
|
||||
l.getEquipment().setItemInOffHand(getOffHand().get(gen.isStudio(), rng));
|
||||
}
|
||||
}
|
||||
|
||||
if (e instanceof Ageable && isBaby()) {
|
||||
((Ageable) e).setBaby();
|
||||
}
|
||||
|
||||
if (e instanceof Panda) {
|
||||
((Panda) e).setMainGene(getBukkitPandaMainGene());
|
||||
((Panda) e).setHiddenGene(getBukkitPandaHiddenGene());
|
||||
}
|
||||
|
||||
if (e instanceof Villager) {
|
||||
Villager villager = (Villager) e;
|
||||
villager.setRemoveWhenFarAway(false);
|
||||
BukkitOps.persistVillager(villager);
|
||||
}
|
||||
|
||||
if (e instanceof Mob) {
|
||||
Mob m = (Mob) e;
|
||||
m.setAware(isAware());
|
||||
}
|
||||
|
||||
if (spawnEffect != null) {
|
||||
spawnEffect.apply(e);
|
||||
}
|
||||
|
||||
if (rawCommands.isNotEmpty()) {
|
||||
final Location fat = at;
|
||||
final PlatformWorld commandWorld = new BukkitWorld(fat.getWorld());
|
||||
rawCommands.forEach(r -> r.run(commandWorld, fat.getBlockX(), fat.getBlockY(), fat.getBlockZ()));
|
||||
}
|
||||
|
||||
Location finalAt1 = at;
|
||||
|
||||
J.runEntity(e, () -> {
|
||||
if (isSpawnEffectRiseOutOfGround() && e instanceof LivingEntity && Chunks.hasPlayersNearby(finalAt1)) {
|
||||
LivingEntity living = (LivingEntity) e;
|
||||
Location start = finalAt1.clone();
|
||||
boolean originalInvulnerable = e.isInvulnerable();
|
||||
boolean originalAi = living.hasAI();
|
||||
boolean originalCollidable = living.isCollidable();
|
||||
int originalNoDamageTicks = living.getNoDamageTicks();
|
||||
EntityRiseSVC.stamp(e, originalInvulnerable, originalAi, originalCollidable);
|
||||
e.setInvulnerable(true);
|
||||
living.setAI(false);
|
||||
living.setCollidable(false);
|
||||
living.setNoDamageTicks(100000);
|
||||
Runnable restore = () -> restoreRiseState(e, living, originalInvulnerable, originalAi,
|
||||
originalCollidable, originalNoDamageTicks);
|
||||
AtomicInteger t = new AtomicInteger(0);
|
||||
Runnable[] loop = new Runnable[1];
|
||||
loop[0] = () -> {
|
||||
if (t.get() > 100 || e.isDead()) {
|
||||
restore.run();
|
||||
return;
|
||||
}
|
||||
|
||||
t.incrementAndGet();
|
||||
if (e.getLocation().getBlock().getType().isSolid() || living.getEyeLocation().getBlock().getType().isSolid()) {
|
||||
e.teleport(start.add(new Vector(0, 0.1, 0)));
|
||||
ItemStack itemCrackData = new ItemStack(living.getEyeLocation().clone().subtract(0, 2, 0).getBlock().getBlockData().getMaterial());
|
||||
e.getWorld().spawnParticle(ITEM, living.getEyeLocation(), 6, 0.2, 0.4, 0.2, 0.06f, itemCrackData);
|
||||
if (M.r(0.2)) {
|
||||
e.getWorld().playSound(e.getLocation(), Sound.BLOCK_CHORUS_FLOWER_GROW, 0.8f, 0.1f);
|
||||
}
|
||||
if (!J.runEntity(e, loop[0], 1, restore)) {
|
||||
restore.run();
|
||||
}
|
||||
} else {
|
||||
restore.run();
|
||||
}
|
||||
};
|
||||
if (!J.runEntity(e, loop[0], 0, restore)) {
|
||||
restore.run();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return e;
|
||||
}
|
||||
|
||||
private static void restoreRiseState(Entity entity, LivingEntity living, boolean invulnerable,
|
||||
boolean ai, boolean collidable, int noDamageTicks) {
|
||||
living.setNoDamageTicks(noDamageTicks);
|
||||
living.setCollidable(collidable);
|
||||
living.setAI(ai);
|
||||
entity.setInvulnerable(invulnerable);
|
||||
EntityRiseSVC.clearStamp(entity);
|
||||
}
|
||||
|
||||
private int surfaceY(Location l) {
|
||||
int m = l.getBlockY();
|
||||
|
||||
while (m-- > 0) {
|
||||
Location ll = l.clone();
|
||||
ll.setY(m);
|
||||
|
||||
if (ll.getBlock().getType().isSolid()) {
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private Entity doSpawn(Location at) {
|
||||
if (!Chunks.isSafe(at)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (EntityType.UNKNOWN.equals(getBukkitType()) && !isSpecialType()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!Bukkit.isPrimaryThread()) {
|
||||
// Someone called spawn (worldedit maybe?) on a non server thread
|
||||
// Due to the structure of iris, we will call it sync and busy wait until it's done.
|
||||
AtomicReference<Entity> ae = new AtomicReference<>();
|
||||
|
||||
try {
|
||||
if (!J.runAt(at, () -> ae.set(doSpawn(at)))) {
|
||||
return null;
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
return null;
|
||||
}
|
||||
PrecisionStopwatch p = PrecisionStopwatch.start();
|
||||
|
||||
while (ae.get() == null) {
|
||||
J.sleep(25);
|
||||
|
||||
if (p.getMilliseconds() > 500) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return ae.get();
|
||||
}
|
||||
|
||||
if (isSpecialType()) {
|
||||
return IrisServices.get(ExternalDataSVC.class).spawnMob(at, Identifier.fromString(specialType));
|
||||
}
|
||||
|
||||
|
||||
return BukkitPlatform.spawnEntity(at, getBukkitType(), getBukkitReason());
|
||||
}
|
||||
|
||||
public boolean isSpecialType() {
|
||||
return specialType != null && !specialType.equals("");
|
||||
}
|
||||
|
||||
private static final class BukkitOps {
|
||||
private static void persistVillager(Villager villager) {
|
||||
villager.setPersistent(true);
|
||||
}
|
||||
|
||||
private static void bindLoot(IrisEntity entity, Engine gen, Lootable l, Location finalAt, RNG rng) {
|
||||
l.setLootTable(new LootTable() {
|
||||
@Override
|
||||
public NamespacedKey getKey() {
|
||||
return new NamespacedKey("iris", "loot-" + entity.hashCode());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<ItemStack> populateLoot(Random random, LootContext context) {
|
||||
KList<ItemStack> items = new KList<>();
|
||||
|
||||
for (String fi : entity.getLoot().getTables()) {
|
||||
IrisLootTable i = gen.getData().getLootLoader().load(fi);
|
||||
items.addAll(i.getLoot(gen.isStudio(), gen.getSeedManager().getLoot(), InventorySlotType.STORAGE, finalAt.getWorld(), finalAt.getBlockX(), finalAt.getBlockY(), finalAt.getBlockZ()));
|
||||
}
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fillInventory(Inventory inventory, Random random, LootContext context) {
|
||||
for (ItemStack i : populateLoot(random, context)) {
|
||||
inventory.addItem(i);
|
||||
}
|
||||
|
||||
EngineBukkitOps.scramble(gen, inventory, rng);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFolderName() {
|
||||
return "entities";
|
||||
|
||||
@@ -21,34 +21,18 @@ package art.arcane.iris.engine.object.entity;
|
||||
import art.arcane.iris.engine.object.entity.spawn.IrisSpawner;
|
||||
import art.arcane.iris.engine.object.marker.IrisMarker;
|
||||
import art.arcane.iris.engine.object.value.IRare;
|
||||
import art.arcane.iris.engine.object.value.IrisPosition;
|
||||
import art.arcane.iris.platform.bukkit.BukkitWorldBinding;
|
||||
import art.arcane.iris.platform.bukkit.BukkitPlatform;
|
||||
import art.arcane.iris.engine.data.cache.AtomicCache;
|
||||
import art.arcane.iris.engine.framework.Engine;
|
||||
import art.arcane.iris.engine.framework.LootResolver;
|
||||
import art.arcane.iris.engine.object.annotations.Desc;
|
||||
import art.arcane.iris.engine.object.annotations.MinNumber;
|
||||
import art.arcane.iris.engine.object.annotations.RegistryListResource;
|
||||
import art.arcane.iris.engine.object.annotations.Required;
|
||||
import art.arcane.iris.engine.object.annotations.Snippet;
|
||||
import art.arcane.iris.spi.IrisLogging;
|
||||
import art.arcane.iris.util.common.format.C;
|
||||
import art.arcane.volmlib.util.collection.KList;
|
||||
import art.arcane.volmlib.util.math.RNG;
|
||||
import art.arcane.volmlib.util.math.Vector3d;
|
||||
import art.arcane.volmlib.util.matter.MatterMarker;
|
||||
import art.arcane.volmlib.util.matter.slices.MarkerMatter;
|
||||
import art.arcane.iris.util.common.scheduling.J;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
import org.bukkit.Chunk;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Entity;
|
||||
|
||||
@Snippet("entity-spawn")
|
||||
@Accessors(chain = true)
|
||||
@@ -75,164 +59,7 @@ public class IrisEntitySpawn implements IRare {
|
||||
private transient IrisSpawner referenceSpawner;
|
||||
private transient IrisMarker referenceMarker;
|
||||
|
||||
public int spawn(Engine gen, Chunk c, RNG rng) {
|
||||
int spawns = LootResolver.inclusive(rng, minSpawns, maxSpawns);
|
||||
int s = 0;
|
||||
|
||||
if (spawns > 0) {
|
||||
for (int id = 0; id < spawns; id++) {
|
||||
int x = (c.getX() * 16) + rng.i(16);
|
||||
int z = (c.getZ() * 16) + rng.i(16);
|
||||
World world = BukkitWorldBinding.tryBind(gen.getWorld()) ? BukkitWorldBinding.world(gen.getWorld()) : null;
|
||||
int h = gen.getHeight(x, z, true) + (world == null ? -64 : world.getMinHeight());
|
||||
int hf = gen.getHeight(x, z, false) + (world == null ? -64 : world.getMinHeight());
|
||||
Location l = switch (getReferenceSpawner().getGroup()) {
|
||||
case NORMAL -> new Location(c.getWorld(), x, hf + 1, z);
|
||||
case CAVE -> findCaveSpawnLocation(gen, c, rng);
|
||||
case UNDERWATER, BEACH -> new Location(c.getWorld(), x, rng.i(h + 1, hf), z);
|
||||
};
|
||||
|
||||
if (l != null) {
|
||||
if (referenceSpawner.getAllowedLightLevels().getMin() > 0 || referenceSpawner.getAllowedLightLevels().getMax() < 15) {
|
||||
if (referenceSpawner.getAllowedLightLevels().contains(l.getBlock().getLightLevel())) {
|
||||
if (spawn100(gen, l) != null) {
|
||||
s++;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (spawn100(gen, l) != null) {
|
||||
s++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
public int spawn(Engine gen, IrisPosition c, RNG rng) {
|
||||
int spawns = LootResolver.inclusive(rng, minSpawns, maxSpawns);
|
||||
int s = 0;
|
||||
|
||||
if (!BukkitWorldBinding.tryBind(gen.getWorld())) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
World world = BukkitWorldBinding.world(gen.getWorld());
|
||||
if (spawns > 0) {
|
||||
|
||||
if (referenceMarker != null && referenceMarker.shouldExhaust()) {
|
||||
if (J.isFolia()) {
|
||||
J.a(() -> gen.getMantle().getMantle().remove(c.getX(), c.getY() - gen.getWorld().minHeight(), c.getZ(), MatterMarker.class));
|
||||
} else {
|
||||
gen.getMantle().getMantle().remove(c.getX(), c.getY() - gen.getWorld().minHeight(), c.getZ(), MatterMarker.class);
|
||||
}
|
||||
}
|
||||
|
||||
for (int id = 0; id < spawns; id++) {
|
||||
Location l = BukkitPlatform.toLocation(c, world).add(0, 1, 0);
|
||||
|
||||
if (referenceSpawner.getAllowedLightLevels().getMin() > 0 || referenceSpawner.getAllowedLightLevels().getMax() < 15) {
|
||||
if (referenceSpawner.getAllowedLightLevels().contains(l.getBlock().getLightLevel())) {
|
||||
if (spawn100(gen, l, true) != null) {
|
||||
s++;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (spawn100(gen, l, true) != null) {
|
||||
s++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
public IrisEntity getRealEntity(Engine g) {
|
||||
return ent.aquire(() -> g.getData().getEntityLoader().load(getEntity()));
|
||||
}
|
||||
|
||||
public Entity spawn(Engine g, Location at) {
|
||||
if (getRealEntity(g) == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (LootResolver.oneIn(rng.aquire(() -> new RNG(g.getSeedManager().getEntity())), getRarity())) {
|
||||
return spawn100(g, at);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
static Location findCaveSpawnLocation(Engine engine, Chunk chunk, RNG rng) {
|
||||
if (J.isFolia()) {
|
||||
return null;
|
||||
}
|
||||
KList<IrisPosition> markers = engine.getMantle().findMarkers(chunk.getX(), chunk.getZ(), MarkerMatter.CAVE_FLOOR);
|
||||
return selectCaveSpawnLocation(markers, chunk.getWorld(), rng);
|
||||
}
|
||||
|
||||
static Location selectCaveSpawnLocation(KList<IrisPosition> markers, World world, RNG rng) {
|
||||
return markers.convert((marker) -> BukkitPlatform.toLocation(marker, world).add(0, 1, 0)).getRandom(rng);
|
||||
}
|
||||
|
||||
private Entity spawn100(Engine g, Location at) {
|
||||
return spawn100(g, at, false);
|
||||
}
|
||||
|
||||
private Entity spawn100(Engine g, Location at, boolean ignoreSurfaces) {
|
||||
try {
|
||||
IrisEntity irisEntity = getRealEntity(g);
|
||||
if (irisEntity == null) { // No entity
|
||||
IrisLogging.debug(" You are trying to spawn an entity that does not exist!");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!ignoreSurfaces && !irisEntity.getSurface().matches(at.clone().subtract(0, 1, 0).getBlock())) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Vector3d boundingBox = BukkitPlatform.entityBoundingBox(irisEntity.getBukkitType());
|
||||
if (!ignoreSurfaces && boundingBox != null) {
|
||||
boolean isClearForSpawn = isAreaClearForSpawn(at, boundingBox);
|
||||
if (!isClearForSpawn) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Entity e = irisEntity.spawn(g, at.add(0.5, 0.5, 0.5), rng.aquire(() -> new RNG(g.getSeedManager().getEntity())));
|
||||
if (e != null) {
|
||||
IrisLogging.debug("Spawned " + C.DARK_AQUA + "Entity<" + getEntity() + "> " + C.GREEN + e.getType() + C.LIGHT_PURPLE + " @ " + C.GRAY + e.getLocation().getX() + ", " + e.getLocation().getY() + ", " + e.getLocation().getZ());
|
||||
}
|
||||
|
||||
return e;
|
||||
} catch (Throwable e) {
|
||||
IrisLogging.reportError(e);
|
||||
IrisLogging.error(" Failed to retrieve real entity @ " + at + " (entity: " + getEntity() + ")");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isAreaClearForSpawn(Location center, Vector3d boundingBox) {
|
||||
World world = center.getWorld();
|
||||
int startX = center.getBlockX() - (int) (boundingBox.x / 2);
|
||||
int endX = center.getBlockX() + (int) (boundingBox.x / 2);
|
||||
int startY = center.getBlockY();
|
||||
int endY = center.getBlockY() + (int) boundingBox.y;
|
||||
int startZ = center.getBlockZ() - (int) (boundingBox.z / 2);
|
||||
int endZ = center.getBlockZ() + (int) (boundingBox.z / 2);
|
||||
|
||||
for (int x = startX; x <= endX; x++) {
|
||||
for (int y = startY; y <= endY; y++) {
|
||||
for (int z = startZ; z <= endZ; z++) {
|
||||
if (world.getBlockAt(x, y, z).getType() != Material.AIR) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -19,9 +19,6 @@
|
||||
package art.arcane.iris.engine.object.entity.spawn;
|
||||
|
||||
import art.arcane.iris.engine.object.annotations.Desc;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.data.Waterlogged;
|
||||
|
||||
@Desc("The type of surface entities should spawn on")
|
||||
public enum IrisSurface {
|
||||
@@ -40,29 +37,4 @@ public enum IrisSurface {
|
||||
|
||||
@Desc("Within lava")
|
||||
LAVA;
|
||||
|
||||
/**
|
||||
* Check if this Iris surface matches the blockstate provided
|
||||
*
|
||||
* @param state The blockstate
|
||||
* @return True if it matches
|
||||
*/
|
||||
public boolean matches(Block state) {
|
||||
Material type = state.getType();
|
||||
if (type.isSolid()) {
|
||||
return this == LAND || this == OVERWORLD || (this == ANIMAL
|
||||
&& (type == Material.GRASS_BLOCK || type == Material.DIRT
|
||||
|| type == Material.DIRT_PATH || type == Material.COARSE_DIRT
|
||||
|| type == Material.ROOTED_DIRT || type == Material.PODZOL
|
||||
|| type == Material.MYCELIUM || type == Material.SNOW_BLOCK));
|
||||
}
|
||||
if (type == Material.LAVA) return this == LAVA;
|
||||
if (type == Material.WATER || type == Material.SEAGRASS
|
||||
|| type == Material.TALL_SEAGRASS || type == Material.KELP_PLANT
|
||||
|| type == Material.KELP ||
|
||||
(state instanceof Waterlogged && ((Waterlogged) state).isWaterlogged()))
|
||||
return this == WATER || this == OVERWORLD;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -31,13 +31,7 @@ import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.Registry;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
|
||||
@Snippet("enchantment")
|
||||
@@ -65,40 +59,6 @@ public class IrisEnchantment {
|
||||
@Desc("The chance that this enchantment is applied (0 to 1)")
|
||||
private double chance = 1;
|
||||
|
||||
public void apply(RNG rng, ItemMeta meta) {
|
||||
try {
|
||||
Enchantment enchant = resolve();
|
||||
if (enchant == null) {
|
||||
IrisLogging.warn("Unknown Enchantment: " + getEnchantment());
|
||||
return;
|
||||
}
|
||||
if (rng.nextDouble() < chance) {
|
||||
if (meta instanceof EnchantmentStorageMeta) {
|
||||
((EnchantmentStorageMeta) meta).addStoredEnchant(enchant, getLevel(rng), true);
|
||||
return;
|
||||
}
|
||||
meta.addEnchant(enchant, getLevel(rng), true);
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
IrisLogging.reportError(e);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the authored key against the live enchantment registry. Accepts a bare path
|
||||
* ({@code sharpness}) or a full namespaced key ({@code mymod:vorpal}) - parity with the modded resolver.
|
||||
*/
|
||||
private Enchantment resolve() {
|
||||
String raw = getEnchantment();
|
||||
if (raw == null || raw.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
String value = raw.trim().toLowerCase(Locale.ROOT).replace(' ', '_');
|
||||
NamespacedKey key = value.indexOf(':') >= 0 ? NamespacedKey.fromString(value) : NamespacedKey.minecraft(value);
|
||||
return key == null ? null : Registry.ENCHANTMENT.get(key);
|
||||
}
|
||||
|
||||
public int getLevel(RNG rng) {
|
||||
return LootResolver.inclusive(rng, getMinLevel(), getMaxLevel());
|
||||
}
|
||||
|
||||
@@ -19,13 +19,6 @@
|
||||
package art.arcane.iris.engine.object.loot;
|
||||
|
||||
import art.arcane.iris.engine.object.entity.IrisAttributeModifier;
|
||||
import art.arcane.iris.platform.bukkit.BukkitBlockResolution;
|
||||
|
||||
import art.arcane.iris.spi.IrisServices;
|
||||
import art.arcane.iris.core.link.Identifier;
|
||||
import art.arcane.iris.core.service.ExternalDataSVC;
|
||||
import art.arcane.iris.engine.data.cache.AtomicCache;
|
||||
import art.arcane.iris.engine.framework.LootResolver;
|
||||
import art.arcane.iris.engine.object.annotations.ArrayType;
|
||||
import art.arcane.iris.engine.object.annotations.Desc;
|
||||
import art.arcane.iris.engine.object.annotations.MaxNumber;
|
||||
@@ -33,32 +26,14 @@ import art.arcane.iris.engine.object.annotations.MinNumber;
|
||||
import art.arcane.iris.engine.object.annotations.RegistryListItemType;
|
||||
import art.arcane.iris.engine.object.annotations.Required;
|
||||
import art.arcane.iris.engine.object.annotations.Snippet;
|
||||
import art.arcane.iris.platform.bukkit.BukkitPlatform;
|
||||
import art.arcane.iris.spi.IrisLogging;
|
||||
import art.arcane.volmlib.util.collection.KList;
|
||||
import art.arcane.volmlib.util.collection.KMap;
|
||||
import art.arcane.iris.util.common.format.C;
|
||||
import art.arcane.volmlib.util.format.Form;
|
||||
import art.arcane.volmlib.util.math.RNG;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
import lombok.experimental.Accessors;
|
||||
import org.bukkit.DyeColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemFlag;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.Damageable;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.inventory.meta.LeatherArmorMeta;
|
||||
import org.bukkit.inventory.meta.components.CustomModelDataComponent;
|
||||
import org.bukkit.material.Colorable;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Snippet("loot")
|
||||
@Accessors(chain = true)
|
||||
@@ -71,7 +46,6 @@ import java.util.Optional;
|
||||
public class IrisLoot {
|
||||
public static final int MAX_AMOUNT = 64;
|
||||
|
||||
private final transient AtomicCache<DyeColor> dyeColorResolved = new AtomicCache<>();
|
||||
@Desc("The target inventory slot types to fill this loot with. World generation only ever fills STORAGE; the other values are inert during worldgen.")
|
||||
private InventorySlotType slotTypes = InventorySlotType.STORAGE;
|
||||
@MinNumber(1)
|
||||
@@ -123,175 +97,17 @@ public class IrisLoot {
|
||||
@Desc("Defines a custom NBT Tag for the item.")
|
||||
private KMap<String, Object> customNbt;
|
||||
|
||||
public Material getType() {
|
||||
return BukkitBlockResolution.getMaterial(type);
|
||||
}
|
||||
|
||||
/**
|
||||
* The authored item key, exactly as written in the pack. Platform-neutral: {@link #getType()} resolves it against
|
||||
* Bukkit, mod loaders resolve it against their own item registry.
|
||||
* The authored item key, exactly as written in the pack. Each platform resolves it against its own item registry.
|
||||
*/
|
||||
public String getTypeKey() {
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* The authored dye colour name, or null when unset. Platform-neutral counterpart to {@link #getDyeColor()}.
|
||||
* The authored dye colour name, or null when unset.
|
||||
*/
|
||||
public String getDyeColorKey() {
|
||||
return dyeColor;
|
||||
}
|
||||
|
||||
public DyeColor getDyeColor() {
|
||||
if (dyeColor == null) {
|
||||
return null;
|
||||
}
|
||||
return dyeColorResolved.aquire(() -> {
|
||||
try {
|
||||
return DyeColor.valueOf(dyeColor);
|
||||
} catch (IllegalArgumentException e) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private KList<ItemFlag> getResolvedItemFlags() {
|
||||
KList<ItemFlag> resolved = new KList<>();
|
||||
for (String flag : itemFlags) {
|
||||
if (flag == null) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
resolved.add(ItemFlag.valueOf(flag));
|
||||
} catch (IllegalArgumentException ignored) {
|
||||
}
|
||||
}
|
||||
return resolved;
|
||||
}
|
||||
|
||||
public ItemStack get(boolean debug, RNG rng) {
|
||||
try {
|
||||
ItemStack is = getItemStack(rng);
|
||||
if (is == null)
|
||||
return new ItemStack(Material.AIR);
|
||||
is.setItemMeta(applyProperties(is, rng, debug, null));
|
||||
return BukkitPlatform.applyCustomNbt(is, customNbt);
|
||||
} catch (Throwable e) {
|
||||
IrisLogging.reportError(e);
|
||||
return new ItemStack(Material.AIR);
|
||||
}
|
||||
}
|
||||
|
||||
public ItemStack get(boolean debug, IrisLootTable table, RNG rng, long lootSeed, int entryIndex, int x, int y, int z) {
|
||||
long combinedRarity = LootResolver.combinedRarity(rarity, table.getRarity());
|
||||
if (!art.arcane.iris.engine.loot.LootTables.spatialOneIn(lootSeed, table, entryIndex, x, y, z, combinedRarity)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
ItemStack is = getItemStack(rng);
|
||||
if (is == null)
|
||||
return null;
|
||||
is.setItemMeta(applyProperties(is, rng, debug, table));
|
||||
return BukkitPlatform.applyCustomNbt(is, customNbt);
|
||||
} catch (Throwable e) {
|
||||
IrisLogging.reportError(e);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// TODO Better Third Party Item Acquisition
|
||||
private ItemStack getItemStack(RNG rng) {
|
||||
if (!type.startsWith("minecraft:") && type.contains(":")) {
|
||||
Optional<ItemStack> opt = IrisServices.get(ExternalDataSVC.class).getItemStack(Identifier.fromString(type), customNbt);
|
||||
if (opt.isEmpty()) {
|
||||
IrisLogging.warn("Unknown Material: " + type);
|
||||
return new ItemStack(Material.AIR);
|
||||
}
|
||||
ItemStack is = opt.get();
|
||||
is.setAmount(Math.max(1, LootResolver.inclusive(rng, getMinAmount(), getMaxAmount())));
|
||||
return is;
|
||||
}
|
||||
return new ItemStack(getType(), Math.max(1, LootResolver.inclusive(rng, getMinAmount(), getMaxAmount())));
|
||||
}
|
||||
|
||||
private ItemMeta applyProperties(ItemStack is, RNG rng, boolean debug, IrisLootTable table) {
|
||||
ItemMeta m = is.getItemMeta();
|
||||
if (m == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
for (IrisEnchantment i : getEnchantments()) {
|
||||
i.apply(rng, m);
|
||||
}
|
||||
|
||||
for (IrisAttributeModifier i : getAttributes()) {
|
||||
i.apply(rng, m);
|
||||
}
|
||||
|
||||
m.setUnbreakable(isUnbreakable());
|
||||
for (ItemFlag i : getResolvedItemFlags()) {
|
||||
m.addItemFlags(i);
|
||||
}
|
||||
|
||||
if (getCustomModel() != null) {
|
||||
CustomModelDataComponent customModelData = m.getCustomModelDataComponent();
|
||||
customModelData.setFloats(List.of(getCustomModel().floatValue()));
|
||||
m.setCustomModelDataComponent(customModelData);
|
||||
}
|
||||
|
||||
if (is.getType().getMaxDurability() > 0 && m instanceof Damageable d) {
|
||||
int max = is.getType().getMaxDurability();
|
||||
d.setDamage((int) Math.round(Math.max(0, Math.min(max, (1D - rng.d(getMinDurability(), getMaxDurability())) * max))));
|
||||
}
|
||||
|
||||
if (getLeatherColor() != null && m instanceof LeatherArmorMeta leather) {
|
||||
Color c = Color.decode(getLeatherColor());
|
||||
leather.setColor(org.bukkit.Color.fromRGB(c.getRed(), c.getGreen(), c.getBlue()));
|
||||
}
|
||||
|
||||
if (getDyeColor() != null && m instanceof Colorable colorable) {
|
||||
colorable.setColor(getDyeColor());
|
||||
}
|
||||
|
||||
if (displayName != null) {
|
||||
m.setDisplayName(C.translateAlternateColorCodes('&', displayName));
|
||||
}
|
||||
|
||||
KList<String> lore = new KList<>();
|
||||
|
||||
getLore().forEach((i) ->
|
||||
{
|
||||
String mf = C.translateAlternateColorCodes('&', i);
|
||||
|
||||
if (mf.length() > 24) {
|
||||
for (String g : Form.wrapWords(mf, 24).split("\\Q\n\\E")) {
|
||||
lore.add(g.trim());
|
||||
}
|
||||
} else {
|
||||
lore.add(mf);
|
||||
}
|
||||
});
|
||||
|
||||
if (debug) {
|
||||
if (table == null) {
|
||||
if (lore.isNotEmpty()) {
|
||||
lore.add(C.GRAY + "--------------------");
|
||||
}
|
||||
lore.add(C.GRAY + "1 in " + (getRarity()) + " Chance (" + Form.pc(1D / (getRarity()), 5) + ")");
|
||||
} else {
|
||||
if (lore.isNotEmpty()) {
|
||||
lore.add(C.GRAY + "--------------------");
|
||||
}
|
||||
|
||||
lore.add(C.GRAY + "From: " + table.getName() + " (" + Form.pc(1D / table.getRarity(), 5) + ")");
|
||||
lore.add(C.GRAY + "1 in " + (table.getRarity() * getRarity()) + " Chance (" + Form.pc(1D / (table.getRarity() * getRarity()), 5) + ")");
|
||||
}
|
||||
}
|
||||
|
||||
m.setLore(lore);
|
||||
|
||||
return m;
|
||||
}
|
||||
}
|
||||
@@ -19,22 +19,17 @@
|
||||
package art.arcane.iris.engine.object.loot;
|
||||
|
||||
import art.arcane.iris.core.loader.IrisRegistrant;
|
||||
import art.arcane.iris.engine.framework.LootResolver;
|
||||
import art.arcane.iris.engine.object.annotations.ArrayType;
|
||||
import art.arcane.iris.engine.object.annotations.Desc;
|
||||
import art.arcane.iris.engine.object.annotations.MaxNumber;
|
||||
import art.arcane.iris.engine.object.annotations.MinNumber;
|
||||
import art.arcane.iris.engine.object.annotations.Required;
|
||||
import art.arcane.volmlib.util.collection.KList;
|
||||
import art.arcane.volmlib.util.math.RNG;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
@Accessors(chain = true)
|
||||
@NoArgsConstructor
|
||||
@@ -74,35 +69,6 @@ public class IrisLootTable extends IrisRegistrant {
|
||||
@ArrayType(min = 1, type = IrisLoot.class)
|
||||
private KList<IrisLoot> loot = new KList<>();
|
||||
|
||||
public KList<ItemStack> getLoot(boolean debug, long lootSeed, InventorySlotType slot, World world, int x, int y, int z) {
|
||||
KList<ItemStack> lootf = new KList<>();
|
||||
if (loot.isEmpty() || maxTries <= 0) {
|
||||
return lootf;
|
||||
}
|
||||
|
||||
RNG rng = art.arcane.iris.engine.loot.LootTables.rng(lootSeed, this, x, y, z);
|
||||
int m = 0;
|
||||
int c = 0;
|
||||
int mx = Math.max(0, LootResolver.inclusive(rng, getMinPicked(), getMaxPicked()));
|
||||
|
||||
while (m < mx && c++ < getMaxTries()) {
|
||||
int num = rng.nextInt(loot.size());
|
||||
|
||||
IrisLoot l = loot.get(num);
|
||||
|
||||
if (l != null && l.getSlotTypes() == slot) {
|
||||
ItemStack item = l.get(debug, this, rng, lootSeed, num, x, y, z);
|
||||
|
||||
if (item != null && item.getType() != Material.AIR) {
|
||||
lootf.add(item);
|
||||
m++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return lootf;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFolderName() {
|
||||
return "loot";
|
||||
|
||||
@@ -1,20 +1,9 @@
|
||||
package art.arcane.iris.engine.object.loot;
|
||||
|
||||
import art.arcane.iris.core.loader.IrisData;
|
||||
import art.arcane.iris.engine.data.cache.AtomicCache;
|
||||
import art.arcane.iris.engine.framework.LootResolver;
|
||||
import art.arcane.iris.spi.IrisLogging;
|
||||
import art.arcane.volmlib.util.collection.KList;
|
||||
import art.arcane.volmlib.util.math.RNG;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.loot.LootContext;
|
||||
import org.bukkit.loot.LootTable;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
@@ -25,22 +14,13 @@ public class IrisVanillaLootTable extends IrisLootTable {
|
||||
* The vanilla loot table key ("minecraft:chests/simple_dungeon"), not the LootTable itself. A
|
||||
* raw org.bukkit.loot.LootTable field - and the constructor lombok would generate for it - makes
|
||||
* every reflective walk over the IrisLootTable hierarchy (Gson, schema generation, the purity
|
||||
* gate) resolve a Bukkit class. The table is resolved lazily at pull time, which only ever
|
||||
* happens on Bukkit. The key text is identical to what {@code LootTable.getKey().toString()}
|
||||
* produced before, so {@code LootResolver.tableIdentity} (which falls back to
|
||||
* gate) resolve a Bukkit class. The host resolves the table when it pulls loot from it. The key text is
|
||||
* identical to what {@code LootTable.getKey().toString()} produced before, so
|
||||
* {@code LootTables.identity} (which falls back to
|
||||
* {@link #getName()}) yields the same loot seed.
|
||||
*/
|
||||
private final String lootTableKey;
|
||||
|
||||
/**
|
||||
* The resolved table, looked up once. Every chest pull resolved it again - a NamespacedKey parse plus a registry
|
||||
* lookup per container, and a dungeon room is a lot of containers.
|
||||
* <p>
|
||||
* Transient for the same reason the field above is a String: it keeps the Bukkit type argument out of the Gson
|
||||
* field walk and out of the pack purity gate, both of which resolve generic field types for the fields they visit.
|
||||
*/
|
||||
private final transient AtomicCache<LootTable> resolvedTable = new AtomicCache<>();
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Vanilla " + lootTableKey;
|
||||
@@ -71,24 +51,6 @@ public class IrisVanillaLootTable extends IrisLootTable {
|
||||
return new KList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public KList<ItemStack> getLoot(boolean debug, long lootSeed, InventorySlotType slot, World world, int x, int y, int z) {
|
||||
LootTable table = resolveTable();
|
||||
if (table == null) {
|
||||
IrisLogging.warn("Unknown vanilla loot table " + lootTableKey);
|
||||
return new KList<>();
|
||||
}
|
||||
RNG rng = art.arcane.iris.engine.loot.LootTables.rng(lootSeed, this, x, y, z);
|
||||
return new KList<>(table.populateLoot(rng, new LootContext.Builder(new Location(world, x, y, z)).build()));
|
||||
}
|
||||
|
||||
private LootTable resolveTable() {
|
||||
return resolvedTable.aquire(() -> {
|
||||
NamespacedKey key = NamespacedKey.fromString(lootTableKey);
|
||||
return key == null ? null : Bukkit.getLootTable(key);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFolderName() {
|
||||
throw new UnsupportedOperationException("VanillaLootTables do not have a folder name");
|
||||
|
||||
@@ -24,9 +24,8 @@ import art.arcane.iris.engine.object.annotations.ArrayType;
|
||||
import art.arcane.iris.engine.object.annotations.Desc;
|
||||
import art.arcane.iris.engine.object.annotations.Required;
|
||||
import art.arcane.iris.engine.object.annotations.Snippet;
|
||||
import art.arcane.iris.spi.IrisPlatforms;
|
||||
import art.arcane.iris.spi.PlatformWorld;
|
||||
import art.arcane.iris.util.common.scheduling.J;
|
||||
import art.arcane.iris.util.common.scheduling.ConsoleCommands;
|
||||
import art.arcane.volmlib.util.collection.KList;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
@@ -68,19 +67,6 @@ public class IrisCommand {
|
||||
return;
|
||||
}
|
||||
|
||||
for (String command : commands) {
|
||||
command = (command.startsWith("/") ? command.replaceFirst("/", "") : command)
|
||||
.replaceAll("\\Q{x}\\E", String.valueOf(x))
|
||||
.replaceAll("\\Q{y}\\E", String.valueOf(y))
|
||||
.replaceAll("\\Q{z}\\E", String.valueOf(z));
|
||||
final String finalCommand = command;
|
||||
int safeDelay = (int) Math.max(0, Math.min(Integer.MAX_VALUE, delay));
|
||||
if (repeat) {
|
||||
int safeRepeatDelay = (int) Math.max(1, Math.min(Integer.MAX_VALUE, repeatDelay));
|
||||
J.s(() -> J.sr(() -> IrisPlatforms.get().dispatchConsoleCommand(finalCommand), safeRepeatDelay), safeDelay);
|
||||
} else {
|
||||
J.s(() -> IrisPlatforms.get().dispatchConsoleCommand(finalCommand), safeDelay);
|
||||
}
|
||||
}
|
||||
ConsoleCommands.run(commands, delay, repeat, repeatDelay, x, y, z);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Servers
|
||||
* Copyright (c) 2026 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package art.arcane.iris.engine.spawn;
|
||||
|
||||
import art.arcane.iris.engine.framework.LootResolver;
|
||||
import art.arcane.iris.engine.loot.LootItems;
|
||||
import art.arcane.iris.engine.object.entity.IrisAttributeModifier;
|
||||
import art.arcane.iris.engine.object.entity.IrisEffect;
|
||||
import art.arcane.iris.engine.object.entity.IrisEntity;
|
||||
import art.arcane.iris.engine.object.loot.IrisLoot;
|
||||
import art.arcane.iris.engine.object.marker.IrisCommand;
|
||||
import art.arcane.iris.spi.PlatformWorld;
|
||||
import art.arcane.volmlib.util.math.RNG;
|
||||
import com.volmit.entity.EntitySpec;
|
||||
import com.volmit.item.ItemAttribute;
|
||||
import com.volmit.item.ItemDrop;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Turns an entity of the pack into the {@link EntitySpec} the host spawns, making every random choice in the order Iris
|
||||
* always made them: the passengers, the attribute modifiers, the leash holder and then the six pieces of equipment.
|
||||
*/
|
||||
final class EntityResolution {
|
||||
private static final long PASSENGER_STREAM_BASE = 234858L;
|
||||
private static final long LEASH_HOLDER_STREAM = 234548L;
|
||||
|
||||
private final LootItems items;
|
||||
private final PlatformWorld world;
|
||||
|
||||
/** @param world the world the entity appears in, whose time and weather decide which of its commands run */
|
||||
EntityResolution(LootItems items, PlatformWorld world) {
|
||||
this.items = items;
|
||||
this.world = world;
|
||||
}
|
||||
|
||||
EntitySpec resolve(IrisEntity entity, RNG rng) {
|
||||
List<EntitySpec> passengers = new ArrayList<>();
|
||||
long passengerIndex = 0;
|
||||
for (IrisEntity passenger : entity.getPassengers()) {
|
||||
passengers.add(resolve(passenger, rng.nextParallelRNG(PASSENGER_STREAM_BASE + passengerIndex++)));
|
||||
}
|
||||
|
||||
List<ItemAttribute> attributes = drawAttributes(entity, rng);
|
||||
List<String> lootTables = List.copyOf(entity.getLoot().getTables());
|
||||
long lootShuffleSeed = lootTables.isEmpty() ? 0L : rng.nextLong();
|
||||
EntitySpec leashHolder = entity.getLeashHolder() == null ? null : resolve(entity.getLeashHolder(), rng.nextParallelRNG(LEASH_HOLDER_STREAM));
|
||||
EntitySpec.Equipment equipment = new EntitySpec.Equipment(
|
||||
equip(entity.getHelmet(), rng), equip(entity.getChestplate(), rng), equip(entity.getLeggings(), rng),
|
||||
equip(entity.getBoots(), rng), equip(entity.getMainHand(), rng), equip(entity.getOffHand(), rng));
|
||||
|
||||
return new EntitySpec(
|
||||
entity.getType() == null ? "" : entity.getType(),
|
||||
entity.getSpecialType() == null ? "" : entity.getSpecialType(),
|
||||
entity.getReason(),
|
||||
entity.getCustomName() == null ? "" : entity.getCustomName(),
|
||||
flagsOf(entity), equipment, passengers, attributes, lootTables, lootShuffleSeed, leashHolder,
|
||||
spawnEffectOf(entity), entity.isSpawnEffectRiseOutOfGround(), entity.getPandaMainGene(),
|
||||
entity.getPandaHiddenGene(), entity.isApplySettingsToCustomMobAnyways(), commandsOf(entity));
|
||||
}
|
||||
|
||||
private static EntitySpec.Flags flagsOf(IrisEntity entity) {
|
||||
return new EntitySpec.Flags(entity.isCustomNameVisible(), entity.isAware(), entity.isAi(), entity.isGlowing(),
|
||||
entity.isGravity(), entity.isInvulnerable(), entity.isSilent(), entity.isPickupItems(), entity.isRemovable(),
|
||||
entity.isKeepEntity(), entity.isBaby());
|
||||
}
|
||||
|
||||
private static List<ItemAttribute> drawAttributes(IrisEntity entity, RNG rng) {
|
||||
List<ItemAttribute> attributes = new ArrayList<>();
|
||||
for (IrisAttributeModifier modifier : entity.getAttributes()) {
|
||||
if (rng.nextDouble() >= modifier.getChance()) {
|
||||
continue;
|
||||
}
|
||||
String attribute = modifier.getAttribute();
|
||||
if (attribute == null || attribute.isBlank()) {
|
||||
continue;
|
||||
}
|
||||
attributes.add(new ItemAttribute(attribute, modifier.getName(), modifier.getOperation(), modifier.getAmount(rng)));
|
||||
}
|
||||
return attributes;
|
||||
}
|
||||
|
||||
private ItemDrop equip(IrisLoot piece, RNG rng) {
|
||||
if (piece == null || !LootResolver.oneIn(rng, piece.getRarity())) {
|
||||
return null;
|
||||
}
|
||||
return items.give(piece, rng);
|
||||
}
|
||||
|
||||
/** The entity's spawn effect when it shows this time: an effect only shows now and then, as its interval and chance say. */
|
||||
private static EntitySpec.SpawnEffect spawnEffectOf(IrisEntity entity) {
|
||||
IrisEffect effect = entity.getSpawnEffect();
|
||||
if (effect == null || !effect.shouldApplyNow()) {
|
||||
return null;
|
||||
}
|
||||
return new EntitySpec.SpawnEffect(effect.getSoundKey(), effect.getSoundDistance(), effect.getMinPitch(),
|
||||
effect.getMaxPitch(), effect.getVolume(), effect.getParticleEffectKey(), effect.getParticleOffset(),
|
||||
effect.getParticleCount(), effect.getParticleAltX(), effect.getParticleAltY(), effect.getParticleAltZ(),
|
||||
effect.isRandomAltX(), effect.isRandomAltY(), effect.isRandomAltZ(), effect.getExtra());
|
||||
}
|
||||
|
||||
private List<EntitySpec.Command> commandsOf(IrisEntity entity) {
|
||||
List<EntitySpec.Command> commands = new ArrayList<>();
|
||||
for (IrisCommand command : entity.getRawCommands()) {
|
||||
if (command.isValid(world)) {
|
||||
commands.add(new EntitySpec.Command(command.getCommands(), command.getDelay(), command.isRepeat(), command.getRepeatDelay()));
|
||||
}
|
||||
}
|
||||
return commands;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,269 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Servers
|
||||
* Copyright (c) 2026 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package art.arcane.iris.engine.spawn;
|
||||
|
||||
import art.arcane.iris.core.modules.IrisPackWorld;
|
||||
import art.arcane.iris.engine.framework.Engine;
|
||||
import art.arcane.iris.engine.framework.LootResolver;
|
||||
import art.arcane.iris.engine.loot.LootItems;
|
||||
import art.arcane.iris.engine.object.entity.IrisEntity;
|
||||
import art.arcane.iris.engine.object.entity.IrisEntitySpawn;
|
||||
import art.arcane.iris.engine.object.entity.spawn.IrisSpawner;
|
||||
import art.arcane.iris.engine.object.entity.spawn.IrisSurface;
|
||||
import art.arcane.iris.engine.object.marker.IrisMarker;
|
||||
import art.arcane.iris.engine.object.value.IRare;
|
||||
import art.arcane.iris.engine.object.value.IrisPosition;
|
||||
import art.arcane.iris.engine.object.value.IrisRange;
|
||||
import art.arcane.iris.engine.object.world.biome.IrisBiome;
|
||||
import art.arcane.iris.engine.object.world.region.IrisRegion;
|
||||
import art.arcane.iris.spi.IrisLogging;
|
||||
import art.arcane.iris.util.common.scheduling.J;
|
||||
import art.arcane.volmlib.util.collection.KList;
|
||||
import art.arcane.volmlib.util.math.PowerOfTwoCoordinates;
|
||||
import art.arcane.volmlib.util.math.RNG;
|
||||
import art.arcane.volmlib.util.matter.slices.MarkerMatter;
|
||||
import com.volmit.entity.EntitySpec;
|
||||
import com.volmit.service.EntitySpawns;
|
||||
import com.volmit.world.BlockPosition;
|
||||
import com.volmit.world.PackWorld;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* How {@code iris:spawn} spawns entities: the spawners of the dimension, region and surface biome that may fire in a
|
||||
* chunk, or the spawners of a marker, each with the entities it spawns and the places it picks for them. What the
|
||||
* platform decides (light, the surface, free space, the entity itself) is the host's, the plan only names it.
|
||||
*/
|
||||
public final class EntitySpawning implements EntitySpawns {
|
||||
private static final int BLOCKS_TO_CHUNK_CENTRE = 8;
|
||||
private static final int CHUNK_SIZE = 16;
|
||||
private static final String CAVE_FLOOR_MARKER = "cave_floor";
|
||||
private static final String CAVE_CEILING_MARKER = "cave_ceiling";
|
||||
private static final int AIR_ABOVE_MARKER = 1;
|
||||
|
||||
@Override
|
||||
public Optional<Plan> plan(PackWorld world, Site site) {
|
||||
Engine engine = engine(world);
|
||||
return switch (site) {
|
||||
case Chunk chunk -> planChunk(world, engine, chunk);
|
||||
case Marker marker -> planMarker(world, engine, marker);
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void spawned(PackWorld world, Plan plan, int count) {
|
||||
if (count <= 0) {
|
||||
return;
|
||||
}
|
||||
IrisSpawner spawner = engine(world).getData().getSpawnerLoader().load(plan.spawner());
|
||||
if (spawner != null) {
|
||||
spawner.spawn(engine(world), plan.chunkX(), plan.chunkZ());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<EntitySpec> entity(PackWorld world, String key) {
|
||||
IrisEntity entity = engine(world).getData().getEntityLoader().load(key);
|
||||
if (entity == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return Optional.of(resolution(world, engine(world)).resolve(entity, entityRandom(world).rng()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<MarkerRule> marker(PackWorld world, String tag) {
|
||||
if (tag.equals(CAVE_FLOOR_MARKER) || tag.equals(CAVE_CEILING_MARKER)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
Engine engine = engine(world);
|
||||
IrisMarker marker = engine.getData().getMarkerLoader().load(tag);
|
||||
if (marker == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
List<String> spawners = new ArrayList<>();
|
||||
for (String key : marker.getSpawners()) {
|
||||
if (engine.getData().getSpawnerLoader().load(key) == null) {
|
||||
IrisLogging.error("Cannot load spawner: " + key + " for marker " + tag);
|
||||
continue;
|
||||
}
|
||||
spawners.add(key);
|
||||
}
|
||||
return Optional.of(new MarkerRule(spawners, marker.isEmptyAbove(), marker.isRemoveOnChange()));
|
||||
}
|
||||
|
||||
private Optional<Plan> planChunk(PackWorld world, Engine engine, Chunk chunk) {
|
||||
Predicate<IrisSpawner> canSpawn = spawner -> spawner.canSpawn(engine, chunk.chunkX(), chunk.chunkZ());
|
||||
int blockX = PowerOfTwoCoordinates.chunkToBlock(chunk.chunkX());
|
||||
int blockZ = PowerOfTwoCoordinates.chunkToBlock(chunk.chunkZ());
|
||||
IrisBiome centreBiome = engine.getSurfaceBiome(blockX + BLOCKS_TO_CHUNK_CENTRE, blockZ + BLOCKS_TO_CHUNK_CENTRE);
|
||||
IrisRegion region = engine.getRegion(blockX, blockZ);
|
||||
IrisBiome biome = engine.getSurfaceBiome(blockX, blockZ);
|
||||
|
||||
List<IrisSpawner> candidates = Stream.concat(
|
||||
shuffled(engine, engine.getDimension().getEntitySpawners()).stream()
|
||||
.filter(canSpawn).filter(spawner -> spawner.isValid(centreBiome)),
|
||||
Stream.concat(
|
||||
shuffled(engine, region.getEntitySpawners()).stream().filter(canSpawn),
|
||||
shuffled(engine, biome.getEntitySpawners()).stream().filter(canSpawn)))
|
||||
.filter(spawner -> chunk.livingEntities() < spawner.getMaxEntitiesPerChunk())
|
||||
.toList();
|
||||
|
||||
List<Candidate> spawns = candidates.stream().flatMap(spawner -> spawnsOf(spawner, chunk.initial())).collect(Collectors.toList());
|
||||
Candidate chosen = IRare.expandWeighted(spawns).getRandom();
|
||||
if (chosen == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return planChunkSpawn(world, engine, chunk, chosen);
|
||||
}
|
||||
|
||||
private Optional<Plan> planChunkSpawn(PackWorld world, Engine engine, Chunk chunk, Candidate chosen) {
|
||||
IrisSpawner spawner = chosen.spawner();
|
||||
IrisEntitySpawn spawn = chosen.spawn();
|
||||
int count = LootResolver.inclusive(RNG.r, spawn.getMinSpawns(), spawn.getMaxSpawns());
|
||||
List<Attempt> attempts = new ArrayList<>();
|
||||
for (int i = 0; i < count; i++) {
|
||||
int x = (chunk.chunkX() * CHUNK_SIZE) + RNG.r.i(CHUNK_SIZE);
|
||||
int z = (chunk.chunkZ() * CHUNK_SIZE) + RNG.r.i(CHUNK_SIZE);
|
||||
BlockPosition at = spawnPosition(engine, spawner, chunk.chunkX(), chunk.chunkZ(), x, z);
|
||||
if (at != null) {
|
||||
addAttempt(attempts, world, engine, chosen, at, true);
|
||||
}
|
||||
}
|
||||
return attempts.isEmpty() ? Optional.empty() : Optional.of(new Plan(spawner.getLoadKey(), chunk.chunkX(), chunk.chunkZ(), false, attempts));
|
||||
}
|
||||
|
||||
/** Where a spawner of its group puts an entity at the column; null when it has no place for one. */
|
||||
private static BlockPosition spawnPosition(Engine engine, IrisSpawner spawner, int chunkX, int chunkZ, int x, int z) {
|
||||
int height = engine.getHeight(x, z, true);
|
||||
int heightWithFluid = engine.getHeight(x, z, false);
|
||||
return switch (spawner.getGroup()) {
|
||||
case NORMAL -> new BlockPosition(x, heightWithFluid + 1, z);
|
||||
case CAVE -> caveSpawnPosition(engine, chunkX, chunkZ);
|
||||
case UNDERWATER, BEACH -> new BlockPosition(x, RNG.r.i(height + 1, heightWithFluid), z);
|
||||
};
|
||||
}
|
||||
|
||||
static BlockPosition caveSpawnPosition(Engine engine, int chunkX, int chunkZ) {
|
||||
if (J.isFolia()) {
|
||||
return null;
|
||||
}
|
||||
KList<IrisPosition> floors = engine.getMantle().findMarkers(chunkX, chunkZ, MarkerMatter.CAVE_FLOOR);
|
||||
IrisPosition floor = floors.getRandom(RNG.r);
|
||||
return floor == null ? null : new BlockPosition(floor.getX(), floor.getY() - engine.getMinHeight() + AIR_ABOVE_MARKER, floor.getZ());
|
||||
}
|
||||
|
||||
private Optional<Plan> planMarker(PackWorld world, Engine engine, Marker marker) {
|
||||
IrisSpawner spawner = engine.getData().getSpawnerLoader().load(marker.spawner());
|
||||
IrisMarker irisMarker = engine.getData().getMarkerLoader().load(marker.marker());
|
||||
if (spawner == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
KList<IrisEntitySpawn> candidates = marker.initial() ? spawner.getInitialSpawns() : spawner.getSpawns();
|
||||
if (candidates.isEmpty()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
IrisEntitySpawn spawn = IRare.expandWeighted(candidates).getRandom();
|
||||
|
||||
BlockPosition position = marker.position();
|
||||
int chunkX = PowerOfTwoCoordinates.blockToChunkFloor(position.x());
|
||||
int chunkZ = PowerOfTwoCoordinates.blockToChunkFloor(position.z());
|
||||
if (!spawner.canSpawn(engine, chunkX, chunkZ)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
int count = LootResolver.inclusive(RNG.r, spawn.getMinSpawns(), spawn.getMaxSpawns());
|
||||
if (count <= 0) {
|
||||
return Optional.empty();
|
||||
}
|
||||
boolean exhaustsMarker = irisMarker != null && irisMarker.shouldExhaust();
|
||||
List<Attempt> attempts = new ArrayList<>();
|
||||
BlockPosition above = new BlockPosition(position.x(), position.y() + AIR_ABOVE_MARKER, position.z());
|
||||
for (int i = 0; i < count; i++) {
|
||||
addAttempt(attempts, world, engine, new Candidate(spawner, spawn), above, false);
|
||||
}
|
||||
return attempts.isEmpty() ? Optional.empty() : Optional.of(new Plan(spawner.getLoadKey(), chunkX, chunkZ, exhaustsMarker, attempts));
|
||||
}
|
||||
|
||||
private void addAttempt(List<Attempt> attempts, PackWorld world, Engine engine, Candidate candidate, BlockPosition at, boolean checkSurface) {
|
||||
IrisEntity entity = candidate.spawn().getRealEntity(engine);
|
||||
if (entity == null) {
|
||||
IrisLogging.debug(" You are trying to spawn an entity that does not exist!");
|
||||
return;
|
||||
}
|
||||
|
||||
IrisRange light = candidate.spawner().getAllowedLightLevels();
|
||||
EntitySpec spec = resolution(world, engine).resolve(entity, entityRandom(world).rng());
|
||||
attempts.add(new Attempt(at, spec, surfaceOf(entity.getSurface()),
|
||||
(int) Math.ceil(light.getMin()), (int) Math.floor(light.getMax()), checkSurface));
|
||||
}
|
||||
|
||||
private static EntityResolution resolution(PackWorld world, Engine engine) {
|
||||
LootItems items = new LootItems(world.resources().items(), engine.isStudio());
|
||||
return new EntityResolution(items, engine.getWorld().platformWorld());
|
||||
}
|
||||
|
||||
private static EntityRandom entityRandom(PackWorld world) {
|
||||
return world.state(EntityRandom.class, EntityRandom::new);
|
||||
}
|
||||
|
||||
private static Surface surfaceOf(IrisSurface surface) {
|
||||
return Surface.valueOf(surface.name());
|
||||
}
|
||||
|
||||
private static KList<IrisSpawner> shuffled(Engine engine, KList<String> keys) {
|
||||
return engine.getData().getSpawnerLoader().loadAll(keys).shuffleCopy(RNG.r);
|
||||
}
|
||||
|
||||
private static Stream<Candidate> spawnsOf(IrisSpawner spawner, boolean initial) {
|
||||
KList<IrisEntitySpawn> spawns = initial ? spawner.getInitialSpawns() : spawner.getSpawns();
|
||||
return spawns.stream().map(spawn -> new Candidate(spawner, spawn));
|
||||
}
|
||||
|
||||
/** A spawn of a spawner, which picks as often as the spawn's rarity says. */
|
||||
private record Candidate(IrisSpawner spawner, IrisEntitySpawn spawn) implements IRare {
|
||||
@Override
|
||||
public int getRarity() {
|
||||
return spawn.getRarity();
|
||||
}
|
||||
}
|
||||
|
||||
private static Engine engine(PackWorld world) {
|
||||
return IrisPackWorld.of(world).engine();
|
||||
}
|
||||
|
||||
/** The random stream entities are made from, seeded from the world like Iris always seeded it; one per world. */
|
||||
private static final class EntityRandom {
|
||||
private final RNG rng;
|
||||
|
||||
private EntityRandom(PackWorld world) {
|
||||
this.rng = new RNG(IrisPackWorld.of(world).engine().getSeedManager().getEntity());
|
||||
}
|
||||
|
||||
private RNG rng() {
|
||||
return rng;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Servers
|
||||
* Copyright (c) 2026 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package art.arcane.iris.engine.spawn;
|
||||
|
||||
import com.volmit.FeatureRegistry;
|
||||
import com.volmit.IrisModule;
|
||||
import com.volmit.ModuleInfo;
|
||||
import com.volmit.service.EntitySpawns;
|
||||
import com.volmit.stage.StageGraph;
|
||||
|
||||
/**
|
||||
* Module {@code iris:spawn}: the spawners, entities and markers of the pack, and the entities that appear in the world
|
||||
* over time because of them. It has no generation stage; Iris asks it through {@link EntitySpawns} when it looks for
|
||||
* something to spawn in a chunk or at a marker.
|
||||
*/
|
||||
public final class SpawnModule implements IrisModule {
|
||||
private static final String VERSION = "built-in";
|
||||
|
||||
@Override
|
||||
public ModuleInfo info() {
|
||||
return ModuleInfo.of("iris:spawn", "Spawn", VERSION);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void install(StageGraph graph) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void register(FeatureRegistry registry) {
|
||||
registry.registerService(EntitySpawns.class, new EntitySpawning());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user