mirror of
https://github.com/GTNewHorizons/StructureLib.git
synced 2026-11-17 02:45:11 +00:00
expose what blocks this element might place
This commit is contained in:
1 parent
0ac8908b37
commit
7d74a65ec0
4 files changed
+358
-488
No files matched your search
@@ -4,10 +4,18 @@ import static com.gtnewhorizon.structurelib.StructureLib.LOGGER;
|
||||
import static com.gtnewhorizon.structurelib.StructureLib.PANIC_MODE;
|
||||
|
||||
import com.gtnewhorizon.structurelib.StructureLibAPI;
|
||||
import com.gtnewhorizon.structurelib.util.ItemStackPredicate;
|
||||
import com.gtnewhorizon.structurelib.util.ItemStackPredicate.NBTMode;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.IChatComponent;
|
||||
import net.minecraft.world.World;
|
||||
@@ -58,6 +66,30 @@ public interface IStructureElement<T> {
|
||||
return PlaceResult.SKIP;
|
||||
}
|
||||
|
||||
/**
|
||||
* A simplified version of survivalPlaceBlock. Return null if not implemented.
|
||||
* <p>
|
||||
* <b>BY OVERRIDING THIS TO NULL, YOU ACKNOWLEDGE THAT THE {@link #check(Object, World, int, int, int)} OF THIS
|
||||
* CLASS IS SIDE EFFECT FREE AND CAN HAVE ITS CHECK CALLED WITHOUT BREAKING ANYTHING. OTHERWISE OVERRIDE THE
|
||||
* {@link #survivalPlaceBlock(Object, World, int, int, int, ItemStack, AutoPlaceEnvironment)} TO PROVIDE A SIDEEFFECT
|
||||
* FREE {@link PlaceResult#SKIP}.</b>
|
||||
* <p>
|
||||
* It should be noticed that this filter is only advisory and the actual {@link #survivalPlaceBlock(Object, World, int, int, int, ItemStack, AutoPlaceEnvironment)}
|
||||
* or {@link #check(Object, World, int, int, int)} are free to reject/accept/place blocks not contained in this list,
|
||||
* e.g. when the element can be placed anywhere in the structure, but no more than 3 overall; when the structure
|
||||
* element cannot know all blocks it will accept at the time of calling; when this is backed by a legacy
|
||||
* {@link IStructureElement} that just didn't implement this API.
|
||||
*
|
||||
* @param trigger trigger item
|
||||
* @param env autoplacing environment
|
||||
* @return null if not implemented, otherwise an instance describing what kind of blocks will be placed by {@link #survivalPlaceBlock(Object, World, int, int, int, ItemStack, AutoPlaceEnvironment)}
|
||||
*/
|
||||
@Nullable
|
||||
default BlocksToPlace getBlocksToPlace(
|
||||
T t, World world, int x, int y, int z, ItemStack trigger, AutoPlaceEnvironment env) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Try place the block by taking resource from given IItemSource.
|
||||
* <p>
|
||||
@@ -69,10 +101,26 @@ public interface IStructureElement<T> {
|
||||
*/
|
||||
default PlaceResult survivalPlaceBlock(
|
||||
T t, World world, int x, int y, int z, ItemStack trigger, AutoPlaceEnvironment env) {
|
||||
BlocksToPlace e = getBlocksToPlace(t, world, x, y, z, trigger, env);
|
||||
IItemSource source = env.getSource();
|
||||
EntityPlayer actor = env.getActor();
|
||||
Consumer<IChatComponent> chatter = env.getChatter();
|
||||
if (e != null) {
|
||||
if (check(t, world, x, y, z)) return PlaceResult.SKIP;
|
||||
if (e.getStacks() == null) {
|
||||
ItemStack taken = source.takeOne(e.getPredicate(), true);
|
||||
return StructureUtility.survivalPlaceBlock(
|
||||
taken, NBTMode.EXACT, taken.stackTagCompound, true, world, x, y, z, source, actor, chatter);
|
||||
}
|
||||
for (ItemStack stack : e.getStacks()) {
|
||||
if (!source.takeOne(stack, true)) continue;
|
||||
return StructureUtility.survivalPlaceBlock(
|
||||
stack, NBTMode.EXACT, stack.stackTagCompound, true, world, x, y, z, source, actor, chatter);
|
||||
}
|
||||
return PlaceResult.REJECT;
|
||||
}
|
||||
if (actor instanceof EntityPlayerMP)
|
||||
return survivalPlaceBlock(
|
||||
t, world, x, y, z, trigger, env.getSource(), (EntityPlayerMP) actor, env.getChatter());
|
||||
return survivalPlaceBlock(t, world, x, y, z, trigger, source, (EntityPlayerMP) actor, chatter);
|
||||
if (PANIC_MODE) throw new RuntimeException("Panic Tripwire hit");
|
||||
if (StructureLibAPI.isDebugEnabled())
|
||||
LOGGER.info(
|
||||
@@ -154,4 +202,81 @@ public interface IStructureElement<T> {
|
||||
*/
|
||||
ACCEPT_STOP;
|
||||
}
|
||||
|
||||
final class BlocksToPlace {
|
||||
public static final BlocksToPlace errored = createEmpty();
|
||||
private final Predicate<ItemStack> predicate;
|
||||
private final Iterable<ItemStack> stacks;
|
||||
|
||||
public static BlocksToPlace createEmpty() {
|
||||
return new BlocksToPlace(s -> false, Collections.emptyList());
|
||||
}
|
||||
|
||||
public static BlocksToPlace create(ItemStack... stacks) {
|
||||
return create(Arrays.asList(stacks));
|
||||
}
|
||||
|
||||
public static BlocksToPlace create(Iterable<ItemStack> stacks) {
|
||||
Predicate<ItemStack> predicate = null;
|
||||
|
||||
for (ItemStack stack : stacks) {
|
||||
ItemStackPredicate p = ItemStackPredicate.from(stack, NBTMode.EXACT);
|
||||
if (predicate == null) predicate = p;
|
||||
else predicate = predicate.or(p);
|
||||
}
|
||||
|
||||
return new BlocksToPlace(predicate, stacks);
|
||||
}
|
||||
|
||||
public static BlocksToPlace create(Block block, int meta) {
|
||||
Item itemBlock = Item.getItemFromBlock(block);
|
||||
if (itemBlock instanceof ISpecialItemBlock) {
|
||||
meta = ((ISpecialItemBlock) itemBlock).getItemMetaFromBlockMeta(block, meta);
|
||||
}
|
||||
return create(itemBlock, meta);
|
||||
}
|
||||
|
||||
public static BlocksToPlace create(Item item, int meta) {
|
||||
return new BlocksToPlace(
|
||||
ItemStackPredicate.from(item).setMeta(meta),
|
||||
Collections.singletonList(new ItemStack(item, 1, meta)));
|
||||
}
|
||||
|
||||
public static BlocksToPlace create(ItemStack itemStack) {
|
||||
return new BlocksToPlace(ItemStackPredicate.from(itemStack), Collections.singletonList(itemStack));
|
||||
}
|
||||
|
||||
public static BlocksToPlace create(Predicate<ItemStack> predicate) {
|
||||
return new BlocksToPlace(predicate, null);
|
||||
}
|
||||
|
||||
BlocksToPlace(Predicate<ItemStack> predicate, Iterable<ItemStack> stacks) {
|
||||
this.predicate = predicate;
|
||||
this.stacks = stacks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the blocks to place filter. This is usually slower as it requires walking through inventories one by one.
|
||||
* <p>
|
||||
* Suitable for use with {@link IItemSource#takeOne(Predicate, boolean)}
|
||||
* @return a predicate. never null.
|
||||
*/
|
||||
@Nonnull
|
||||
public Predicate<ItemStack> getPredicate() {
|
||||
return predicate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the ItemStacks that this one is known to accept. Can be null if this info is not available at the time
|
||||
* this instance is constructed.
|
||||
* <p>
|
||||
* Suitable for use with {@link IItemSource#takeOne(ItemStack, boolean)}.
|
||||
* <p>
|
||||
* This should be equivalent to the predicate returned from {@link #getPredicate()}
|
||||
*/
|
||||
@Nullable
|
||||
public Iterable<ItemStack> getStacks() {
|
||||
return stacks;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user