Skip to content

Commit

Permalink
feat: Conditions API
Browse files Browse the repository at this point in the history
  • Loading branch information
Septicuss committed Oct 3, 2023
1 parent 825a077 commit 5854a6d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 54 deletions.
68 changes: 34 additions & 34 deletions src/main/java/fi/septicuss/tooltips/Tooltips.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

Expand All @@ -23,7 +20,7 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import fi.septicuss.tooltips.api.event.ConditionRegisterEvent;
import fi.septicuss.tooltips.api.TooltipsAPI;
import fi.septicuss.tooltips.commands.TooltipsCommand;
import fi.septicuss.tooltips.commands.subcommands.EvalCommand;
import fi.septicuss.tooltips.commands.subcommands.ReloadCommand;
Expand Down Expand Up @@ -92,7 +89,7 @@
import fi.septicuss.tooltips.utils.placeholder.impl.SimplePlaceholderParser;
import fi.septicuss.tooltips.utils.variable.Variables;

public class Tooltips extends JavaPlugin implements Listener {
public class Tooltips extends JavaPlugin {

public static Gson GSON = new GsonBuilder().create();
public static boolean SUPPORT_DISPLAY_ENTITIES;
Expand Down Expand Up @@ -144,6 +141,11 @@ private static boolean checkIfSupportsDisplayEntities() {

// ------------------------------------------------------

@Override
public void onLoad() {
registerDefaultConditions();
}

@Override
public void onEnable() {
protocolManager = ProtocolLibrary.getProtocolManager();
Expand Down Expand Up @@ -179,6 +181,32 @@ public void onDisable() {

// ------------------------------------------------------

private void registerDefaultConditions() {
TooltipsAPI.registerCondition("day", new Day());
TooltipsAPI.registerCondition("night", new Night());
TooltipsAPI.registerCondition("world", new World());
TooltipsAPI.registerCondition("gamemode", new Gamemode());
TooltipsAPI.registerCondition("sneaking", new Sneaking());
TooltipsAPI.registerCondition("compare", new Compare());
TooltipsAPI.registerCondition("lookingatblock", new LookingAtBlock());
TooltipsAPI.registerCondition("lookingatfurniture", new LookingAtFurniture());
TooltipsAPI.registerCondition("lookingatentity", new LookingAtEntity());
TooltipsAPI.registerCondition("region", new Region());
TooltipsAPI.registerCondition("incuboid", new InCuboid());
TooltipsAPI.registerCondition("location", new Location());
TooltipsAPI.registerCondition("standingon", new StandingOn());
TooltipsAPI.registerCondition("itemnbtequals", new ItemNbtEquals());
TooltipsAPI.registerCondition("entitynbtequals", new EntityNbtEquals());
TooltipsAPI.registerCondition("tileentitynbtequals", new TileEntityNbtEquals());
TooltipsAPI.registerCondition("blocknbtequals", new BlockNbtEquals());
TooltipsAPI.registerCondition("blockstateequals", new BlockStateEquals());
TooltipsAPI.registerCondition("time", new Time());
TooltipsAPI.registerCondition("equipped", new Equipped());
TooltipsAPI.registerCondition("op", new Op());
TooltipsAPI.registerCondition("lookingatcitizen", new LookingAtCitizen());
TooltipsAPI.registerCondition("permission", new Permission());
}

private void loadVariables() {
final File variablesDirectory = new File(getDataFolder(), ".data/variables");
Variables.PERSISTENT.load(variablesDirectory);
Expand Down Expand Up @@ -251,7 +279,6 @@ private void loadIntegrations() {

private void loadListeners() {
PluginManager pluginManager = Bukkit.getPluginManager();
pluginManager.registerEvents(this, this);

if (this.areaProvider != null) {
pluginManager.registerEvents(new PlayerMovementListener(this.areaProvider), this);
Expand All @@ -262,33 +289,6 @@ private void loadListeners() {
pluginManager.registerEvents(new PlayerConnectionListener(), this);
}

@EventHandler(priority = EventPriority.LOWEST)
public void on(ConditionRegisterEvent event) {
event.register("day", new Day());
event.register("night", new Night());
event.register("world", new World());
event.register("gamemode", new Gamemode());
event.register("sneaking", new Sneaking());
event.register("compare", new Compare());
event.register("lookingatblock", new LookingAtBlock());
event.register("lookingatfurniture", new LookingAtFurniture(this.furnitureProvider));
event.register("lookingatentity", new LookingAtEntity());
event.register("region", new Region());
event.register("incuboid", new InCuboid());
event.register("location", new Location());
event.register("standingon", new StandingOn());
event.register("itemnbtequals", new ItemNbtEquals());
event.register("entitynbtequals", new EntityNbtEquals());
event.register("tileentitynbtequals", new TileEntityNbtEquals());
event.register("blocknbtequals", new BlockNbtEquals());
event.register("blockstateequals", new BlockStateEquals());
event.register("time", new Time());
event.register("equipped", new Equipped());
event.register("op", new Op());
event.register("lookingatcitizen", new LookingAtCitizen());
event.register("permission", new Permission());
}

private void loadCommands() {
TooltipsCommand tooltipsCommand = new TooltipsCommand(this);
tooltipsCommand.register("sendtheme", new SendThemeCommand(this));
Expand Down Expand Up @@ -439,7 +439,7 @@ private void addLocalPlaceholders() {

String variableName = s.substring(cutIndex);
variableName = Placeholders.replacePlaceholders(p, variableName);

Argument returnArgument = null;

if (global) {
Expand Down
19 changes: 16 additions & 3 deletions src/main/java/fi/septicuss/tooltips/api/TooltipsAPI.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package fi.septicuss.tooltips.api;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.*;

import javax.annotation.Nullable;

import fi.septicuss.tooltips.object.preset.condition.Condition;
import org.bukkit.entity.Player;

import fi.septicuss.tooltips.Tooltips;
Expand All @@ -17,6 +16,20 @@

public class TooltipsAPI {

private static final Map<String, Condition> REGISTERED_CONDITIONS = new HashMap<>();

public static void registerCondition(String name, Condition condition) {
REGISTERED_CONDITIONS.put(name, condition);
}

public static void unregisterCondition(String name) {
REGISTERED_CONDITIONS.remove(name);
}

public static Map<String, Condition> getRegisteredConditions(){
return Collections.unmodifiableMap(REGISTERED_CONDITIONS);
}

public static void sendTooltip(Player player, Preset preset) {
if (player == null) throw new NullPointerException("Player cannot be null");
if (preset == null) throw new NullPointerException("Preset cannot be null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import java.util.HashMap;
import java.util.Map;

import org.bukkit.Bukkit;

import fi.septicuss.tooltips.api.event.ConditionRegisterEvent;
import fi.septicuss.tooltips.api.TooltipsAPI;
import fi.septicuss.tooltips.object.preset.condition.parser.ArgumentParser;
import fi.septicuss.tooltips.object.preset.condition.parser.CompositeConditionParser;
import fi.septicuss.tooltips.object.preset.condition.parser.ConditionParser;
Expand All @@ -21,15 +19,10 @@ public class ConditionManager {
private StatementParser statementParser;

public ConditionManager() {

this.registeredConditions = new HashMap<>();

ConditionRegisterEvent registerEvent = new ConditionRegisterEvent();
Bukkit.getPluginManager().callEvent(registerEvent);

for (var entry : registerEvent.getRegisteredConditions().entrySet()) {
register(entry.getKey(), entry.getValue());
}

this.registeredConditions.putAll(TooltipsAPI.getRegisteredConditions());

this.argumentParser = new ArgumentParser();
this.conditionParser = new ConditionParser(this, argumentParser);
this.compositeParser = new CompositeConditionParser(conditionParser);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.bukkit.entity.Player;

import fi.septicuss.tooltips.Tooltips;
import fi.septicuss.tooltips.integrations.FurnitureProvider;
import fi.septicuss.tooltips.object.preset.condition.Condition;
import fi.septicuss.tooltips.object.preset.condition.argument.Arguments;
import fi.septicuss.tooltips.object.preset.condition.type.MultiString;
Expand All @@ -20,14 +19,11 @@ public class LookingAtFurniture implements Condition {
private static final String[] DISTANCE = { "d", "distance" };
private static final String[] ID = { "id" };

private FurnitureProvider provider;

public LookingAtFurniture(FurnitureProvider provider) {
this.provider = provider;
}

@Override
public boolean check(Player player, Arguments args) {
final var provider = Tooltips.get().getFurnitureProvider();

MultiString id = null;
int distance = 3;

Expand Down Expand Up @@ -93,6 +89,8 @@ public boolean check(Player player, Arguments args) {
@Override
public Validity valid(Arguments args) {

var provider = Tooltips.get().getFurnitureProvider();

if (provider == null) {
return Validity.of(false, "No furniture plugin present");
}
Expand Down

0 comments on commit 5854a6d

Please sign in to comment.