ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/common/misc/apiimpl/ConnectionPair.java

96 lines
3.1 KiB
Java
Raw Normal View History

2020-09-09 16:49:01 +02:00
package de.ellpeck.actuallyadditions.common.misc.apiimpl;
2016-09-12 16:13:39 +02:00
import de.ellpeck.actuallyadditions.api.laser.IConnectionPair;
import de.ellpeck.actuallyadditions.api.laser.LaserType;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.math.BlockPos;
2019-05-02 09:10:29 +02:00
public class ConnectionPair implements IConnectionPair {
2016-09-12 16:13:39 +02:00
private final BlockPos[] positions = new BlockPos[2];
private boolean suppressConnectionRender;
private LaserType type;
2019-05-02 09:10:29 +02:00
public ConnectionPair() {
2016-09-12 16:13:39 +02:00
}
2019-05-02 09:10:29 +02:00
public ConnectionPair(BlockPos firstRelay, BlockPos secondRelay, LaserType type, boolean suppressConnectionRender) {
this.positions[0] = firstRelay;
this.positions[1] = secondRelay;
this.suppressConnectionRender = suppressConnectionRender;
this.type = type;
}
2016-09-12 16:13:39 +02:00
@Override
2019-05-02 09:10:29 +02:00
public void readFromNBT(NBTTagCompound compound) {
if (compound != null) {
for (int i = 0; i < this.positions.length; i++) {
int anX = compound.getInteger("x" + i);
int aY = compound.getInteger("y" + i);
int aZ = compound.getInteger("z" + i);
2016-09-12 16:13:39 +02:00
this.positions[i] = new BlockPos(anX, aY, aZ);
}
2016-09-12 16:13:39 +02:00
this.suppressConnectionRender = compound.getBoolean("SuppressRender");
String typeStrg = compound.getString("Type");
2019-05-02 09:10:29 +02:00
if (typeStrg != null && !typeStrg.isEmpty()) {
2016-09-12 16:13:39 +02:00
this.type = LaserType.valueOf(typeStrg);
}
}
2016-09-12 16:13:39 +02:00
}
@Override
2019-05-02 09:10:29 +02:00
public BlockPos[] getPositions() {
2016-09-12 16:13:39 +02:00
return this.positions;
}
@Override
2019-05-02 09:10:29 +02:00
public boolean doesSuppressRender() {
2016-09-12 16:13:39 +02:00
return this.suppressConnectionRender;
}
@Override
2019-05-02 09:10:29 +02:00
public LaserType getType() {
2016-09-12 16:13:39 +02:00
return this.type;
}
@Override
2019-05-02 09:10:29 +02:00
public boolean contains(BlockPos relay) {
for (BlockPos position : this.positions) {
if (position != null && position.equals(relay)) { return true; }
}
return false;
}
@Override
2019-05-02 09:10:29 +02:00
public String toString() {
return (this.positions[0] == null ? "-" : this.positions[0].toString()) + " | " + (this.positions[1] == null ? "-" : this.positions[1].toString());
}
2016-09-12 16:13:39 +02:00
@Override
2019-05-02 09:10:29 +02:00
public void writeToNBT(NBTTagCompound compound) {
for (int i = 0; i < this.positions.length; i++) {
BlockPos relay = this.positions[i];
2019-05-02 09:10:29 +02:00
compound.setInteger("x" + i, relay.getX());
compound.setInteger("y" + i, relay.getY());
compound.setInteger("z" + i, relay.getZ());
}
2019-05-02 09:10:29 +02:00
if (this.type != null) {
2016-09-01 18:08:15 +02:00
compound.setString("Type", this.type.name());
}
compound.setBoolean("SuppressRender", this.suppressConnectionRender);
}
@Override
2019-05-02 09:10:29 +02:00
public boolean equals(Object obj) {
if (obj instanceof ConnectionPair) {
ConnectionPair pair = (ConnectionPair) obj;
for (int i = 0; i < this.positions.length; i++) {
if (this.positions[i] == pair.positions[i] || this.positions[i] != null && this.positions[i].equals(pair.positions[i])) { return true; }
}
}
return super.equals(obj);
}
}