From 0b8afe49946f832694242b6f986f955e2b2c35ab Mon Sep 17 00:00:00 2001 From: Sollace Date: Thu, 15 Sep 2022 00:41:53 +0200 Subject: [PATCH] Commit missing file --- .../unicopia/ability/data/tree/TreeTypes.java | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/main/java/com/minelittlepony/unicopia/ability/data/tree/TreeTypes.java diff --git a/src/main/java/com/minelittlepony/unicopia/ability/data/tree/TreeTypes.java b/src/main/java/com/minelittlepony/unicopia/ability/data/tree/TreeTypes.java new file mode 100644 index 00000000..9225bf48 --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/ability/data/tree/TreeTypes.java @@ -0,0 +1,79 @@ +package com.minelittlepony.unicopia.ability.data.tree; + +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import com.minelittlepony.unicopia.util.PosHelper; +import net.minecraft.block.BlockState; +import net.minecraft.block.Blocks; +import net.minecraft.block.LeavesBlock; +import net.minecraft.item.ItemStack; +import net.minecraft.tag.BlockTags; +import net.minecraft.util.Identifier; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; + +public class TreeTypes { + private static Set entries = new HashSet<>(); + + private static final TreeType any1x = createDynamic(false); + private static final TreeType any2x = createDynamic(true); + + public static void load(Map types) { + entries = types.entrySet().stream().map(e -> e.getValue().toTreeType(e.getKey())).collect(Collectors.toSet()); + } + + static TreeType get(BlockState state, BlockPos pos, World world) { + return entries.stream() + .filter(type -> type.matches(state)) + .findFirst() + .map(type -> TreeType.of(type, type.findLeavesType(world, pos))) + .orElseGet(() -> { + if (any1x.matches(state)) { + if (PosHelper.any(pos, p -> world.getBlockState(p).isOf(state.getBlock()), PosHelper.HORIZONTAL)) { + return any2x; + } + + return any1x; + } + + return TreeType.NONE; + }); + } + + static TreeType get(BlockState state) { + return entries.stream() + .filter(type -> type.matches(state)) + .findFirst() + .orElse(TreeType.NONE); + } + + private static TreeType createDynamic(boolean wide) { + return new TreeType() { + @Override + public boolean isLeaves(BlockState state) { + return (state.isIn(BlockTags.LEAVES) || state.getBlock() instanceof LeavesBlock || entries.stream().anyMatch(t -> t.isLeaves(state))) && TreeTypeImpl.isNonPersistent(state); + } + + @Override + public boolean isLog(BlockState state) { + return state.isIn(BlockTags.LOGS_THAT_BURN) || entries.stream().anyMatch(t -> t.isLog(state)); + } + + @Override + public ItemStack pickRandomStack(BlockState state) { + TreeType type = get(state); + if (type == TreeType.NONE) { + type = get(Blocks.OAK_LOG.getDefaultState()); + } + return type.pickRandomStack(state); + } + + @Override + public boolean isWide() { + return wide; + } + }; + } +}