Show Scoreboard below name and top screen

This commit is contained in:
Debucquoy Anthony 2024-01-25 20:27:16 +01:00
parent a7a775d1c8
commit 3e3a6e9a5d
Signed by: tonitch
GPG Key ID: A78D6421F083D42E
4 changed files with 105 additions and 47 deletions

View File

@ -41,7 +41,7 @@ public class DatabaseManager {
private void InitVoids() throws SQLException{ private void InitVoids() throws SQLException{
String query = "CREATE TABLE IF NOT EXISTS voids (\n" String query = "CREATE TABLE IF NOT EXISTS voids (\n"
+ "id integer PRIMARY KEY,\n" + "id integer PRIMARY KEY,\n"
+ "uuid text NOT NULL,\n" + "uuid text NOT NULL UNIQUE,\n"
+ "amount INTEGER DEFAULT 0)"; + "amount INTEGER DEFAULT 0)";
Statement st = con.createStatement(); Statement st = con.createStatement();
@ -49,7 +49,8 @@ public class DatabaseManager {
st.close(); st.close();
} }
public int getVoids(UUID uuid) throws SQLException{ public int getVoids(UUID uuid){
try {
Statement st = con.createStatement(); Statement st = con.createStatement();
String query = "SELECT amount FROM voids WHERE uuid = ? LIMIT 1"; String query = "SELECT amount FROM voids WHERE uuid = ? LIMIT 1";
PreparedStatement p = con.prepareStatement(query); PreparedStatement p = con.prepareStatement(query);
@ -58,9 +59,14 @@ public class DatabaseManager {
int value = rs.getInt(1); int value = rs.getInt(1);
st.close(); st.close();
return value; return value;
} catch(Exception e){
e.printStackTrace();
}
return 0;
} }
public void setVoids(UUID uuid, int amount) throws SQLException{ public void setVoids(UUID uuid, int amount) {
try {
Statement st = con.createStatement(); Statement st = con.createStatement();
String query = "INSERT OR REPLACE INTO voids (uuid, amount) VALUES (?, ?)"; String query = "INSERT OR REPLACE INTO voids (uuid, amount) VALUES (?, ?)";
PreparedStatement p = con.prepareStatement(query); PreparedStatement p = con.prepareStatement(query);
@ -68,6 +74,9 @@ public class DatabaseManager {
p.setInt(2, amount); p.setInt(2, amount);
p.execute(); p.execute();
st.close(); st.close();
} catch(Exception e){
e.printStackTrace();
}
} }
public HashMap<UUID, Integer> getVoidsScoreboard() throws SQLException{ public HashMap<UUID, Integer> getVoidsScoreboard() throws SQLException{

View File

@ -15,6 +15,9 @@ import org.bukkit.entity.Player;
import org.bukkit.entity.Villager; import org.bukkit.entity.Villager;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryInteractEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent; import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.world.EntitiesLoadEvent; import org.bukkit.event.world.EntitiesLoadEvent;
@ -25,6 +28,7 @@ import org.bukkit.persistence.PersistentDataType;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import ovh.herisson.thevoidroad.DatabaseManager; import ovh.herisson.thevoidroad.DatabaseManager;
import ovh.herisson.thevoidroad.TheVoidRoad; import ovh.herisson.thevoidroad.TheVoidRoad;
import ovh.herisson.thevoidroad.Voids;
import ovh.herisson.thevoidroad.Event.HourEvent; import ovh.herisson.thevoidroad.Event.HourEvent;
import ovh.herisson.thevoidroad.util.Tuple; import ovh.herisson.thevoidroad.util.Tuple;
@ -54,7 +58,8 @@ public class Merchand implements Listener{
} catch(Exception e){ } catch(Exception e){
e.printStackTrace(); e.printStackTrace();
} }
if(DatabaseManager.getInstance().getConfig("init").equalsIgnoreCase("true")){ if(DatabaseManager.getInstance().getConfig("init") != null &&
DatabaseManager.getInstance().getConfig("init").equalsIgnoreCase("true")){
regenerate(); regenerate();
} }
} }
@ -121,4 +126,26 @@ public class Merchand implements Listener{
e.getPlayer().setCompassTarget(m.getLocation()); e.getPlayer().setCompassTarget(m.getLocation());
} }
} }
//Inventory interaction
@EventHandler
public void ClickInsideInventory(InventoryClickEvent e){
// Did the click the right slot in the right inventory
if(!e.getInventory().equals(inv)) return;
e.setCancelled(true);
if(!e.getInventory().equals(inv) || e.getSlot() != 4) return;
if(e.getWhoClicked() instanceof Player ply){
//Do we have the item and if so remove them
if(ply.getInventory().contains(current.x)){
for (ItemStack i : ply.getInventory().getContents()) {
if(i.getType() == current.x){
i.subtract(1);
Voids.add(ply, current.y);
return;
}
}
}
}
}
} }

View File

@ -29,7 +29,7 @@ public class TheVoidRoad extends JavaPlugin{
//Events //Events
getServer().getPluginManager().registerEvents(new HourEvent(), this); //For HourEvent getServer().getPluginManager().registerEvents(new HourEvent(), this); //For HourEvent
getServer().getPluginManager().registerEvents(Merchand.getInstance(), this); getServer().getPluginManager().registerEvents(Merchand.getInstance(), this);
getServer().getPluginManager().registerEvents(new Voids(), this);
} }
@Override @Override

View File

@ -6,46 +6,44 @@ import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.scoreboard.Criteria;
import org.bukkit.scoreboard.DisplaySlot;
import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.RenderType;
import org.bukkit.scoreboard.Score;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.ScoreboardManager;
public final class Voids{ import net.kyori.adventure.bossbar.BossBar;
import net.kyori.adventure.text.Component;
private Voids(){} // Prohib the instantiation public final class Voids implements Listener{
private static HashMap<Player, BossBar> bossbars = new HashMap<>();
public static int get(Player ply){ public static int get(Player ply){
try {
return TheVoidRoad.db.getVoids(ply.getUniqueId()); return TheVoidRoad.db.getVoids(ply.getUniqueId());
} catch(SQLException e){
e.printStackTrace();
}
return 0;
} }
public static void set(Player ply, int amount){ public static void set(Player ply, int amount){
try {
TheVoidRoad.db.setVoids(ply.getUniqueId(), amount); TheVoidRoad.db.setVoids(ply.getUniqueId(), amount);
UpdateVoids(ply);
} catch(SQLException e){
e.printStackTrace();
}
} }
public static void add(Player ply, int amount){ public static void add(Player ply, int amount){
try { int curr = DatabaseManager.getInstance().getVoids(ply.getUniqueId());
int curr = TheVoidRoad.db.getVoids(ply.getUniqueId()); DatabaseManager.getInstance().setVoids(ply.getUniqueId(), curr + amount);
TheVoidRoad.db.setVoids(ply.getUniqueId(), curr + amount); UpdateVoids(ply);
} catch(SQLException e){
e.printStackTrace();
}
} }
public static boolean subtract(Player ply, int amount, boolean force){ public static boolean subtract(Player ply, int amount, boolean force){
try {
int curr = TheVoidRoad.db.getVoids(ply.getUniqueId()); int curr = TheVoidRoad.db.getVoids(ply.getUniqueId());
if(curr - amount < 0 && !force) return false; if(curr - amount < 0 && !force) return false;
TheVoidRoad.db.setVoids(ply.getUniqueId(), curr - amount); TheVoidRoad.db.setVoids(ply.getUniqueId(), curr - amount);
} catch(SQLException e){ UpdateVoids(ply);
e.printStackTrace();
}
return true; return true;
} }
@ -69,4 +67,28 @@ public final class Voids{
} }
return ret; return ret;
} }
public static void UpdateVoids(Player ply){
Integer count = get(ply);
String countString = count.toString() + TheVoidRoad.CoinGlyph;
Scoreboard sb = Bukkit.getScoreboardManager().getMainScoreboard();
Objective o = sb.getObjective("VOIDS") != null ? sb.getObjective("VOIDS") : sb.registerNewObjective("VOIDS", Criteria.DUMMY, "voids");
o.setDisplaySlot(DisplaySlot.BELOW_NAME);
o.displayName(Component.text(TheVoidRoad.CoinGlyph));
Score s = o.getScore(ply);
s.setScore(count);
BossBar bb = bossbars.containsKey(ply) ?
bossbars.get(ply).name(Component.text(countString)) :
BossBar.bossBar(Component.text(countString), 1, BossBar.Color.GREEN, BossBar.Overlay.PROGRESS);
bossbars.put(ply, bb);
ply.showBossBar(bb);
}
@EventHandler
public void UpdateVoids(PlayerJoinEvent e){
UpdateVoids(e.getPlayer());
}
} }