Skip to content

Commit f21074f

Browse files
committed
PitPreferences修正
1 parent 1b82f9a commit f21074f

File tree

7 files changed

+146
-12
lines changed

7 files changed

+146
-12
lines changed

src/main/java/com/github/elic0de/thejpspit/spigot/TheJpsPit.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,6 @@ private void createNPCs() {
196196

197197
@Override
198198
public void onDisable() {
199-
pitPreferences.ifPresent(preferences -> database.updatePitPreferences(preferences));
200199
// Plugin shutdown logic
201200
if (database != null) {
202201
database.terminate();

src/main/java/com/github/elic0de/thejpspit/spigot/command/PitCommand.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.github.elic0de.thejpspit.spigot.item.PitItemEntry;
1212
import com.github.elic0de.thejpspit.spigot.player.PitPlayer;
1313
import com.github.elic0de.thejpspit.spigot.player.PitPlayerManager;
14+
import com.github.elic0de.thejpspit.spigot.util.LocationData;
1415
import com.github.elic0de.thejpspit.spigot.villager.VillagerNPCManager;
1516
import org.bukkit.entity.Player;
1617

@@ -41,7 +42,9 @@ public void onReset(Player player) {
4142
@Subcommand("set spawn")
4243
@CommandPermission("tjp.spawn")
4344
public void onSetSpawn(Player player) {
44-
pit.getPitPreferences().ifPresent(pitPreferences -> pitPreferences.setSpawn(player.getLocation()));
45+
pit.getPitPreferences().ifPresent(pitPreferences -> pitPreferences.setSpawn(LocationData.at(player.getLocation())));
46+
PitPlayerManager.getPitPlayer(player).sendMessage("&cスポーン地点を設定しました");
47+
pit.getPitPreferences().ifPresent(preferences -> pit.getDatabase().updatePitPreferences(preferences));
4548
}
4649

4750
@Subcommand("shop")

src/main/java/com/github/elic0de/thejpspit/spigot/command/SpawnCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ public void spawn(Player player) {
1919
pitPlayer.sendMessage("&cあなたは戦闘中です");
2020
return;
2121
}
22-
pit.getPitPreferences().ifPresent(pitPreferences -> player.teleport(pitPreferences.getSpawn().orElse(player.getLocation())));
22+
pit.getPitPreferences().ifPresent(pitPreferences -> pitPreferences.getSpawn().ifPresent(location -> player.teleport(location.getLocation())));
2323
}
2424
}
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.elic0de.thejpspit.spigot.config;
22

3+
import com.github.elic0de.thejpspit.spigot.util.LocationData;
34
import com.google.gson.annotations.Expose;
45
import com.google.gson.annotations.SerializedName;
56
import java.util.Optional;
@@ -10,21 +11,22 @@ public class PitPreferences {
1011

1112
@Expose
1213
@SerializedName("spawn")
13-
private Location spawn;
14+
private LocationData spawn;
1415

1516
public static PitPreferences getDefaults() {
16-
return new PitPreferences(Bukkit.getWorlds().stream().findAny().get().getSpawnLocation());
17+
final Location location = Bukkit.getWorlds().stream().findAny().get().getSpawnLocation();
18+
return new PitPreferences(LocationData.at(location.getX(), location.getY(), location.getZ(), location.getWorld()));
1719
}
1820

19-
private PitPreferences(Location spawn) {
21+
private PitPreferences(LocationData spawn) {
2022
this.spawn = spawn;
2123
}
2224

23-
public Optional<Location> getSpawn() {
25+
public Optional<LocationData> getSpawn() {
2426
return Optional.ofNullable(spawn);
2527
}
2628

27-
public void setSpawn(Location location) {
29+
public void setSpawn(LocationData location) {
2830
this.spawn = location;
2931
}
3032
}

src/main/java/com/github/elic0de/thejpspit/spigot/database/SqLiteDatabase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public Optional<PitPreferences> getPitPreferences() {
171171
try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables("""
172172
SELECT `preferences`
173173
FROM `%pit_preferences%`
174-
LIMIT 1"""))) {
174+
"""))) {
175175
final ResultSet resultSet = statement.executeQuery();
176176
if (resultSet.next()) {
177177
final String preferences = new String(resultSet.getBytes("preferences"), StandardCharsets.UTF_8);

src/main/java/com/github/elic0de/thejpspit/spigot/listener/EventListener.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.github.elic0de.thejpspit.spigot.villager.VillagerNPCManager;
1313
import org.bukkit.Bukkit;
1414
import org.bukkit.GameMode;
15+
import org.bukkit.Location;
1516
import org.bukkit.Material;
1617
import org.bukkit.entity.Entity;
1718
import org.bukkit.entity.Player;
@@ -48,11 +49,11 @@ public void onJoin(PlayerJoinEvent event) {
4849
Object packet = packetManager.buildScoreboardTeam(player);
4950
packetManager.sendPacket(packet, player);
5051

51-
/*plugin.getPitPreferences().ifPresent(pitPreferences -> {
52-
final Location location = pitPreferences.getSpawn().get();
52+
plugin.getPitPreferences().ifPresent(pitPreferences -> {
53+
final Location location = pitPreferences.getSpawn().get().getLocation();
5354

5455
if (location != null) player.teleport(location);
55-
});*/
56+
});
5657
if (userData.isEmpty()) {
5758
plugin.getDatabase().createPitPlayer(player);
5859
PitPlayerManager.registerUser(new PitPlayer(player));
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package com.github.elic0de.thejpspit.spigot.util;
2+
3+
import com.google.gson.annotations.Expose;
4+
import java.util.UUID;
5+
import org.bukkit.Bukkit;
6+
import org.bukkit.Location;
7+
import org.bukkit.World;
8+
import org.jetbrains.annotations.NotNull;
9+
10+
public class LocationData {
11+
12+
@Expose
13+
private double x;
14+
@Expose
15+
private double y;
16+
@Expose
17+
private double z;
18+
@Expose
19+
private UUID uniqueId;
20+
@Expose
21+
private float yaw;
22+
@Expose
23+
private float pitch;
24+
25+
protected LocationData(double x, double y, double z, UUID uniqueId, float yaw, float pitch) {
26+
this.x = x;
27+
this.y = y;
28+
this.z = z;
29+
this.uniqueId = uniqueId;
30+
this.yaw = yaw;
31+
this.pitch = pitch;
32+
}
33+
34+
private LocationData() {
35+
}
36+
@NotNull
37+
public static LocationData at(Location location) {
38+
return new LocationData(location.getX(), location.getY(), location.getZ(),
39+
location.getWorld().getUID(), location.getYaw(), location.getPitch());
40+
}
41+
42+
@NotNull
43+
public static LocationData at(double x, double y, double z, @NotNull World world, float yaw, float pitch) {
44+
return new LocationData(x, y, z, world.getUID(), yaw, pitch);
45+
}
46+
47+
@NotNull
48+
public static LocationData at(double x, double y, double z, @NotNull World world) {
49+
return new LocationData(x, y, z, world.getUID(), 0, 0);
50+
}
51+
52+
public double getX() {
53+
return x;
54+
}
55+
56+
public double getY() {
57+
return y;
58+
}
59+
60+
public double getZ() {
61+
return z;
62+
}
63+
64+
@NotNull
65+
public UUID getUniqueId() {
66+
return uniqueId;
67+
}
68+
69+
public float getYaw() {
70+
return yaw;
71+
}
72+
73+
public float getPitch() {
74+
return pitch;
75+
}
76+
77+
public Location getLocation() {
78+
final World world = Bukkit.getWorld(uniqueId);
79+
if (world != null) return new Location(world, x, y, z, yaw, pitch);
80+
return Bukkit.getWorlds().stream().findAny().get().getSpawnLocation();
81+
}
82+
83+
public void setX(double x) {
84+
this.x = x;
85+
}
86+
87+
public void setY(double y) {
88+
this.y = y;
89+
}
90+
91+
public void setZ(double z) {
92+
this.z = z;
93+
}
94+
95+
public void setWorld(World world) {
96+
this.uniqueId = world.getUID();
97+
}
98+
99+
public void setYaw(float yaw) {
100+
this.yaw = yaw;
101+
}
102+
103+
public void setPitch(float pitch) {
104+
this.pitch = pitch;
105+
}
106+
107+
108+
public double distanceBetween(@NotNull LocationData other) {
109+
return Math.sqrt(Math.pow(x - other.x, 2) + Math.pow(y - other.y, 2) + Math.pow(z - other.z, 2));
110+
}
111+
112+
@NotNull
113+
public LocationData interpolate(LocationData next, double scalar) {
114+
return LocationData.at(
115+
x + (next.x - x) * scalar,
116+
y + (next.y - y) * scalar,
117+
z + (next.z - z) * scalar,
118+
Bukkit.getWorld(uniqueId),
119+
(float) (yaw + (next.yaw - yaw) * scalar),
120+
(float) (pitch + (next.pitch - pitch) * scalar)
121+
);
122+
}
123+
124+
@NotNull
125+
public String toString() {
126+
return "(x: " + x + ", y: " + y + ", z: " + z + ", world: " + uniqueId +
127+
", yaw: " + yaw + ", pitch: " + pitch + ")";
128+
}
129+
}

0 commit comments

Comments
 (0)