com.ethylamine.fsynthesis.util.position.ChunkCoord.java Source code

Java tutorial

Introduction

Here is the source code for com.ethylamine.fsynthesis.util.position.ChunkCoord.java

Source

package com.ethylamine.fsynthesis.util.position;
/*
 * Copyright (c) 2014 Robin Glover
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, see <http://www.gnu.org/licenses>.
 */

import com.google.common.base.Objects;
import net.minecraftforge.event.world.ChunkEvent;
import org.apache.commons.lang3.tuple.ImmutablePair;

public class ChunkCoord implements Comparable<ChunkCoord> {
    private final ImmutablePair<Integer, Integer> data;

    private ChunkCoord(int x, int z) {
        data = ImmutablePair.of(x, z);
    }

    private ChunkCoord(ChunkEvent event) {
        this(event.getChunk().xPosition, event.getChunk().zPosition);
    }

    public static ChunkCoord of(int x, int z) {
        return new ChunkCoord(x, z);
    }

    public static ChunkCoord of(WorldBlockCoord coord) {
        return new ChunkCoord(coord.getX() >> 4, coord.getZ() >> 4);
    }

    public static ChunkCoord of(ChunkEvent event) {
        return new ChunkCoord(event);
    }

    public boolean containsWorldCoord(WorldBlockCoord coord) {
        return equals(of(coord));
    }

    public int getX() {
        return data.left;
    }

    public int getZ() {
        return data.right;
    }

    public WorldBlockCoord localToWorldCoords(ChunkBlockCoord coord) {
        return WorldBlockCoord.of((data.left << 4) + coord.getX(), coord.getY(), (data.right << 4) + coord.getZ());
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(data.left, data.right);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        final ChunkCoord that = (ChunkCoord) o;
        return data.left.equals(that.data.left) && data.right.equals(that.data.right);
    }

    @Override
    public String toString() {
        return Objects.toStringHelper(this).add("X", data.left).add("Z", data.right).toString();
    }

    @Override
    public int compareTo(ChunkCoord o) {
        return data.left.equals(o.data.left) ? data.right.compareTo(o.data.right)
                : data.left.compareTo(o.data.left);
    }
}