Implement equals and hashCode for BlueMapMap and BlueMapWorld

This commit is contained in:
Lukas Rieger (Blue) 2024-03-24 00:07:10 +01:00
parent 6e8247ae3a
commit 757979b7b4
No known key found for this signature in database
GPG Key ID: AA33883B1BBA03E6
2 changed files with 34 additions and 1 deletions

View File

@ -45,17 +45,20 @@ public class BlueMapMapImpl implements BlueMapMap {
private final WeakReference<Plugin> plugin;
private final WeakReference<BmMap> map;
private final BlueMapWorldImpl world;
private final String mapId;
public BlueMapMapImpl(Plugin plugin, BmMap map) throws IOException {
this.plugin = new WeakReference<>(plugin);
this.map = new WeakReference<>(map);
this.world = new BlueMapWorldImpl(plugin, map.getWorld());
this.mapId = map.getId();
}
public BlueMapMapImpl(Plugin plugin, BmMap map, BlueMapWorldImpl world) {
this.plugin = new WeakReference<>(plugin);
this.map = new WeakReference<>(map);
this.world = world;
this.mapId = map.getId();
}
public BmMap getBmMap() {
@ -64,7 +67,7 @@ public class BlueMapMapImpl implements BlueMapMap {
@Override
public String getId() {
return unpack(map).getId();
return mapId;
}
@Override
@ -147,6 +150,21 @@ public class BlueMapMapImpl implements BlueMapMap {
return !unpack(plugin).getPluginState().getMapState(unpack(map)).isUpdateEnabled();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BlueMapMapImpl that = (BlueMapMapImpl) o;
return mapId.equals(that.mapId);
}
@Override
public int hashCode() {
return mapId.hashCode();
}
private <T> T unpack(WeakReference<T> ref) {
return Objects.requireNonNull(ref.get(), "Reference lost to delegate object. Most likely BlueMap got reloaded and this instance is no longer valid.");
}

View File

@ -78,6 +78,21 @@ public class BlueMapWorldImpl implements BlueMapWorld {
.collect(Collectors.toUnmodifiableSet());
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BlueMapWorldImpl that = (BlueMapWorldImpl) o;
return id.equals(that.id);
}
@Override
public int hashCode() {
return id.hashCode();
}
private <T> T unpack(WeakReference<T> ref) {
return Objects.requireNonNull(ref.get(), "Reference lost to delegate object. Most likely BlueMap got reloaded and this instance is no longer valid.");
}