|
1 | 1 | package org.mvplugins.multiverse.inventories.util; |
2 | 2 |
|
3 | | -import org.bukkit.Bukkit; |
4 | 3 | import org.bukkit.Location; |
5 | 4 | import org.bukkit.Material; |
6 | 5 | import org.bukkit.Utility; |
|
12 | 11 | import org.jetbrains.annotations.ApiStatus; |
13 | 12 | import org.jetbrains.annotations.NotNull; |
14 | 13 | import org.jetbrains.annotations.Nullable; |
| 14 | +import org.mvplugins.multiverse.core.world.location.UnloadedWorldLocation; |
15 | 15 | import org.mvplugins.multiverse.external.vavr.control.Try; |
16 | 16 |
|
17 | | -import java.util.HashMap; |
18 | 17 | import java.util.Locale; |
19 | 18 | import java.util.Map; |
20 | | -import java.util.Objects; |
21 | 19 |
|
22 | 20 | /** |
23 | 21 | * 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!!! |
26 | 22 | */ |
27 | 23 | @SerializableAs("RespawnLocation") |
28 | 24 | @ApiStatus.AvailableSince("5.2") |
29 | | -public class RespawnLocation extends Location { |
| 25 | +public class RespawnLocation extends UnloadedWorldLocation { |
30 | 26 |
|
31 | | - private @Nullable String worldName; |
32 | | - private @NotNull RespawnLocationType respawnType = RespawnLocationType.UNKNOWN; |
| 27 | + private @NotNull RespawnLocationType respawnType; |
33 | 28 |
|
34 | 29 | 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); |
37 | 31 | this.respawnType = respawnType; |
38 | 32 | } |
39 | 33 |
|
40 | 34 | 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); |
43 | 36 | this.respawnType = respawnType; |
44 | 37 | } |
45 | 38 |
|
46 | 39 | 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); |
49 | 41 | this.respawnType = respawnType; |
50 | 42 | } |
51 | 43 |
|
52 | 44 | 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); |
55 | 46 | this.respawnType = respawnType; |
56 | 47 | } |
57 | 48 |
|
58 | 49 | public RespawnLocation(@NotNull Location location, @NotNull RespawnLocationType respawnType) { |
59 | 50 | this(location.getWorld(), location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch(), respawnType); |
60 | 51 | } |
61 | 52 |
|
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 | | - |
92 | 53 | @Override |
93 | 54 | @Utility |
94 | 55 | @NotNull |
95 | 56 | 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(); |
109 | 58 | data.put("respawnType", this.respawnType.name()); |
110 | | - |
111 | 59 | return data; |
112 | 60 | } |
113 | 61 |
|
@@ -138,58 +86,25 @@ public static Location deserialize(@NotNull Map<String, Object> args) { |
138 | 86 |
|
139 | 87 | @Override |
140 | 88 | public boolean equals(Object obj) { |
141 | | - if (obj == null) { |
142 | | - return false; |
143 | | - } |
144 | 89 | if (!(obj instanceof Location other)) { |
145 | 90 | return false; |
146 | 91 | } |
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)) { |
171 | 93 | return false; |
172 | 94 | } |
173 | | - return true; |
| 95 | + return !(other instanceof RespawnLocation otherRespawnLocation) |
| 96 | + || this.respawnType == otherRespawnLocation.respawnType; |
174 | 97 | } |
175 | 98 |
|
176 | 99 | @Override |
177 | 100 | 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(); |
187 | 102 | } |
188 | 103 |
|
189 | 104 | @Override |
190 | 105 | public String toString() { |
191 | 106 | return "RespawnLocation{" + |
192 | | - "world=" + worldName + |
| 107 | + "world=" + getWorldName() + |
193 | 108 | ",x=" + getX() + |
194 | 109 | ",y=" + getY() + |
195 | 110 | ",z=" + getZ() + |
|
0 commit comments