Skip to content

Commit

Permalink
Better support for 1.20+
Browse files Browse the repository at this point in the history
  • Loading branch information
srnyx committed Apr 30, 2024
1 parent f68b237 commit db23dc2
Show file tree
Hide file tree
Showing 17 changed files with 239 additions and 52 deletions.
3 changes: 1 addition & 2 deletions api/src/main/java/xyz/srnyx/annoyingapi/AnnoyingPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

import xyz.srnyx.javautilities.objects.SemanticVersion;

import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.*;
Expand Down Expand Up @@ -68,7 +67,7 @@ public void log(@NotNull LogRecord logRecord) {
/**
* The API options for the plugin
*/
@NotNull public final AnnoyingOptions options = AnnoyingOptions.load(new InputStreamReader(getResource("plugin.yml")));
@NotNull public final AnnoyingOptions options = AnnoyingOptions.load(getResource("plugin.yml"));
/**
* The {@link Metrics bStats} instance for the plugin
*/
Expand Down
9 changes: 4 additions & 5 deletions api/src/main/java/xyz/srnyx/annoyingapi/PluginPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,10 @@ public static Multi load(@NotNull ConfigurationSection section, @NotNull String
// String
final ConfigurationSection platformSection = platformsSection.getConfigurationSection(platformKey);
if (platformSection == null) {
try {
multi.addIfAbsent(new PluginPlatform(Platform.valueOf(platformKey.toUpperCase()), platformsSection.getString(platformKey)));
} catch (final IllegalArgumentException e) {
// Ignore
}
final String platformKeyString = platformsSection.getString(platformKey);
if (platformKeyString != null) try {
multi.addIfAbsent(new PluginPlatform(Platform.valueOf(platformKey.toUpperCase()), platformKeyString));
} catch (final IllegalArgumentException ignored) {}
continue;
}
// Section
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,13 @@ public AnnoyingDependency(@NotNull String name, boolean required, boolean enable
*/
@NotNull
public static AnnoyingDependency load(@NotNull ConfigurationSection section) {
final String name = section.getName();
String name = section.getName();
if (name.isEmpty()) {
name = section.getString("name");
if (name == null) throw new IllegalArgumentException("The name of the dependency is missing");
}
return new AnnoyingDependency(
name.isEmpty() ? section.getString("name") : name,
name,
PluginPlatform.Multi.load(section, "platforms"),
section.getBoolean("required"),
section.getBoolean("enableAfterDownload"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ private void finish(@NotNull AnnoyingDependency dependency, boolean enable) {
try {
// Load and enable plugin
final Plugin dependencyPlugin = manager.loadPlugin(dependency.file);
if (dependencyPlugin == null) {
AnnoyingPlugin.log(Level.SEVERE, "&4" + dependency.name + " &8|&c Failed to load plugin!");
return;
}
dependencyPlugin.onLoad();
manager.enablePlugin(dependencyPlugin);
// Register commands
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class AnnoyingPlayerMoveEvent extends PlayerMoveEvent {
* @param from {@link #getFrom()}
* @param to {@link #getTo()}
*/
public AnnoyingPlayerMoveEvent(@NotNull Player player, @NotNull Location from, @NotNull Location to) {
public AnnoyingPlayerMoveEvent(@NotNull Player player, @NotNull Location from, @Nullable Location to) {
super(player, from, to);
}

Expand Down Expand Up @@ -81,6 +81,7 @@ public MovementType getMovementType() {
if (movementType != null) return movementType;
final Location from = getFrom();
final Location to = getTo();
if (to == null) return MovementType.TRANSLATION;
if (from.getYaw() == to.getYaw() && from.getPitch() == to.getPitch()) {
movementType = MovementType.TRANSLATION;
} else if (from.getX() == to.getX() && from.getY() == to.getY() && from.getZ() == to.getZ()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package xyz.srnyx.annoyingapi.events;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
Expand Down Expand Up @@ -50,7 +51,8 @@ public void onPlayerMove(@NotNull PlayerMoveEvent event) {
Bukkit.getPluginManager().callEvent(newEvent);
event.setCancelled(newEvent.isCancelled());
event.setFrom(newEvent.getFrom());
event.setTo(newEvent.getTo());
final Location to = newEvent.getTo();
if (to != null) event.setTo(to);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.jetbrains.annotations.Nullable;

import xyz.srnyx.annoyingapi.AnnoyingPlugin;
import xyz.srnyx.annoyingapi.reflection.org.bukkit.RefRegistry;
import xyz.srnyx.annoyingapi.reflection.org.bukkit.RefSoundCategory;
import xyz.srnyx.annoyingapi.utility.BukkitUtility;
import xyz.srnyx.annoyingapi.data.ItemData;
Expand Down Expand Up @@ -148,7 +149,7 @@ public void delete() {
* @param path the path to the node
* @param value the value to set the node to
*/
public void setSave(@Nullable String path, @Nullable Object value) {
public void setSave(@NotNull String path, @Nullable Object value) {
set(path, value);
save();
}
Expand Down Expand Up @@ -294,7 +295,7 @@ public PotionEffect getPotionEffect(@NotNull String path, @Nullable PotionEffect
}

// Get type
final PotionEffectType type = PotionEffectType.getByName(typeString);
final PotionEffectType type = RefRegistry.getEffect(plugin, typeString);
if (type == null) {
log(Level.WARNING, path, "&cInvalid potion effect type for &4" + path + "&c: &4" + typeString);
return def;
Expand Down Expand Up @@ -463,7 +464,7 @@ public ItemStack getItemStack(@NotNull String path, @Nullable ItemStack def) {
// Enchantments
final ConfigurationSection enchantmentsSection = section.getConfigurationSection("enchantments");
if (enchantmentsSection != null) for (final String enchantmentKey : enchantmentsSection.getKeys(false)) {
final Enchantment enchantment = Enchantment.getByName(enchantmentKey);
final Enchantment enchantment = RefRegistry.getEnchantment(plugin, enchantmentKey);
if (enchantment == null) {
log(Level.WARNING, path, "&cInvalid enchantment for &4" + path + "&c: &4" + enchantmentKey);
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import xyz.srnyx.annoyingapi.reflection.net.md_5.bungee.api.chat.RefHoverEvent;
import xyz.srnyx.annoyingapi.utility.BukkitUtility;

import java.util.ArrayList;
Expand Down Expand Up @@ -64,7 +65,7 @@ public AnnoyingJSON append(@NotNull BaseComponent component) {
@NotNull
public AnnoyingJSON append(@NotNull String display, @Nullable String hover, @Nullable ClickEvent.Action clickAction, @Nullable String clickValue) {
final TextComponent component = new TextComponent(BukkitUtility.color(display));
if (hover != null) component.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(BukkitUtility.color(hover)).create()));
if (hover != null) component.setHoverEvent(RefHoverEvent.createHoverEvent(HoverEvent.Action.SHOW_TEXT, BukkitUtility.color(hover)));
if (clickAction != null && clickValue != null) component.setClickEvent(new ClickEvent(clickAction, BukkitUtility.color(clickValue)));
return append(component);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@

import org.jetbrains.annotations.NotNull;

import org.jetbrains.annotations.Nullable;
import xyz.srnyx.javautilities.parents.Stringable;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.function.Consumer;

Expand Down Expand Up @@ -54,25 +57,32 @@ public AnnoyingOptions() {
@NotNull
public static AnnoyingOptions load(@NotNull ConfigurationSection section) {
final AnnoyingOptions options = new AnnoyingOptions();
if (section.contains("pluginOptions")) options.pluginOptions(PluginOptions.load(section.getConfigurationSection("pluginOptions")));
if (section.contains("registrationOptions")) options.registrationOptions(RegistrationOptions.load(section.getConfigurationSection("registrationOptions")));
if (section.contains("bStatsOptions")) options.bStatsOptions(BStatsOptions.load(section.getConfigurationSection("bStatsOptions")));
if (section.contains("dataOptions")) options.dataOptions(DataOptions.load(section.getConfigurationSection("dataOptions")));
if (section.contains("messagesOptions")) options.messagesOptions(MessagesOptions.load(section.getConfigurationSection("messagesOptions")));
final ConfigurationSection pluginOptionsSection = section.getConfigurationSection("pluginOptions");
if (pluginOptionsSection != null) options.pluginOptions(PluginOptions.load(pluginOptionsSection));
final ConfigurationSection registrationOptionsSection = section.getConfigurationSection("registrationOptions");
if (registrationOptionsSection != null) options.registrationOptions(RegistrationOptions.load(registrationOptionsSection));
final ConfigurationSection bStatsOptionsSection = section.getConfigurationSection("bStatsOptions");
if (bStatsOptionsSection != null) options.bStatsOptions(BStatsOptions.load(bStatsOptionsSection));
final ConfigurationSection dataOptionsSection = section.getConfigurationSection("dataOptions");
if (dataOptionsSection != null) options.dataOptions(DataOptions.load(dataOptionsSection));
final ConfigurationSection messagesOptionsSection = section.getConfigurationSection("messagesOptions");
if (messagesOptionsSection != null) options.messagesOptions(MessagesOptions.load(messagesOptionsSection));
return options;
}

/**
* Loads the options from the specified {@link Reader}
* Loads the options from the specified {@link InputStream}
*
* @param reader the reader to load the options from
* @param inputStream the input stream to load the options from
*
* @return the loaded options
* @return the loaded options
*/
@NotNull
public static AnnoyingOptions load(@NotNull Reader reader) {
final ConfigurationSection annoying = YamlConfiguration.loadConfiguration(reader).getConfigurationSection("annoying");
return annoying != null ? load(annoying) : new AnnoyingOptions();
public static AnnoyingOptions load(@Nullable InputStream inputStream) {
if (inputStream == null) return new AnnoyingOptions();
final ConfigurationSection annoying = YamlConfiguration.loadConfiguration(new InputStreamReader(inputStream)).getConfigurationSection("annoying");
if (annoying == null) return new AnnoyingOptions();
return load(annoying);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,12 @@ public BStatsOptions() {
public static BStatsOptions load(@NotNull ConfigurationSection section) {
final BStatsOptions options = new BStatsOptions();
if (section.contains("id")) options.id = section.getInt("id");
if (section.contains("fileName")) options.fileName = section.getString("fileName");
if (section.contains("fileOptions")) options.fileOptions = AnnoyingResource.Options.load(section.getConfigurationSection("fileOptions"));
if (section.contains("toggleKey")) options.toggleKey = section.getString("toggleKey");
final String fileNameString = section.getString("fileName");
if (fileNameString != null) options.fileName = fileNameString;
final ConfigurationSection fileOptionsSection = section.getConfigurationSection("fileOptions");
if (fileOptionsSection != null) options.fileOptions = AnnoyingResource.Options.load(fileOptionsSection);
final String toggleKeyString = section.getString("toggleKey");
if (toggleKeyString != null) options.toggleKey = toggleKeyString;
return options;
}

Expand Down
29 changes: 19 additions & 10 deletions api/src/main/java/xyz/srnyx/annoyingapi/options/DataOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,18 @@ public DataOptions() {
public static DataOptions load(@NotNull ConfigurationSection section) {
final DataOptions options = new DataOptions();
if (section.contains("enabled")) options.enabled(section.getBoolean("enabled"));
if (section.contains("tables")) {
final ConfigurationSection tablesSection = section.getConfigurationSection("tables");
if (tablesSection != null) {
final Map<String, Set<String>> tables = new HashMap<>();
section.getConfigurationSection("tables").getKeys(false).forEach(table -> tables.put(table, new HashSet<>(section.getStringList("tables." + table))));
tablesSection.getKeys(false).forEach(table -> tables.put(table, new HashSet<>(section.getStringList("tables." + table))));
options.tables(tables);
}
if (section.contains("cache")) options.cache(Cache.load(section.getConfigurationSection("cache")));
if (section.contains("entities")) options.entities(Entities.load(section.getConfigurationSection("entities")));
if (section.contains("configFile")) options.configFile(ConfigFile.load(section.getConfigurationSection("configFile")));
final ConfigurationSection cacheSection = section.getConfigurationSection("cache");
if (cacheSection != null) options.cache(Cache.load(cacheSection));
final ConfigurationSection entitiesSection = section.getConfigurationSection("entities");
if (entitiesSection != null) options.entities(Entities.load(entitiesSection));
final ConfigurationSection configFileSection = section.getConfigurationSection("configFile");
if (configFileSection != null) options.configFile(ConfigFile.load(configFileSection));
return options;
}

Expand Down Expand Up @@ -336,9 +340,12 @@ public Entities() {
@NotNull
public static Entities load(@NotNull ConfigurationSection section) {
final Entities options = new Entities();
if (section.contains("path")) options.path(section.getString("path"));
if (section.contains("fileOptions")) options.fileOptions(AnnoyingFile.Options.load(section.getConfigurationSection("fileOptions")));
if (section.contains("section")) options.node(section.getString("section"));
final String pathString = section.getString("path");
if (pathString != null) options.path(pathString);
final ConfigurationSection fileOptionsSection = section.getConfigurationSection("fileOptions");
if (fileOptionsSection != null) options.fileOptions(AnnoyingFile.Options.load(fileOptionsSection));
final String sectionString = section.getString("section");
if (sectionString != null) options.node(sectionString);
return options;
}

Expand Down Expand Up @@ -436,8 +443,10 @@ public ConfigFile() {
@NotNull
public static DataOptions.ConfigFile load(@NotNull ConfigurationSection section) {
final ConfigFile options = new ConfigFile();
if (section.contains("fileName")) options.fileName(section.getString("fileName"));
if (section.contains("fileOptions")) options.fileOptions(AnnoyingResource.Options.load(section.getConfigurationSection("fileOptions")));
final String fileNameString = section.getString("fileName");
if (fileNameString != null) options.fileName(fileNameString);
final ConfigurationSection fileOptionsSection = section.getConfigurationSection("fileOptions");
if (fileOptionsSection != null) options.fileOptions(AnnoyingResource.Options.load(fileOptionsSection));
return options;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ public MessagesOptions() {
@NotNull
public static MessagesOptions load(@NotNull ConfigurationSection section) {
final MessagesOptions options = new MessagesOptions();
if (section.contains("fileName")) options.fileName(section.getString("fileName"));
if (section.contains("fileOptions")) options.fileOptions(AnnoyingResource.Options.load(section.getConfigurationSection("fileOptions")));
if (section.contains("keys")) options.keys(MessageKeys.load(section.getConfigurationSection("keys")));
final String fileNameString = section.getString("fileName");
if (fileNameString != null) options.fileName(fileNameString);
final ConfigurationSection fileOptionsSection = section.getConfigurationSection("fileOptions");
if (fileOptionsSection != null) options.fileOptions(AnnoyingResource.Options.load(fileOptionsSection));
final ConfigurationSection keysSection = section.getConfigurationSection("keys");
if (keysSection != null) options.keys(MessageKeys.load(keysSection));
return options;
}

Expand Down Expand Up @@ -185,15 +188,24 @@ public MessageKeys() {
@NotNull
public static MessagesOptions.MessageKeys load(@NotNull ConfigurationSection section) {
final MessageKeys keys = new MessageKeys();
if (section.contains("globalPlaceholders")) keys.globalPlaceholders(section.getString("globalPlaceholders"));
if (section.contains("splitterJson")) keys.splitterJson(section.getString("splitterJson"));
if (section.contains("splitterPlaceholder")) keys.splitterPlaceholder(section.getString("splitterPlaceholder"));
if (section.contains("updateAvailable")) keys.updateAvailable(section.getString("updateAvailable"));
if (section.contains("noPermission")) keys.noPermission(section.getString("noPermission"));
if (section.contains("playerOnly")) keys.playerOnly(section.getString("playerOnly"));
if (section.contains("invalidArgument")) keys.invalidArgument(section.getString("invalidArgument"));
if (section.contains("invalidArguments")) keys.invalidArguments(section.getString("invalidArguments"));
if (section.contains("disabledCommand")) keys.disabledCommand(section.getString("disabledCommand"));
final String globalPlaceholdersString = section.getString("globalPlaceholders");
if (globalPlaceholdersString != null) keys.globalPlaceholders(globalPlaceholdersString);
final String splitterJsonString = section.getString("splitterJson");
if (splitterJsonString != null) keys.splitterJson(splitterJsonString);
final String splitterPlaceholderString = section.getString("splitterPlaceholder");
if (splitterPlaceholderString != null) keys.splitterPlaceholder(splitterPlaceholderString);
final String updateAvailableString = section.getString("updateAvailable");
if (updateAvailableString != null) keys.updateAvailable(updateAvailableString);
final String noPermissionString = section.getString("noPermission");
if (noPermissionString != null) keys.noPermission(noPermissionString);
final String playerOnlyString = section.getString("playerOnly");
if (playerOnlyString != null) keys.playerOnly(playerOnlyString);
final String invalidArgumentString = section.getString("invalidArgument");
if (invalidArgumentString != null) keys.invalidArgument(invalidArgumentString);
final String invalidArgumentsString = section.getString("invalidArguments");
if (invalidArgumentsString != null) keys.invalidArguments(invalidArgumentsString);
final String disabledCommandString = section.getString("disabledCommand");
if (disabledCommandString != null) keys.disabledCommand(disabledCommandString);
return keys;
}

Expand Down
Loading

0 comments on commit db23dc2

Please sign in to comment.