-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from BTE-Germany/BOTM
BOTM
- Loading branch information
Showing
7 changed files
with
241 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
src/main/java/dev/nachwahl/lobby/commands/BOTMCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters