summaryrefslogtreecommitdiff
path: root/src/main/java/com/sosnitzka/taiga/util/StateMatcher.java
blob: 5c6da464519da22c532347dfdbd1a3c4f1a70316 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package com.sosnitzka.taiga.util;

import com.google.common.base.Predicate;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.state.IBlockState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;


public class StateMatcher implements Predicate<IBlockState> {
    private final IBlockState state;
    private final IProperty property;
    private final Comparable value;

    private StateMatcher(IBlockState state, IProperty property, Comparable value) {
        this.state = state;
        this.property = property;
        this.value = value;
    }

    static StateMatcher forState(IBlockState state, IProperty property, Comparable value) {
        return new StateMatcher(state, property, value);
    }

    public boolean apply(IBlockState state, BlockPos pos, World world) {
        if (state != null) {
            if (property != null && value != null) {
                if (state.getBlock() == this.state.getBlock())
                    return checkLayerForBlocks(3, 3, -1, world, pos) ||
                            checkLayerForBlocks(3, 3, 0, world, pos) ||
                            checkLayerForBlocks(3, 3, 1, world, pos);

            } else
                return state.getBlock() == this.state.getBlock();
        }
        return false;
    }

    @Override
    public boolean apply(IBlockState input) {
        if (state != null) {
            if (property != null && value != null) {
                return state.getBlock() == this.state.getBlock() && state.getValue(property) == value;
            } else
                return state.getBlock() == this.state.getBlock();
        }
        return false;
    }

    private boolean checkLayerForBlocks(int X, int Z, int Y, World world, BlockPos origin) {
        int x = 0, z = 0, dx = 0, dz = -1;
        int t = Math.max(X, Z);
        int maxI = t * t;

        for (int i = 0; i < maxI; i++) {
            if ((-X / 2 <= x) && (x <= X / 2) && (-Z / 2 <= z) && (z <= Z / 2)) {
                BlockPos blockPos = new BlockPos(origin.getX() + x, origin.getY() + Y, origin.getZ() + z);
                if (blockPos == origin)
                    continue;

                if (i % 2 == 0)
                    continue;

                IBlockState bState = world.getBlockState(blockPos);
                if (bState.getBlock() == this.state.getBlock() && bState.getValue(property) == value) {
                    return true;
                }
            }

            if ((x == z) || ((x < 0) && (x == -z)) || ((x > 0) && (x == 1 - z))) {
                t = dx;
                dx = -dz;
                dz = t;
            }
            x += dx;
            z += dz;
        }

        return false;
    }
}