Skip to content

Commit

Permalink
Merge pull request #6 from ModularSoftAU/holograms
Browse files Browse the repository at this point in the history
Implementation of Holograms and product documentation.
  • Loading branch information
benrobson authored Apr 16, 2024
2 parents c3ac495 + f3379ab commit cfa977a
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 37 deletions.
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,34 @@
# MobHunt
# Mob Hunt

## Installation
* Clone this repo.
* Configure your config.
* Run the dbinit.
* Download the latest version of Mob Hunt from our [releases page](https://github.com/ModularSoftAU/MobHunt/releases/)
* Drop the Mob Hunt jar file into your plugins' folder.
* Set all of your point allocations per mob in the `config.yml` under `MobHunt.Points` and define your kill cap under `MobHunt.KillCap`

## Requirements
* MySQL Database.

## Soft Dependencies
- [Optional] `DecentHolograms` if you would like to have a holographic live update leaderboard.

## Gameplay
When a player kills a mob it is logged into a database and a number incremented in their name.
Most amount and unique amount of mobs that a player kills, wins.

#### Milestones
When a player kills over an X amount of mobs, a message will broadcast to all online players that they have reached a milestone.
The milestones can be changed and modified in the `config.yml` under `Milestones.Messages` and you can change them based on Major and Minor.

### Holographic Leaderboard


## Commands
| Command | Description | Permission |
|-----------------------|----------------------------------------------------------|-----------------|
| /mobstats | Shows a breakdown of the mobs you killed | |
| /mobclear | Clear all mobs from yourself or another player. | `mobhunt.admin` |
| /mobleaderboard [mob] | Show the Top 5 Mob Hunters overall or of a specific mob. | |
| /mobhelp | Helps the player know how to play Mob Hunt. | |
17 changes: 17 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@
<id>papermc</id>
<url>https://papermc.io/repo/repository/maven-public/</url>
</repository>

<repository>
<id>jitpack</id>
<url>https://jitpack.io/</url>
</repository>

<repository>
<id>codemc</id>
<url>https://repo.codemc.io/repository/maven-public/</url>
</repository>
</repositories>

<dependencies>
Expand Down Expand Up @@ -68,6 +78,13 @@
<version>1.18.22</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>
43 changes: 43 additions & 0 deletions src/main/java/org/modularsoft/MobHunt/HologramController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.modularsoft.MobHunt;

import eu.decentsoftware.holograms.api.DHAPI;
import eu.decentsoftware.holograms.api.holograms.Hologram;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;

import java.util.List;

public class HologramController {
private final MobHuntMain plugin;
private final HunterController hunterController;

public HologramController(MobHuntMain plugin, HunterController hunterController) {
this.plugin = plugin;
this.hunterController = hunterController;
}

public void reloadHunterLeaderboard() {
Hologram leaderboardHologram = DHAPI.getHologram("mh_leaderboard");
if (leaderboardHologram == null) {
double x = plugin.config().getHologramLocationX();
double y = plugin.config().getHologramLocationY();
double z = plugin.config().getHologramLocationZ();
String worldName = plugin.config().getHologramLocationWorld();

World world = Bukkit.getWorld(worldName);
if (world == null) {
plugin.getLogger().warning("World '" + worldName + "' not found.");
return;
}

Location location = new Location(world, x, y, z);
leaderboardHologram = DHAPI.createHologram("mh_leaderboard", location, false);
}

List<MobHuntQuery.MobHunter> bestHunters = MobHuntQuery.getBestHunters(
plugin, null, 10);
List<String> lines = hunterController.getLeaderboardText(bestHunters);
DHAPI.setHologramLines(leaderboardHologram, lines);
}
}
38 changes: 18 additions & 20 deletions src/main/java/org/modularsoft/MobHunt/HunterController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.List;

public class HunterController {
Expand Down Expand Up @@ -56,13 +57,13 @@ public void mobStatsResponse(Player player, List<MobHuntQuery.MobStat> stats) {
String title = plugin.config().getLangStatsTitle()
.replace("%Player%", player.getName());

sendMessageInCentre(player, title, centrePixel);
player.sendMessage(centreMessage(title, centrePixel));

for (MobHuntQuery.MobStat stat : stats) {
String statMessage = plugin.config().getLangStatsFormat()
.replace("%MobType%", stat.mobType())
.replace("%Kills%", "" + stat.mobsKilled());
sendMessageInCentre(player, statMessage, centrePixel);
player.sendMessage(centreMessage(statMessage, centrePixel));
}
}

Expand Down Expand Up @@ -160,7 +161,7 @@ private static String rankToOrdinal(int rank) {
* @param message The message to measure its length.
* @return The length of the message in pixels (" " characters).
*/
private static int minecraftMessageLengthInPixels(String message) {
public static int minecraftMessageLengthInPixels(String message) {
if (message == null || message.equals(""))
return 0;

Expand Down Expand Up @@ -202,46 +203,42 @@ private static String getPixelPadding(int pixels) {
/**
* From: https://www.spigotmc.org/threads/free-code-sending-perfectly-centered-chat-message.95872/
* Sends the message to the chat such that it is in the middle of the chat box.
* @param player The player sending the message.
* @param message The message to send.
* @param centrePixel The pixel to treat as the centre of the message.
*/
private static void sendMessageInCentre(Player player, String message, int centrePixel) {
public static String centreMessage(String message, int centrePixel) {
int messagePxSize = minecraftMessageLengthInPixels(message);
int halvedMessageSize = messagePxSize / 2;
int toCompensate = centrePixel - halvedMessageSize;
player.sendMessage(getPixelPadding(toCompensate) + message);
return getPixelPadding(toCompensate) + message;
}

/**
* For when a player wants to see who the best hunters are.
* @param player The player who requested the leaderboard.
* @param bestHunters A (presorted) list of stats to show on the leaderboard.
*/
public void showLeaderBoardResponse(Player player, List<MobHuntQuery.MobHunter> bestHunters) {
showLeaderBoardResponse(player, bestHunters, null);
public List<String> getLeaderboardText(List<MobHuntQuery.MobHunter> bestHunters) {
return getLeaderboardText(bestHunters, null);
}

/**
* When you want to see the leaderboard with a heading of a specific mob.
* @param player The player who requested the leaderboard.
* @param bestHunters A (presorted) list of stats to show on the leaderboard.
* @param title The title to give the leaderboard.
*/
public void showLeaderBoardResponse(Player player, List<MobHuntQuery.MobHunter> bestHunters, String title) {
int centrePixel = minecraftMessageLengthInPixels(
plugin.config().getLangLeaderboardHeader()) / 2;
public List<String> getLeaderboardText(List<MobHuntQuery.MobHunter> bestHunters, String title) {
List<String> lines = new ArrayList<>();

// Show the header first
sendMessageInCentre(player, plugin.config().getLangLeaderboardHeader(), centrePixel);
player.sendMessage("");
lines.add(plugin.config().getLangLeaderboardHeader());
lines.add("");

if (title != null)
sendMessageInCentre(player, title, centrePixel);
lines.add(title);

if (bestHunters.size() == 0) {
// If there are no hunters we should probably tell the player
sendMessageInCentre(player, plugin.config().getLangLeaderboardNoMobsKilled(), centrePixel);
lines.add(plugin.config().getLangLeaderboardNoMobsKilled());
} else {
for (int i = 0; i < bestHunters.size(); i++) {
MobHuntQuery.MobHunter hunter = bestHunters.get(i);
Expand All @@ -265,11 +262,12 @@ public void showLeaderBoardResponse(Player player, List<MobHuntQuery.MobHunter>
.replace("%Ranking%", rankToOrdinal(rank))
.replace("%Player%", hunter.name())
.replace("%Points%", "" + hunter.points());
sendMessageInCentre(player, rankingMessage, centrePixel);
lines.add(rankingMessage);
}
}

player.sendMessage("");
sendMessageInCentre(player, plugin.config().getLangLeaderboardHeader(), centrePixel);
lines.add("");
lines.add(plugin.config().getLangLeaderboardHeader());
return lines;
}
}
16 changes: 15 additions & 1 deletion src/main/java/org/modularsoft/MobHunt/MobHuntMain.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.modularsoft.MobHunt;

import org.bukkit.scheduler.BukkitScheduler;
import org.modularsoft.MobHunt.commands.mobclear;
import org.modularsoft.MobHunt.commands.mobhelp;
import org.modularsoft.MobHunt.commands.mobstats;
Expand Down Expand Up @@ -33,8 +34,15 @@ public void onEnable() {
config = new PluginConfig(this);
console = getServer().getConsoleSender();

// Check if DecentHolograms enabled.
if (!getServer().getPluginManager().isPluginEnabled("DecentHolograms")) {
getLogger().severe("DecentHolograms plugin is not enabled, hologram features will not work.");
return;
}

HunterController hunterController = new HunterController(this);
ScoreboardController scoreboardController = new ScoreboardController(this);
HologramController hologramController = new HologramController(this, hunterController);

// Connect to the database
establishConnection();
Expand All @@ -53,9 +61,15 @@ public void onEnable() {
if (config.isFeatureOnEnableConsoleMessageEnabled()) {
console.sendMessage(ChatColor.GREEN + getDescription().getName() + " is now enabled.");
console.sendMessage(ChatColor.GREEN + "Running Version: " + getDescription().getVersion());
console.sendMessage(ChatColor.GREEN + "GitHub Repository: https://github.com/ModularEnigma/MobHunt");
console.sendMessage(ChatColor.GREEN + "GitHub Repository: https://github.com/ModularSoftAU/MobHunt");
console.sendMessage(ChatColor.GREEN + "Created By: " + getDescription().getAuthors());
}

// Create hologram if it doesn't exist.
hologramController.reloadHunterLeaderboard();

BukkitScheduler scheduler = getServer().getScheduler();
scheduler.scheduleSyncRepeatingTask(this, hologramController::reloadHunterLeaderboard, 0L, 20L * 10);
}

@Override
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/org/modularsoft/MobHunt/MobHuntQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import javax.annotation.Nullable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
Expand Down Expand Up @@ -230,7 +231,7 @@ public static boolean addNewHunter(MobHuntMain plugin, Player player) {
* @param topHunters The limit of the size of the hunters list.
* @return Returns a list of the Best Hunters. Index 0 is the best player and so on...
*/
public static List<MobHunter> getBestHunters(MobHuntMain plugin, Player player, int topHunters) {
public static List<MobHunter> getBestHunters(MobHuntMain plugin, @Nullable Player player, int topHunters) {
List<MobHunter> bestHunters = new ArrayList<>();

try {
Expand All @@ -248,7 +249,9 @@ public static List<MobHunter> getBestHunters(MobHuntMain plugin, Player player,
}
} catch (SQLException e) {
e.printStackTrace();
player.sendMessage(plugin.config().getLangDatabaseConnectionError());
if (player != null) {
player.sendMessage(plugin.config().getLangDatabaseConnectionError());
}
}
return bestHunters;
}
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/modularsoft/MobHunt/PluginConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
public class PluginConfig {
private final MobHuntMain plugin;
private FileConfiguration config;
private HologramController hologramController;

@Getter private String databaseHost;
@Getter private int databasePort;
Expand All @@ -20,6 +21,11 @@ public class PluginConfig {

@Getter private String adminRole;

@Getter private String hologramLocationWorld;
@Getter private int hologramLocationX;
@Getter private int hologramLocationY;
@Getter private int hologramLocationZ;

@Getter private boolean featureOnEnableConsoleMessageEnabled;
@Getter private boolean featureOnDisableConsoleMessageEnabled;
@Getter private boolean featureOnNewHunterConsoleMessageEnabled;
Expand Down Expand Up @@ -76,6 +82,12 @@ public void reloadConfig() {

adminRole = config.getString("AdminRole");

hologramLocationWorld = config.getString("Hologram.LocationWorld");
hologramLocationX = config.getInt("Hologram.LocationX");
hologramLocationY = config.getInt("Hologram.LocationY");
hologramLocationZ = config.getInt("Hologram.LocationZ");


featureOnEnableConsoleMessageEnabled = config.getBoolean("Features.OnEnabledConsoleMessage");
featureOnDisableConsoleMessageEnabled = config.getBoolean("Features.OnDisabledConsoleMessage");
featureOnNewHunterConsoleMessageEnabled = config.getBoolean("Features.OnNewHunterConsoleMessage");
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/org/modularsoft/MobHunt/commands/mobleaderboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N

int showPlayers = plugin.config().getLeaderboardShowPlayers();

List<String> leaderboardText;
// If another argument is present, then we assume this command is asking for the
// leaderboard of a specific Mob Type.
if (args.length > 0) {
Expand All @@ -50,13 +51,20 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N
// Show the leaderboard with a specific title.
String leaderboardTitle = plugin.config().getLangLeaderboardMobTitleFormat()
.replace("%MobType%", mobType);
hunterController.showLeaderBoardResponse(player, bestHunters, leaderboardTitle);
leaderboardText = hunterController.getLeaderboardText(bestHunters, leaderboardTitle);
} else {
// This is for seeing the overall leaderboard for points.
List<MobHuntQuery.MobHunter> bestHunters = MobHuntQuery.getBestHunters(
plugin, player, showPlayers);
hunterController.showLeaderBoardResponse(player, bestHunters);
leaderboardText = hunterController.getLeaderboardText(bestHunters);
}

int centrePixel = HunterController.minecraftMessageLengthInPixels(
plugin.config().getLangLeaderboardHeader()) / 2;
for (String line: leaderboardText) {
player.sendMessage(HunterController.centreMessage(line, centrePixel));
}

return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package org.modularsoft.MobHunt.events;

import org.modularsoft.MobHunt.MobHuntMain;
import org.modularsoft.MobHunt.HunterController;
import org.modularsoft.MobHunt.MobHuntQuery;
import org.modularsoft.MobHunt.ScoreboardController;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.modularsoft.MobHunt.*;

public class OnHunterJoin implements Listener {
private final MobHuntMain plugin;
Expand Down Expand Up @@ -37,4 +34,4 @@ public void onPlayerJoin(PlayerJoinEvent event) {
hunterController.newPlayerJoinsTheHunt(player);
}
}
}
}
Loading

0 comments on commit cfa977a

Please sign in to comment.