-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: database & progress on merge command
- Loading branch information
1 parent
089d01a
commit 3f449be
Showing
12 changed files
with
384 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,3 +40,5 @@ build/ | |
### NixOS ### | ||
*.nix | ||
.envrc | ||
|
||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/com/slampvp/factory/command/plot/sub/InfoCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.slampvp.factory.command.plot.sub; | ||
|
||
import com.slampvp.factory.command.Command; | ||
import com.slampvp.factory.command.FactoryCommand; | ||
import com.slampvp.factory.common.Locale; | ||
import com.slampvp.factory.player.Rank; | ||
import com.slampvp.factory.plot.Plot; | ||
import com.slampvp.factory.plot.PlotFlag; | ||
import com.slampvp.factory.plot.PlotManager; | ||
import net.kyori.adventure.text.TextReplacementConfig; | ||
import net.minestom.server.command.builder.arguments.ArgumentType; | ||
import net.minestom.server.command.builder.arguments.minecraft.ArgumentEntity; | ||
import net.minestom.server.entity.Player; | ||
|
||
import java.util.Optional; | ||
|
||
@Command( | ||
description = "Get info of a plot.", | ||
usage = "/plot info", | ||
minimumRank = Rank.ADMIN, | ||
playerOnly = true | ||
) | ||
public class InfoCommand extends FactoryCommand { | ||
public InfoCommand() { | ||
super("info"); | ||
} | ||
|
||
@Override | ||
public void init() { | ||
|
||
addSyntax((sender, context) -> { | ||
Player player = (Player) sender; | ||
Optional<Plot> optionalPlot = PlotManager.getInstance().getPlot(player.getPosition()); | ||
|
||
if (optionalPlot.isEmpty()) { | ||
sender.sendMessage(Locale.Plot.NOT_IN_PLOT); | ||
return; | ||
} | ||
|
||
Plot plot = optionalPlot.get(); | ||
|
||
player.sendMessage("ID: " + plot.getId()); | ||
player.sendMessage("Owner: " + plot.getOwner()); | ||
player.sendMessage("Start: " + plot.getStart()); | ||
player.sendMessage("End: " + plot.getEnd()); | ||
player.sendMessage(""); | ||
}); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/slampvp/factory/command/plot/sub/MergeCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.slampvp.factory.command.plot.sub; | ||
|
||
import com.slampvp.factory.FactoryServer; | ||
import com.slampvp.factory.command.Command; | ||
import com.slampvp.factory.command.FactoryCommand; | ||
import com.slampvp.factory.common.Locale; | ||
import com.slampvp.factory.player.Rank; | ||
import com.slampvp.factory.plot.ClaimResult; | ||
import com.slampvp.factory.plot.MergeResult; | ||
import com.slampvp.factory.plot.PlotManager; | ||
import net.minestom.server.entity.Player; | ||
|
||
@Command(description = "Merge two plots together into one large plot.", usage = "/plot merge", minimumRank = Rank.DEFAULT, playerOnly = true) | ||
public class MergeCommand extends FactoryCommand { | ||
public MergeCommand() { | ||
super("merge"); | ||
} | ||
|
||
@Override | ||
public void init() { | ||
addSyntax((sender, context) -> { | ||
Player player = (Player) sender; | ||
MergeResult mergeResult = PlotManager.getInstance().mergePlot(player); | ||
|
||
switch (mergeResult) { | ||
case NOT_IN_PLOT -> sender.sendMessage(Locale.Plot.NOT_IN_PLOT); | ||
case NO_MERGE_CANDIDATE -> sender.sendMessage(Locale.Plot.NO_MERGE_CANDIDATE); | ||
case SUCCESS -> { | ||
PlotManager.getInstance().getPlot(player.getPosition()).ifPresent(plot -> player.teleport(plot.getSpawn())); | ||
sender.sendMessage(Locale.Plot.MERGED); | ||
} | ||
case FAILURE -> { | ||
FactoryServer.LOGGER.error("Plot merging failed."); | ||
} | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/main/java/com/slampvp/factory/database/DatabaseManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.slampvp.factory.database; | ||
|
||
import com.slampvp.factory.FactoryServer; | ||
import com.zaxxer.hikari.HikariConfig; | ||
import com.zaxxer.hikari.HikariDataSource; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.function.Consumer; | ||
|
||
public class DatabaseManager { | ||
private static DatabaseManager instance; | ||
private final HikariDataSource dataSource; | ||
|
||
private DatabaseManager() { | ||
HikariConfig config = new HikariConfig(); | ||
config.setDataSourceClassName("org.postgresql.ds.PGSimpleDataSource"); | ||
|
||
config.addDataSourceProperty("serverName", "localhost"); | ||
config.addDataSourceProperty("portNumber", "5432"); | ||
config.addDataSourceProperty("databaseName", "factory"); | ||
config.addDataSourceProperty("user", "postgres"); | ||
config.addDataSourceProperty("password", "password"); | ||
|
||
config.setMaximumPoolSize(5); | ||
config.setMinimumIdle(2); | ||
config.setConnectionTimeout(30000); | ||
|
||
dataSource = new HikariDataSource(config); | ||
} | ||
|
||
public static synchronized DatabaseManager getInstance() { | ||
if (instance == null) { | ||
instance = new DatabaseManager(); | ||
} | ||
return instance; | ||
} | ||
|
||
public void init() { | ||
FactoryServer.LOGGER.info("Creating database tables..."); | ||
executeUpdate(DatabaseTables.TABLES_QUERY); | ||
} | ||
|
||
public Connection getConnection() throws SQLException { | ||
return dataSource.getConnection(); | ||
} | ||
|
||
public void close() { | ||
if (dataSource != null && !dataSource.isClosed()) { | ||
dataSource.close(); | ||
} | ||
} | ||
|
||
public void executeUpdate(String query) { | ||
executeUpdate(query, statement -> { | ||
}); | ||
} | ||
|
||
public void executeUpdate(String query, Consumer<PreparedStatement> statementConsumer) { | ||
try (Connection connection = getConnection(); PreparedStatement pstmt = connection.prepareStatement(query)) { | ||
statementConsumer.accept(pstmt); | ||
pstmt.executeUpdate(); | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public void executeQuery(String query, Consumer<ResultSet> resultSetConsumer) { | ||
executeQuery(query, statement -> { | ||
}, resultSetConsumer); | ||
} | ||
|
||
public void executeQuery(String query, Consumer<PreparedStatement> statementConsumer, Consumer<ResultSet> resultSetConsumer) { | ||
try (Connection connection = getConnection(); PreparedStatement pstmt = connection.prepareStatement(query)) { | ||
statementConsumer.accept(pstmt); | ||
try (ResultSet rs = pstmt.executeQuery()) { | ||
resultSetConsumer.accept(rs); | ||
} | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
src/main/java/com/slampvp/factory/database/DatabaseTables.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package com.slampvp.factory.database; | ||
|
||
final class DatabaseTables { | ||
static final String TABLES_QUERY = """ | ||
DO | ||
$$ | ||
BEGIN | ||
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'plot_id') THEN | ||
CREATE TYPE PLOT_ID AS | ||
( | ||
x INT, | ||
z INT | ||
); | ||
END IF; | ||
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'vec') THEN | ||
CREATE TYPE VEC AS | ||
( | ||
x FLOAT, | ||
y FLOAT, | ||
z FLOAT | ||
); | ||
END IF; | ||
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'pos') THEN | ||
CREATE TYPE POS AS | ||
( | ||
x FLOAT, | ||
y FLOAT, | ||
z FLOAT, | ||
yaw FLOAT, | ||
pitch FLOAT | ||
); | ||
END IF; | ||
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'plot_target') THEN | ||
CREATE TYPE PLOT_TARGET AS ENUM ('TRUSTED', 'MEMBER', 'PUBLIC'); | ||
END IF; | ||
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'plot_member') THEN | ||
CREATE TYPE PLOT_MEMBER AS ENUM ('TRUSTED', 'MEMBER'); | ||
END IF; | ||
END | ||
$$; | ||
DO | ||
$$ | ||
BEGIN | ||
CREATE TABLE IF NOT EXISTS plots | ||
( | ||
id BIGINT GENERATED ALWAYS AS IDENTITY, | ||
plot_id PLOT_ID NOT NULL, | ||
owner VARCHAR(36) NOT NULL, | ||
start VEC NOT NULL, | ||
"end" VEC NOT NULL, | ||
spawn POS NOT NULL, | ||
PRIMARY KEY (id) | ||
); | ||
CREATE INDEX IF NOT EXISTS index_plots_on_owner ON plots (owner); | ||
CREATE TABLE IF NOT EXISTS plot_banned_players | ||
( | ||
plot_id BIGINT NOT NULL, | ||
uuid VARCHAR(36) NOT NULL, | ||
PRIMARY KEY (plot_id, uuid), | ||
CONSTRAINT fk_plot FOREIGN KEY (plot_id) REFERENCES plots (id) ON DELETE CASCADE | ||
); | ||
CREATE UNIQUE INDEX IF NOT EXISTS index_plot_banned_players_on_both ON plot_banned_players (plot_id, uuid); | ||
CREATE INDEX IF NOT EXISTS index_plot_banned_players_on_plot_id ON plot_banned_players (plot_id); | ||
CREATE TABLE IF NOT EXISTS plot_warps | ||
( | ||
plot_id BIGINT NOT NULL, | ||
name TEXT NOT NULL, | ||
pos POS NOT NULL, | ||
PRIMARY KEY (plot_id, name, pos), | ||
CONSTRAINT fk_plot FOREIGN KEY (plot_id) REFERENCES plots (id) ON DELETE CASCADE | ||
); | ||
CREATE UNIQUE INDEX IF NOT EXISTS index_plot_warps_on_all ON plot_warps (plot_id, name, pos); | ||
CREATE INDEX IF NOT EXISTS index_plot_warps_on_plot_id ON plot_warps (plot_id); | ||
CREATE TABLE IF NOT EXISTS plot_members | ||
( | ||
plot_id BIGINT NOT NULL, | ||
uuid VARCHAR(36) NOT NULL, | ||
type PLOT_MEMBER NOT NULL, | ||
PRIMARY KEY (plot_id, uuid, type), | ||
CONSTRAINT fk_plot FOREIGN KEY (plot_id) REFERENCES plots (id) ON DELETE CASCADE | ||
); | ||
CREATE UNIQUE INDEX IF NOT EXISTS index_plot_members_on_all ON plot_members (plot_id, uuid, type); | ||
CREATE INDEX IF NOT EXISTS index_plot_members_on_plot_id ON plot_members (plot_id); | ||
CREATE TABLE IF NOT EXISTS plot_flags | ||
( | ||
plot_id BIGINT NOT NULL, | ||
target PLOT_TARGET NOT NULL, | ||
flags INT NOT NULL, | ||
PRIMARY KEY (plot_id, target, flags), | ||
CONSTRAINT fk_plot FOREIGN KEY (plot_id) REFERENCES plots (id) ON DELETE CASCADE | ||
); | ||
CREATE UNIQUE INDEX IF NOT EXISTS index_plot_flags_on_all ON plot_flags (plot_id, target, flags); | ||
CREATE INDEX IF NOT EXISTS index_plot_flags_on_plot_id ON plot_flags (plot_id); | ||
END | ||
$$; | ||
"""; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.slampvp.factory.plot; | ||
|
||
public enum MergeResult { | ||
NOT_IN_PLOT, | ||
NO_MERGE_CANDIDATE, | ||
SUCCESS, | ||
FAILURE; | ||
} |
Oops, something went wrong.