Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,18 @@ repositories {
maven {
url = uri("https://repo.glaremasters.me/repository/towny/")
}
maven {
name = "papermc"
url = uri("https://repo.papermc.io/repository/maven-public/")
}
}

dependencies {
api("org.bstats:bstats-bukkit:3.1.0")
api("com.jeff_media:SpigotUpdateChecker:3.0.4")
api("de.tr7zw:item-nbt-api:2.15.0")
api("cn.handyplus.lib.adapter:FoliaLib:1.2.0")
compileOnly("org.spigotmc:spigot-api:1.20.5-R0.1-SNAPSHOT")
compileOnly("io.papermc.paper:paper-api:1.21.5-R0.1-SNAPSHOT")
compileOnly("io.lumine:Mythic-Dist:5.9.0")
compileOnly("com.sk89q.worldguard:worldguard-bukkit:7.0.13")
compileOnly("com.github.TechFortress:GriefPrevention:17.0.0")
Expand All @@ -87,7 +91,7 @@ tasks.processResources {
val props = mapOf("version" to version)
inputs.properties(props)
filteringCharset = "UTF-8"
filesMatching("plugin.yml") {
filesMatching("*plugin.yml") {
expand(props)
}
}
Expand Down Expand Up @@ -117,7 +121,7 @@ tasks.jar {
}

tasks.runServer {
minecraftVersion("1.21.4")
minecraftVersion("1.21.5")
}

runPaper.folia.registerTask()
35 changes: 19 additions & 16 deletions src/main/java/com/github/nutt1101/CatchBall.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
package com.github.nutt1101;

import cn.handyplus.lib.adapter.HandySchedulerUtil;
import com.github.nutt1101.command.Command;
import com.github.nutt1101.command.TabComplete;
import com.github.nutt1101.command.BrigadierCommandHandler;
import com.github.nutt1101.event.*;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bstats.bukkit.Metrics;
import com.jeff_media.updatechecker.UpdateCheckSource;
import com.jeff_media.updatechecker.UpdateChecker;
import org.bukkit.ChatColor;
import org.bukkit.command.PluginCommand;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.logging.Level;

public class CatchBall extends JavaPlugin {
private FileConfiguration config = this.getConfig();
{
this.getConfig();
}

public static Plugin plugin;

private Metrics metrics;

private void checkPluginHook(String pluginName) {
if (this.getServer().getPluginManager().getPlugin(pluginName) != null) {
plugin.getLogger().log(Level.INFO, ChatColor.GREEN + pluginName + " Hook!");
plugin.getLogger().log(Level.INFO, NamedTextColor.GREEN + pluginName + " Hook!");
}
}

Expand All @@ -36,10 +35,12 @@ public void onEnable() {

ConfigSetting.checkConfig();

Metrics metrics = new Metrics(this, 12380);
// Initialize metrics
this.metrics = new Metrics(this, 12380);

registerEvent();
registerCommand();
// Remove legacy command registration for Paper plugins
// Commands are now registered via Brigadier in registerBrigadierCommands()

new UpdateChecker(this, UpdateCheckSource.GITHUB_RELEASE_TAG, "MagicTeaMC/CatchBall2")
.checkEveryXHours(1) // Check every hour
Expand All @@ -60,6 +61,8 @@ public void onEnable() {

HandySchedulerUtil.init(this);

// Register Brigadier commands
new BrigadierCommandHandler(this).registerCommands();
}

@Override
Expand All @@ -86,17 +89,17 @@ public void registerEvent() {
registerEvent.registerEvents(new GUIClick(), this);
}

// register command
/*
// register command (not used in Paper plugins - kept for reference)
// Paper plugins use Brigadier commands instead
public void registerCommand() {
// This method is not called in Paper plugins
// Commands are registered via registerBrigadierCommands() instead
PluginCommand ctbCommand = this.getCommand("ctb");
if (ctbCommand != null) {
ctbCommand.setExecutor(new Command());
ctbCommand.setTabCompleter(new TabComplete());
}
}

public static String getServerVersion() {
return plugin.getServer().getBukkitVersion();
}

}
*/
}
86 changes: 34 additions & 52 deletions src/main/java/com/github/nutt1101/ConfigSetting.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import com.github.nutt1101.Recipe.BallRecipe;
import com.github.nutt1101.utils.TranslationFileReader;
import me.ryanhamshire.GriefPrevention.ClaimPermission;
import org.bukkit.ChatColor;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
Expand All @@ -16,7 +17,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.logging.Level;
Expand Down Expand Up @@ -90,26 +90,26 @@ public static void checkConfig() {
DropEnable = !config.isSet("DropEnable") || config.getBoolean("DropEnable");
DropNeedPermission = config.isSet("DropNeedPermission") && config.getBoolean("DropNeedPermission");
DropItemChance = config.isSet("DropItemChance")
? Integer.parseInt(config.getString("DropItemChance").replace("%", ""))
? Integer.parseInt(Objects.requireNonNull(config.getString("DropItemChance")).replace("%", ""))
: 50;
try {
DropItemMaterial = config.isSet("DropItemMaterial") ? Material.matchMaterial(Objects.requireNonNull(config.getString("DropItemMaterial"))) : Material.EGG;
} catch (IllegalArgumentException e) {
plugin.getLogger().log(Level.WARNING, ChatColor.RED + "Invalid DropItemMaterial in config.yml, using default 'EGG' material.");
plugin.getLogger().log(Level.WARNING, "Invalid DropItemMaterial in config.yml, using default 'EGG' material.");
DropItemMaterial = Material.EGG;
}

try {
DropMethod = config.isSet("DropMethod") ? DropMethodType.valueOf(config.getString("DropMethod").toUpperCase()) : DropMethodType.CHICKEN;
DropMethod = config.isSet("DropMethod") ? DropMethodType.valueOf(Objects.requireNonNull(config.getString("DropMethod")).toUpperCase()) : DropMethodType.CHICKEN;
} catch (IllegalArgumentException e) {
plugin.getLogger().log(Level.WARNING, ChatColor.RED + "Invalid DropMethod in config.yml, using default 'CHICKEN' method.");
plugin.getLogger().log(Level.WARNING, "Invalid DropMethod in config.yml, using default 'CHICKEN' method.");
DropMethod = DropMethodType.CHICKEN;
}

// Store as string instead of EntityType
DropEntityType = config.isSet("DropEntityType") ? config.getString("DropEntityType").toUpperCase() : "CHICKEN";
DropEntityType = config.isSet("DropEntityType") ? Objects.requireNonNull(config.getString("DropEntityType")).toUpperCase() : "CHICKEN";
if (!isValidEntityType(DropEntityType)) {
plugin.getLogger().log(Level.WARNING, ChatColor.RED + "Invalid DropEntityType in config.yml, using default 'CHICKEN'.");
plugin.getLogger().log(Level.WARNING, "Invalid DropEntityType in config.yml, using default 'CHICKEN'.");
DropEntityType = "CHICKEN";
}

Expand All @@ -119,16 +119,16 @@ public static void checkConfig() {

entityFile = YamlConfiguration.loadConfiguration(new File(plugin.getDataFolder(), "entity.yml"));

catchSuccessSound = config.isSet("CatchSuccessSound") ? config.getString("CatchSuccessSound").toUpperCase()
catchSuccessSound = config.isSet("CatchSuccessSound") ? Objects.requireNonNull(config.getString("CatchSuccessSound")).toUpperCase()
: "ENTITY_ARROW_HIT_PLAYER".toUpperCase();

recipeEnabled = !config.isSet("Recipe.enabled") || config.getBoolean("Recipe.enabled");

residenceFlag = config.isSet("ResidenceFlag") ? config.getStringList("ResidenceFlag")
: Arrays.asList("animalkilling");
: List.of("animalkilling");

griefPreventionFlag = config.isSet("GriefPreventionFlag") ? config.getStringList("GriefPreventionFlag")
: Arrays.asList("Access");
: List.of("Access");
allowCatchableTamedOwnerIsNull = !config.isSet("AllowCatchableTamedOwnerIsNull")
|| config.getBoolean("AllowCatchableTamedOwnerIsNull");
ShowParticles = !config.isSet("ShowParticles")
Expand All @@ -145,11 +145,11 @@ public static void checkConfig() {
try {
TranslationFileReader.init();
} catch (IOException e) {
e.printStackTrace();
plugin.getLogger().warning("An error caused " + e.getMessage());
plugin.getLogger().log(Level.WARNING,
ChatColor.RED + String.format("The locale you have selected '%s' is currently not supported",
String.format("The locale you have selected '%s' is currently not supported",
locale));
plugin.getLogger().log(Level.WARNING, ChatColor.RED + "We only support: en, zh_tw");
plugin.getLogger().log(Level.WARNING, "We only support: en, zh_tw");
locale = "en";
}

Expand All @@ -161,14 +161,14 @@ public static void checkConfig() {

try {
if (plugin.getServer().getPluginManager().getPlugin("Residence") != null) {
residenceFlag = residenceFlag.stream().map(flag -> Flags.valueOf(flag)).map(Flags::name)
residenceFlag = residenceFlag.stream().map(Flags::valueOf).map(Flags::name)
.collect(Collectors.toList());
}

} catch (IllegalArgumentException e) {
plugin.getLogger().log(Level.WARNING, ChatColor.RED + e.getMessage());
plugin.getLogger().log(Level.WARNING, ChatColor.RED + "Unknown Residence flag!");
plugin.getLogger().log(Level.WARNING, ChatColor.RED + "Please check your config setting!");
plugin.getLogger().log(Level.WARNING, e.getMessage());
plugin.getLogger().log(Level.WARNING, "Unknown Residence flag!");
plugin.getLogger().log(Level.WARNING, "Please check your config setting!");
residenceFlag.clear();
residenceFlag.add("animalkilling");
}
Expand All @@ -178,15 +178,15 @@ public static void checkConfig() {
griefPreventionFlag = griefPreventionFlag.stream()
.map(flag -> flag.substring(0, 1).toUpperCase() + flag.substring(1))
.map(String::toUpperCase).collect(Collectors.toList());
for (int i = 0; i < griefPreventionFlag.size(); i++) {
ClaimPermission.valueOf(griefPreventionFlag.get(i));
for (String s : griefPreventionFlag) {
ClaimPermission.valueOf(s);
}
}

} catch (IllegalArgumentException e) {
plugin.getLogger().log(Level.WARNING, ChatColor.RED + e.getMessage());
plugin.getLogger().log(Level.WARNING, ChatColor.RED + "Unknown GriefPrevention flag!");
plugin.getLogger().log(Level.WARNING, ChatColor.RED + "Please check your config setting!");
plugin.getLogger().log(Level.WARNING, e.getMessage());
plugin.getLogger().log(Level.WARNING, "Unknown GriefPrevention flag!");
plugin.getLogger().log(Level.WARNING, "Please check your config setting!");
griefPreventionFlag.clear();
griefPreventionFlag.add("Access");
}
Expand All @@ -197,7 +197,7 @@ public static void checkConfig() {
if (isValidEntityType(entityName)) {
catchableEntity.add(entityName);
} else {
plugin.getLogger().log(Level.WARNING, ChatColor.YELLOW + "Unknown entity type '" + entity + "' in CatchableEntity list, skipping...");
plugin.getLogger().log(Level.WARNING, "Unknown entity type '" + entity + "' in CatchableEntity list, skipping...");
}
}

Expand Down Expand Up @@ -246,10 +246,10 @@ public static void entityFileCreate() {
outputStream.close();
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
plugin.getLogger().warning("An error caused " + e.getMessage());
}
} else {
plugin.getLogger().log(Level.WARNING, ChatColor.RED + "Unknown Error make plugin file can not place!");
plugin.getLogger().log(Level.WARNING, "Unknown Error make plugin file can not place!");
}
}

Expand All @@ -260,7 +260,7 @@ public static void entityFileCreate() {
* @return entityDisplayName
*/
public static String getEntityDisplayName(String entityName) {
if (entityFile.getConfigurationSection("EntityList").contains(entityName)) {
if (Objects.requireNonNull(entityFile.getConfigurationSection("EntityList")).contains(entityName)) {
return entityFile.getString("EntityList." + entityName.toUpperCase() + ".DisplayName");
}
return entityName;
Expand All @@ -269,17 +269,20 @@ public static String getEntityDisplayName(String entityName) {
/**
* Replace the message argument from config.yml.
*
* @param message the source message with placeholders
* @param location replace {LOCATION} from source message
* @param entity replace {ENTITY} from source message
* @return replaced message
* @return Component ready to send to player
*/
public static String toChat(String message, String location, String entity) {
public static Component toChat(String message, String location, String entity) {
// Replace placeholders
message = message.contains("{BALL}") ? message.replace("{BALL}", TranslationFileReader.catchBallName)
: message;
message = message.contains("{LOCATION}") ? message.replace("{LOCATION}", location) : message;
message = message.contains("{ENTITY}") ? message.replace("{ENTITY}", getEntityDisplayName(entity)) : message;

return ChatColor.translateAlternateColorCodes('&', message);
// Parse with MiniMessage
return MiniMessage.miniMessage().deserialize(message);
}

/**
Expand All @@ -294,31 +297,10 @@ public static void saveEntityList() {
try {
fileConfiguration.save(new File(plugin.getDataFolder(), "config.yml"));
} catch (IOException e) {
e.printStackTrace();
plugin.getLogger().log(Level.WARNING, "An error caused " + e.getMessage());
}
}

public static boolean isLatestVersion(String current, String latest) {

String[] currentParts = current.split("\\.");

String[] latestParts = latest.split("\\.");

int minLength = Math.min(currentParts.length, latestParts.length);

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

int currentPart = Integer.parseInt(currentParts[i]);

int latestPart = Integer.parseInt(latestParts[i]);

if (currentPart < latestPart) return false;

if (currentPart > latestPart) return true;
}
return currentParts.length >= latestParts.length;
}

public enum DropMethodType {
CHICKEN,
ENTITY,
Expand Down
Loading