- Improved the info menus.

This commit is contained in:
GB6 2019-04-04 14:40:11 +02:00
parent 817aa2cb8d
commit 19caa00bc5
16 changed files with 84 additions and 52 deletions

View File

@ -31,8 +31,8 @@ public abstract class EffectExecutor {
this.section = section;
this.triggerTypes = GeneralUtils.parseTrigger(section.getString("trigger"));
this.condition = Condition.of(section.getString("condition"));
this.simultaneous = section.isConfigurationSection("effects") ? section.getConfigurationSection("effects").getKeys(false).stream()
.map(s -> "effects." + s)
this.simultaneous = section.isConfigurationSection("simultaneous") ? section.getConfigurationSection("simultaneous").getKeys(false).stream()
.map(s -> "simultaneous." + s)
.map(section::getConfigurationSection)
.map(EffectManager::getEffect)
.filter(Optional::isPresent)
@ -41,7 +41,11 @@ public abstract class EffectExecutor {
}
public void testAndRun(@NotNull Player user, @Nullable LivingEntity opponent, int level, TriggerType type, Event event, EventType eventType) {
if (!triggerTypes.contains(type)) {
testAndRun(user, opponent, level, type, event, eventType, false);
}
public void testAndRun(@NotNull Player user, @Nullable LivingEntity opponent, int level, TriggerType type, Event event, EventType eventType, boolean simul) {
if (!simul && !triggerTypes.contains(type)) {
return;
}
@ -59,7 +63,7 @@ public abstract class EffectExecutor {
execute(user, opponent, level, eventType);
}
simultaneous.forEach(e -> e.execute(user, opponent, level, eventType));
simultaneous.forEach(e -> e.testAndRun(user, opponent, level, type, event, eventType, true));
}
public abstract void execute(@NotNull Player user, @Nullable LivingEntity opponent, int level, EventType eventType);

View File

@ -41,8 +41,8 @@ public class Potion extends EffectExecutor {
LeveledModifier duration = LeveledModifier.of(getSection().getString("duration"));
consume(entity -> entity.addPotionEffect(new PotionEffect(effectType, ((int) duration.get(level, 60, user, opponent)),
((int) amplifier.get(level - 1, 0, user, opponent)), false, false)), user, opponent);
consume(entity -> entity.addPotionEffect(new PotionEffect(effectType, (int) duration.get(level, 60, user, opponent) * 20,
(int) amplifier.get(level - 1, 0, user, opponent), false, false)), user, opponent);
}
}

View File

@ -1,7 +1,7 @@
package com.songoda.epicenchants.managers;
import com.songoda.epicenchants.EpicEnchants;
import com.songoda.epicenchants.utils.objects.FileLocationObject;
import com.songoda.epicenchants.utils.objects.FileLocation;
import org.apache.commons.io.FileUtils;
import org.bukkit.Bukkit;
import org.bukkit.configuration.InvalidConfigurationException;
@ -12,14 +12,14 @@ import java.io.File;
import java.io.IOException;
import java.util.*;
import static com.songoda.epicenchants.utils.objects.FileLocationObject.of;
import static com.songoda.epicenchants.utils.objects.FileLocation.of;
import static java.io.File.separator;
import static java.util.Arrays.asList;
public class FileManager extends Manager<String, FileConfiguration> {
private final String directory;
private final LinkedHashSet<FileLocationObject> files = new LinkedHashSet<>(asList(
private final LinkedHashSet<FileLocation> files = new LinkedHashSet<>(asList(
of("config.yml", true),
of("menus/main-info-menu.yml", true),
of("menus/enchanter-menu.yml", true, true),

View File

@ -285,4 +285,4 @@ public class AlchemistMenu extends FastInv {
return (int) GeneralUtils.parseJS(toTest, "alchemist expression", 0);
}
}
}

View File

@ -6,11 +6,13 @@ import com.songoda.epicenchants.objects.Group;
import com.songoda.epicenchants.utils.objects.FastInv;
import com.songoda.epicenchants.utils.objects.ItemBuilder;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
import java.util.Iterator;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static com.songoda.epicenchants.objects.Placeholder.of;
import static com.songoda.epicenchants.utils.single.GeneralUtils.color;
@ -18,22 +20,33 @@ import static java.util.Arrays.stream;
public class InfoMenu extends FastInv {
public InfoMenu(EpicEnchants instance, FileConfiguration config) {
super(config.getInt("size"), color(config.getString("title")));
super(config.getInt("rows") * 9, color(config.getString("title")));
Group group = instance.getGroupManager().getValue(config.getString("group")).orElseThrow(() -> new IllegalArgumentException("Invalid group: " + config.getString("group")));
String[] split = config.getString("slots").split(",");
Set<Integer> slots = stream(split, 0, split.length)
.filter(StringUtils::isNumeric)
.map(Integer::parseInt)
.collect(Collectors.toSet());
Set<Integer> slots;
if (config.getString("slots").equalsIgnoreCase("ALL_SLOTS")) {
slots = IntStream.range(0, config.getInt("rows") * 9).boxed().collect(Collectors.toSet());
} else {
String[] split = config.getString("slots").split(",");
slots = stream(split, 0, split.length).filter(StringUtils::isNumeric).map(Integer::parseInt).collect(Collectors.toSet());
}
Iterator<Enchant> enchantIterator = instance.getEnchantManager().getEnchants(group).iterator();
slots.stream().filter(slot -> enchantIterator.hasNext()).forEach(slot -> {
Enchant enchant = enchantIterator.next();
String whitelist = instance.getItemGroup().getGroup(enchant.getItemWhitelist())
.map(s -> StringUtils.capitalize(s.getName().toLowerCase()))
.orElse(String.join(", ", enchant.getItemWhitelist().stream().map(Material::toString).collect(Collectors.toSet())));
addItem(slot, new ItemBuilder(config.getConfigurationSection("enchant-item"),
of("group_color", enchant.getGroup().getColor()),
of("enchant", enchant.getIdentifier()),
of("max_level", enchant.getMaxLevel()),
of("applicable_to", whitelist),
of("enchant", enchant.getIdentifier()),
of("description", enchant.getDescription())).build());
});

View File

@ -17,13 +17,11 @@ public class MainInfoMenu extends FastInv implements Listener {
.stream()
.map(s -> "contents." + s)
.map(config::getConfigurationSection)
.forEach(section -> {
addItem(section.getInt("slot"), new ItemBuilder(section).build(), event -> {
Group group = instance.getGroupManager().getValue(section.getString("group"))
.orElseThrow(() -> new IllegalArgumentException("Invalid group: " + section.getString("group")));
instance.getInfoManager().getMenu(group).ifPresent(menu -> menu.open(event.getPlayer()));
});
});
.forEach(section -> addItem(section.getInt("slot"), new ItemBuilder(section).build(), event -> {
Group group = instance.getGroupManager().getValue(section.getString("group"))
.orElseThrow(() -> new IllegalArgumentException("Invalid group: " + section.getString("group")));
instance.getInfoManager().getMenu(group).ifPresent(menu -> menu.open(event.getPlayer()));
}));
}

View File

@ -27,6 +27,6 @@ public class LeveledModifier {
String toTest = Placeholders.setPlaceholders(string, user, opponent, level);
return (double) GeneralUtils.parseJS(toTest, "LeveledModifier", def);
return (double) (int) GeneralUtils.parseJS(toTest, "LeveledModifier", def);
}
}

View File

@ -2,22 +2,22 @@ package com.songoda.epicenchants.utils.objects;
import lombok.Getter;
public class FileLocationObject {
public class FileLocation {
@Getter private final boolean required, versionDependent;
@Getter private final String path;
private FileLocationObject(String path, boolean required, boolean versionDependent) {
private FileLocation(String path, boolean required, boolean versionDependent) {
this.required = required;
this.versionDependent = versionDependent;
this.path = path;
}
public static FileLocationObject of(String path, boolean required) {
return new FileLocationObject(path, required, false);
public static FileLocation of(String path, boolean required) {
return new FileLocation(path, required, false);
}
public static FileLocationObject of(String path, boolean required, boolean versionDependent) {
return new FileLocationObject(path, required, versionDependent);
public static FileLocation of(String path, boolean required, boolean versionDependent) {
return new FileLocation(path, required, versionDependent);
}
public String getResourcePath(String dir) {

View File

@ -72,15 +72,15 @@ public class ItemBuilder {
if (section.contains("lore")) {
List<String> lore = section.getStringList("lore");
outer:
for (int i = 0; i < lore.size(); i++) {
String string = lore.get(i);
for (Placeholder placeholder : placeholders) {
if (placeholder.getToReplace() instanceof HashSet && string.contains(placeholder.getPlaceholder())) {
if (placeholder.getToReplace() instanceof ArrayList && string.contains(placeholder.getPlaceholder())) {
lore.remove(i);
Set<String> stringSet = (Set<String>) placeholder.getToReplace();
lore.addAll(i, stringSet);
lore.addAll(i, (ArrayList<String>) placeholder.getToReplace());
continue outer;
} else {
string = string.replace(placeholder.getPlaceholder(), placeholder.getToReplace().toString());
}

View File

@ -62,7 +62,7 @@ public class GeneralUtils {
}
public static Set<TriggerType> parseTrigger(String triggers) {
return Arrays.stream(triggers.replaceAll("\\s+", "").split(",")).map(TriggerType::valueOf).collect(Collectors.toSet());
return triggers == null ? Collections.emptySet() : Arrays.stream(triggers.replaceAll("\\s+", "").split(",")).map(TriggerType::valueOf).collect(Collectors.toSet());
}
public static ItemStack getHeldItem(Player player, Event event) {
@ -79,10 +79,9 @@ public class GeneralUtils {
}
public static Object parseJS(String toParse, String type, Object def) {
try {
return SCRIPT_ENGINE.eval(toParse);
} catch (ScriptException | NumberFormatException e) {
} catch (ScriptException e) {
Bukkit.getLogger().warning("[EpicEnchants] One of your " + type + " expressions is not properly formatted.");
Bukkit.getLogger().warning(toParse);
return def;

View File

@ -76,6 +76,14 @@ public class ItemGroup {
}
public Optional<Group> getGroup(Set<Material> materials) {
Optional<Group> group = Arrays.stream(Group.values())
.filter(s -> !s.getChildren().isEmpty() && s.getChildren().stream().allMatch(child -> materials.containsAll(groupMap.get(child))))
.findFirst();
if (group.isPresent()) {
return group;
}
return groupMap.asMap().entrySet().stream().filter(s -> s.getValue().equals(materials)).map(Map.Entry::getKey).findFirst();
}

View File

@ -1,15 +1,17 @@
title: "Elite enchants"
size: 9
rows: 1
#Slots that you want enchants to be displayed on
slots: "4,5,6"
slots: "ALL_SLOTS"
#The group of the enchants to be displayed
group: ELITE
enchant-item:
material: "PAPER"
display-name: "{group_color} {enchant}"
display-name: "{group_color}{enchant}"
lore:
- "&7Max level: &f{max_level}"
- "&7Applicable to: &f{applicable_to}"
- "&7Description:"
- "{description}"

View File

@ -1,15 +1,17 @@
title: "Legendary enchants"
size: 9
rows: 1
#Slots that you want enchants to be displayed on
slots: "4,5,6"
slots: "ALL_SLOTS"
#The group of the enchants to be displayed
group: LEGENDARY
enchant-item:
material: "PAPER"
display-name: "{group_color} {enchant}"
display-name: "{group_color}{enchant}"
lore:
- "&7Max level: &f{max_level}"
- "&7Applicable to: &f{applicable_to}"
- "&7Description:"
- "{description}"

View File

@ -1,15 +1,17 @@
title: "Simple enchants"
size: 9
rows: 1
#Slots that you want enchants to be displayed on
slots: "4,5,6"
slots: "ALL_SLOTS"
#The group of the enchants to be displayed
group: SIMPLE
enchant-item:
material: "PAPER"
display-name: "{group_color} {enchant}"
display-name: "{group_color}{enchant}"
lore:
- "&7Max level: &f{max_level}"
- "&7Applicable to: &f{applicable_to}"
- "&7Description:"
- "{description}"

View File

@ -1,15 +1,17 @@
title: "Ultimate enchants"
size: 9
rows: 1
#Slots that you want enchants to be displayed on
slots: "4,5,6"
slots: "ALL_SLOTS"
#The group of the enchants to be displayed
group: ULTIMATE
enchant-item:
material: "PAPER"
display-name: "{group_color} {enchant}"
display-name: "{group_color}{enchant}"
lore:
- "&7Max level: &f{max_level}"
- "&7Applicable to: &f{applicable_to}"
- "&7Description:"
- "{description}"

View File

@ -1,15 +1,17 @@
title: "Unique enchants"
size: 9
rows: 1
#Slots that you want enchants to be displayed on
slots: "4,5,6"
slots: "ALL_SLOTS"
#The group of the enchants to be displayed
group: UNIQUE
enchant-item:
material: "PAPER"
display-name: "{group_color} {enchant}"
display-name: "{group_color}{enchant}"
lore:
- "&7Max level: &f{max_level}"
- "&7Applicable to: &f{applicable_to}"
- "&7Description:"
- "{description}"