Skip to content

Commit

Permalink
Merge pull request #16 from BTE-Germany/BOTM
Browse files Browse the repository at this point in the history
BOTM
  • Loading branch information
realMorgon authored Feb 9, 2025
2 parents 3b86e86 + b3854a1 commit ca4dfa4
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 6 deletions.
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@
<id>btegermany</id>
<url>https://repo.dachstein.cloud/nexus/content/repositories/btegermany</url>
</repository>
<repository>
<id>jitpack</id>
<url>https://jitpack.io/</url>
</repository>
</repositories>

<dependencies>
Expand Down Expand Up @@ -178,5 +182,11 @@
<version>1.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.decentsoftware-eu</groupId>
<artifactId>decentholograms</artifactId>
<version>2.8.6</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
20 changes: 16 additions & 4 deletions src/main/java/dev/nachwahl/lobby/Lobby.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.text.minimessage.tag.standard.StandardTags;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.messaging.PluginMessageListener;
Expand All @@ -40,8 +40,6 @@
import java.util.HashMap;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

@Getter
public final class Lobby extends JavaPlugin implements PluginMessageListener {
Expand All @@ -65,6 +63,7 @@ public final class Lobby extends JavaPlugin implements PluginMessageListener {
private MiniGameBlockUtil miniGameBlockUtil;
private CinematicUtil cinematicUtil;
private LeaderboardManager leaderboardManager;
private BOTMScoreAPI botmScoreAPI;

private QuestManager questManager;
private PoolManager poolManager;
Expand Down Expand Up @@ -115,6 +114,7 @@ public void onEnable() {
this.hotbarItems = new HotbarItems(this);
this.locationAPI = new LocationAPI(this);
this.userSettingsAPI = new UserSettingsAPI(this);
this.botmScoreAPI = new BOTMScoreAPI(this);
this.realTime = new RealTime(this.getConfig().getString("time.timezone"), this.getConfig().getInt("time.updateInterval"), Bukkit.getWorld("world"));
this.vanish = new Vanish();
this.hologramAPI = new HologramAPI(this);
Expand Down Expand Up @@ -147,16 +147,27 @@ public void onEnable() {
throw new RuntimeException(e);
}


// Update scoreboards
getServer().getScheduler().runTaskTimer(this, () -> {
scoreboard.updateScoreboards();
}, 0, 100L);

// try {
// Location location = botmScoreAPI.getLocation();
// if (location != null) {
// BOTMCommand.create(location, this.getDatabase());
// }
// } catch (SQLException e) {
// Bukkit.getLogger().warning("Es wurde keine Location für das BOTM Hologramm gefunden.");
// throw new RuntimeException(e);
// }
// botmScoreAPI.clearLocation();

}

@Override
public void onDisable() {

Bukkit.getLogger().info("Das Lobby Plugin wurde deaktiviert.");
this.database.disconnect();
this.getServer().getMessenger().unregisterOutgoingPluginChannel(this);
Expand Down Expand Up @@ -197,6 +208,7 @@ public void registerCommand() {
this.manager.registerCommand(new SoonCommand());
this.manager.registerCommand(new VisitCommand());
this.manager.registerCommand(new CinematicCommand(cinematicUtil));
this.manager.registerCommand(new BOTMCommand(this));
}


Expand Down
120 changes: 120 additions & 0 deletions src/main/java/dev/nachwahl/lobby/commands/BOTMCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package dev.nachwahl.lobby.commands;

import co.aikar.commands.BaseCommand;
import co.aikar.commands.annotation.*;
import co.aikar.idb.Database;
import co.aikar.idb.DbRow;
import dev.nachwahl.lobby.Lobby;
import dev.nachwahl.lobby.language.Language;
import eu.decentsoftware.holograms.api.DHAPI;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.sql.SQLException;
import java.util.*;

@CommandAlias("botm")
public class BOTMCommand extends BaseCommand {

@Dependency
private static Lobby lobby;

public BOTMCommand(Lobby lobby) {
this.lobby = lobby;
}

private static final int entries = 3;


@CommandPermission("bteg.lobby.botm")
@Subcommand("create")
public void onBOTMCreate(CommandSender sender) throws SQLException {
Player player = (Player) sender;

player.sendMessage(create(player.getLocation(), this.lobby.getDatabase(), this.lobby.getLanguageAPI().getLanguage(player)));
}

@CommandPermission("bteg.lobby.botm")
@Subcommand("move")
public void onBOTMMove(CommandSender sender) throws SQLException {
DHAPI.moveHologram("BOTM", ((Player)sender).getLocation());
this.lobby.getLocationAPI().setLocation(((Player) sender).getLocation(), "botm");
this.lobby.getLanguageAPI().sendMessageToPlayer((Player) sender, "botm.moved");
}

@CommandPermission("bteg.lobby.botm")
@Subcommand("remove")
public void onBOTMRemove(CommandSender sender) {
DHAPI.removeHologram("BOTM");
this.lobby.getLanguageAPI().sendMessageToPlayer((Player) sender, "botm.removed");
}

@CommandPermission("bteg.lobby.botm")
@Syntax("<player>")
@Subcommand("add")
public void onBOTMAdd(CommandSender sender, String target) throws SQLException {

Player player = (Player) sender;

this.lobby.getBotmScoreAPI().addPoints(target);
update(player);

this.lobby.getLanguageAPI().sendMessageToPlayer(player, "botm.added");
}

public static String create(Location location, Database database, Language language) throws SQLException {

HashMap<String, Integer> scores = new HashMap<>();
List<DbRow> dbRows = database.getResults("SELECT * FROM botm");

dbRows.forEach(row -> {
String name = row.getString("name");
int score = row.getInt("score");
scores.put(name, score);
});

// Create a hologram
if (scores.size() >= 3) {

ArrayList<String> keys = new ArrayList<>(scores.keySet());

Map.Entry<Integer, String>[] relevantEntries = new Map.Entry[entries];

for (int i = 0; i < entries; i++) {

TreeMap<Integer, String> map = new TreeMap<>();

for (String key : keys) {
map.put(scores.get(key), key);
}
relevantEntries[i] = map.lastEntry();
keys.remove(relevantEntries[i].getValue());
}

List<String> lines = new ArrayList<>();
lines.add(ChatColor.GOLD + "Build of the Month");
for (int i = 0; i < entries; i++) lines.add(ChatColor.GRAY + relevantEntries[i].getValue() + ": " + ChatColor.GREEN + relevantEntries[i].getKey());

DHAPI.createHologram("BOTM", location, lines);
lobby.getLocationAPI().setLocation(location, "botm");

return lobby.getLanguageAPI().getMessageString(language, "botm.create.success");
} else {
// Send feedback
return lobby.getLanguageAPI().getMessageString(language, "botm.create.failed");

}
}

public void update(Player player) throws SQLException {
if (DHAPI.getHologram("BOTM") != null) {
Location location = DHAPI.getHologram("BOTM").getLocation();

DHAPI.removeHologram("BOTM");
create(location, this.lobby.getDatabase(), this.lobby.getLanguageAPI().getLanguage(player));
}
}

}
19 changes: 19 additions & 0 deletions src/main/java/dev/nachwahl/lobby/events/PlayerEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

import co.aikar.idb.DbRow;
import dev.nachwahl.lobby.Lobby;
import dev.nachwahl.lobby.commands.BOTMCommand;
import dev.nachwahl.lobby.guis.PrivacyGUI;
import dev.nachwahl.lobby.language.Language;
import dev.nachwahl.lobby.utils.Actions;
import dev.triumphteam.gui.builder.item.ItemBuilder;
import eu.decentsoftware.holograms.api.DHAPI;
import lombok.SneakyThrows;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.EntityType;
Expand All @@ -24,6 +29,8 @@
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;

import java.sql.SQLException;

public class PlayerEvents implements Listener {

private Lobby lobby;
Expand Down Expand Up @@ -63,6 +70,18 @@ public void onJoin(PlayerJoinEvent event) {
this.lobby.getScoreboard().initScoreboard(player);
}

if (DHAPI.getHologram("BOTM") == null && this.lobby.getLocationAPI().getLocation("botm") != null) {
try {
Location location = this.lobby.getLocationAPI().getLocation("botm");
if (location != null) {
Bukkit.getLogger().info(BOTMCommand.create(location, this.lobby.getDatabase(), Language.GERMAN));
}
} catch (SQLException e) {
Bukkit.getLogger().warning("Es wurde keine Location für das BOTM Hologramm gefunden.");
throw new RuntimeException(e);
}
}

}

@EventHandler
Expand Down
62 changes: 62 additions & 0 deletions src/main/java/dev/nachwahl/lobby/utils/BOTMScoreAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package dev.nachwahl.lobby.utils;

import co.aikar.idb.DbRow;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import dev.nachwahl.lobby.Lobby;
import org.bukkit.Bukkit;
import org.bukkit.Location;

import java.sql.SQLException;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

public class BOTMScoreAPI {


private final Cache<String, Integer> scoreCache = CacheBuilder.newBuilder()
.expireAfterWrite(5, TimeUnit.MINUTES)
.build();
private final Lobby lobby;

public BOTMScoreAPI(Lobby lobby) {
this.lobby = lobby;
}

public void setScore(String name, int score) throws SQLException {
scoreCache.put(name, score);

this.lobby.getDatabase().executeUpdate("INSERT INTO botm (name, score) VALUES (?, ?) ON DUPLICATE KEY UPDATE score = VALUES(score)", name, score);
}

public void getScore(String name, Consumer<Integer> callback) throws SQLException {
Integer cache = scoreCache.getIfPresent(name);

if (cache == null) {
DbRow dbRow = this.lobby.getDatabase().getFirstRow("SELECT * FROM botm WHERE name = ?", name);
if (dbRow == null) {
callback.accept(0);
} else {
scoreCache.put(name, dbRow.get("score"));
callback.accept(dbRow.get("score"));
}
}else {
callback.accept(cache);
}
}

public void addPoints(String name) throws SQLException {
getScore(name, score -> {
try{
if (score == null) {
setScore(name, 1);
}else {
setScore(name, score + 1);
}
} catch (SQLException e) {
e.printStackTrace();
}
});
}
}
8 changes: 7 additions & 1 deletion src/main/resources/lang_de_DE.properties
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,10 @@ map.tpll.description=Nutze diesen Befehl um mithilfe von Koordinaten zu einem ex
map.tpll.message=<prefix> <aqua>Schaue dir das Tutorial hier an: <red><b>youtube.com</b></red>.</aqua>
tutorials.title=<dark_gray>✕</dark_gray> <b><color:#479dff>Tutorials</color></b> <dark_gray>✕</dark_gray>

scoreboard.playtime=<gray>Spielzeit</gray>
scoreboard.playtime=<gray>Spielzeit</gray>

botm.added=<green>[BOTM] Punkte wurden hinzugefügt.</green>
botm.removed=<red>[BOTM] Hologramm wurde entfernt.</red>
botm.moved=<green>[BOTM] Hologramm wurde verschoben.</green>
botm.create.success=<green>[BOTM] Hologramm wurde erstellt.</green>
botm.create.failed=<red>[BOTM] Hologramm konnte nicht erstellt werden. Sind genug Ergebnisse eingetragen?</red>
8 changes: 7 additions & 1 deletion src/main/resources/lang_en_EN.properties
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,10 @@ map.tpll.description=Use this command to get to an exact location using coordina
map.tpll.message=<prefix> <aqua>Check out the tutorial here: <red><b>youtube.com</b></red>.</aqua>
tutorials.title=<dark_gray>✕</dark_gray> <b><color:#479dff>Tutorials</color></b> <dark_gray>✕</dark_gray>

scoreboard.playtime=<gray>Playtime</gray>
scoreboard.playtime=<gray>Playtime</gray>

botm.added=<green>[BOTM] Points have been added.</green>
botm.removed=<red>[BOTM] Hologram has been removed.</red>
botm.moved=<green>[BOTM] Hologram has been moved.</green>
botm.create.success=<green>[BOTM] Hologram has been created.</green>
botm.create.failed=<red>[BOTM] Could not create hologram. Perhaps there are not enough entries.</red>

0 comments on commit ca4dfa4

Please sign in to comment.