Added ability to sort enchants in folders, also exclude enchants that

are in the "old" folder. + Fixed some bugs.
This commit is contained in:
GB6 2019-03-05 16:59:00 +01:00
parent 2669b44471
commit 8306b51842
13 changed files with 55 additions and 41 deletions

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>EpicEnchants-Parent</artifactId>
<groupId>com.songoda</groupId>
<version>1.0.3-ALPHA</version>
<version>1.0.4-ALPHA</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -128,7 +128,6 @@ public class EnchantCommand extends BaseCommand {
//ee reload [enchantFileName]
@Subcommand("reload")
@Description("Reload all config files.")
@CommandCompletion("@enchantFiles")
@CommandPermission("epicenchants.reload")
public void onReload(CommandSender sender) {
instance.reload();

View File

@ -16,8 +16,7 @@ import org.bukkit.event.player.PlayerJoinEvent;
import java.util.Arrays;
import java.util.Map;
import static com.songoda.epicenchants.enums.EventType.OFF;
import static com.songoda.epicenchants.enums.EventType.ON;
import static com.songoda.epicenchants.enums.EventType.*;
import static com.songoda.epicenchants.enums.TriggerType.*;
public class PlayerListener implements Listener {

View File

@ -8,9 +8,7 @@ import com.songoda.epicenchants.enums.GiveType;
import com.songoda.epicenchants.objects.Enchant;
import com.songoda.epicenchants.objects.Group;
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
@ -28,9 +26,6 @@ public class CommandManager extends BukkitCommandManager {
getCommandCompletions().registerCompletion("enchants", c ->
instance.getEnchantManager().getValues().stream().map(Enchant::getIdentifier).collect(Collectors.toList()));
getCommandCompletions().registerCompletion("enchantFiles", c ->
instance.getFileManager().getYmlFiles("enchants").orElse(Collections.emptyList()).stream().map(File::getName).collect(Collectors.toList()));
getCommandCompletions().registerCompletion("giveType", c ->
Arrays.stream(GiveType.values()).map(s -> s.toString().replace("_", "").toLowerCase()).collect(Collectors.toList()));

View File

@ -30,7 +30,7 @@ public class EnchantManager extends Manager<String, Enchant> {
}
public void loadEnchants() {
instance.getFileManager().getYmlFiles("enchants").ifPresent(list -> list.forEach(file -> {
instance.getFileManager().getYmlFiles("enchants").forEach(file -> {
try {
loadEnchant(file);
} catch (Exception e) {
@ -38,7 +38,7 @@ public class EnchantManager extends Manager<String, Enchant> {
Bukkit.getConsoleSender().sendMessage("Please check to make sure there are no errors in the file.");
e.printStackTrace();
}
}));
});
}
public void loadEnchant(File file) throws Exception {

View File

@ -71,13 +71,23 @@ public class FileManager extends Manager<String, FileConfiguration> {
return getValueUnsafe(key);
}
public Optional<List<File>> getYmlFiles(String directory) {
public List<File> getYmlFiles(String directory) {
File dir = new File(instance.getDataFolder() + separator + directory + separator);
File[] files = dir.listFiles((dir1, filename) -> filename.endsWith(".yml"));
File[] allFiles = dir.listFiles();
List<File> output = new ArrayList<>();
if (files != null)
return Optional.of(Arrays.asList(files));
if (allFiles == null) {
return output;
}
return Optional.empty();
Optional.ofNullable(dir.listFiles((dir1, filename) -> filename.endsWith(".yml"))).ifPresent(list -> {
output.addAll(Arrays.asList(list));
});
Arrays.stream(allFiles)
.filter(File::isDirectory)
.filter(s -> !s.getName().equalsIgnoreCase("old"))
.forEach(f -> output.addAll(getYmlFiles(directory + separator + f.getName())));
return output;
}
}

View File

@ -28,7 +28,7 @@ public class InfoManager {
public void loadMenus() {
mainInfoMenu = new MainInfoMenu(instance, instance.getFileManager().getConfiguration("menus/main-info-menu"));
instance.getFileManager().getYmlFiles("menus/groups").ifPresent(list -> list.forEach(file -> {
instance.getFileManager().getYmlFiles("menus/groups").forEach(file -> {
try {
YamlConfiguration config = YamlConfiguration.loadConfiguration(file);
infoMenus.put(instance.getGroupManager().getValue(config.getString("group"))
@ -38,7 +38,7 @@ public class InfoManager {
Bukkit.getConsoleSender().sendMessage("Please check to make sure there are no errors in the file.");
e.printStackTrace();
}
}));
});
}
}

View File

@ -65,6 +65,7 @@ public class TinkererMenu extends FastInv {
int amount = 0;
outer:
for (int i = 0; i < inventory.getSize(); i++) {
ItemStack itemStack = inventory.getItem(i);
ItemType itemType = isTinkerable(itemStack);
@ -73,21 +74,24 @@ public class TinkererMenu extends FastInv {
continue;
}
int toSet = itemStack.getAmount();
for (int j = 0; j < itemStack.getAmount(); j++) {
if (!handleItem(itemStack, itemType)) {
continue;
continue outer;
}
amount++;
if (itemStack.getAmount() > 1) {
itemStack.setAmount(itemStack.getAmount() - 1);
inventory.setItem(i, itemStack);
continue;
}
inventory.clear(i);
toSet--;
}
if (toSet < 1) {
inventory.clear(i);
continue;
}
itemStack.setAmount(toSet);
inventory.setItem(i, itemStack);
}
instance.getAction().perform(event.getPlayer(), "tinkerer.deposited-all", of("amount", amount));
@ -137,10 +141,13 @@ public class TinkererMenu extends FastInv {
}
});
onClose(event -> slotMap.keySet().stream().filter(s -> getInventory().getItem(s) != null).forEach(s -> {
onClose(event -> {
slotMap.keySet().stream().filter(s -> getInventory().getItem(s) != null).forEach(s -> {
event.getPlayer().getInventory().addItem(getInventory().getItem(s));
});
instance.getAction().perform(event.getPlayer(), "tinkerer.cancelled");
event.getPlayer().getInventory().addItem(getInventory().getItem(s));
}));
});
}
private ItemType isTinkerable(ItemStack itemStack) {
@ -170,7 +177,9 @@ public class TinkererMenu extends FastInv {
}
private boolean handleItem(ItemStack itemStack, ItemType itemType) {
Optional<Map.Entry<Integer, Integer>> emptySlot = slotMap.entrySet().stream().filter(slot -> getInventory().getItem(slot.getKey()) == null || getInventory().getItem(slot.getKey()).getType() == Material.AIR).findFirst();
Optional<Map.Entry<Integer, Integer>> emptySlot = slotMap.entrySet().stream()
.filter(slot -> getInventory().getItem(slot.getKey()) == null || getInventory().getItem(slot.getKey()).getType() == Material.AIR)
.findFirst();
if (!emptySlot.isPresent()) {
return false;

View File

@ -31,7 +31,7 @@ public class BookItem {
public ItemStack get(Enchant enchant, @Optional Integer level, @Optional Integer successRate, @Optional Integer destroyRate) {
successRate = successRate == null ? current().nextInt(101) : successRate;
destroyRate = destroyRate == null ? current().nextInt(101) : destroyRate;
level = level == null ? current().nextInt(0, enchant.getMaxLevel() + 1) : level;
level = level == null ? current().nextInt(1, enchant.getMaxLevel() + 1) : level;
int finalSuccessRate = successRate;
int finalDestroyRate = destroyRate;

View File

@ -36,18 +36,20 @@ public class EnchantUtils {
public Pair<ItemStack, EnchantResult> apply(ItemStack itemStack, Enchant enchant, int level, int successRate, int destroyRate) {
boolean hasProtection = new NBTItem(itemStack).hasKey("protected");
Map<Enchant, Integer> enchantMap = getEnchants(itemStack);
Map<Enchant, Integer> currentEnchantMap = getEnchants(itemStack);
Set<String> currentIds = currentEnchantMap.keySet().stream().map(Enchant::getIdentifier).collect(Collectors.toSet());
Set<String> currentConflicts = currentEnchantMap.keySet().stream().map(Enchant::getConflict).flatMap(Collection::stream).collect(Collectors.toSet());
if (enchantMap.keySet().stream().anyMatch(s -> enchant.getConflict().contains(s.getIdentifier())) ||
enchant.getConflict().stream().anyMatch(s -> enchantMap.keySet().stream().map(Enchant::getIdentifier).collect(Collectors.toList()).contains(s))) {
if (enchant.getConflict().stream().anyMatch(currentIds::contains) || currentConflicts.contains(enchant.getIdentifier())) {
return Pair.of(itemStack, CONFLICT);
}
if (enchantMap.entrySet().stream().anyMatch(entry -> entry.getKey().equals(enchant) && entry.getValue() == enchant.getMaxLevel())) {
if (currentEnchantMap.entrySet().stream().anyMatch(entry -> entry.getKey().equals(enchant) && entry.getValue() == enchant.getMaxLevel())) {
return Pair.of(itemStack, MAXED_OUT);
}
if (enchantMap.entrySet().stream().anyMatch(entry -> entry.getKey().equals(enchant) && entry.getValue() == level)) {
if (currentEnchantMap.entrySet().stream().anyMatch(entry -> entry.getKey().equals(enchant) && entry.getValue() == level)) {
return Pair.of(itemStack, ALREADY_APPLIED);
}
@ -93,7 +95,7 @@ public class EnchantUtils {
NBTItem nbtItem = new NBTItem(itemStack);
if (!nbtItem.hasNBTData() || nbtItem.hasKey("enchants")) {
if (!nbtItem.hasNBTData() || !nbtItem.hasKey("enchants")) {
return Collections.emptyMap();
}

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>hooks</artifactId>
<groupId>com.songoda</groupId>
<version>1.0.3-ALPHA</version>
<version>1.0.4-ALPHA</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>EpicEnchants-Parent</artifactId>
<groupId>com.songoda</groupId>
<version>1.0.3-ALPHA</version>
<version>1.0.4-ALPHA</version>
</parent>
<packaging>pom</packaging>

View File

@ -7,7 +7,7 @@
<groupId>com.songoda</groupId>
<artifactId>EpicEnchants-Parent</artifactId>
<packaging>pom</packaging>
<version>1.0.3-ALPHA</version>
<version>1.0.4-ALPHA</version>
<modules>
<module>core</module>
<module>hooks</module>