Skip to content

Commit

Permalink
Redo most of the plugin (#8)
Browse files Browse the repository at this point in the history
* Main changes

* Update README and also update the version checker

Due to how the plugin reads the config, it falls back onto the default value if the value is not present. To get around this issue, I made slot #0 the slot that can't be removed. This can reduce issues due to it being a single slot and can simply have it's material set to 'air'.
  • Loading branch information
7man7LMYT authored Jul 31, 2022
1 parent 5e29190 commit 446b0e2
Show file tree
Hide file tree
Showing 15 changed files with 468 additions and 227 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
.project
.classpath
/out/
dependency-reduced-pom.xml
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# CustomStats
[![discord](https://discord.com/api/guilds/339280188926066689/embed.png)](https://discord.gg/Z5MyDwp) [![](https://img.shields.io/badge/contributions-welcome-brightgreen)](https://github.com/7man7LMYT/CustomStats)[![HitCount](http://hits.dwyl.com/7man7LMYT/CustomStats.svg)](http://hits.dwyl.com/7man7LMYT/CustomStats)

bStats: https://bstats.org/plugin/bukkit/CustomStats/10123
[![Discord Server](https://discord.com/api/guilds/339280188926066689/embed.png)](https://discord.gg/Z5MyDwp)
[![](https://img.shields.io/badge/contributions-welcome-brightgreen)](https://github.com/7man7LMYT/CustomStats)
[![HitCount](http://hits.dwyl.com/7man7LMYT/CustomStats.svg)](http://hits.dwyl.com/7man7LMYT/CustomStats)
[![Spigot Version](https://img.shields.io/spiget/version/88300?label=CustomStats&color=red)](https://www.spigotmc.org/resources/88300/)
[![CustomStats Wiki](https://img.shields.io/badge/CustomStats-wiki-blue)](https://7man7lmyt.github.io/CustomStats/)

Spigot Link: https://www.spigotmc.org/resources/customstats.88300/
A Minecraft anarchy server oriented plugin that adds /stats.

An anarchy server oriented plugin that adds /stats.
Check out our [bStats page](https://bstats.org/plugin/bukkit/CustomStats/10123)!
135 changes: 83 additions & 52 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,93 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<modelVersion>4.0.0</modelVersion>

<groupId>me.stats</groupId>
<artifactId>CustomStats</artifactId>
<version>1.2.1</version>
<packaging>jar</packaging>
<groupId>me.gamersclub.customstats</groupId>
<artifactId>CustomStats</artifactId>
<version>2.0.0</version>
<packaging>jar</packaging>

<name>CustomStats</name>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

<description>An anarchy server oriented plugin that adds /stats.</description>
<name>CustomStats</name>
<description>An anarchy server oriented plugin that adds /stats.</description>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<relocations>
<relocation>
<pattern>org.bstats</pattern>
<!-- Replace this with your package! -->
<shadedPattern>me.gamersclub.customstats.bStats</shadedPattern>
</relocation>
</relocations>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>spigotmc-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
</repositories>
<repositories>
<repository>
<id>spigotmc-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>placeholderapi</id>
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
</repository>
<repository>
<id>opencollab-snapshot</id>
<url>https://repo.opencollab.dev/maven-snapshots/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.16.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.19.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>23.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.geysermc.floodgate</groupId>
<artifactId>api</artifactId>
<version>2.2.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.11.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.bstats</groupId>
<artifactId>bstats-bukkit</artifactId>
<version>3.0.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
57 changes: 57 additions & 0 deletions src/main/java/me/gamersclub/customstats/CustomStats.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package me.gamersclub.customstats;

import me.gamersclub.customstats.bStats.Metrics;
import me.gamersclub.customstats.util.UpdateChecker;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.logging.Logger;

public class CustomStats extends JavaPlugin {
public final Logger log = this.getLogger();
public boolean floodgateIsInstalled = false;
public boolean placeholderAPIInstalled = false;
@SuppressWarnings("CanBeFinal")
List<String> uuidsWithGuiOpen = new ArrayList<>();

public void onEnable() {
log.info("CustomStats v2.0.0");

File configFile = new File(getDataFolder(), "config.yml");
if (!configFile.exists()) {
saveResource("config.yml", false);
}

floodgateIsInstalled = this.getServer().getPluginManager().isPluginEnabled("floodgate");
if (floodgateIsInstalled) {
log.info("Floodgate detected!");
}

placeholderAPIInstalled = this.getServer().getPluginManager().isPluginEnabled("PlaceholderAPI");
if (placeholderAPIInstalled) {
log.info("PlaceholderAPI detected!");
}

log.info("Registering commands/events..");
Objects.requireNonNull(getCommand("stats")).setExecutor(new StatsCommand(this));

this.getServer().getPluginManager().registerEvents(new GuiListener(this), this);

log.info("Starting Metrics..");
Metrics metrics = new Metrics(this, 10123);

log.info("Checking for a newer version..");
new UpdateChecker(this, 88300).getVersion(version -> {
if (this.getDescription().getVersion().equalsIgnoreCase(version)) {
log.info("You are up to date! (v2.0.0)");
} else {
log.warning("There is a new update available! \nDownload it at https://www.spigotmc.org/resources/88300/");
}
});

log.info("Successfully started!");
}
}
26 changes: 26 additions & 0 deletions src/main/java/me/gamersclub/customstats/GuiListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package me.gamersclub.customstats;

import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.jetbrains.annotations.NotNull;

public class GuiListener implements Listener {
public GuiListener(CustomStats customStats) {
this.customStats = customStats;
}
final CustomStats customStats;

@EventHandler
public void onInventoryClick (InventoryClickEvent event){
if (customStats.uuidsWithGuiOpen.contains(event.getWhoClicked().getUniqueId().toString())) {
event.setCancelled(true);
}
}

@EventHandler
public void onInventoryClose (@NotNull InventoryCloseEvent event){
customStats.uuidsWithGuiOpen.remove(event.getPlayer().getUniqueId().toString());
}
}
142 changes: 142 additions & 0 deletions src/main/java/me/gamersclub/customstats/StatsCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package me.gamersclub.customstats;

import me.gamersclub.customstats.util.PlaceholderManager;
import me.gamersclub.customstats.menus.StatForm;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.*;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.geysermc.floodgate.api.FloodgateApi;
import org.jetbrains.annotations.NotNull;

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

public class StatsCommand implements CommandExecutor, TabExecutor {
private final boolean floodgate;
final CustomStats customStats;
final PlaceholderManager placeholderManager;
@SuppressWarnings("CanBeFinal")
List<Integer> valid = new ArrayList<>();

public StatsCommand(CustomStats customStats) {
this.customStats = customStats;
this.floodgate = customStats.floodgateIsInstalled;
this.placeholderManager = new PlaceholderManager(customStats);

valid.add(9);
valid.add(18);
valid.add(27);
valid.add(36);
valid.add(45);
valid.add(54);
}

@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String alias, String @NotNull [] args) {
if (args.length == 0) {
if (sender instanceof ConsoleCommandSender) {
List<String> statCommand = customStats.getConfig().getStringList("stats.stat-command");

//color code is &
for (String s : statCommand) {
sender.sendMessage(ChatColor.translateAlternateColorCodes(('&'), placeholderManager.placeholderReplacer(null, s)));
}
} else {
List<String> statCommand = customStats.getConfig().getStringList("stats.stat-command");

//color code is &
for (String s : statCommand) {
sender.sendMessage(ChatColor.translateAlternateColorCodes(('&'), placeholderManager.placeholderReplacer((Player) sender, s)));
}
}

} else {
if (args[0].equalsIgnoreCase("reload")) {
if (sender.hasPermission("customstats.reload")) {
customStats.reloadConfig();
customStats.getConfig();
sender.sendMessage(ChatColor.GREEN + "Configuration reloaded!");
} else {
sender.sendMessage(ChatColor.RED + "Usage: /stats (menu)");
}
} else if (args[0].equalsIgnoreCase("menu")) {
if (sender instanceof Player) {
Player player = (Player) sender;

if (floodgate) {
if (FloodgateApi.getInstance().isFloodgatePlayer(player.getUniqueId())) {
StatForm statForm = new StatForm(customStats, placeholderManager, player);
statForm.sendStatForm(player.getUniqueId());
return true;
}
}

String title = Objects.requireNonNull(customStats.getConfig().getString("stats.stat-menu.title"));
int size = customStats.getConfig().getInt("stats.stat-menu.menu-size");
if (!valid.contains(size)) {
customStats.log.warning(size + " is not a valid multiple of 9!");
sender.sendMessage(ChatColor.RED + "An internal issue occurred while running this command.");
return true;
}

Inventory inventory = Bukkit.createInventory(player, size, ChatColor.translateAlternateColorCodes('&', title));

for (int i = 0; i < size; i++) {
if (customStats.getConfig().contains("stats.stat-menu.items." + i)) {
List<String> statsGui = customStats.getConfig().getStringList("stats.stat-menu.items." + i);

ItemStack item = new ItemStack(Material.valueOf(statsGui.get(0).toUpperCase()), 1);

//set item to name if not air
if (item.getType() != Material.AIR) {
ItemMeta itemMeta = item.getItemMeta();
String name = placeholderManager.placeholderReplacer(player, statsGui.get(1));

assert itemMeta != null;
itemMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', name));

if (statsGui.size() > 2) {
List<String> lore = new ArrayList<>();
for (int l = 2; l < statsGui.size(); l++) {
lore.add(ChatColor.translateAlternateColorCodes('&',placeholderManager.placeholderReplacer(player, statsGui.get(l))));
}
itemMeta.setLore(lore);
}

item.setItemMeta(itemMeta);
}
inventory.setItem(i, item);
} else {
inventory.clear(i);
}
}

player.openInventory(inventory);
customStats.uuidsWithGuiOpen.add(player.getUniqueId().toString());
} else {
sender.sendMessage(ChatColor.RED + "You must be a player to use this command!");
}
} else {
sender.sendMessage(ChatColor.RED + "Usage: /stats (menu)");
}
}
return true;
}

@Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String s, String[] strings) {
List<String> completions = new ArrayList<>();
completions.add("menu");

if (sender.hasPermission("customstats.reload")) {
completions.add("reload");
}
return completions;
}
}
Loading

0 comments on commit 446b0e2

Please sign in to comment.