2018-11-23 19:21:52 +01:00
|
|
|
package de.ellpeck.naturesaura.api.multiblock;
|
|
|
|
|
|
|
|
import net.minecraft.block.Block;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraft.block.BlockState;
|
|
|
|
import net.minecraft.block.Blocks;
|
2018-11-23 19:21:52 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
import net.minecraftforge.oredict.OreDictionary;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class Matcher {
|
|
|
|
|
2019-10-20 22:30:49 +02:00
|
|
|
private final BlockState defaultState;
|
2018-11-23 19:21:52 +01:00
|
|
|
private final ICheck check;
|
|
|
|
|
2019-10-20 22:30:49 +02:00
|
|
|
public Matcher(BlockState defaultState, ICheck check) {
|
2018-11-23 19:21:52 +01:00
|
|
|
this.defaultState = defaultState;
|
|
|
|
this.check = check;
|
|
|
|
}
|
|
|
|
|
2019-10-20 22:30:49 +02:00
|
|
|
public BlockState getDefaultState() {
|
2018-11-23 19:21:52 +01:00
|
|
|
return this.defaultState;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ICheck getCheck() {
|
|
|
|
return this.check;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Matcher wildcard() {
|
|
|
|
return new Matcher(Blocks.AIR.getDefaultState(), null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Matcher oreDict(Block defaultBlock, String name) {
|
|
|
|
return new Matcher(defaultBlock.getDefaultState(), new ICheck() {
|
2019-10-20 22:30:49 +02:00
|
|
|
private List<BlockState> states;
|
2018-11-23 19:21:52 +01:00
|
|
|
|
|
|
|
@Override
|
2019-10-20 22:30:49 +02:00
|
|
|
public boolean matches(World world, BlockPos start, BlockPos offset, BlockPos pos, BlockState state, char c) {
|
2018-11-23 19:21:52 +01:00
|
|
|
if (this.states == null) {
|
|
|
|
this.states = new ArrayList<>();
|
|
|
|
for (ItemStack stack : OreDictionary.getOres(name)) {
|
|
|
|
Block block = Block.getBlockFromItem(stack.getItem());
|
|
|
|
if (block != null && block != Blocks.AIR) {
|
|
|
|
int damage = stack.getItemDamage();
|
|
|
|
if (damage == OreDictionary.WILDCARD_VALUE)
|
|
|
|
this.states.addAll(block.getBlockState().getValidStates());
|
|
|
|
else
|
|
|
|
this.states.add(block.getStateFromMeta(damage));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.states.isEmpty() || this.states.contains(state);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface ICheck {
|
2019-10-20 22:30:49 +02:00
|
|
|
boolean matches(World world, BlockPos start, BlockPos offset, BlockPos pos, BlockState state, char c);
|
2018-11-23 19:21:52 +01:00
|
|
|
}
|
|
|
|
}
|