Skip to content

Commit 39e44c9

Browse files
committed
Refactor RespawnLocation to extend UnloadedWorldLocation to reduce duplicate code
1 parent 13162b5 commit 39e44c9

File tree

1 file changed

+13
-98
lines changed

1 file changed

+13
-98
lines changed

src/main/java/org/mvplugins/multiverse/inventories/util/RespawnLocation.java

Lines changed: 13 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.mvplugins.multiverse.inventories.util;
22

3-
import org.bukkit.Bukkit;
43
import org.bukkit.Location;
54
import org.bukkit.Material;
65
import org.bukkit.Utility;
@@ -12,102 +11,51 @@
1211
import org.jetbrains.annotations.ApiStatus;
1312
import org.jetbrains.annotations.NotNull;
1413
import org.jetbrains.annotations.Nullable;
14+
import org.mvplugins.multiverse.core.world.location.UnloadedWorldLocation;
1515
import org.mvplugins.multiverse.external.vavr.control.Try;
1616

17-
import java.util.HashMap;
1817
import java.util.Locale;
1918
import java.util.Map;
20-
import java.util.Objects;
2119

2220
/**
2321
* Location information with respawn type. See also {@link RespawnLocationType}.
24-
* <br />
25-
* TODO: This should extend UnloadedWorldLocation, but that class is currently a final class!!!
2622
*/
2723
@SerializableAs("RespawnLocation")
2824
@ApiStatus.AvailableSince("5.2")
29-
public class RespawnLocation extends Location {
25+
public class RespawnLocation extends UnloadedWorldLocation {
3026

31-
private @Nullable String worldName;
32-
private @NotNull RespawnLocationType respawnType = RespawnLocationType.UNKNOWN;
27+
private @NotNull RespawnLocationType respawnType;
3328

3429
public RespawnLocation(@Nullable String worldName, double x, double y, double z, @NotNull RespawnLocationType respawnType) {
35-
super(null, x, y, z);
36-
setWorldName(worldName);
30+
super(worldName, x, y, z);
3731
this.respawnType = respawnType;
3832
}
3933

4034
public RespawnLocation(@Nullable String worldName, double x, double y, double z, float yaw, float pitch, @NotNull RespawnLocationType respawnType) {
41-
super(null, x, y, z, yaw, pitch);
42-
setWorldName(worldName);
35+
super(worldName, x, y, z, yaw, pitch);
4336
this.respawnType = respawnType;
4437
}
4538

4639
public RespawnLocation(@Nullable World world, double x, double y, double z, @NotNull RespawnLocationType respawnType) {
47-
super(null, x, y, z);
48-
setWorldName(world == null ? null : world.getName());
40+
super(world, x, y, z);
4941
this.respawnType = respawnType;
5042
}
5143

5244
public RespawnLocation(@Nullable World world, double x, double y, double z, float yaw, float pitch, @NotNull RespawnLocationType respawnType) {
53-
super(null, x, y, z, yaw, pitch);
54-
setWorldName(world == null ? null : world.getName());
45+
super(world, x, y, z, yaw, pitch);
5546
this.respawnType = respawnType;
5647
}
5748

5849
public RespawnLocation(@NotNull Location location, @NotNull RespawnLocationType respawnType) {
5950
this(location.getWorld(), location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch(), respawnType);
6051
}
6152

62-
/**
63-
* Makes a bukkit {@link Location} copy from this SpawnLocation.
64-
*
65-
* @return The bukkit location
66-
*/
67-
public Location toBukkitLocation() {
68-
return new Location(getWorld(), getX(), getY(), getZ(), getYaw(), getPitch());
69-
}
70-
71-
public void setWorldName(@Nullable String worldName) {
72-
this.worldName = worldName;
73-
}
74-
75-
public @Nullable String getWorldName() {
76-
return worldName;
77-
}
78-
79-
@Override
80-
public void setWorld(@Nullable World world) {
81-
this.worldName = (world == null) ? null : world.getName();
82-
}
83-
84-
@Override
85-
public @Nullable World getWorld() {
86-
if (worldName == null) {
87-
return null;
88-
}
89-
return Bukkit.getWorld(worldName);
90-
}
91-
9253
@Override
9354
@Utility
9455
@NotNull
9556
public Map<String, Object> serialize() {
96-
Map<String, Object> data = new HashMap<>();
97-
98-
if (this.worldName != null) {
99-
data.put("world", this.worldName);
100-
}
101-
102-
data.put("x", getX());
103-
data.put("y", getY());
104-
data.put("z", getZ());
105-
106-
data.put("yaw", getYaw());
107-
data.put("pitch", getPitch());
108-
57+
Map<String, Object> data = super.serialize();
10958
data.put("respawnType", this.respawnType.name());
110-
11159
return data;
11260
}
11361

@@ -138,58 +86,25 @@ public static Location deserialize(@NotNull Map<String, Object> args) {
13886

13987
@Override
14088
public boolean equals(Object obj) {
141-
if (obj == null) {
142-
return false;
143-
}
14489
if (!(obj instanceof Location other)) {
14590
return false;
14691
}
147-
String otherWorldName = Try.of(() -> other instanceof RespawnLocation RespawnLocation
148-
? RespawnLocation.worldName
149-
: other.getWorld().getName())
150-
.getOrNull();
151-
if (!Objects.equals(this.worldName, otherWorldName)) {
152-
return false;
153-
}
154-
if (Double.doubleToLongBits(this.getX()) != Double.doubleToLongBits(other.getX())) {
155-
return false;
156-
}
157-
if (Double.doubleToLongBits(this.getY()) != Double.doubleToLongBits(other.getY())) {
158-
return false;
159-
}
160-
if (Double.doubleToLongBits(this.getZ()) != Double.doubleToLongBits(other.getZ())) {
161-
return false;
162-
}
163-
if (Float.floatToIntBits(this.getPitch()) != Float.floatToIntBits(other.getPitch())) {
164-
return false;
165-
}
166-
if (Float.floatToIntBits(this.getYaw()) != Float.floatToIntBits(other.getYaw())) {
167-
return false;
168-
}
169-
if (other instanceof RespawnLocation otherRespawnLocation
170-
&& this.respawnType != otherRespawnLocation.respawnType) {
92+
if (!super.equals(obj)) {
17193
return false;
17294
}
173-
return true;
95+
return !(other instanceof RespawnLocation otherRespawnLocation)
96+
|| this.respawnType == otherRespawnLocation.respawnType;
17497
}
17598

17699
@Override
177100
public int hashCode() {
178-
int hash = 3;
179-
hash = 19 * hash + String.valueOf(worldName).hashCode();
180-
hash = 19 * hash + Long.hashCode(Double.doubleToLongBits(this.getX()));
181-
hash = 19 * hash + Long.hashCode(Double.doubleToLongBits(this.getY()));
182-
hash = 19 * hash + Long.hashCode(Double.doubleToLongBits(this.getZ()));
183-
hash = 19 * hash + Float.floatToIntBits(this.getPitch());
184-
hash = 19 * hash + Float.floatToIntBits(this.getYaw());
185-
hash = 19 * hash + this.respawnType.hashCode();
186-
return hash;
101+
return 19 * super.hashCode() + this.respawnType.hashCode();
187102
}
188103

189104
@Override
190105
public String toString() {
191106
return "RespawnLocation{" +
192-
"world=" + worldName +
107+
"world=" + getWorldName() +
193108
",x=" + getX() +
194109
",y=" + getY() +
195110
",z=" + getZ() +

0 commit comments

Comments
 (0)