Skip to content

Commit

Permalink
Add more specific uses cases to test plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
rchomczyk committed Nov 19, 2024
1 parent 7483a17 commit b366a07
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 16 deletions.
1 change: 0 additions & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 22 additions & 12 deletions honey-test-plugin/src/dev/shiza/honey/ExampleListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,44 @@

final class ExampleListener implements Listener {

private final AdventureMessageFormatter formatter;
private final AdventureMessageFormatter defaultMessageFormatter;
private final AdventureMessageFormatter reflectMessageFormatter;

ExampleListener(final AdventureMessageFormatter formatter) {
this.formatter = formatter;
ExampleListener(
final AdventureMessageFormatter defaultMessageFormatter,
final AdventureMessageFormatter reflectMessageFormatter) {
this.defaultMessageFormatter = defaultMessageFormatter;
this.reflectMessageFormatter = reflectMessageFormatter;
}

@EventHandler
public void onPlayerJoin(final PlayerJoinEvent event) {
// 1) Using the default message formatter
AdventureMessageDispatcher.createTitle()
.viewer(event.getPlayer())
.times(2, 4, 2)
.title(it -> it.template(formatter, "Hello {{number}}!"))
.title(it -> it.template(defaultMessageFormatter, "Hello {{number}}!"))
.subtitle(
it ->
it.template(formatter, "It is a pleasure to see you there {{number}} {{player}}")
it.template(
defaultMessageFormatter,
"It is a pleasure to see you there {{number}} {{player}}")
.placeholders(
environment -> environment.replace("player", event.getPlayer().getName())))
.placeholders(environment -> environment.replace("number", 15))
mapping -> mapping.replace("player", event.getPlayer().getName())))
.placeholders(mapping -> mapping.replace("number", 15))
.dispatch();

// 2) Using the reflective message formatter
AdventureMessageDispatcher.createActionBar()
.viewer(event.getPlayer())
.template(reflectMessageFormatter, "Hello {{player.getName}}, {{player.getUniqueId}}!")
.placeholders(mapping -> mapping.replace("player", event.getPlayer()))
.dispatch();

// 3) Using dispatcher without any message formatter
AdventureMessageDispatcher.createChat()
.viewer(Bukkit.getServer())
.template(Component.text("Somebody joined to the server!").color(NamedTextColor.RED))
.dispatch();

AdventureMessageDispatcher.createActionBar()
.viewer(event.getPlayer())
.template(formatter, "Honey is great, isn't it?")
.dispatch();
}
}
42 changes: 40 additions & 2 deletions honey-test-plugin/src/dev/shiza/honey/ExamplePlugin.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,52 @@
package dev.shiza.honey;

import dev.shiza.honey.adventure.message.compiler.AdventureMessageCompilerFactory;
import dev.shiza.honey.adventure.message.formatter.AdventureMessageFormatter;
import dev.shiza.honey.adventure.message.formatter.AdventureMessageFormatterFactory;
import dev.shiza.honey.adventure.placeholder.sanitizer.AdventurePlaceholderSanitizerFactory;
import dev.shiza.honey.conversion.ImplicitConversion;
import dev.shiza.honey.message.compiler.MessageCompiler;
import dev.shiza.honey.placeholder.PlaceholderContext;
import dev.shiza.honey.placeholder.evaluator.PlaceholderEvaluator;
import dev.shiza.honey.placeholder.evaluator.reflection.ReflectivePlaceholderEvaluatorFactory;
import dev.shiza.honey.placeholder.processor.PlaceholderProcessor;
import dev.shiza.honey.placeholder.processor.PlaceholderProcessorFactory;
import dev.shiza.honey.placeholder.resolver.PlaceholderResolver;
import dev.shiza.honey.placeholder.resolver.PlaceholderResolverFactory;
import dev.shiza.honey.placeholder.sanitizer.PlaceholderSanitizer;
import dev.shiza.honey.processor.ProcessorRegistry;
import dev.shiza.honey.processor.ProcessorRegistryFactory;
import net.kyori.adventure.text.Component;
import org.bukkit.plugin.java.JavaPlugin;

public final class ExamplePlugin extends JavaPlugin {

@Override
public void onEnable() {
final AdventureMessageFormatter messageFormatter = AdventureMessageFormatterFactory.create();
getServer().getPluginManager().registerEvents(new ExampleListener(messageFormatter), this);
final AdventureMessageFormatter defaultMessageFormatter = AdventureMessageFormatterFactory.create();
final AdventureMessageFormatter reflectMessageFormatter = createReflectMessageFormatter();
getServer().getPluginManager().registerEvents(new ExampleListener(defaultMessageFormatter, reflectMessageFormatter), this);
}

private AdventureMessageFormatter createReflectMessageFormatter() {
final MessageCompiler<Component> messageCompiler = AdventureMessageCompilerFactory.create();
final ImplicitConversion implicitConversion = ImplicitConversion.create();
final PlaceholderContext placeholderContext = PlaceholderContext.create();
final PlaceholderResolver placeholderResolver = PlaceholderResolverFactory.create();
final PlaceholderSanitizer placeholderSanitizer =
AdventurePlaceholderSanitizerFactory.createReflective();
final PlaceholderEvaluator placeholderEvaluator =
ReflectivePlaceholderEvaluatorFactory.create();
final PlaceholderProcessor placeholderProcessor =
PlaceholderProcessorFactory.create(
placeholderEvaluator, placeholderSanitizer, implicitConversion);
final ProcessorRegistry processorRegistry = ProcessorRegistryFactory.create();
return AdventureMessageFormatterFactory.create(
messageCompiler,
placeholderContext,
placeholderResolver,
placeholderProcessor,
placeholderSanitizer,
processorRegistry);
}
}
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ include(":honey-common")
include(":honey-kt-extension")

// Uncomment in case if you would like to run test plugin
include(":honey-test-plugin")
// include(":honey-test-plugin")

0 comments on commit b366a07

Please sign in to comment.