Skip to content

Commit

Permalink
Fixed legacy support
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorenzo0111 committed Sep 18, 2021
1 parent e02efdf commit 59dd57d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import me.lorenzo0111.multilang.exceptions.ConfigException;
import me.lorenzo0111.multilang.exceptions.ReloadException;
import me.lorenzo0111.multilang.requirements.LangRequirement;
import me.lorenzo0111.rocketplaceholders.creator.Placeholder;
import me.lorenzo0111.rocketplaceholders.creator.conditions.ConditionNode;
import org.bukkit.configuration.ConfigurationSection;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -112,9 +111,7 @@ public void save(String identifier,String defaultText,Map<Locale,String> locales

localesMap.forEach((locale,string) -> conditions.add(new ConditionNode(new LangRequirement(plugin,locale),string)));

Placeholder placeholder = new Placeholder(identifier,plugin,defaultText,conditions, null);

plugin.getHook().addPlaceholder(placeholder);
plugin.getHook().addPlaceholder(identifier,plugin,defaultText,conditions);
}

public void unregisterAll() {
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/me/lorenzo0111/multilang/hooks/Hook.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package me.lorenzo0111.multilang.hooks;

import me.lorenzo0111.rocketplaceholders.api.IRocketPlaceholdersAPI;
import me.lorenzo0111.rocketplaceholders.creator.Placeholder;
import me.lorenzo0111.rocketplaceholders.creator.conditions.ConditionNode;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public interface Hook {
void register(IRocketPlaceholdersAPI api);
void unregister(IRocketPlaceholdersAPI api);

void addPlaceholder(Placeholder placeholder);
void addPlaceholder(@NotNull String identifier, JavaPlugin owner, @NotNull String text, @Nullable List<ConditionNode> nodes);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package me.lorenzo0111.multilang.hooks;

import me.lorenzo0111.multilang.MultiLangPlugin;
import me.lorenzo0111.rocketplaceholders.api.IRocketPlaceholdersAPI;
import me.lorenzo0111.rocketplaceholders.creator.Placeholder;
import me.lorenzo0111.rocketplaceholders.creator.conditions.ConditionNode;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

public class LegacyRocketPlaceholdersHook implements Hook {
private final List<Placeholder> placeholders;
private Constructor<Placeholder> constructor;

public LegacyRocketPlaceholdersHook(List<Placeholder> placeholders) {
this.placeholders = placeholders;
Expand All @@ -29,7 +37,22 @@ public void unregister(IRocketPlaceholdersAPI api) {
}

@Override
public void addPlaceholder(Placeholder placeholder) {
placeholders.add(placeholder);
public void addPlaceholder(@NotNull String identifier, JavaPlugin owner, @NotNull String text, @Nullable List<ConditionNode> nodes) {
if (constructor == null) {
try {
this.constructor = Placeholder.class.getConstructor(String.class, String.class, JavaPlugin.class, String.class, List.class);
} catch (NoSuchMethodException e) {
MultiLangPlugin.getInstance().getLogger().severe("An error has occurred while registering a placeholder with reflection legacy support.");
}
}

try {
Placeholder placeholder = constructor.newInstance(null,identifier,owner,text,nodes);

placeholders.add(placeholder);
} catch (InstantiationException | InvocationTargetException | IllegalAccessException e) {
MultiLangPlugin.getInstance().getLogger().severe("An error has occurred while registering a placeholder with reflection legacy support: " + e.getMessage());
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@
import me.lorenzo0111.rocketplaceholders.RocketPlaceholders;
import me.lorenzo0111.rocketplaceholders.api.IRocketPlaceholdersAPI;
import me.lorenzo0111.rocketplaceholders.creator.Placeholder;
import me.lorenzo0111.rocketplaceholders.creator.conditions.ConditionNode;
import me.lorenzo0111.rocketplaceholders.exceptions.InvalidConditionException;
import me.lorenzo0111.rocketplaceholders.providers.Provider;
import me.lorenzo0111.rocketplaceholders.storage.StorageManager;
import org.bukkit.OfflinePlayer;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class RocketPlaceholdersHook extends Provider implements Hook {

Expand All @@ -27,7 +33,9 @@ public void unregister(IRocketPlaceholdersAPI api) {
}

@Override
public void addPlaceholder(Placeholder placeholder) {
public void addPlaceholder(@NotNull String identifier, JavaPlugin owner, @NotNull String text, @Nullable List<ConditionNode> nodes) {
Placeholder placeholder = new Placeholder(identifier,owner,text,nodes, null);

manager.getInternalPlaceholders().add(placeholder);
}

Expand Down

0 comments on commit 59dd57d

Please sign in to comment.