Skip to content

Commit

Permalink
implement FunctionalMassBuilder for some builders
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Jun 30, 2024
1 parent bc475a6 commit bc440bd
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@
import me.hsgamer.bettergui.api.argument.ArgumentProcessor;
import me.hsgamer.bettergui.api.menu.Menu;
import me.hsgamer.bettergui.argument.type.*;
import me.hsgamer.hscore.builder.MassBuilder;
import me.hsgamer.hscore.builder.FunctionalMassBuilder;
import me.hsgamer.hscore.collections.map.CaseInsensitiveStringMap;

import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;

/**
* The argument processor builder
*/
public final class ArgumentProcessorBuilder extends MassBuilder<ArgumentProcessorBuilder.Input, ArgumentProcessor> {
public final class ArgumentProcessorBuilder extends FunctionalMassBuilder<ArgumentProcessorBuilder.Input, ArgumentProcessor> {
/**
* The instance of the argument processor builder
*/
Expand All @@ -29,23 +27,9 @@ private ArgumentProcessorBuilder() {
register(MaterialArgumentProcessor::new, "material", "item");
}

/**
* Register a new processor creator
*
* @param creator the creator
* @param type the type
*/
public void register(Function<ArgumentProcessorBuilder.Input, ArgumentProcessor> creator, String... type) {
register(input -> {
Map<String, Object> keys = new CaseInsensitiveStringMap<>(input.options);
String processor = Objects.toString(keys.get("type"), "store");
for (String s : type) {
if (processor.equalsIgnoreCase(s)) {
return Optional.of(creator.apply(input));
}
}
return Optional.empty();
});
@Override
protected String getType(Input input) {
return Objects.toString(new CaseInsensitiveStringMap<>(input.options).get("type"), "store");
}

/**
Expand Down
26 changes: 5 additions & 21 deletions src/main/java/me/hsgamer/bettergui/builder/ButtonBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@
import me.hsgamer.bettergui.api.menu.Menu;
import me.hsgamer.bettergui.button.*;
import me.hsgamer.bettergui.config.MainConfig;
import me.hsgamer.hscore.builder.MassBuilder;
import me.hsgamer.hscore.builder.FunctionalMassBuilder;
import me.hsgamer.hscore.collections.map.CaseInsensitiveStringMap;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* The button builder
*/
public final class ButtonBuilder extends MassBuilder<ButtonBuilder.Input, WrappedButton> {
public final class ButtonBuilder extends FunctionalMassBuilder<ButtonBuilder.Input, WrappedButton> {
/**
* The instance of the button builder
*/
Expand All @@ -42,23 +40,9 @@ private ButtonBuilder() {
);
}

/**
* Register a new button creator
*
* @param creator the creator
* @param type the type
*/
public void register(Function<Input, WrappedButton> creator, String... type) {
register(input -> {
Map<String, Object> keys = new CaseInsensitiveStringMap<>(input.options);
String button = Objects.toString(keys.get("type"), "simple");
for (String s : type) {
if (button.equalsIgnoreCase(s)) {
return Optional.of(creator.apply(input));
}
}
return Optional.empty();
});
@Override
protected String getType(Input input) {
return Objects.toString(new CaseInsensitiveStringMap<>(input.options).get("type"), "simple");
}

/**
Expand Down
35 changes: 8 additions & 27 deletions src/main/java/me/hsgamer/bettergui/builder/ConfigBuilder.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package me.hsgamer.bettergui.builder;

import me.hsgamer.hscore.builder.MassBuilder;
import me.hsgamer.hscore.builder.FunctionalMassBuilder;
import me.hsgamer.hscore.bukkit.config.BukkitConfig;
import me.hsgamer.hscore.config.Config;

import java.io.File;
import java.util.Optional;
import java.util.function.Function;

/**
* The config builder
*/
public class ConfigBuilder extends MassBuilder<File, Config> {
public class ConfigBuilder extends FunctionalMassBuilder<File, Config> {
/**
* the singleton instance
*/
Expand All @@ -21,28 +19,11 @@ private ConfigBuilder() {
register(BukkitConfig::new, "yml", "yaml");
}

/**
* Register a new config creator
*
* @param creator the creator
* @param type the file type (extension)
*/
public void register(Function<File, Config> creator, String... type) {
register(file -> {
String name = file.getName();
int index = name.lastIndexOf('.');
if (index < 0) return Optional.empty();

String extension = name.substring(index + 1);
boolean isMatched = false;
for (String s : type) {
if (s.equalsIgnoreCase(extension)) {
isMatched = true;
break;
}
}

return isMatched ? Optional.of(creator.apply(file)) : Optional.empty();
});
@Override
protected String getType(File input) {
String name = input.getName();
int index = name.lastIndexOf('.');
if (index < 0) return "";
return name.substring(index + 1);
}
}
26 changes: 12 additions & 14 deletions src/main/java/me/hsgamer/bettergui/builder/ItemModifierBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@

import me.hsgamer.bettergui.modifier.NBTModifier;
import me.hsgamer.bettergui.modifier.SkullModifier;
import me.hsgamer.hscore.builder.MassBuilder;
import me.hsgamer.hscore.builder.FunctionalMassBuilder;
import me.hsgamer.hscore.bukkit.item.modifier.*;
import me.hsgamer.hscore.minecraft.item.ItemModifier;
import org.bukkit.inventory.ItemStack;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* The item modifier builder
*/
public class ItemModifierBuilder extends MassBuilder<Map.Entry<String, Object>, ItemModifier<ItemStack>> {
public class ItemModifierBuilder extends FunctionalMassBuilder<Map.Entry<String, Object>, ItemModifier<ItemStack>> {
/**
* The instance of the item modifier builder
*/
Expand All @@ -36,6 +35,11 @@ private ItemModifierBuilder() {
register(PotionEffectModifier::new, "potion-effect", "potion", "effect");
}

@Override
protected String getType(Map.Entry<String, Object> input) {
return input.getKey();
}

/**
* Register a new modifier creator
*
Expand All @@ -44,17 +48,11 @@ private ItemModifierBuilder() {
*/
public void register(Supplier<ItemModifier<ItemStack>> creator, String... type) {
register(input -> {
String modifier = input.getKey();
for (String s : type) {
if (modifier.equalsIgnoreCase(s)) {
ItemModifier<ItemStack> itemModifier = creator.get();
Object value = input.getValue();
itemModifier.loadFromObject(value);
return Optional.of(itemModifier);
}
}
return Optional.empty();
});
ItemModifier<ItemStack> itemModifier = creator.get();
Object value = input.getValue();
itemModifier.loadFromObject(value);
return itemModifier;
}, type);
}

/**
Expand Down
39 changes: 12 additions & 27 deletions src/main/java/me/hsgamer/bettergui/builder/MenuBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@
import me.hsgamer.bettergui.menu.AddonMenu;
import me.hsgamer.bettergui.menu.PredicateMenu;
import me.hsgamer.bettergui.menu.SimpleMenu;
import me.hsgamer.hscore.builder.MassBuilder;
import me.hsgamer.hscore.builder.FunctionalMassBuilder;
import me.hsgamer.hscore.config.Config;

import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;

/**
* The menu builder
*/
public final class MenuBuilder extends MassBuilder<Config, Menu> {
public final class MenuBuilder extends FunctionalMassBuilder<Config, Menu> {
/**
* The instance of the menu builder
*/
Expand All @@ -27,29 +25,16 @@ private MenuBuilder() {
register(PredicateMenu::new, "predicate");
}

/**
* Register a new menu creator
*
* @param creator the creator
* @param type the type
*/
public void register(Function<Config, Menu> creator, String... type) {
register(input -> {
String menu = "simple";
for (Map.Entry<String[], Object> entry : input.getNormalizedValues(true).entrySet()) {
String[] path = entry.getKey();
if (path.length == 2 && path[0].equalsIgnoreCase(Menu.MENU_SETTINGS_PATH) && path[1].equalsIgnoreCase("menu-type")) {
menu = Objects.toString(entry.getValue(), "simple");
break;
}
}

for (String s : type) {
if (menu.equalsIgnoreCase(s)) {
return Optional.of(creator.apply(input));
}
@Override
protected String getType(Config input) {
String type = "simple";
for (Map.Entry<String[], Object> entry : input.getNormalizedValues(true).entrySet()) {
String[] path = entry.getKey();
if (path.length == 2 && path[0].equalsIgnoreCase(Menu.MENU_SETTINGS_PATH) && path[1].equalsIgnoreCase("menu-type")) {
type = Objects.toString(entry.getValue(), "simple");
break;
}
return Optional.empty();
});
}
return type;
}
}
26 changes: 5 additions & 21 deletions src/main/java/me/hsgamer/bettergui/builder/RequirementBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@
import me.hsgamer.bettergui.api.menu.Menu;
import me.hsgamer.bettergui.api.requirement.Requirement;
import me.hsgamer.bettergui.requirement.type.*;
import me.hsgamer.hscore.builder.MassBuilder;

import java.util.Optional;
import java.util.function.Function;
import me.hsgamer.hscore.builder.FunctionalMassBuilder;

/**
* The requirement builder
*/
public final class RequirementBuilder extends MassBuilder<RequirementBuilder.Input, Requirement> {
public final class RequirementBuilder extends FunctionalMassBuilder<RequirementBuilder.Input, Requirement> {
public static final RequirementBuilder INSTANCE = new RequirementBuilder();

/**
Expand All @@ -25,22 +22,9 @@ private RequirementBuilder() {
register(ConditionRequirement::new, "condition");
}

/**
* Register a new requirement creator
*
* @param creator the creator
* @param type the type
*/
public void register(Function<Input, Requirement> creator, String... type) {
register(input -> {
String requirement = input.type;
for (String s : type) {
if (requirement.equalsIgnoreCase(s)) {
return Optional.of(creator.apply(input));
}
}
return Optional.empty();
});
@Override
protected String getType(Input input) {
return input.type;
}

/**
Expand Down

0 comments on commit bc440bd

Please sign in to comment.