Skip to content

Commit

Permalink
tier zero
Browse files Browse the repository at this point in the history
  • Loading branch information
UhMarco committed Jun 17, 2023
1 parent 5f5d78f commit 144fbe7
Show file tree
Hide file tree
Showing 32 changed files with 1,166 additions and 307 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ repositories {

dependencies {
compileOnly "org.spigotmc:spigot-api:1.19.4-R0.1-SNAPSHOT"
compileOnly files("/Users/marco/Documents/build/Spigot/Spigot-Server/target/spigot-1.19.4-R0.1-SNAPSHOT.jar")
compileOnly "com.mojang:authlib:1.5.21"
}

Expand Down
148 changes: 127 additions & 21 deletions src/main/java/com/marco/simplecivilisations/SimpleCivilisations.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package com.marco.simplecivilisations;

import com.marco.simplecivilisations.commands.CivilisationsCommand;
import com.marco.simplecivilisations.commands.OfflineTeleportCommand;
import com.marco.simplecivilisations.commands.ReviveCommand;
import com.marco.simplecivilisations.commands.SeenCommand;
import com.marco.simplecivilisations.listeners.PlayerRespawnEvent;
import com.marco.simplecivilisations.listeners.PlayerJoinListener;
import com.marco.simplecivilisations.listeners.PlayerKickListener;
import com.marco.simplecivilisations.listeners.PlayerQuitListener;
import com.marco.simplecivilisations.sql.MySQL;
import com.marco.simplecivilisations.commands.*;
import com.marco.simplecivilisations.listeners.*;
import com.marco.simplecivilisations.sql.*;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.Instant;
import java.util.*;

// Yes, this is the British spelling.
Expand All @@ -26,17 +23,20 @@ public final class SimpleCivilisations extends JavaPlugin {

public List<Location> spawns = new ArrayList<>();

public Map<UUID, User> users = new HashMap<>();
public Map<UUID, Civilisation> civilisations = new HashMap<>();

@Override
public void onEnable() {
// Get these from config file at some point.
saveDefaultConfig();

this.SQL = new MySQL(
this,
"localhost",
"3306",
"test",
"root",
""
getConfig().getString("host"),
getConfig().getString("port"),
getConfig().getString("database"),
getConfig().getString("username"),
getConfig().getString("password")
);

try {
Expand All @@ -53,16 +53,107 @@ public void onEnable() {
Objects.requireNonNull(getCommand("revive")).setExecutor(new ReviveCommand(this));
Objects.requireNonNull(getCommand("seen")).setExecutor(new SeenCommand(this));
Objects.requireNonNull(getCommand("offlinetp")).setExecutor(new OfflineTeleportCommand(this));
Objects.requireNonNull(getCommand("screload")).setExecutor(new ReloadCommand(this));


Bukkit.getPluginManager().registerEvents(new PlayerJoinListener(this), this);
Bukkit.getPluginManager().registerEvents(new PlayerQuitListener(this), this);
Bukkit.getPluginManager().registerEvents(new PlayerKickListener(this), this);
Bukkit.getPluginManager().registerEvents(new PlayerRespawnEvent(this), this);
Bukkit.getPluginManager().registerEvents(new PlayerMoveListener(this), this);
Bukkit.getPluginManager().registerEvents(new BlockPlaceListener(this), this);
Bukkit.getPluginManager().registerEvents(new BlockBreakListener(this), this);
Bukkit.getPluginManager().registerEvents(new BlockDamageListener(this), this);
Bukkit.getPluginManager().registerEvents(new PlayerInteractListener(this), this);
Bukkit.getPluginManager().registerEvents(new EntityExplodeListener(this), this);

getLogger().info("Calculating spawns (faster on preloaded worlds)...");
generateSpawns(20);
getLogger().info("Spawns calculated.");

getLogger().info("Fetching users...");
try {
PreparedStatement ps = SQL.getConnection().prepareStatement("SELECT * FROM users");
ResultSet results = ps.executeQuery();
while (results.next()) {
try {
String potentialUuid = results.getString("civilisation");
UUID uuid = UUID.fromString(results.getString("uuid"));
users.put(uuid, new User(
this,
uuid,
potentialUuid != null ? UUID.fromString(potentialUuid) : null,
results.getInt("role"),
MySQL.deserialiseLocation(results.getString("spawnPoint")),
results.getTimestamp("lastSession"),
MySQL.deserialiseLocation(results.getString("lastLocation")),
results.getInt("lives"),
results.getTimestamp("lastDeath")
));
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (SQLException e) {
e.printStackTrace();
}
getLogger().info(users.size() + " user(s) found.");

getLogger().info("Fetching civilisations...");
try {
PreparedStatement ps = SQL.getConnection().prepareStatement("SELECT * FROM civilisations");
ResultSet results = ps.executeQuery();
while (results.next()) {
// Literally just copied this code from another class, didn't even read it properly.
UUID uuid = UUID.fromString(results.getString("uuid"));
ArrayList<UUID> members = new ArrayList<>();
PreparedStatement psm = SQL.getConnection().prepareStatement("SELECT * FROM users WHERE civilisation = ?");
psm.setString(1, uuid.toString());
ResultSet membersResult = psm.executeQuery();
while (membersResult.next()) {
String mUUID = membersResult.getString("uuid");
members.add(UUID.fromString(mUUID));
}

ArrayList<Pillar> pillars = new ArrayList<>();
PreparedStatement psp = SQL.getConnection().prepareStatement("SELECT * FROM pillars WHERE civilisation = ?");
psp.setString(1, uuid.toString());
ResultSet pillarsResult = psp.executeQuery();
while (pillarsResult.next()) {
pillars.add(new Pillar(
this,
uuid,
MySQL.deserialiseLocation(pillarsResult.getString("location")),
pillarsResult.getTimestamp("destroyed")
));
}

String waypoint = results.getString("waypoint");

try {
civilisations.put(uuid, new Civilisation(
this,
uuid,
results.getString("name"),
results.getString("description"),
UUID.fromString(results.getString("leader")),
members,
results.getBoolean("open"),
pillars,
results.getInt("pillarsAvailable"),
waypoint != null ? MySQL.deserialiseLocation(waypoint) : null
));
} catch (Exception e) {
e.printStackTrace();
}
// IT WORKED!
}
} catch (SQLException e) {
e.printStackTrace();
}
getLogger().info(civilisations.size() + " civilisation(s) found.");

getLogger().info("Ready");
}

@Override
Expand All @@ -89,13 +180,12 @@ public static UUID uuidFromName(String name) {

public void generateSpawns(int t) {
Random r = new Random();
// TODO: Get these from config
World world = Bukkit.getWorld("world");
assert world != null;
int maxX = 4000;
int minX = 0;
int maxZ = 4000;
int minZ = 0;
int maxX = getConfig().getInt("max-x");
int minX = getConfig().getInt("min-x");
int maxZ = getConfig().getInt("max-z");
int minZ = getConfig().getInt("min-z");
for (int i = 0; i < t; i++) {
int x = r.nextInt(minX, maxX);
int z = r.nextInt(minZ, maxZ);
Expand All @@ -109,8 +199,24 @@ public void generateSpawns(int t) {
private boolean isValid(Block block) {
if (block.isEmpty() || block.isLiquid()) return false;
if (block.getType().toString().endsWith("LEAVES")) return false;
//noinspection RedundantIfStatement
if (List.of(Material.CACTUS, Material.MAGMA_BLOCK, Material.SWEET_BERRY_BUSH, Material.POWDER_SNOW).contains(block.getType())) return false;
return true;
}

public static boolean inRangeOfPillar(Location location, Pillar pillar) {
double x = location.getChunk().getX() * 16 + 7.5;
double z = location.getChunk().getZ() * 16 + 7.5;
double halfLength = (4 * 16 + 7.5); // This means 4 chunks in each direction.


double cx = pillar.getLocation().getChunk().getX() * 16 + 7.5;
double cz = pillar.getLocation().getChunk().getZ() * 16 + 7.5;

double ax = cx - halfLength;
double az = cz - halfLength;
double bx = cx + halfLength;
double bz = cz + halfLength;

return x > ax && x < bx && z > az && z < bz;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class CivilisationsCommand implements TabExecutor {
public CivilisationsCommand(SimpleCivilisations plugin) {
subcommands.add(new InfoCommand(plugin));
subcommands.add(new CreateCommand(plugin));
subcommands.add(new PlacePillarCommand(plugin));
subcommands.add(new RemovePillarCommand(plugin));
subcommands.add(new RenameCommand(plugin));
subcommands.add(new DescriptionCommand(plugin));
subcommands.add(new DisbandCommand(plugin));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.marco.simplecivilisations.commands;

import com.marco.simplecivilisations.SimpleCivilisations;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;

import java.util.Collections;
import java.util.List;

public class ReloadCommand implements TabExecutor {
private final SimpleCivilisations plugin;
public ReloadCommand(SimpleCivilisations plugin) {
this.plugin = plugin;
}

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
plugin.reloadConfig();
sender.sendMessage("SimpleCivilisations config reloaded.");
return true;
}

@Override
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.marco.simplecivilisations.SimpleCivilisations;
import com.marco.simplecivilisations.commands.SubCommand;
import com.marco.simplecivilisations.sql.Civilisation;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
Expand Down Expand Up @@ -32,21 +33,22 @@ public String getUsage() {
@Override
public void perform(CommandSender sender, String[] args) {
if (sender instanceof Player player) {
if (plugin.users.get(player.getUniqueId()).getCivilisationId() != null) {
player.sendMessage(SimpleCivilisations.color + "You are already in a civilisation.");
return;
} else if (args.length == 0) {
player.sendMessage(SimpleCivilisations.color + "Usage: " + getUsage());
return;
} else if (args.length > 1) {
player.sendMessage(SimpleCivilisations.color + "Civilisation names cannot contain spaces.");
return;
} else if (args[0].length() > 20) {
player.sendMessage(SimpleCivilisations.color + "Civilisation names cannot exceed 20 characters.");
return;
}
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
if (plugin.getSQL().isInCivilisation(player)) {
player.sendMessage(SimpleCivilisations.color + "You are already in a civilisation.");
return;
} else if (args.length == 0) {
player.sendMessage(SimpleCivilisations.color + "Usage: " + getUsage());
return;
} else if (args.length > 1) {
player.sendMessage(SimpleCivilisations.color + "Civilisation names cannot contain spaces.");
return;
} else if (args[0].length() > 20) {
player.sendMessage(SimpleCivilisations.color + "Civilisation names cannot exceed 20 characters.");
return;
}
SQL.createCivilisation(args[0], player);
Civilisation civilisation = SQL.createCivilisation(args[0], player);
plugin.civilisations.put(civilisation.getUniqueId(), civilisation);
});
} else {
sender.sendMessage(ChatColor.RED + "Only players may run this command.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.marco.simplecivilisations.SimpleCivilisations;
import com.marco.simplecivilisations.commands.SubCommand;
import com.marco.simplecivilisations.sql.Civilisation;
import com.marco.simplecivilisations.sql.User;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
Expand Down Expand Up @@ -33,23 +34,24 @@ public String getUsage() {
@Override
public void perform(CommandSender sender, String[] args) {
if (sender instanceof Player player) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
Civilisation civilisation = SQL.getCivilisationFromPlayerUUID(player.getUniqueId());

if (civilisation == null) {
player.sendMessage(SimpleCivilisations.color + "You must be in a civilisation to run this command.");
return;
} else if (!civilisation.getLeader().toString().equals(player.getUniqueId().toString())) {
player.sendMessage(SimpleCivilisations.color + "Only the leader may set the civilisation description.");
return;
} else if (args.length == 0) {
player.sendMessage(SimpleCivilisations.color + getUsage());
return;
} else if (String.join(" ", args).length() > 100) {
player.sendMessage(SimpleCivilisations.color + "Civilisation descriptions cannot exceed 100 characters.");
return;
}
User user = plugin.users.get(player.getUniqueId());
Civilisation civilisation = plugin.civilisations.get(user.getCivilisationId());

if (civilisation == null) {
player.sendMessage(SimpleCivilisations.color + "You must be in a civilisation to run this command.");
return;
} else if (!civilisation.getLeader().toString().equals(player.getUniqueId().toString())) {
player.sendMessage(SimpleCivilisations.color + "Only the leader may set the civilisation description.");
return;
} else if (args.length == 0) {
player.sendMessage(SimpleCivilisations.color + getUsage());
return;
} else if (String.join(" ", args).length() > 100) {
player.sendMessage(SimpleCivilisations.color + "Civilisation descriptions cannot exceed 100 characters.");
return;
}

Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
civilisation.setDescription(String.join(" ", args));
player.sendMessage(SimpleCivilisations.color + "Civilisation description updated.");
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,20 @@ public String getUsage() {
@Override
public void perform(CommandSender sender, String[] args) {
if (sender instanceof Player player) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
User user = SQL.getUser(player.getUniqueId());
if (user == null) {
player.sendMessage(ChatColor.RED + "Something went wrong!");
return;
} else if (user.getCivilisationId() == null) {
player.sendMessage(SimpleCivilisations.color + "You must be in a civilisation to run this command.");
return;
} else if (user.getRole() < 3) {
player.sendMessage(SimpleCivilisations.color + "Only the leader of your civilisation can run this command.");
return;
}

Civilisation civilisation = SQL.getCivilisation(user);
civilisation.messageOnlineMembers(civilisation.getName() + " has been disbanded.");
civilisation.disband();
});
User user = plugin.users.get(player.getUniqueId());
if (user.getCivilisationId() == null) {
player.sendMessage(SimpleCivilisations.color + "You must be in a civilisation to run this command.");
return;
} else if (user.getRole() < 3) {
player.sendMessage(SimpleCivilisations.color + "Only the leader of your civilisation can run this command.");
return;
}

Civilisation civilisation = plugin.civilisations.get(user.getCivilisationId());
civilisation.messageOnlineMembers(civilisation.getName() + " has been disbanded.");
plugin.civilisations.remove(civilisation.getUniqueId());

Bukkit.getScheduler().runTaskAsynchronously(plugin, civilisation::disband);
return;
}
sender.sendMessage(ChatColor.RED + "Only player can run this command.");
Expand Down
Loading

0 comments on commit 144fbe7

Please sign in to comment.