feat: Void economy basics
kitchen sink commit but trying to start making sence of this repo. so the main thing is void economy Kitchen-Sink
This commit is contained in:
parent
762ab5f926
commit
42c25d7095
@ -1,54 +1,76 @@
|
||||
package ovh.herisson.thevoidroad.Commands;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.WorldBorder;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabCompleter;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class VoidCommands implements CommandExecutor{
|
||||
import ovh.herisson.thevoidroad.TheVoidRoad;
|
||||
import ovh.herisson.thevoidroad.Voids;
|
||||
|
||||
public class VoidCommands implements CommandExecutor, TabCompleter{
|
||||
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||
|
||||
//balance
|
||||
if(args.length == 0){
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
args[i] = args[i].toLowerCase();
|
||||
}
|
||||
if(args.length < 1){
|
||||
if(sender instanceof Player ply){
|
||||
// long voids = ply.getPersistentDataContainer().has(TheVoidRoad.voids) ? ply.getPersistentDataContainer().get(TheVoidRoad.voids, PersistentDataType.LONG) : 0;
|
||||
// ply.sendMessage(Component.text("Vous avez : ").append(Component.text(voids).color(NamedTextColor.GREEN).append(Component.text(TheVoidRoad.CoinGlyph))));
|
||||
sender.sendMessage("You got: "+Voids.get(ply) + TheVoidRoad.CoinGlyph);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//Args
|
||||
switch (args[0].toLowerCase()) {
|
||||
case "event":
|
||||
switch (args[0]) {
|
||||
case "score":
|
||||
Voids.scoreboard().forEach((p, a) -> {
|
||||
sender.sendMessage(p + " : " + a);
|
||||
});
|
||||
return true;
|
||||
case "give":
|
||||
sender.sendMessage("TODO");
|
||||
//TODO(Balance): Give to a player
|
||||
return true;
|
||||
case "event":
|
||||
//TODO(event) manage event
|
||||
return true;
|
||||
case "manage":
|
||||
if(sender.hasPermission("TheVoidRoad.manage"))
|
||||
return manage(sender, args);
|
||||
else
|
||||
sender.sendMessage("You don't have the permissions to do that!");
|
||||
if(!sender.hasPermission("void.manage") || args.length < 2) return false;
|
||||
switch (args[1]) {
|
||||
case "setspawn":
|
||||
return setspawn(sender);
|
||||
case "init":
|
||||
return init();
|
||||
case "reset":
|
||||
return reset();
|
||||
case "skip":
|
||||
//TODO(Merchand): Skip trade
|
||||
case "balance":
|
||||
if(args.length < 5) return false;
|
||||
switch (args[2]) {
|
||||
case "set":
|
||||
Player target = Bukkit.getPlayer(args[3]);
|
||||
Voids.set(target, Integer.parseInt(args[4]));
|
||||
return true;
|
||||
case "add":
|
||||
case "sub":
|
||||
sender.sendMessage("TODO"); //TODO
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Manage sub command
|
||||
private boolean manage(CommandSender sender, String[] args) {
|
||||
switch(args[1]){
|
||||
case "balance":
|
||||
switch (args[2]) {
|
||||
case "set":
|
||||
// Player ply = Bukkit.getServer().getPlayer(args[3]);
|
||||
// ply.getPersistentDataContainer().set(TheVoidRoad.voids, PersistentDataType.LONG, Long.parseLong(args[4]));
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
case "setspawn":
|
||||
|
||||
private boolean setspawn(CommandSender sender){
|
||||
if(sender instanceof Player ply){
|
||||
Location loc = ply.getLocation();
|
||||
WorldBorder border = ply.getWorld().getWorldBorder();
|
||||
@ -58,11 +80,45 @@ public class VoidCommands implements CommandExecutor{
|
||||
border.setSize(750);
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case "init":
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean init(){
|
||||
//TODO(merchand): init the game
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean reset(){
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
|
||||
if(args.length == 1){
|
||||
list.addAll(List.of("give","event", "score"));
|
||||
if(sender.hasPermission("void.manage")){
|
||||
list.add("manage");
|
||||
}
|
||||
}
|
||||
if(args.length >= 2){
|
||||
switch (args[0]) {
|
||||
case "give":
|
||||
return null;
|
||||
case "event":
|
||||
return list;
|
||||
case "manage":
|
||||
if(sender.hasPermission("void.manage")){
|
||||
if(args[1] == "balance"){
|
||||
list.addAll(List.of("set", "add", "sub"));
|
||||
return list;
|
||||
}
|
||||
list.addAll(List.of("setspawn", "init", "skip", "reset", "balance")); //TODO: should add balance management
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
@ -3,19 +3,28 @@ package ovh.herisson.thevoidroad;
|
||||
import java.io.File;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.HashMap;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class DatabaseManager {
|
||||
|
||||
private static DatabaseManager instance;
|
||||
|
||||
public static DatabaseManager getInstance(){
|
||||
if (instance != null) return instance;
|
||||
try { instance = new DatabaseManager();}
|
||||
catch(SQLException e){ Bukkit.getLogger().warning("Could not Initiate database : " + e); }
|
||||
if (instance == null){
|
||||
try {
|
||||
instance = new DatabaseManager();
|
||||
} catch(SQLException e) {
|
||||
Bukkit.getLogger().warning("Could not Initiate database : " + e);
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
@ -24,16 +33,98 @@ public class DatabaseManager {
|
||||
private DatabaseManager() throws SQLException{
|
||||
new File("plugins/TheVoidRoad").mkdirs();
|
||||
con = DriverManager.getConnection("jdbc:sqlite:plugins/TheVoidRoad/voids.db");
|
||||
InitTables();
|
||||
InitVoids();
|
||||
InitConfig();
|
||||
}
|
||||
|
||||
private void InitTables() throws SQLException{
|
||||
// Voids table and methods
|
||||
private void InitVoids() throws SQLException{
|
||||
String query = "CREATE TABLE IF NOT EXISTS voids (\n"
|
||||
+ "id integer PRIMARY KEY,\n"
|
||||
+ "uuid text NOT NULL,\n"
|
||||
+ "ammount INTEGER DEFAULT 0)";
|
||||
+ "amount INTEGER DEFAULT 0)";
|
||||
|
||||
Statement st = con.createStatement();
|
||||
st.execute(query);
|
||||
st.close();
|
||||
}
|
||||
|
||||
public int getVoids(UUID uuid) throws SQLException{
|
||||
Statement st = con.createStatement();
|
||||
String query = "SELECT amount FROM voids WHERE uuid = ? LIMIT 1";
|
||||
PreparedStatement p = con.prepareStatement(query);
|
||||
p.setString(1, uuid.toString());
|
||||
ResultSet rs = p.executeQuery();
|
||||
int value = rs.getInt(1);
|
||||
st.close();
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setVoids(UUID uuid, int amount) throws SQLException{
|
||||
Statement st = con.createStatement();
|
||||
String query = "INSERT OR REPLACE INTO voids (uuid, amount) VALUES (?, ?)";
|
||||
PreparedStatement p = con.prepareStatement(query);
|
||||
p.setString(1, uuid.toString());
|
||||
p.setInt(2, amount);
|
||||
p.execute();
|
||||
st.close();
|
||||
}
|
||||
|
||||
public HashMap<UUID, Integer> getVoidsScoreboard() throws SQLException{
|
||||
HashMap<UUID, Integer> ret = new HashMap<>();
|
||||
|
||||
Statement st = con.createStatement();
|
||||
String query = "SELECT uuid, amount FROM voids";
|
||||
Statement p = con.createStatement();
|
||||
ResultSet rs = p.executeQuery(query);
|
||||
do{
|
||||
ret.put(UUID.fromString(rs.getString(1)), rs.getInt(2));
|
||||
}while(rs.next());
|
||||
|
||||
st.close();
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Config table and methods
|
||||
private void InitConfig() throws SQLException{
|
||||
String query = "CREATE TABLE IF NOT EXISTS config (\n"
|
||||
+ "id integer PRIMARY KEY,\n"
|
||||
+ "name text NOT NULL UNIQUE,\n"
|
||||
+ "value text default NULL)";
|
||||
|
||||
Statement st = con.createStatement();
|
||||
st.execute(query);
|
||||
st.close();
|
||||
}
|
||||
|
||||
public String getConfig(String name) throws SQLException{
|
||||
Statement st = con.createStatement();
|
||||
String query = "SELECT value FROM config WHERE name = ? LIMIT 1";
|
||||
PreparedStatement p = con.prepareStatement(query);
|
||||
p.setString(1, name);
|
||||
ResultSet rs = p.executeQuery();
|
||||
String value = rs.getString(1);
|
||||
st.close();
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setConfig(String name, @Nullable String value) throws SQLException{
|
||||
Statement st = con.createStatement();
|
||||
String query = "INSERT OR REPLACE INTO config (name, value) VALUES (?, ?)";
|
||||
PreparedStatement p = con.prepareStatement(query);
|
||||
p.setString(1, name);
|
||||
p.setString(2, value);
|
||||
p.execute();
|
||||
st.close();
|
||||
}
|
||||
|
||||
|
||||
public void close(){
|
||||
try {
|
||||
con.close();
|
||||
} catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,8 +5,13 @@ import java.util.Random;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.entity.Villager;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.world.EntitiesUnloadEvent;
|
||||
|
||||
import ovh.herisson.thevoidroad.Event.HourEvent;
|
||||
|
||||
//Unique, so singleton patern
|
||||
public class Merchand implements Listener{
|
||||
@ -20,20 +25,36 @@ public class Merchand implements Listener{
|
||||
|
||||
private Villager m ;
|
||||
private Merchand() {
|
||||
generate();
|
||||
}
|
||||
|
||||
private void generate(){
|
||||
int x = new Random().nextInt(750 * 2) - 750, z = new Random().nextInt(750 * 2) - 750;
|
||||
m = (Villager) Bukkit.getWorld("world").spawnEntity(new Location(Bukkit.getWorld("world"), x, 300, z), EntityType.VILLAGER);
|
||||
m.setGlowing(true);
|
||||
m.setInvulnerable(true);
|
||||
Bukkit.getServer().forEachAudience((a) -> {
|
||||
if(a instanceof Player ply){
|
||||
ply.setCompassTarget(m.getLocation());
|
||||
}
|
||||
}); //Temporary
|
||||
}
|
||||
|
||||
public void regenerate(){
|
||||
m.remove();
|
||||
int x = new Random().nextInt(750 * 2) - 750, z = new Random().nextInt(750 * 2) - 750;
|
||||
m = (Villager) Bukkit.getWorld("world").spawnEntity(new Location(Bukkit.getWorld("world"), x, 300, z), EntityType.VILLAGER);
|
||||
m.setGlowing(true);
|
||||
m.setInvulnerable(true);
|
||||
if(m != null){ m.remove(); }
|
||||
generate();
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onHour(HourEvent e){
|
||||
regenerate();
|
||||
}
|
||||
|
||||
@EventHandler //TODO(Merchand): Check if can't do using ticket chunk
|
||||
public void onUnload(EntitiesUnloadEvent e){
|
||||
if(e.getEntities().contains(m)){
|
||||
m.getChunk().load();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,34 @@
|
||||
package ovh.herisson.thevoidroad.Event;
|
||||
|
||||
import java.time.LocalTime;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
import com.destroystokyo.paper.event.server.ServerTickStartEvent;
|
||||
|
||||
public class HourEvent extends Event implements Listener{
|
||||
private static final HandlerList handlerList = new HandlerList();
|
||||
public int hour = LocalTime.now().getHour();
|
||||
|
||||
public static HandlerList getHandlerList(){
|
||||
return handlerList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HandlerList getHandlers() {
|
||||
return handlerList;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onHour(ServerTickStartEvent e){
|
||||
if(LocalTime.now().getHour() != hour){
|
||||
hour = LocalTime.now().getHour();
|
||||
Bukkit.getPluginManager().callEvent(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,27 +0,0 @@
|
||||
package ovh.herisson.thevoidroad.Event;
|
||||
|
||||
import java.time.LocalTime;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
import com.destroystokyo.paper.event.server.ServerTickStartEvent;
|
||||
|
||||
public class NewHourEvent implements Listener{
|
||||
|
||||
public int hour = 0;
|
||||
@EventHandler
|
||||
public void onHour(ServerTickStartEvent e){
|
||||
if(LocalTime.now().getHour() != hour){
|
||||
hour = LocalTime.now().getHour();
|
||||
hourEvent();
|
||||
}
|
||||
}
|
||||
|
||||
//This function will be called once per hours
|
||||
private void hourEvent() {
|
||||
Bukkit.getLogger().info("test"); //TODO
|
||||
}
|
||||
}
|
||||
|
@ -4,21 +4,27 @@ import org.bukkit.event.Listener;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import ovh.herisson.thevoidroad.Commands.VoidCommands;
|
||||
import ovh.herisson.thevoidroad.Event.NewHourEvent;
|
||||
import ovh.herisson.thevoidroad.Event.HourEvent;
|
||||
|
||||
public class TheVoidRoad extends JavaPlugin implements Listener{
|
||||
|
||||
public static String CoinGlyph = "Ⓥ"; // Ɣ, √, ▼, Ṿ, ṿ
|
||||
public static final DatabaseManager db = DatabaseManager.getInstance();
|
||||
|
||||
@Override
|
||||
public void onEnable(){
|
||||
|
||||
DatabaseManager db = DatabaseManager.getInstance();
|
||||
|
||||
//Commands
|
||||
getCommand("void").setExecutor(new VoidCommands());
|
||||
getCommand("void").setTabCompleter(new VoidCommands());
|
||||
|
||||
//Events
|
||||
getServer().getPluginManager().registerEvents(new NewHourEvent(), this);
|
||||
getServer().getPluginManager().registerEvents(new HourEvent(), this); //For HourEvent
|
||||
getServer().getPluginManager().registerEvents(this, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable(){
|
||||
db.close();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,72 @@
|
||||
package ovh.herisson.thevoidroad;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public final class Voids{
|
||||
|
||||
private Voids(){} // Prohib the instantiation
|
||||
|
||||
public static int get(Player ply){
|
||||
try {
|
||||
return TheVoidRoad.db.getVoids(ply.getUniqueId());
|
||||
} catch(SQLException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static void set(Player ply, int amount){
|
||||
try {
|
||||
TheVoidRoad.db.setVoids(ply.getUniqueId(), amount);
|
||||
|
||||
} catch(SQLException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void add(Player ply, int amount){
|
||||
try {
|
||||
int curr = TheVoidRoad.db.getVoids(ply.getUniqueId());
|
||||
TheVoidRoad.db.setVoids(ply.getUniqueId(), curr + amount);
|
||||
} catch(SQLException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean subtract(Player ply, int amount, boolean force){
|
||||
try {
|
||||
int curr = TheVoidRoad.db.getVoids(ply.getUniqueId());
|
||||
if(curr - amount < 0 && !force) return false;
|
||||
TheVoidRoad.db.setVoids(ply.getUniqueId(), curr - amount);
|
||||
} catch(SQLException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean give(Player giver, Player receiver, int amount){
|
||||
if(subtract(giver, amount, false)){
|
||||
add(receiver, amount);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static HashMap<Player, Integer> scoreboard(){
|
||||
HashMap<Player, Integer> ret = new HashMap<>();
|
||||
try {
|
||||
HashMap<UUID, Integer> scores_uuid = TheVoidRoad.db.getVoidsScoreboard();
|
||||
scores_uuid.forEach((u, a) -> {
|
||||
ret.put(Bukkit.getPlayer(u), a);
|
||||
});
|
||||
} catch(SQLException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
@ -8,4 +8,17 @@ api-version: '1.20'
|
||||
commands:
|
||||
void:
|
||||
description: Interact with the void road plugin
|
||||
usage: "/void <give|manage>"
|
||||
usage: "/void [give|event]"
|
||||
# permission: void.balance
|
||||
# permission-message: "you are disliked :/"
|
||||
|
||||
permissions:
|
||||
void.manage:
|
||||
description: "GameMaster's command"
|
||||
default: op
|
||||
void.balance:
|
||||
description: "User of thevoidroad"
|
||||
default: not op
|
||||
void.event:
|
||||
description: "Create event"
|
||||
default: op
|
||||
|
Loading…
Reference in New Issue
Block a user