PrettyPipes/src/main/java/de/ellpeck/prettypipes/network/NetworkLock.java

58 lines
1.7 KiB
Java
Raw Normal View History

package de.ellpeck.prettypipes.network;
2020-05-09 16:27:49 +02:00
import net.minecraft.item.ItemStack;
2020-05-09 12:57:25 +02:00
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT;
import net.minecraftforge.common.util.INBTSerializable;
import java.util.Objects;
import java.util.UUID;
2020-05-09 12:57:25 +02:00
public class NetworkLock implements INBTSerializable<CompoundNBT> {
// identify locks by UUID since network locks can't be identified by location and locked item alone
// (two locks could be set for the same item and the same amount if it exists twice in the chest)
private UUID lockId = UUID.randomUUID();
2020-05-09 12:57:25 +02:00
public NetworkLocation location;
2020-05-09 16:27:49 +02:00
public ItemStack stack;
2020-05-09 16:27:49 +02:00
public NetworkLock(NetworkLocation location, ItemStack stack) {
this.location = location;
2020-05-09 16:27:49 +02:00
this.stack = stack;
}
2020-05-09 12:57:25 +02:00
public NetworkLock(CompoundNBT nbt) {
this.deserializeNBT(nbt);
}
@Override
public CompoundNBT serializeNBT() {
CompoundNBT nbt = new CompoundNBT();
nbt.putUniqueId("id", this.lockId);
2020-05-09 12:57:25 +02:00
nbt.put("location", this.location.serializeNBT());
2020-05-09 16:27:49 +02:00
nbt.put("stack", this.stack.write(new CompoundNBT()));
2020-05-09 12:57:25 +02:00
return nbt;
}
@Override
public void deserializeNBT(CompoundNBT nbt) {
this.lockId = nbt.getUniqueId("id");
2020-05-09 12:57:25 +02:00
this.location = new NetworkLocation(nbt.getCompound("location"));
2020-05-09 16:27:49 +02:00
this.stack = ItemStack.read(nbt.getCompound("stack"));
2020-05-09 12:57:25 +02:00
}
@Override
public boolean equals(Object o) {
if (o instanceof NetworkLock) {
NetworkLock that = (NetworkLock) o;
return this.lockId.equals(that.lockId);
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(this.lockId);
}
}