This commit is contained in:
GB6 2019-01-30 23:05:54 +01:00
parent eac363e23f
commit 12114a55f2
11 changed files with 208 additions and 14 deletions

View File

@ -3,10 +3,8 @@ package com.songoda.epicenchants;
import co.aikar.commands.BukkitCommandManager;
import co.aikar.commands.InvalidCommandArgument;
import com.songoda.epicenchants.commands.EnchantCommand;
import com.songoda.epicenchants.listeners.ArmorListener;
import com.songoda.epicenchants.listeners.BookListener;
import com.songoda.epicenchants.listeners.EntityListener;
import com.songoda.epicenchants.listeners.PlayerListener;
import com.songoda.epicenchants.enums.GiveType;
import com.songoda.epicenchants.listeners.*;
import com.songoda.epicenchants.managers.EnchantManager;
import com.songoda.epicenchants.managers.FileManager;
import com.songoda.epicenchants.managers.GroupManager;
@ -14,6 +12,7 @@ import com.songoda.epicenchants.managers.InfoManager;
import com.songoda.epicenchants.objects.Enchant;
import com.songoda.epicenchants.utils.EnchantUtils;
import com.songoda.epicenchants.utils.FastInv;
import com.songoda.epicenchants.utils.SpecialItems;
import com.songoda.epicenchants.utils.VersionDependent;
import lombok.Getter;
import net.milkbowl.vault.economy.Economy;
@ -22,6 +21,7 @@ import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.stream.Collectors;
@ -39,6 +39,7 @@ public class EpicEnchants extends JavaPlugin {
private GroupManager groupManager;
private EnchantUtils enchantUtils;
private FileManager fileManager;
private SpecialItems specialItems;
private Locale locale;
@Override
@ -56,6 +57,7 @@ public class EpicEnchants extends JavaPlugin {
this.enchantManager = new EnchantManager(this);
this.enchantUtils = new EnchantUtils(this);
this.infoManager = new InfoManager(this);
this.specialItems = new SpecialItems(this);
this.economy = getServer().getServicesManager().getRegistration(Economy.class).getProvider();
fileManager.createFiles();
@ -89,10 +91,16 @@ public class EpicEnchants extends JavaPlugin {
commandManager.getCommandCompletions().registerCompletion("enchants", c -> enchantManager.getEnchants().stream().map(Enchant::getIdentifier).collect(Collectors.toList()));
commandManager.getCommandCompletions().registerCompletion("enchantFiles", c -> fileManager.getYmlFiles("enchants").orElse(Collections.emptyList()).stream().map(File::getName).collect(Collectors.toList()));
commandManager.getCommandCompletions().registerCompletion("giveType", c -> Arrays.stream(GiveType.values()).map(s -> s.toString().replace("_", "").toLowerCase()).collect(Collectors.toList()));
commandManager.getCommandContexts().registerContext(Enchant.class, c -> enchantManager.getEnchant(c.popFirstArg()).orElseThrow(() -> new InvalidCommandArgument("No enchant exists by that name")));
commandManager.getCommandContexts().registerContext(File.class, c -> enchantManager.getEnchantFile(c.popFirstArg()).orElseThrow(() -> new InvalidCommandArgument("No EnchantFile exists by that name")));
commandManager.getCommandContexts().registerContext(GiveType.class, c -> Arrays.stream(GiveType.values())
.filter(s -> s.toString().toLowerCase().replace("_", "").equalsIgnoreCase(c.popFirstArg()))
.findFirst()
.orElseThrow(() -> new InvalidCommandArgument("No item by that type.")));
commandManager.registerCommand(new EnchantCommand());
}
@ -103,6 +111,7 @@ public class EpicEnchants extends JavaPlugin {
add(new ArmorListener());
add(new PlayerListener(instance));
add(new EntityListener(instance));
add(new WhiteScrollListener(instance));
}}.forEach(listener -> Bukkit.getPluginManager().registerEvents(listener, this));
}

View File

@ -31,17 +31,43 @@ public class EnchantCommand extends BaseCommand {
new EnchanterMenu(instance, instance.getFileManager().getConfiguration("menus/enchanter-menu"), player).open(player);
}
//ee give {player} {enchant} {group}
@Subcommand("give")
@CommandCompletion("@players @enchants @nothing @nothing @nothing")
//ee give book {player} {enchant} {group}
@Subcommand("give book")
@CommandCompletion("@giveType @players @enchants @nothing @nothing @nothing")
@Description("Give enchant books to players")
@CommandPermission("epicenchants.give")
public void onGiveBook(CommandSender sender, @Flags("other") Player target, Enchant enchant, @Optional Integer level, @Optional Integer successRate, @Optional Integer destroyRate) {
public void onGive(CommandSender sender, @Flags("other") Player target, Enchant enchant, @Optional Integer level, @Optional Integer successRate, @Optional Integer destroyRate) {
target.getInventory().addItem(enchant.getBookItem().get(enchant, level, successRate, destroyRate));
target.sendMessage(instance.getLocale().getMessageWithPrefix("command.book.received", of("enchant", enchant.getIdentifier())));
sender.sendMessage(instance.getLocale().getMessageWithPrefix("command.book.gave", of("player", target.getName()), of("enchant", enchant.getIdentifier())));
}
//ee give item {player} {giveType} {group}
@Subcommand("give item")
@CommandCompletion("@giveType @players @enchants @nothing @nothing @nothing")
@Description("Give enchant books to players")
@CommandPermission("epicenchants.give")
public void onGive(CommandSender sender, @Flags("other") Player target, String giveType, @Optional Integer amount, @Optional Integer successRate) {
String messageKey;
switch (giveType.toLowerCase()) {
case "whitescroll":
target.getInventory().addItem(instance.getSpecialItems().getWhiteScroll(amount));
messageKey = "whitescroll";
break;
case "blackscroll":
messageKey = "blackscroll";
target.getInventory().addItem(instance.getSpecialItems().getBlackScroll(amount, successRate));
break;
default:
return;
}
target.sendMessage(instance.getLocale().getMessageWithPrefix("command." + messageKey + ".received"));
sender.sendMessage(instance.getLocale().getMessageWithPrefix("command." + messageKey + ".gave", of("player", target.getName())));
}
//ee apply {enchant} {group}
@Subcommand("apply")
@CommandCompletion("@enchants @nothing")

View File

@ -0,0 +1,5 @@
package com.songoda.epicenchants.enums;
public enum GiveType {
WHITE_SCROLL, BLACK_SCROLL
}

View File

@ -0,0 +1,40 @@
package com.songoda.epicenchants.listeners;
import com.songoda.epicenchants.EpicEnchants;
import de.tr7zw.itemnbtapi.NBTItem;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
public class BlackScrollListener {
private final EpicEnchants instance;
public BlackScrollListener(EpicEnchants instance) {
this.instance = instance;
}
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
public void onInventoryClick(InventoryClickEvent event) {
if (event.getCursor() == null || event.getCurrentItem() == null || event.getAction() != InventoryAction.SWAP_WITH_CURSOR || event.getClickedInventory().getType() == InventoryType.CREATIVE) {
return;
}
NBTItem nbtItem = new NBTItem(event.getCursor());
NBTItem toApplyTo = new NBTItem(event.getCurrentItem());
if (!nbtItem.getBoolean("black-scroll")) {
return;
}
if (toApplyTo.getCompound("enchants") == null || toApplyTo.getCompound("enchants").getKeys().isEmpty()) {
event.getWhoClicked().sendMessage(instance.getLocale().getMessageWithPrefix("blackscroll.noenchants"));
return;
}
//TODO: Blackscroll
event.getWhoClicked().sendMessage(instance.getLocale().getMessageWithPrefix("blackscroll.success"));
}
}

View File

@ -65,7 +65,6 @@ public class BookListener implements Listener {
if (result.getRight() == BROKEN_FAILURE) {
event.getClickedInventory().clear(event.getSlot());
return;
}
if (result.getRight() != CONFLICT && result.getRight() != MAXED_OUT) {
@ -81,5 +80,4 @@ public class BookListener implements Listener {
event.getClickedInventory().setItem(event.getSlot(), result.getLeft());
}
}

View File

@ -0,0 +1,45 @@
package com.songoda.epicenchants.listeners;
import com.songoda.epicenchants.EpicEnchants;
import de.tr7zw.itemnbtapi.NBTItem;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
public class WhiteScrollListener implements Listener {
private final EpicEnchants instance;
public WhiteScrollListener(EpicEnchants instance) {
this.instance = instance;
}
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
public void onInventoryClick(InventoryClickEvent event) {
if (event.getCursor() == null || event.getCurrentItem() == null || event.getAction() != InventoryAction.SWAP_WITH_CURSOR || event.getClickedInventory().getType() == InventoryType.CREATIVE) {
return;
}
NBTItem nbtItem = new NBTItem(event.getCursor());
NBTItem toApplyTo = new NBTItem(event.getCurrentItem());
if (!nbtItem.getBoolean("white-scroll")) {
return;
}
if (toApplyTo.hasKey("protected")) {
event.getWhoClicked().sendMessage(instance.getLocale().getMessageWithPrefix("whitescroll.alreadyapplied"));
return;
}
toApplyTo.setBoolean("protected", true);
event.getWhoClicked().sendMessage(instance.getLocale().getMessageWithPrefix("whitescroll.applied"));
//TODO: add lore
event.getWhoClicked().setItemOnCursor(null);
event.getClickedInventory().setItem(event.getSlot(), toApplyTo.getItem());
}
}

View File

@ -0,0 +1,45 @@
package com.songoda.epicenchants.utils;
import com.songoda.epicenchants.EpicEnchants;
import de.tr7zw.itemnbtapi.NBTItem;
import org.bukkit.inventory.ItemStack;
import java.util.concurrent.ThreadLocalRandom;
import static com.songoda.epicenchants.objects.Placeholder.of;
public class SpecialItems {
private final EpicEnchants instance;
public SpecialItems(EpicEnchants instance) {
this.instance = instance;
}
public ItemStack getWhiteScroll(Integer amount) {
NBTItem nbtItem = new ItemBuilder(instance.getFileManager().getConfiguration("special-items").getConfigurationSection("white-scroll")).nbt();
nbtItem.setBoolean("white-scroll", true);
ItemStack itemStack = nbtItem.getItem();
if (amount != null) {
itemStack.setAmount(amount);
}
return itemStack;
}
public ItemStack getBlackScroll(Integer amount, Integer chance) {
int percentage = chance == null ? ThreadLocalRandom.current().nextInt(instance.getConfig().getInt("rates.black-scroll-min"), instance.getConfig().getInt("rates.black-scroll-max") + 1) : chance;
NBTItem nbtItem = new ItemBuilder(instance.getFileManager().getConfiguration("special-items").getConfigurationSection("black-scroll"), of("percentage", percentage)).nbt();
nbtItem.setBoolean("black-scroll", true);
nbtItem.setInteger("percentage", percentage);
ItemStack itemStack = nbtItem.getItem();
if (amount != null) {
itemStack.setAmount(amount);
}
return itemStack;
}
}

View File

@ -6,6 +6,13 @@ general.nametag.prefix= "&8[&6EpicEnchants&8]"
command.book.received= "&7You have been given a &6{enchant} &7book."
command.book.gave= "&7You gave {player} a &6{enchant} &7book."
command.whitescroll;.received= "&7You have been given a whitescroll."
command.whitescroll.gave= "&7You gave {player} a whitescroll."
command.blackscroll.received= "&7You have been given a blackscroll."
command.blackscroll.gave= "&7You gave {player} a blackscroll."
command.reload= "&6Configuration files reload"
command.filereload.success= "&6{file-name} has been successfully reloaded."
command.filereload.failed= "&c{file-name} failed to be reloaded. &7Please check console for errors."
@ -25,3 +32,9 @@ enchant.success= "&aYou have success fully applied &6{enchant}."
enchant.conflict= "&cYou cannot apply this enchant as it conflicts with another enchant."
enchant.maxedout= "&cYou already have that enchant maxed on this item."
enchant.alreadyapplied= "&cYou already have that enchant with that level applied on this item."
#Item Messages
whitescroll.applied="&aThis item is now protected."
whitescroll.alreadyapplied= "&cThis item is already protected."
blackscroll.success= "&aYou have successfully extracted an enchant from this item."
blackscroll.noenchants= "&cThis item has no enchants to extract"

View File

@ -3,5 +3,4 @@ version: ${project.version}
main: com.songoda.epicenchants.EpicEnchants
authors: [GB6]
website: https://songoda.com/
depend: [Vault]
api-version: 1.13
depend: [Vault]

View File

@ -0,0 +1,16 @@
white-scroll:
material: EMPTY_MAP
display-name: "&e&lWhite Scroll"
lore:
- "&7Prevents an item from being destroyed"
- "&7due to a failed Enchantment Book."
- "&ePlace scroll on item to apply."
black-scroll:
material: INK_SAC
display-name: "&f&lBlack Scroll"
lore:
- "&7Removes a random enchantment"
- "&7from an item and converts"
- "&7it into a {percentage} success book."
- "&fPlace scroll on item to extract."

View File

@ -31,7 +31,6 @@
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
@ -39,5 +38,4 @@
</resource>
</resources>
</build>
</project>