Skip to content

Commit

Permalink
1.0-SNAPSHOT
Browse files Browse the repository at this point in the history
  • Loading branch information
iInvisibilities authored May 14, 2022
0 parents commit 29b1ccd
Show file tree
Hide file tree
Showing 27 changed files with 810 additions and 0 deletions.
116 changes: 116 additions & 0 deletions src/main/java/me/invis/report/Report.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package me.invis.report;

import me.invis.report.command.MainCommand;
import me.invis.report.command.ReportsCommand;
import me.invis.report.config.messages.Messages;
import me.invis.report.config.messages.enums.MessageType;
import me.invis.report.config.settings.Settings;
import me.invis.report.inventory.ReportsInventory;
import me.invis.report.listener.PlayerJoin;
import me.invis.report.listener.PlayerLeave;
import me.invis.report.listener.SimplePrevent;
import me.invis.report.staff.StaffManager;
import me.invis.report.staff.listener.Join;
import me.invis.report.staff.listener.Leave;
import me.invis.report.util.YamlFile;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.inventory.Inventory;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.Arrays;
import java.util.stream.Collectors;

public final class Report extends JavaPlugin {

private static Report INSTANCE;
private static FileConfiguration CONFIG, REPORTS;
private static Inventory REPORTS_INVENTORY;

private Messages messages;
private Settings settings;

@Override
public void onEnable() {
// Self-identifying
INSTANCE = this;

// Creating files
saveDefaultConfig();
YamlFile reportsYamlFile = new YamlFile("reports.yml").create();

// File variables identification
CONFIG = getConfig();
REPORTS = reportsYamlFile.get();

// Initialization
initSettings();
initMessages();

// Reports inventory identification
REPORTS_INVENTORY = new ReportsInventory().getInventory();

// Events registering
Arrays.asList(
new PlayerJoin(), new PlayerLeave(),
new Join(), new Leave(),
new SimplePrevent()
)
.forEach(listener -> Bukkit.getPluginManager().registerEvents(listener, this));

// Commands registering
Arrays.asList
("report", "chatreport")
.forEach(commandName -> getCommand(commandName).setExecutor(new MainCommand()));

getCommand("reports").setExecutor(new ReportsCommand());

// Saving current staff members (only applicable if the server has been RELOADED, which is really discouraged!)
Bukkit.getOnlinePlayers().stream().filter(StaffManager::isStaff).collect(Collectors.toList()).forEach(StaffManager::addNewStaffMember);
}

@Override
public void onDisable() {
new YamlFile("reports.yml").save(Report.getCurrentReportsFile());
}

private void initMessages() {
ConfigurationSection messagesSection = getConfiguration().getConfigurationSection("MESSAGES");
this.messages = new Messages(
messagesSection.getKeys(false).stream().collect(Collectors.toMap(messageTypeName -> MessageType.valueOf(messageTypeName.replaceAll("-", "_")), messagesSection::getString, (a, b) -> b))
);
}

public Messages getMessages() {
return this.messages;
}

public void initSettings() {
this.settings = new Settings(
getConfiguration().getStringList("INGAME-REPORT"),
getConfiguration().getStringList("CHAT-REPORT"),
getConfiguration().getInt("REPORT-DELAY")
);
}

public Settings getSettings() {
return this.settings;
}

public static Report getInstance() {
return INSTANCE;
}

public static FileConfiguration getConfiguration() {
return CONFIG;
}

public static FileConfiguration getCurrentReportsFile() {
return REPORTS;
}

public static Inventory getReportsInventory() {
return REPORTS_INVENTORY;
}
}
57 changes: 57 additions & 0 deletions src/main/java/me/invis/report/ReportManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package me.invis.report;

import me.invis.report.inventory.ReportsInventory;
import me.invis.report.preset.Message;
import me.invis.report.preset.Setting;
import me.invis.report.staff.StaffManager;
import me.invis.report.util.ArrayUtil;
import me.invis.report.util.Cooldown;
import org.bukkit.entity.Player;

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

public class ReportManager {

public static boolean isReportValid(ReportType reportType, String report) {
for (String registeredReportString : (reportType == ReportType.INGAME ? Setting.INGAME_REPORT_TYPES : Setting.CHAT_REPORT_TYPES)) {
String[] reportStringParts = registeredReportString.split("\\|");
String reportName = reportStringParts[0];
String[] reportAliases = (reportStringParts.length >= 2 ? reportStringParts[1].split("-") : new String[0]);

if(report.equalsIgnoreCase(reportName) || ArrayUtil.contains(reportAliases, report)) return true;
}

return false;
}

public static void executeReport(ReportType reportType, String report, Player executor, Player reported) {
if(reported == null) {
executor.sendMessage(Message.REPORT_FAIL);
return;
}

if(!isReportValid(reportType, report)) {
executor.sendMessage(Message.REPORT_FAIL);
return;
}

if(Cooldown.isInCooldown(executor)) {
executor.sendMessage(Message.REPORT_COOLDOWN);
return;
}

executor.sendMessage(Message.REPORT_SUCCESS);
Cooldown.addPlayer(executor);

List<String> reportedPlayers = new ArrayList<>(Report.getCurrentReportsFile().getStringList(report));
reportedPlayers.add(reported.getUniqueId().toString());

Report.getCurrentReportsFile().set(report, reportedPlayers);

ReportsInventory.initItems();

StaffManager.getCurrentStaff().forEach(staffMember -> staffMember.sendMessage(Message.REPORT_RECEIVED));
}

}
39 changes: 39 additions & 0 deletions src/main/java/me/invis/report/ReportType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package me.invis.report;

import me.invis.report.preset.Setting;
import me.invis.report.util.ArrayUtil;

import java.util.List;

public enum ReportType {

CHAT,
INGAME;

public static ReportType getByReport(String report) {
List<String> reportTypesList = Setting.INGAME_REPORT_TYPES;

// First check...
if(checkReport(report, reportTypesList)) return ReportType.INGAME;
else {
// Second check...
reportTypesList = Setting.CHAT_REPORT_TYPES;
if(checkReport(report, reportTypesList)) return ReportType.CHAT;
}

return null;
}

private static boolean checkReport(String report, List<String> reportTypesStrings) {
for (String reportType : reportTypesStrings) {
String[] reportStringParts = reportType.split("\\|");
String reportName = reportStringParts[0];
String[] reportAliases = (reportStringParts.length >= 2 ? reportStringParts[1].split("-") : new String[0]);

if(report.equalsIgnoreCase(reportName) || ArrayUtil.contains(reportAliases, report)) return true;
}

return false;
}

}
27 changes: 27 additions & 0 deletions src/main/java/me/invis/report/command/MainCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package me.invis.report.command;

import me.invis.report.ReportManager;
import me.invis.report.ReportType;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class MainCommand implements CommandExecutor {

@Override
public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) {
if(!(commandSender instanceof Player) || args.length < 2) return true;

ReportType reportType = ReportType.valueOf((command.getName().equalsIgnoreCase("chatreport") ? "CHAT" : "INGAME"));
Player executor = (Player) commandSender;
Player reported = Bukkit.getPlayer(args[0]);
String report = args[1];

ReportManager.executeReport(reportType, report, executor, reported);

return true;
}

}
45 changes: 45 additions & 0 deletions src/main/java/me/invis/report/command/ReportsCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package me.invis.report.command;

import me.invis.report.Report;
import me.invis.report.inventory.ReportsInventory;
import me.invis.report.util.ListUtil;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;

import java.util.ArrayList;

public class ReportsCommand implements CommandExecutor {

private final FileConfiguration reportsFile = Report.getCurrentReportsFile();

@Override
public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) {

if(args.length <= 1) {
if(!(commandSender instanceof Player)) return true;
((Player) commandSender).openInventory(Report.getReportsInventory());
}

else {
if(args[0].equalsIgnoreCase("clear")) {
String playerName = args[1];
OfflinePlayer player = Bukkit.getOfflinePlayer(playerName);
String playerUUIDString = player.getUniqueId().toString();

reportsFile.getKeys(false).forEach(report -> reportsFile.set(report, ListUtil.removeAll(new ArrayList<>(reportsFile.getStringList(report)), playerUUIDString)));
ReportsInventory.initItems();

commandSender.sendMessage(ChatColor.GREEN + "Cleared all reports for " + playerName + "(" + playerUUIDString + ")!");
}
}

return true;
}

}
21 changes: 21 additions & 0 deletions src/main/java/me/invis/report/config/messages/Messages.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package me.invis.report.config.messages;

import me.invis.report.config.messages.enums.MessageType;
import me.invis.report.util.CC;

import java.util.Map;

public class Messages {

private final Map<MessageType, String> messages;

public Messages(Map<MessageType, String> messages) {
messages.forEach((messageType, message) -> messages.put(messageType, CC.translateColors(message)));
this.messages = messages;
}

public Map<MessageType, String> getValues() {
return this.messages;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package me.invis.report.config.messages.enums;

public enum MessageType {

REPORT_SUCCESS,
REPORT_FAIL,
REPORT_COOLDOWN,
REPORT_RECEIVED

}
29 changes: 29 additions & 0 deletions src/main/java/me/invis/report/config/settings/Settings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package me.invis.report.config.settings;

import java.util.List;

public class Settings {

private final List<String> ingameReportTypes, chatReportTypes;
private final int reportDelay;

public Settings(List<String> ingameReportTypes, List<String> chatReportTypes, int reportDelay) {
this.ingameReportTypes = ingameReportTypes;
this.chatReportTypes = chatReportTypes;

this.reportDelay = reportDelay;
}

public List<String> getIngameReportTypes() {
return this.ingameReportTypes;
}

public List<String> getChatReportTypes() {
return this.chatReportTypes;
}

public int getReportDelay() {
return this.reportDelay;
}

}
24 changes: 24 additions & 0 deletions src/main/java/me/invis/report/event/StaffJoinEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package me.invis.report.event;

import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;

public class StaffJoinEvent extends PlayerEvent {

private static final HandlerList HANDLERS_LIST = new HandlerList();

public StaffJoinEvent(Player player) {
super(player);
}

@Override
public HandlerList getHandlers() {
return getHandlerList();
}

public static HandlerList getHandlerList() {
return HANDLERS_LIST;
}

}
Loading

0 comments on commit 29b1ccd

Please sign in to comment.