Skip to content

Commit 76464b0

Browse files
committed
spawnシステム実装
1 parent 1d85597 commit 76464b0

File tree

9 files changed

+147
-8
lines changed

9 files changed

+147
-8
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,5 +116,10 @@
116116
<version>1.7</version>
117117
<scope>provided</scope>
118118
</dependency>
119+
<dependency>
120+
<groupId>com.fatboyindustrial.gson-javatime-serialisers</groupId>
121+
<artifactId>gson-javatime-serialisers</artifactId>
122+
<version>1.1.2</version>
123+
</dependency>
119124
</dependencies>
120125
</project>

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.github.elic0de.thejpspit;
22

33
import co.aikar.commands.PaperCommandManager;
4+
import com.fatboyindustrial.gsonjavatime.Converters;
45
import com.github.elic0de.thejpspit.command.PitCommand;
6+
import com.github.elic0de.thejpspit.command.SpawnCommand;
7+
import com.github.elic0de.thejpspit.config.PitPreferences;
58
import com.github.elic0de.thejpspit.database.Database;
69
import com.github.elic0de.thejpspit.database.SqLiteDatabase;
710
import com.github.elic0de.thejpspit.game.Game;
@@ -13,10 +16,13 @@
1316
import com.github.elic0de.thejpspit.network.PluginMessageReceiver;
1417
import com.github.elic0de.thejpspit.player.PitPlayer;
1518
import com.github.elic0de.thejpspit.player.PitPlayerManager;
19+
import com.github.elic0de.thejpspit.player.Preferences;
1620
import com.github.elic0de.thejpspit.queue.QueueManager;
1721
import com.github.elic0de.thejpspit.task.QueueTask;
1822
import com.github.elic0de.thejpspit.util.KillAssistHelper;
1923
import com.github.elic0de.thejpspit.util.KillRatingHelper;
24+
import com.google.gson.Gson;
25+
import com.google.gson.GsonBuilder;
2026
import java.util.ArrayList;
2127
import java.util.List;
2228
import java.util.Optional;
@@ -46,6 +52,7 @@ public final class TheJpsPit extends JavaPlugin {
4652

4753
private Scoreboard scoreboard;
4854
private Team team;
55+
private Optional<PitPreferences> pitPreferences;
4956

5057
public static TheJpsPit getInstance() {
5158
return instance;
@@ -80,6 +87,7 @@ public void onEnable() {
8087
queueManager = new QueueManager();
8188

8289
optionScoreboard();
90+
setPreferences();
8391

8492
//queueTask = new QueueTask();
8593

@@ -133,12 +141,23 @@ private void optionScoreboard() {
133141
team.setOption(Option.COLLISION_RULE, OptionStatus.NEVER);
134142
}
135143

144+
private void setPreferences() {
145+
final Optional<PitPreferences> preferences = database.getPitPreferences();
146+
if (preferences.isEmpty()) {
147+
database.createPitPreferences(PitPreferences.getDefaults());
148+
this.pitPreferences = Optional.of(PitPreferences.getDefaults());
149+
return;
150+
}
151+
this.pitPreferences = preferences;
152+
}
153+
136154
private void registerCommands() {
137155
PaperCommandManager commandManager = new PaperCommandManager(this);
138156

139157
commandManager.enableUnstableAPI("brigadier");
140158

141159
commandManager.registerCommand(new PitCommand());
160+
commandManager.registerCommand(new SpawnCommand());
142161
}
143162

144163
private void registerListener() {
@@ -218,4 +237,12 @@ public KillAssistHelper getAssistKillHelper() {
218237
public QueueManager getQueueManager() {
219238
return queueManager;
220239
}
240+
241+
public Optional<PitPreferences> getPitPreferences() {
242+
return pitPreferences;
243+
}
244+
245+
public Gson getGson() {
246+
return Converters.registerOffsetDateTime(new GsonBuilder().excludeFieldsWithoutExposeAnnotation()).create();
247+
}
221248
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,10 @@ public void onCommand(Player player, @Optional OnlinePlayer onlinePlayer) {
3434
public void onReset(Player player) {
3535
pit.getDatabase().deletePlayerData();
3636
}
37+
38+
@Subcommand("set spawn")
39+
@CommandPermission("tjp.spawn")
40+
public void onSetSpawn(Player player) {
41+
pit.getPitPreferences().ifPresent(pitPreferences -> pitPreferences.setSpawn(player.getLocation()));
42+
}
3743
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.github.elic0de.thejpspit.command;
2+
3+
import co.aikar.commands.BaseCommand;
4+
import co.aikar.commands.annotation.CommandAlias;
5+
import com.github.elic0de.thejpspit.TheJpsPit;
6+
import com.github.elic0de.thejpspit.listener.CombatTagger;
7+
import com.github.elic0de.thejpspit.player.PitPlayer;
8+
import com.github.elic0de.thejpspit.player.PitPlayerManager;
9+
import org.bukkit.entity.Player;
10+
11+
public class SpawnCommand extends BaseCommand {
12+
13+
private final TheJpsPit pit = TheJpsPit.getInstance();
14+
15+
@CommandAlias("spawn")
16+
public void spawn(Player player) {
17+
final PitPlayer pitPlayer = PitPlayerManager.getPitPlayer(player);
18+
if (CombatTagger.isTagged(player.getUniqueId())) {
19+
pitPlayer.sendMessage("&cあなたは戦闘中です");
20+
return;
21+
}
22+
pit.getPitPreferences().ifPresent(pitPreferences -> player.teleport(pitPreferences.getSpawn().orElse(player.getLocation())));
23+
}
24+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.github.elic0de.thejpspit.config;
2+
3+
import com.google.gson.annotations.Expose;
4+
import com.google.gson.annotations.SerializedName;
5+
import java.util.Optional;
6+
import org.bukkit.Bukkit;
7+
import org.bukkit.Location;
8+
9+
public class PitPreferences {
10+
11+
@Expose
12+
@SerializedName("spawn")
13+
private Location spawn;
14+
15+
public static PitPreferences getDefaults() {
16+
return new PitPreferences(Bukkit.getWorlds().stream().findAny().get().getSpawnLocation());
17+
}
18+
19+
private PitPreferences(Location spawn) {
20+
this.spawn = spawn;
21+
}
22+
23+
public Optional<Location> getSpawn() {
24+
return Optional.ofNullable(spawn);
25+
}
26+
27+
public void setSpawn(Location location) {
28+
this.spawn = location;
29+
}
30+
}

src/main/java/com/github/elic0de/thejpspit/database/Database.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.elic0de.thejpspit.database;
22

33
import com.github.elic0de.thejpspit.TheJpsPit;
4+
import com.github.elic0de.thejpspit.config.PitPreferences;
45
import com.github.elic0de.thejpspit.player.OfflinePitPlayer;
56
import com.github.elic0de.thejpspit.player.PitPlayer;
67
import java.io.IOException;
@@ -17,6 +18,7 @@
1718
public abstract class Database {
1819

1920
protected final String playerTableName;
21+
protected final String preferencesTableName;
2022

2123
protected final TheJpsPit plugin;
2224

@@ -25,6 +27,7 @@ public abstract class Database {
2527
protected Database(TheJpsPit implementor) {
2628
this.plugin = implementor;
2729
this.playerTableName = "player";
30+
this.preferencesTableName = "preferences";
2831
this.logger = implementor.getLogger();
2932
}
3033

@@ -43,6 +46,7 @@ protected final String[] getSchemaStatements() throws IOException {
4346

4447
protected final String formatStatementTables(String sql) {
4548
return sql
49+
.replaceAll("%pit_preferences%", preferencesTableName)
4650
.replaceAll("%players_table%", playerTableName);
4751
}
4852

@@ -53,10 +57,14 @@ public abstract CompletableFuture<Void> runScript(InputStream inputStream,
5357

5458
public abstract void createPitPlayer(Player Player);
5559

60+
public abstract void createPitPreferences(PitPreferences preferences);
61+
5662
public abstract Optional<PitPlayer> getPitPlayer(Player player);
5763

5864
public abstract Optional<PitPlayer> getPitPlayer(UUID uuid);
5965

66+
public abstract Optional<PitPreferences> getPitPreferences();
67+
6068
public abstract Optional<OfflinePitPlayer> getOfflinePitPlayer(UUID uuid);
6169

6270
public abstract CompletableFuture<Optional<Integer>> getPlayerRanking(PitPlayer player,

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

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.elic0de.thejpspit.database;
22

33
import com.github.elic0de.thejpspit.TheJpsPit;
4+
import com.github.elic0de.thejpspit.config.PitPreferences;
45
import com.github.elic0de.thejpspit.player.OfflinePitPlayer;
56
import com.github.elic0de.thejpspit.player.PitPlayer;
67
import java.io.File;
@@ -13,14 +14,11 @@
1314
import java.sql.ResultSet;
1415
import java.sql.SQLException;
1516
import java.sql.Statement;
16-
import java.util.Locale;
1717
import java.util.Map;
1818
import java.util.Optional;
1919
import java.util.UUID;
2020
import java.util.concurrent.CompletableFuture;
2121
import java.util.logging.Level;
22-
import java.util.stream.Collectors;
23-
import java.util.stream.Stream;
2422
import org.bukkit.Bukkit;
2523
import org.bukkit.entity.Player;
2624
import org.sqlite.SQLiteConfig;
@@ -168,6 +166,23 @@ public Optional<PitPlayer> getPitPlayer(UUID uuid) {
168166
return Optional.empty();
169167
}
170168

169+
@Override
170+
public Optional<PitPreferences> getPitPreferences() {
171+
try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables("""
172+
SELECT `preferences`
173+
FROM `%pit_preferences%`
174+
LIMIT 1"""))) {
175+
final ResultSet resultSet = statement.executeQuery();
176+
if (resultSet.next()) {
177+
final String preferences = new String(resultSet.getBytes("preferences"), StandardCharsets.UTF_8);
178+
return Optional.of(plugin.getGson().fromJson(preferences, PitPreferences.class));
179+
}
180+
} catch (SQLException e) {
181+
getLogger().log(Level.SEVERE, "Failed to fetch user data from table by UUID", e);
182+
}
183+
return Optional.empty();
184+
}
185+
171186
@Override
172187
public Optional<OfflinePitPlayer> getOfflinePitPlayer(UUID uuid) {
173188
try (PreparedStatement statement = getConnection().prepareStatement(
@@ -202,11 +217,11 @@ public CompletableFuture<Optional<Integer>> getPlayerRanking(PitPlayer player, R
202217
return CompletableFuture.supplyAsync(() -> {
203218
try {
204219
String test = """
205-
SELECT uuid, rank
206-
FROM(SELECT `uuid`,
207-
RANK()
208-
OVER(ORDER BY %type% DESC)
209-
AS rank FROM `%players_table%`)
220+
SELECT uuid, rank
221+
FROM(SELECT `uuid`,
222+
RANK()
223+
OVER(ORDER BY %type% DESC)
224+
AS rank FROM `%players_table%`)
210225
WHERE `uuid`=?;
211226
""";
212227
try (PreparedStatement statement = getConnection().prepareStatement(
@@ -245,6 +260,23 @@ public void createPitPlayer(Player player) {
245260
}
246261
}
247262

263+
@Override
264+
public void createPitPreferences(PitPreferences pitPreferences) {
265+
try {
266+
try (PreparedStatement statement = getConnection().prepareStatement(
267+
formatStatementTables("""
268+
INSERT INTO `%pit_preferences%` (`username`)
269+
VALUES (?);"""))) {
270+
271+
statement.setBytes(1, plugin.getGson().toJson(pitPreferences).getBytes(StandardCharsets.UTF_8));
272+
statement.executeUpdate();
273+
}
274+
} catch (SQLException e) {
275+
getLogger().log(Level.SEVERE,
276+
"Failed to insert a player into the database", e);
277+
}
278+
}
279+
248280
@Override
249281
public void updateUserData(PitPlayer player) {
250282
try {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public void onJoin(PlayerJoinEvent event) {
6868
if (updateNeeded) {
6969
plugin.getDatabase().updateUserData(pitPlayer);
7070
}
71+
plugin.getPitPreferences().ifPresent(pitPreferences -> player.teleport(pitPreferences.getSpawn().orElse(player.getLocation())));
7172
}
7273

7374
@EventHandler

src/main/resources/database/sqlite_schema.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,10 @@ CREATE TABLE IF NOT EXISTS `%players_table%`
1111
`xp` integer NOT NULL DEFAULT 0,
1212

1313
PRIMARY KEY (`uuid`)
14+
);
15+
16+
CREATE TABLE IF NOT EXISTS `%pit_preferences%`
17+
(
18+
`id` integer NOT NULL PRIMARY KEY AUTOINCREMENT,
19+
`preferences` longblob NOT NULL
1420
);

0 commit comments

Comments
 (0)