454 lines
13 KiB
Java
454 lines
13 KiB
Java
package com.hm.petmaster;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.util.logging.Level;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.command.Command;
|
|
import org.bukkit.command.CommandSender;
|
|
import org.bukkit.configuration.InvalidConfigurationException;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.event.HandlerList;
|
|
import org.bukkit.plugin.PluginManager;
|
|
import org.bukkit.plugin.RegisteredServiceProvider;
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
|
|
import com.hm.mcshared.file.CommentedYamlConfiguration;
|
|
import com.hm.petmaster.command.EnableDisableCommand;
|
|
import com.hm.petmaster.command.FreeCommand;
|
|
import com.hm.petmaster.command.HelpCommand;
|
|
import com.hm.petmaster.command.InfoCommand;
|
|
import com.hm.petmaster.command.PetInvincibleCommand;
|
|
import com.hm.petmaster.command.PetSkillCommand;
|
|
import com.hm.petmaster.command.ReloadCommand;
|
|
import com.hm.petmaster.command.SetColorCommand;
|
|
import com.hm.petmaster.command.SetOwnerCommand;
|
|
import com.hm.petmaster.command.ShareCommand;
|
|
import com.hm.petmaster.files.PetAbilityFile;
|
|
import com.hm.petmaster.listener.PetOwnerLabelListener;
|
|
import com.hm.petmaster.listener.PlayerAttackListener;
|
|
import com.hm.petmaster.listener.PlayerBreedListener;
|
|
import com.hm.petmaster.listener.PlayerInteractListener;
|
|
import com.hm.petmaster.listener.PlayerLeashListener;
|
|
import com.hm.petmaster.listener.PlayerQuitListener;
|
|
import com.hm.petmaster.listener.PlayerTameListener;
|
|
import com.hm.petmaster.utils.MessageSender;
|
|
|
|
import net.kyori.adventure.text.Component;
|
|
import net.kyori.adventure.text.TextComponent;
|
|
import net.trueog.diamondbankog.api.DiamondBankAPIJava;
|
|
|
|
/**
|
|
* Manage pets and display useful information via hover labels or chat messages!
|
|
*
|
|
* PetMaster is under GNU General Public License version 3. Please visit the
|
|
* plugin's GitHub for more information : https://github.com/PyvesB/PetMaster
|
|
*
|
|
* Official plugin's server: hellominecraft.fr
|
|
*
|
|
* Bukkit project page: dev.bukkit.org/bukkit-plugins/pet-master
|
|
*
|
|
* Spigot project page: spigotmc.org/resources/pet-master.15904
|
|
*
|
|
* @since December 2015.
|
|
* @version 1.12.5
|
|
* @author DarkPyves
|
|
*/
|
|
public class PetMaster extends JavaPlugin {
|
|
|
|
// Plugin options and various parameters.
|
|
private TextComponent chatHeader;
|
|
private int serverVersion;
|
|
|
|
// Fields related to file handling.
|
|
private CommentedYamlConfiguration config;
|
|
private CommentedYamlConfiguration lang;
|
|
|
|
// Plugin listeners.
|
|
private PlayerInteractListener playerInteractListener;
|
|
private PlayerLeashListener playerLeashListener;
|
|
private PlayerQuitListener playerQuitListener;
|
|
private PlayerAttackListener playerAttackListener;
|
|
private PlayerTameListener playerTameListener;
|
|
private PlayerBreedListener playerBreedListener;
|
|
private PetOwnerLabelListener petOwnerLabelListener;
|
|
|
|
// Additional classes related to plugin commands.
|
|
private HelpCommand helpCommand;
|
|
private InfoCommand infoCommand;
|
|
private SetOwnerCommand setOwnerCommand;
|
|
private FreeCommand freeCommand;
|
|
private EnableDisableCommand enableDisableCommand;
|
|
private ReloadCommand reloadCommand;
|
|
private SetColorCommand setColorCommand;
|
|
private ShareCommand shareCommand;
|
|
private PetInvincibleCommand petInvincibleCommand;
|
|
private PetSkillCommand petSkillCommand;
|
|
|
|
// Messaging System.
|
|
private MessageSender messageSender;
|
|
|
|
// Plugin instance.
|
|
private static PetMaster instance;
|
|
|
|
// DiamondBank-OG Economy.
|
|
private static DiamondBankAPIJava diamondBankAPI;
|
|
|
|
public static DiamondBankAPIJava getDiamondBankAPI() {
|
|
|
|
return diamondBankAPI;
|
|
|
|
}
|
|
|
|
/**
|
|
* Called when server is launched or reloaded.
|
|
*/
|
|
@Override
|
|
public void onEnable() {
|
|
|
|
// Initialize plugin instance.
|
|
instance = this;
|
|
|
|
// Start enabling plugin.
|
|
final long startTime = System.currentTimeMillis();
|
|
|
|
// Initialize the DiamondBank-OG API.
|
|
final RegisteredServiceProvider<DiamondBankAPIJava> provider = getServer().getServicesManager()
|
|
.getRegistration(DiamondBankAPIJava.class);
|
|
|
|
// If the DiamondBank-OG API failed to initialize, do this...
|
|
if (provider == null) {
|
|
|
|
// Tell Bukkit to disable this plugin, and inform the console.
|
|
disableSelf("DiamondBank-OG API is null - disabling " + getPluginMeta().getName() + "!");
|
|
return;
|
|
|
|
}
|
|
|
|
// Assign the active instance of DiamondBank-OG to the API handler.
|
|
diamondBankAPI = provider.getProvider();
|
|
|
|
// Initializing the Messaging System (Purpur provides Adventure natively).
|
|
this.messageSender = new MessageSender(this);
|
|
|
|
getLogger().info("Server version..." + Bukkit.getServer().getBukkitVersion());
|
|
getLogger().info("Registered subVersion..."
|
|
+ Bukkit.getServer().getBukkitVersion().replace(".", ",").split(",")[1].split("-")[0]);
|
|
getLogger().info("Registering listeners...");
|
|
|
|
serverVersion = Integer
|
|
.parseInt(Bukkit.getServer().getBukkitVersion().replace(".", ",").split(",")[1].split("-")[0]);
|
|
|
|
playerInteractListener = new PlayerInteractListener();
|
|
playerLeashListener = new PlayerLeashListener(this);
|
|
playerQuitListener = new PlayerQuitListener(this);
|
|
playerTameListener = new PlayerTameListener(this);
|
|
playerBreedListener = new PlayerBreedListener(this);
|
|
|
|
final PluginManager pm = getServer().getPluginManager();
|
|
// Register listeners.
|
|
pm.registerEvents(playerInteractListener, this);
|
|
pm.registerEvents(playerLeashListener, this);
|
|
pm.registerEvents(playerQuitListener, this);
|
|
pm.registerEvents(playerTameListener, this);
|
|
if (getServerVersion() >= 10) {
|
|
|
|
pm.registerEvents(playerBreedListener, this);
|
|
|
|
}
|
|
|
|
extractParametersFromConfig(true);
|
|
|
|
PetAbilityFile.petAbilitySetup();
|
|
PetAbilityFile.getPetAbilities().options().copyDefaults(true);
|
|
PetAbilityFile.petAbilitySave();
|
|
|
|
chatHeader = Component.text("<gray>[<gold>\u25b2<gray>]");
|
|
|
|
final File playerColorConfig = new File(getDataFolder() + File.separator + "playersettings.yml");
|
|
|
|
helpCommand = new HelpCommand(this);
|
|
infoCommand = new InfoCommand(this);
|
|
setOwnerCommand = new SetOwnerCommand(this);
|
|
freeCommand = new FreeCommand(this);
|
|
enableDisableCommand = new EnableDisableCommand(this);
|
|
reloadCommand = new ReloadCommand(this);
|
|
setColorCommand = new SetColorCommand(this, playerColorConfig);
|
|
shareCommand = new ShareCommand(this);
|
|
petInvincibleCommand = new PetInvincibleCommand(this);
|
|
petSkillCommand = new PetSkillCommand(this);
|
|
|
|
petOwnerLabelListener = new PetOwnerLabelListener(this);
|
|
pm.registerEvents(petOwnerLabelListener, this);
|
|
|
|
// Warn if an outdated entry is contained in the language file
|
|
if (lang.contains("petmaster-command-info-hover")) {
|
|
|
|
getLogger().log(Level.WARNING,
|
|
"Your language file contains outdated entrys! It is highly reccomended to delete it and let it regenerate so that all messages appear correctly.");
|
|
|
|
}
|
|
|
|
if (getServer().getPluginManager().isPluginEnabled(this)) {
|
|
|
|
getLogger()
|
|
.info("Plugin enabled and ready to run! Took " + (System.currentTimeMillis() - startTime) + "ms.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Extracts plugin parameters from the configuration file.
|
|
*
|
|
* @param attemptUpdate
|
|
*/
|
|
public void extractParametersFromConfig(boolean attemptUpdate) {
|
|
|
|
getLogger().info("Backing up and loading configuration files...");
|
|
|
|
config = loadAndBackupYamlConfiguration("config.yml");
|
|
lang = loadAndBackupYamlConfiguration(config.getString("languageFileName", "lang.yml"));
|
|
|
|
if (!getServer().getPluginManager().isPluginEnabled(this)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
playerInteractListener.extractParameters();
|
|
playerLeashListener.extractParameters();
|
|
if (petOwnerLabelListener != null) {
|
|
|
|
petOwnerLabelListener.extractParameters();
|
|
|
|
}
|
|
|
|
if (config.getBoolean("disablePlayerDamage", false)) {
|
|
|
|
if (playerAttackListener == null) {
|
|
|
|
playerAttackListener = new PlayerAttackListener(this);
|
|
getServer().getPluginManager().registerEvents(playerAttackListener, this);
|
|
playerAttackListener.extractParameters();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (playerAttackListener != null) {
|
|
|
|
HandlerList.unregisterAll(playerAttackListener);
|
|
playerAttackListener = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Loads and backs up file fileName.
|
|
*
|
|
* @param fileName
|
|
* @return the loaded CommentedYamlConfiguration
|
|
*/
|
|
private CommentedYamlConfiguration loadAndBackupYamlConfiguration(String fileName) {
|
|
|
|
final CommentedYamlConfiguration yamlConfiguration = new CommentedYamlConfiguration(fileName, this);
|
|
try {
|
|
|
|
yamlConfiguration.loadConfiguration();
|
|
|
|
} catch (IOException | InvalidConfigurationException e) {
|
|
|
|
getLogger().severe("Error while loading " + fileName + " file, disabling plugin.");
|
|
getLogger().log(Level.SEVERE,
|
|
"Verify your syntax by visiting yaml-online-parser.appspot.com and using the following logs: ", e);
|
|
getServer().getPluginManager().disablePlugin(this);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
yamlConfiguration.backupConfiguration();
|
|
|
|
} catch (IOException error) {
|
|
|
|
getLogger().log(Level.SEVERE, "Error while backing up configuration file: ", error);
|
|
|
|
}
|
|
|
|
return yamlConfiguration;
|
|
|
|
}
|
|
|
|
/**
|
|
* Called when server is stopped or reloaded.
|
|
*/
|
|
@Override
|
|
public void onDisable() {
|
|
|
|
if (petOwnerLabelListener != null) {
|
|
|
|
petOwnerLabelListener.shutdown();
|
|
petOwnerLabelListener = null;
|
|
|
|
}
|
|
|
|
getLogger().info("PetMaster has been disabled.");
|
|
|
|
}
|
|
|
|
/**
|
|
* Called when a player or the console enters a command.
|
|
*/
|
|
@Override
|
|
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
|
|
|
|
if (!StringUtils.equalsIgnoreCase("petm", cmd.getName())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (args.length == 0 || args.length == 1 && StringUtils.equalsIgnoreCase("help", args[0])) {
|
|
|
|
helpCommand.getHelp(sender);
|
|
|
|
} else if (StringUtils.equalsIgnoreCase("info", args[0])) {
|
|
|
|
infoCommand.getInfo(sender);
|
|
|
|
} else if (StringUtils.equalsIgnoreCase("reload", args[0])) {
|
|
|
|
reloadCommand.reload(sender);
|
|
|
|
} else if (StringUtils.equalsIgnoreCase("disable", args[0])) {
|
|
|
|
enableDisableCommand.setState(sender, true);
|
|
|
|
} else if (StringUtils.equalsIgnoreCase("enable", args[0])) {
|
|
|
|
enableDisableCommand.setState(sender, false);
|
|
|
|
} else if (StringUtils.equalsIgnoreCase("setowner", args[0]) && sender instanceof Player) {
|
|
|
|
setOwnerCommand.setOwner(((Player) sender), args);
|
|
|
|
} else if (StringUtils.equalsIgnoreCase("free", args[0]) && sender instanceof Player) {
|
|
|
|
freeCommand.freePet(((Player) sender), args);
|
|
|
|
} else if (StringUtils.equalsIgnoreCase("setcolor", args[0]) && sender instanceof Player) {
|
|
|
|
setColorCommand.setColor(((Player) sender), args);
|
|
|
|
} else if (StringUtils.equalsIgnoreCase("sharepet", args[0]) && sender instanceof Player) {
|
|
|
|
shareCommand.sharePetCommand((Player) sender);
|
|
|
|
} else if (StringUtils.equalsIgnoreCase("godpet", args[0]) && sender instanceof Player) {
|
|
|
|
petInvincibleCommand.godPetCommand((Player) sender);
|
|
|
|
} else if (StringUtils.equalsIgnoreCase("petskill", args[0]) && sender instanceof Player) {
|
|
|
|
petSkillCommand.petSkillCommand((Player) sender);
|
|
|
|
} else {
|
|
|
|
getMessageSender().sendMessage(sender, "misused-command");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
public int getServerVersion() {
|
|
|
|
return serverVersion;
|
|
|
|
}
|
|
|
|
public TextComponent getChatHeader() {
|
|
|
|
return chatHeader;
|
|
|
|
}
|
|
|
|
public CommentedYamlConfiguration getPluginConfig() {
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
public CommentedYamlConfiguration getPluginLang() {
|
|
|
|
return lang;
|
|
|
|
}
|
|
|
|
public SetOwnerCommand getSetOwnerCommand() {
|
|
|
|
return setOwnerCommand;
|
|
|
|
}
|
|
|
|
public FreeCommand getFreeCommand() {
|
|
|
|
return freeCommand;
|
|
|
|
}
|
|
|
|
public EnableDisableCommand getEnableDisableCommand() {
|
|
|
|
return enableDisableCommand;
|
|
|
|
}
|
|
|
|
public SetColorCommand getSetColorCommand() {
|
|
|
|
return setColorCommand;
|
|
|
|
}
|
|
|
|
public MessageSender getMessageSender() {
|
|
|
|
return messageSender;
|
|
|
|
}
|
|
|
|
public static PetMaster getPlugin() {
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
// Helps this plugin kill itself gracefully (in minecraft).
|
|
public static void disableSelf(String reason) {
|
|
|
|
final PetMaster pluginInstance = getPlugin();
|
|
if (pluginInstance == null) {
|
|
|
|
Bukkit.getLogger().severe("[PetMaster-OG] " + reason);
|
|
return;
|
|
|
|
}
|
|
|
|
if (!pluginInstance.isEnabled()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pluginInstance.getLogger().severe(reason);
|
|
Bukkit.getPluginManager().disablePlugin(pluginInstance);
|
|
|
|
}
|
|
|
|
}
|