Skip to content

Commit

Permalink
Minigame start progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Made By IToncek committed Apr 2, 2024
1 parent 7edcb72 commit 1688eac
Show file tree
Hide file tree
Showing 10 changed files with 321 additions and 11 deletions.
26 changes: 26 additions & 0 deletions MP-TEST/src/main/java/space/itoncek/mptest/ImportWorld.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package space.itoncek.mptest;

import org.bukkit.World;
import org.bukkit.WorldCreator;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;

import static space.itoncek.mptest.MP_TEST.pl;

public class ImportWorld implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
World world = WorldCreator.name(args[0]).environment(World.Environment.NORMAL).createWorld();
new BukkitRunnable() {
@Override
public void run() {
((Player) sender).teleportAsync(world.getHighestBlockAt(0,0).getLocation().add(0,1,0));
}
}.runTaskLater(pl,20L);
return true;
}
}
4 changes: 3 additions & 1 deletion MP-TEST/src/main/java/space/itoncek/mptest/MP_TEST.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@

public final class MP_TEST extends JavaPlugin {

public static MP_TEST pl;
@Override
public void onEnable() {
// Plugin startup logic

pl=this;
getCommand("tptest").setExecutor(new TPOtherCommand());
getCommand("importw").setExecutor(new ImportWorld());

System.out.println("MultiLib.isMultiPaper() = " + MultiLib.isMultiPaper());
System.out.println("MultiLib.getLocalServerName() = " + MultiLib.getLocalServerName());
Expand Down
1 change: 1 addition & 0 deletions MP-TEST/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ main: space.itoncek.mptest.MP_TEST
api-version: '1.20'
commands:
tptest:
importw:
11 changes: 11 additions & 0 deletions MSG/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@
<id>clojars</id>
<url>https://repo.clojars.org</url>
</repository>
<repository>
<id>enginehub-maven</id>
<url>https://maven.enginehub.org/repo/</url>
</repository>
</repositories>

<dependencies>
Expand All @@ -96,5 +100,12 @@
<version>1.1.12</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>com.sk89q.worldedit</groupId>
<artifactId>worldedit-bukkit</artifactId>
<version>7.2.9</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
48 changes: 46 additions & 2 deletions MSG/src/main/java/space/itoncek/msg/MultiSurvivalGames.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,72 @@
package space.itoncek.msg;

import com.github.puregero.multilib.MultiLib;
import org.bukkit.World;
import org.bukkit.WorldCreator;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import space.itoncek.msg.database.DBMGR;
import space.itoncek.msg.exceptions.ConfigEntryNotFoundException;
import space.itoncek.msg.protocol.GCRequests;

import java.sql.SQLException;

public final class MultiSurvivalGames extends JavaPlugin {
public static FileConfiguration cfg;
public static DBMGR dbmgr;
@Override
public static boolean isMaster;
public static MultiSurvivalGames pl;

@Override
public void onEnable() {
// Plugin startup logic
saveDefaultConfig();

saveDefaultConfig();
cfg = getConfig();
pl = this;
try {
dbmgr = new DBMGR(cfg.getString("db.host"),
cfg.getInt("db.port"),
cfg.getString("db.database"),
cfg.getString("db.user"),
cfg.getString("db.password"));
isMaster = dbmgr.getConfigEntry("master").equals(MultiLib.getLocalServerName());
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (ConfigEntryNotFoundException e) {
try {
dbmgr.setConfigEntry("master",MultiLib.getLocalServerName());
} catch (SQLException ex) {
throw new RuntimeException(ex);
} finally {
isMaster = true;
}
}

registerMultipaperChannels();
}

public void registerMultipaperChannels() {
MultiLib.onString(this,"msg:gc", (msg) -> {
switch (GCRequests.valueOf(msg)) {
case MASTER_SYNC -> {
try {
isMaster = dbmgr.getConfigEntry("master").equals(MultiLib.getLocalServerName());
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (ConfigEntryNotFoundException e) {
try {
dbmgr.setConfigEntry("master",MultiLib.getLocalServerName());
} catch (SQLException ex) {
throw new RuntimeException(ex);
} finally {
isMaster = true;
}
}

}
}
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,69 @@
package space.itoncek.msg.commands;

import com.github.puregero.multilib.MultiLib;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.*;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import space.itoncek.msg.generic.Team;
import space.itoncek.msg.protocol.GCRequests;

import java.sql.SQLException;
import java.util.List;

import static space.itoncek.msg.MultiSurvivalGames.dbmgr;
import static space.itoncek.msg.MultiSurvivalGames.isMaster;

public class StartRoundCommand implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (sender.hasPermission("msg.command.startround") || sender.isOp()) {
if ((sender.hasPermission("msg.command.startround") || sender.isOp()) && sender instanceof Player p) {
// CLAIM BEING THE MASTER
if (!isMaster) {
try {
dbmgr.setConfigEntry("master", MultiLib.getLocalServerName());
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
MultiLib.notify("msg:gc", GCRequests.MASTER_SYNC.name());
}
}

try {
List<Location> spawns = dbmgr.getSpawns(p.getWorld());
List<Team> teams = dbmgr.getAllTeams();
if(spawns.size() >= teams.size()) {
for (int i = 0; i < teams.size(); i++) {
Team team = teams.get(i);
Location spawn = spawns.get(i);
for (int j = 0; j < team.players.size(); j++) {
Location location = spawn.clone().add(0,j*3,0);
createBoxForSpawn(location);
team.players.get(j).teleportAsync(location);
}
}
} else {
sender.sendMessage(Component.text("Nedostatek míst pro týmy", TextColor.color(255,0,0)));
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
return true;
}

private void createBoxForSpawn(Location loc) {
//FLOOR
loc.clone().subtract(0,1,0).getBlock().setType(Material.GLASS);
//SIDES
loc.clone().add(1,1,0).getBlock().setType(Material.GLASS);
loc.clone().add(-1,1,0).getBlock().setType(Material.GLASS);
loc.clone().add(0,1,-1).getBlock().setType(Material.GLASS);
loc.clone().add(0,1,1).getBlock().setType(Material.GLASS);
}
}
51 changes: 45 additions & 6 deletions MSG/src/main/java/space/itoncek/msg/database/DBMGR.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package space.itoncek.msg.database;

import org.jetbrains.annotations.*;
import org.bukkit.Location;
import org.bukkit.World;
import space.itoncek.msg.exceptions.ConfigEntryNotFoundException;
import space.itoncek.msg.exceptions.PlayerNotFoundException;
import space.itoncek.msg.exceptions.ServerNotFoundException;
import space.itoncek.msg.generic.Team;
Expand All @@ -22,7 +24,7 @@ public void migrate() {

}
public List<Team> getLocalTeams(String serverId) throws SQLException, ServerNotFoundException {
ResultSet rs = conn.createStatement().executeQuery("FROM teams SELECT * WHERE server = %d".formatted(getServerId(serverId)));
ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM teams WHERE server = %d".formatted(getServerId(serverId)));
List<Team> out = new ArrayList<>();
while (rs.next()) {
out.add(parseTeam(rs.getInt("id")));
Expand All @@ -32,7 +34,7 @@ public List<Team> getLocalTeams(String serverId) throws SQLException, ServerNotF
}

private int getServerId(String serverId) throws SQLException, ServerNotFoundException {
ResultSet rs = conn.createStatement().executeQuery("FROM servers SELECT * WHERE name = '%s'".formatted(serverId));
ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM servers WHERE name = '%s'".formatted(serverId));
if(rs.next()) {
return rs.getInt("id");
} else {
Expand All @@ -41,7 +43,7 @@ private int getServerId(String serverId) throws SQLException, ServerNotFoundExce
}

private Team parseTeam(int teamId) throws SQLException {
ResultSet rs = conn.createStatement().executeQuery("FROM players SELECT * WHERE team = '%s'".formatted(teamId));
ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM players WHERE team = '%s'".formatted(teamId));
List<UUID> uuids = new ArrayList<>();
while (rs.next()) {
uuids.add(UUID.fromString(rs.getString("uuid")));
Expand All @@ -50,7 +52,7 @@ private Team parseTeam(int teamId) throws SQLException {
}

public int getPlayerPoints(UUID uuid) throws SQLException, PlayerNotFoundException {
ResultSet rs = conn.createStatement().executeQuery("FROM players SELECT * WHERE uuid = '%s'".formatted(uuid.toString()));
ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM players WHERE uuid = '%s'".formatted(uuid.toString()));
if(rs.next()) {
return rs.getInt("points");
} else {
Expand All @@ -59,11 +61,48 @@ public int getPlayerPoints(UUID uuid) throws SQLException, PlayerNotFoundExcepti
}

public void addPlayerPoints(UUID uuid, int points) throws PlayerNotFoundException, SQLException {
ResultSet rs = conn.createStatement().executeQuery("FROM players SELECT * WHERE uuid = '%s'".formatted(uuid.toString()));
ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM players WHERE uuid = '%s'".formatted(uuid.toString()));
if(rs.next()) {
conn.createStatement().executeUpdate("UPDATE players SET points = %d WHERE uuid = '%s';".formatted(rs.getInt("points") + points,uuid.toString()));
} else {
throw new PlayerNotFoundException("Unable to find player with uuid " + uuid);
}
}

public String getConfigEntry(String key) throws SQLException, ConfigEntryNotFoundException {
ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM config WHERE `key` = '%s';".formatted(key));
if(rs.next()) {
return rs.getString("value");
} else {
throw new ConfigEntryNotFoundException(key);
}
}

public void setConfigEntry(String key, String value) throws SQLException {
conn.createStatement().executeUpdate("INSERT INTO config (`key`, `value`) VALUES('%s', '%s') ON DUPLICATE KEY UPDATE `key`='%s', `value`='%s';".formatted(key,value,key,value));
}

public List<Team> getAllTeams() throws SQLException {
ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM teams");
List<Team> out = new ArrayList<>();
while (rs.next()) {
out.add(parseTeam(rs.getInt("id")));
}
rs.close();
return out;
}

public List<Location> getSpawns(World world) throws SQLException {
ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM spawns WHERE `world` = '%s';".formatted(world.getName()));
List<Location> result = new ArrayList<>();
while (rs.next()) {
result.add(new Location(world,
rs.getDouble("x"),
rs.getDouble("y"),
rs.getDouble("z"),
rs.getFloat("yaw"),
rs.getFloat("pitch")));
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package space.itoncek.msg.exceptions;

public class ConfigEntryNotFoundException extends Throwable {
public ConfigEntryNotFoundException(String key) {
super("Unable to find config entry " + key);
}
}
5 changes: 5 additions & 0 deletions MSG/src/main/java/space/itoncek/msg/protocol/GCRequests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package space.itoncek.msg.protocol;

public enum GCRequests {
MASTER_SYNC,
}
Loading

0 comments on commit 1688eac

Please sign in to comment.