2020-05-09 01:00:59 +02:00
|
|
|
package de.ellpeck.prettypipes.network;
|
|
|
|
|
2021-12-02 12:31:04 +01:00
|
|
|
import net.minecraft.nbt.CompoundTag;
|
2021-12-02 16:55:04 +01:00
|
|
|
import net.minecraft.world.item.ItemStack;
|
2024-02-03 15:17:58 +01:00
|
|
|
import net.neoforged.neoforge.common.util.INBTSerializable;
|
2020-05-09 01:00:59 +02:00
|
|
|
|
2020-05-09 20:12:50 +02:00
|
|
|
import java.util.Objects;
|
|
|
|
import java.util.UUID;
|
2020-05-09 12:57:25 +02:00
|
|
|
|
2021-12-02 12:31:04 +01:00
|
|
|
public class NetworkLock implements INBTSerializable<CompoundTag> {
|
2020-05-09 12:57:25 +02:00
|
|
|
|
2020-05-09 20:12:50 +02:00
|
|
|
// 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 01:00:59 +02:00
|
|
|
|
2020-05-09 16:27:49 +02:00
|
|
|
public NetworkLock(NetworkLocation location, ItemStack stack) {
|
2020-05-09 01:00:59 +02:00
|
|
|
this.location = location;
|
2020-05-09 16:27:49 +02:00
|
|
|
this.stack = stack;
|
2020-05-09 01:00:59 +02:00
|
|
|
}
|
2020-05-09 12:57:25 +02:00
|
|
|
|
2021-12-02 12:31:04 +01:00
|
|
|
public NetworkLock(CompoundTag nbt) {
|
2020-05-09 12:57:25 +02:00
|
|
|
this.deserializeNBT(nbt);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-02 12:31:04 +01:00
|
|
|
public CompoundTag serializeNBT() {
|
2021-12-02 17:46:56 +01:00
|
|
|
var nbt = new CompoundTag();
|
2021-12-02 16:55:04 +01:00
|
|
|
nbt.putUUID("id", this.lockId);
|
2020-05-09 12:57:25 +02:00
|
|
|
nbt.put("location", this.location.serializeNBT());
|
2021-12-02 16:55:04 +01:00
|
|
|
nbt.put("stack", this.stack.save(new CompoundTag()));
|
2020-05-09 12:57:25 +02:00
|
|
|
return nbt;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-02 12:31:04 +01:00
|
|
|
public void deserializeNBT(CompoundTag nbt) {
|
2021-12-02 16:55:04 +01:00
|
|
|
this.lockId = nbt.getUUID("id");
|
2020-05-09 12:57:25 +02:00
|
|
|
this.location = new NetworkLocation(nbt.getCompound("location"));
|
2021-12-02 16:55:04 +01:00
|
|
|
this.stack = ItemStack.of(nbt.getCompound("stack"));
|
2020-05-09 12:57:25 +02:00
|
|
|
}
|
2020-05-09 20:12:50 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object o) {
|
2021-12-02 16:55:04 +01:00
|
|
|
if (o instanceof NetworkLock that)
|
2020-05-09 20:12:50 +02:00
|
|
|
return this.lockId.equals(that.lockId);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return Objects.hash(this.lockId);
|
|
|
|
}
|
2022-02-11 21:56:02 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return "NetworkLock{" + "location=" + this.location.pipePos + ", stack=" + this.stack + '}';
|
|
|
|
}
|
2020-05-09 01:00:59 +02:00
|
|
|
}
|