Config class
Пакет: com.hypixel.hytale.builtin.adventure.worldevents.condition.spawner
Файл: com/hypixel/hytale/builtin/adventure/worldevents/condition/spawner/PrefabSpawner.java
implements: Spawner.Config
Поля (10)
| Модификаторы | Тип | Имя |
|---|---|---|
|
public static final BuilderCodec<PrefabSpawner.Config> |
CODEC |
|
protected static final Codec<String> |
PREFAB_CODEC |
|
protected PlacementMask |
mask |
|
protected int |
maxHeightDelta |
|
protected ContextKey |
prefabKey |
|
protected transient PrefabListAsset |
prefabs |
|
protected String |
prefabsRef |
|
protected PrefabRotation[] |
rotations |
|
Path |
var1 |
|
PrefabListAsset |
var2 |
Методы (11)
| Модификаторы | Возврат | Сигнатура |
|---|---|---|
abstract |
return new |
PrefabSpawnerreturn new PrefabSpawner(this) |
|
protected void |
afterDecodeprotected void afterDecode() |
|
public Spawner |
createpublic Spawner create() |
|
protected Path |
getRandomPrefabPathprotected Path getRandomPrefabPath() |
|
|
if if(this.prefabsRef == null) |
|
|
if if(var2 == null) |
|
|
if if(this.prefabsRef == null) |
|
|
if if(this.prefabs == null) |
|
|
if if(this.prefabs == null) |
|
|
if if(this.prefabsRef != null) |
|
public void |
validatepublic void validate(@Nonnull Validator var1) |
Исходный код
Показать/скрыть
class="kw">package com.hypixel.hytale.builtin.adventure.worldevents.condition.spawner;
class="kw">import com.hypixel.hytale.assetstore.codec.ContainedAssetCodec;
class="kw">import com.hypixel.hytale.builtin.adventure.worldevents.context.ContextKey;
class="kw">import com.hypixel.hytale.builtin.adventure.worldevents.context.LocationContext;
class="kw">import com.hypixel.hytale.builtin.adventure.worldevents.context.PrefabPasteContext;
class="kw">import com.hypixel.hytale.builtin.adventure.worldevents.event.Validator;
class="kw">import com.hypixel.hytale.builtin.adventure.worldevents.event.WorldEvent;
class="kw">import com.hypixel.hytale.builtin.adventure.worldevents.util.PlacementMask;
class="kw">import com.hypixel.hytale.builtin.adventure.worldevents.util.PrefabSnapshotUtil;
class="kw">import com.hypixel.hytale.codec.Codec;
class="kw">import com.hypixel.hytale.codec.KeyedCodec;
class="kw">import com.hypixel.hytale.codec.builder.BuilderCodec;
class="kw">import com.hypixel.hytale.codec.codecs.EnumCodec;
class="kw">import com.hypixel.hytale.codec.codecs.array.ArrayCodec;
class="kw">import com.hypixel.hytale.common.util.RandomUtil;
class="kw">import com.hypixel.hytale.logger.HytaleLogger;
class="kw">import com.hypixel.hytale.math.vector.Rotation3f;
class="kw">import com.hypixel.hytale.server.core.asset.type.buildertool.config.PrefabListAsset;
class="kw">import com.hypixel.hytale.server.core.asset.util.AssetPackPathUtil;
class="kw">import com.hypixel.hytale.server.core.prefab.PrefabRotation;
class="kw">import com.hypixel.hytale.server.core.prefab.selection.buffer.impl.IPrefabBuffer;
class="kw">import com.hypixel.hytale.server.core.universe.world.World;
class="kw">import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore;
class="kw">import java.nio.file.Path;
class="kw">import java.util.Random;
class="kw">import javax.annotation.Nonnull;
class="kw">import javax.annotation.Nullable;
class="kw">import org.joml.Vector3i;
class="kw">import org.joml.Vector3ic;
class="kw">public class PrefabSpawner class="kw">implements Spawner {
class="kw">public class="kw">static class="kw">final String ID = "PrefabSpawner";
class="kw">private class="kw">static class="kw">final HytaleLogger LOGGER = HytaleLogger.forEnclosingClass();
@Nonnull
class="kw">protected class="kw">final PrefabSpawner.Config config;
@Nullable
class="kw">protected Path selected = null;
@Nonnull
class="kw">protected PrefabRotation rotation = PrefabRotation.ROTATION_0;
class="kw">public PrefabSpawner(@Nonnull PrefabSpawner.Config var1) {
this.config = var1;
}
@Override
class="kw">public Spawner.Result test(@Nonnull World var1, @Nonnull Vector3ic var2, @Nonnull Random var3) {
Path var4 = this.config.getRandomPrefabPath();
if (var4 == null) {
((HytaleLogger.Api)LOGGER.atWarning()).log("Spawner failed: config did not class="kw">return a prefab path");
class="kw">return Spawner.Result.ERROR;
}
IPrefabBuffer var5 = PrefabSnapshotUtil.getAssetPrefabOrNull(var4);
if (var5 == null) {
((HytaleLogger.Api)LOGGER.atWarning()).log("Spawner failed: prefab not found under path: %s", var4);
class="kw">return Spawner.Result.ERROR;
}
int var6 = var2.x();
int var7 = var2.y();
int var8 = var2.z();
int var9 = Math.max(1, var5.getMaxY());
ChunkStore var10 = var1.getChunkStore();
PrefabRotation var11 = RandomUtil.selectRandomOrDefault(this.config.rotations, PrefabRotation.ROTATION_0, var3);
if (this.isInvalid(var6, var8, var7, var9, var10)) {
class="kw">return Spawner.Result.FAIL;
}
int var12 = var6 + var5.getMinX(var11);
int var13 = var8 + var5.getMinZ(var11);
int var14 = var6 + var5.getMaxX(var11);
int var15 = var8 + var5.getMaxZ(var11);
if (this.isInvalid(var6, var13, var7, var9, var10)) {
class="kw">return Spawner.Result.FAIL;
}
if (this.isInvalid(var6, var15, var7, var9, var10)) {
class="kw">return Spawner.Result.FAIL;
}
if (this.isInvalid(var12, var8, var7, var9, var10)) {
class="kw">return Spawner.Result.FAIL;
}
if (this.isInvalid(var14, var8, var7, var9, var10)) {
class="kw">return Spawner.Result.FAIL;
}
this.selected = var4;
this.rotation = var11;
class="kw">return Spawner.Result.PASS;
}
@Override
class="kw">public boolean onComplete(@Nonnull Vector3ic var1, @Nonnull WorldEvent var2) {
if (this.config.prefabKey == null) {
((HytaleLogger.Api)LOGGER.atWarning()).log("Spawner failed: prefabKey not set");
class="kw">return false;
} else if (this.selected == null) {
((HytaleLogger.Api)LOGGER.atWarning()).log("Spawner failed: Could not select a prefab to spawn");
class="kw">return false;
} else {
Path var3 = AssetPackPathUtil.relativizeAssetPath(this.selected);
if (var3 == null) {
((HytaleLogger.Api)LOGGER.atWarning()).log("Spawner failed: Failed to relativize asset path: %s", this.selected);
class="kw">return false;
} else {
var2.context.put(this.config.prefabKey, new PrefabPasteContext(var3, new Vector3i(var1), this.rotation));
var2.context.put(this.config.prefabKey, new LocationContext(new Vector3i(var1), new Rotation3f(0.0F, -this.rotation.getYaw(), 0.0F)));
class="kw">return true;
}
}
}
class="kw">protected boolean isInvalid(int var1, int var2, int var3, int var4, @Nonnull ChunkStore var5) {
int var6 = this.config.maxHeightDelta + 1;
int var7 = Spawner.findSurface(var1, var3, var2, 1, var6, var5);
if (var7 != Integer.MIN_VALUE && Math.abs(var3 - var7) <= this.config.maxHeightDelta) {
if (!this.config.mask.testBlock(Spawner.getBlockId(var1, var7, var2, var5))) {
class="kw">return true;
}
if (!this.config.mask.testFluid(Spawner.getFluidId(var1, var7 + 1, var2, var5))) {
class="kw">return true;
}
int var8 = Math.max(var3, var7);
for (int var9 = 1; var9 <= var4; var9++) {
if (Spawner.getBlockId(var1, var8 + var9, var2, var5) != 0) {
class="kw">return true;
}
}
class="kw">return false;
} else {
class="kw">return true;
}
}
class="kw">public class="kw">static class Config class="kw">implements Spawner.Config {
class="kw">protected class="kw">static class="kw">final Codec<String> PREFAB_CODEC = new ContainedAssetCodec<>(PrefabListAsset.class, PrefabListAsset.CODEC);
class="kw">public class="kw">static class="kw">final BuilderCodec<PrefabSpawner.Config> CODEC = BuilderCodec.builder(PrefabSpawner.Config.class, PrefabSpawner.Config::new)
.documentation("A spawner that finds a suitable location to place a prefab.")
.<ContextKey>append(new KeyedCodec<>("PrefabKey", ContextKey.CODEC), (var0, var1) -> var0.prefabKey = var1, var0 -> var0.prefabKey)
.documentation("Define the variable name for the prefab that this spawner places that can be referenced by other conditions and actions.")
.add()
.<String>append(new KeyedCodec<>("Prefab", PREFAB_CODEC), (var0, var1) -> var0.prefabsRef = var1, var0 -> var0.prefabsRef)
.documentation("The prefab asset(s) that the spawner will randomly choose from and attempt to place.")
.add()
.<PrefabRotation[]>append(
new KeyedCodec<>("Rotations", new ArrayCodec<>(new EnumCodec<>(PrefabRotation.class), PrefabRotation[]::new)),
(var0, var1) -> var0.rotations = var1,
var0 -> var0.rotations
)
.documentation("Candidate prefab rotations; one is chosen at random for each placement attempt.")
.add()
.<Integer>append(new KeyedCodec<>("MaxHeightDelta", Codec.INTEGER), (var0, var1) -> var0.maxHeightDelta = var1, var0 -> var0.maxHeightDelta)
.documentation("Maximum terrain height difference allowed across the prefab footprint; placement is rejected when exceeded.")
.add()
.<PlacementMask>append(new KeyedCodec<>("Mask", PlacementMask.CODEC), (var0, var1) -> var0.mask = var1, var0 -> var0.mask)
.documentation(
"A PlacementMask determining the types of blocks and fluids that the prefab can place onto. Can be removed to allow placement on any block or fluid."
)
.add()
.afterDecode(PrefabSpawner.Config::afterDecode)
.build();
@Nullable
class="kw">protected ContextKey prefabKey = null;
@Nullable
class="kw">protected String prefabsRef = null;
@Nonnull
class="kw">protected PrefabRotation[] rotations = PrefabRotation.VALUES;
@Nonnull
class="kw">protected PlacementMask mask = PlacementMask.DEFAULT_ANY;
class="kw">protected int maxHeightDelta = 4;
@Nullable
class="kw">protected class="kw">transient PrefabListAsset prefabs = null;
class="kw">public Config() {
}
@Nonnull
@Override
class="kw">public Spawner create() {
this.mask.compile();
class="kw">return new PrefabSpawner(this);
}
@Override
class="kw">public void validate(@Nonnull Validator var1) {
var1.addDefinedKey("PrefabSpawner", "PrefabKey", this.prefabKey);
if (this.prefabsRef == null) {
var1.addErrorMessage("Missing PrefabList entry");
} else {
PrefabListAsset var2 = PrefabListAsset.getAssetMap().getAsset(this.prefabsRef);
if (var2 == null) {
var1.addErrorMessage("Failed to find PrefabList: %s", this.prefabsRef);
} else {
if (var2.getPrefabPaths() == null || var2.getPrefabPaths().length == 0) {
var1.addErrorMessage("PrefabList is empty: %s", this.prefabsRef);
}
}
}
}
@Nullable
class="kw">protected Path getRandomPrefabPath() {
if (this.prefabsRef == null) {
class="kw">return null;
}
if (this.prefabs == null) {
this.prefabs = PrefabListAsset.getAssetMap().getAsset(this.prefabsRef);
if (this.prefabs == null) {
class="kw">return null;
}
}
Path var1 = this.prefabs.getRandomPrefab();
class="kw">return var1 == null ? null : PrefabSnapshotUtil.resolveAssetReferencePath(var1);
}
class="kw">protected void afterDecode() {
if (this.prefabsRef != null) {
this.prefabs = PrefabListAsset.getAssetMap().getAsset(this.prefabsRef);
} else {
this.prefabs = null;
}
}
}
}