mirror of
https://github.com/GTNewHorizons/StructureLib.git
synced 2026-10-25 20:32:24 +00:00
Add utility method for lazy initialized structure elements
This commit is contained in:
1 parent
74baa66f52
commit
393045d4f2
2 files changed
+61
No files matched your search
@@ -0,0 +1,38 @@
|
||||
package com.gtnewhorizon.structurelib.structure;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
class LazyStructureElement<T> implements IStructureElementDeferred<T> {
|
||||
private Function<T, IStructureElement<T>> to;
|
||||
private IStructureElement<T> elem;
|
||||
|
||||
public LazyStructureElement(Function<T, IStructureElement<T>> to) {
|
||||
this.to = to;
|
||||
}
|
||||
|
||||
private IStructureElement<T> get(T t) {
|
||||
if (to != null) {
|
||||
elem = to.apply(t);
|
||||
to = null;
|
||||
}
|
||||
return elem;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean check(T t, World world, int x, int y, int z) {
|
||||
return get(t).check(t, world, x, y, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean placeBlock(T t, World world, int x, int y, int z, ItemStack trigger) {
|
||||
return get(t).placeBlock(t, world, x, y, z, trigger);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean spawnHint(T t, World world, int x, int y, int z, ItemStack trigger) {
|
||||
return get(t).spawnHint(t, world, x, y, z, trigger);
|
||||
}
|
||||
}
|
||||
@@ -787,6 +787,28 @@ public class StructureUtility {
|
||||
|
||||
//region defer
|
||||
|
||||
/**
|
||||
* Similar to defer, but caches the first returned element returned and won't call it again.
|
||||
* Initialization is not thread safe.
|
||||
*/
|
||||
public static <T> IStructureElementDeferred<T> lazy(Supplier<IStructureElement<T>> to) {
|
||||
if (to == null) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
return new LazyStructureElement<>(t -> to.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Similar to defer, but caches the first returned element returned and won't call it again.
|
||||
* Initialization is not thread safe.
|
||||
*/
|
||||
public static <T> IStructureElementDeferred<T> lazy(Function<T, IStructureElement<T>> to) {
|
||||
if (to == null) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
return new LazyStructureElement<>(to);
|
||||
}
|
||||
|
||||
public static <T> IStructureElementDeferred<T> defer(Supplier<IStructureElement<T>> to) {
|
||||
if (to == null) {
|
||||
throw new IllegalArgumentException();
|
||||
@@ -1414,4 +1436,5 @@ public class StructureUtility {
|
||||
}
|
||||
return shape;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user