Skip to content

Commit

Permalink
introuced the PluginTranslationResolver, which allows to register mul…
Browse files Browse the repository at this point in the history
…tiple plugins; added support for ExcellentEnchants; bumped artifact-version
  • Loading branch information
BlvckBytes committed Oct 6, 2024
1 parent 875ddcd commit 8e69d15
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 27 deletions.
9 changes: 7 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.blvckbytes</groupId>
<artifactId>ItemPredicateParser</artifactId>
<version>0.0.7</version>
<version>0.0.8</version>

<properties>
<compileSource>17</compileSource>
Expand Down Expand Up @@ -80,6 +80,12 @@
</exclusion>
</exclusions>
</dependency>
<!-- I don't think this is hosted anywhere... Clone and manually install into local repo. -->
<dependency>
<groupId>su.nightexpress.excellentenchants</groupId>
<artifactId>Core</artifactId>
<version>4.2.2</version>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -107,7 +113,6 @@
</execution>
</executions>
<configuration>

<artifactSet>
<includes>
<!-- Include all libs for shading -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import me.blvckbytes.bukkitevaluable.ConfigManager;
import me.blvckbytes.item_predicate_parser.config.MainSection;
import me.blvckbytes.item_predicate_parser.translation.LanguageRegistry;
import me.blvckbytes.item_predicate_parser.translation.resolver.TranslationResolver;
import me.blvckbytes.item_predicate_parser.translation.resolver.PluginTranslationResolver;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.Nullable;
Expand All @@ -14,8 +14,6 @@

public class ItemPredicateParserPlugin extends JavaPlugin {

private static final String ECO_ENCHANTS_RESOLVER_PATH = "me/blvckbytes/item_predicate_parser/translation/resolver/EcoEnchantsResolver";

private static ItemPredicateParserPlugin instance;
private PredicateHelper predicateHelper;

Expand All @@ -24,21 +22,10 @@ public void onEnable() {
var logger = getLogger();

try {
TranslationResolver translationResolver = null;

if (Bukkit.getPluginManager().isPluginEnabled("EcoEnchants")) {
translationResolver = (TranslationResolver) Class
.forName(ECO_ENCHANTS_RESOLVER_PATH.replace('/', '.'))
.getConstructor()
.newInstance();

getLogger().log(Level.INFO, "Loaded resolver for EcoEnchants");
}

var configManager = new ConfigManager(this);
var config = new ConfigKeeper<>(configManager, "config.yml", MainSection.class);

var languageRegistry = new LanguageRegistry(this, translationResolver);
var languageRegistry = new LanguageRegistry(this, new PluginTranslationResolver(this));
this.predicateHelper = new PredicateHelper(languageRegistry, config);

Objects.requireNonNull(getCommand(ItemPredicateParserCommand.COMMAND_NAME)).setExecutor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,26 @@
import com.willfp.ecoenchants.enchant.EcoEnchants;
import me.blvckbytes.item_predicate_parser.translation.keyed.LangKeyed;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.Nullable;

public class EcoEnchantsResolver extends TranslationResolver {

public EcoEnchantsResolver(Plugin loadedPlugin) {
super(loadedPlugin);
}

@Override
public @Nullable String resolve(LangKeyed<?> langKeyed) {
if (!(langKeyed.getWrapped() instanceof Enchantment enchantment))
return null;

if (langKeyed.getWrapped() instanceof Enchantment enchantment) {
var id = enchantment.getKey().getKey();
var ecoEnchant = EcoEnchants.INSTANCE.get(id);

if (ecoEnchant == null)
return null;
var id = enchantment.getKey().getKey();
var ecoEnchant = EcoEnchants.INSTANCE.get(id);

return sanitize(ecoEnchant.getRawDisplayName());
}
if (ecoEnchant == null)
return null;

return null;
return sanitize(ecoEnchant.getRawDisplayName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package me.blvckbytes.item_predicate_parser.translation.resolver;

import me.blvckbytes.item_predicate_parser.translation.keyed.LangKeyed;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.Nullable;
import su.nightexpress.excellentenchants.registry.EnchantRegistry;

public class ExcellentEnchantsResolver extends TranslationResolver {

public ExcellentEnchantsResolver(Plugin loadedPlugin) {
super(loadedPlugin);
}

@Override
public @Nullable String resolve(LangKeyed<?> langKeyed) {
if (!(langKeyed.getWrapped() instanceof Enchantment enchantment))
return null;

var customEnchantment = EnchantRegistry.getByKey(enchantment.getKey());

if (customEnchantment == null)
return null;

return sanitize(customEnchantment.getDisplayName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package me.blvckbytes.item_predicate_parser.translation.resolver;

import me.blvckbytes.item_predicate_parser.translation.keyed.LangKeyed;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

public class PluginTranslationResolver extends TranslationResolver {

private static final String ECO_ENCHANTS_RESOLVER_PATH = "me/blvckbytes/item_predicate_parser/translation/resolver/EcoEnchantsResolver";
private static final String EXCELLENT_ENCHANTS_RESOLVER_PATH = "me/blvckbytes/item_predicate_parser/translation/resolver/ExcellentEnchantsResolver";

private final List<TranslationResolver> resolvers;

public PluginTranslationResolver(Plugin plugin) throws Exception {
super(plugin);

this.resolvers = new ArrayList<>();

Plugin loadedPlugin;

if ((loadedPlugin = Bukkit.getPluginManager().getPlugin("EcoEnchants")) != null) {
resolvers.add(loadResolver(ECO_ENCHANTS_RESOLVER_PATH, loadedPlugin));
plugin.getLogger().log(Level.INFO, "Loaded resolver for EcoEnchants");
}

if ((loadedPlugin = Bukkit.getPluginManager().getPlugin("ExcellentEnchants")) != null) {
resolvers.add(loadResolver(EXCELLENT_ENCHANTS_RESOLVER_PATH, loadedPlugin));
plugin.getLogger().log(Level.INFO, "Loaded resolver for ExcellentEnchants");
}
}

@Override
public @Nullable String resolve(LangKeyed<?> langKeyed) {
String result;

for (var resolver : resolvers) {
if ((result = resolver.resolve(langKeyed)) != null)
return result;
}

return null;
}

private static TranslationResolver loadResolver(String path, Plugin loadedPlugin) throws Exception {
return (TranslationResolver) Class
.forName(path.replace('/', '.'))
.getConstructor(Plugin.class)
.newInstance(loadedPlugin);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package me.blvckbytes.item_predicate_parser.translation.resolver;

import me.blvckbytes.item_predicate_parser.translation.keyed.LangKeyed;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.Nullable;

import java.util.Stack;

public abstract class TranslationResolver {

protected final Plugin loadedPlugin;

public TranslationResolver(Plugin loadedPlugin) {
this.loadedPlugin = loadedPlugin;
}

public abstract @Nullable String resolve(LangKeyed<?> langKeyed);

public static String sanitize(String input) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ main: me.blvckbytes.item_predicate_parser.ItemPredicateParserPlugin
version: ${project.version}
author: BlvckBytes
api-version: 1.13
softdepend: [EcoEnchants]
softdepend: [EcoEnchants, ExcellentEnchants]
commands:
itempredicateparser:
aliases: [ipp]
Expand Down

0 comments on commit 8e69d15

Please sign in to comment.