Initial commit
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>ProjectCranked</artifactId>
|
||||
<groupId>net.crankedpvp</groupId>
|
||||
<version>1.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>MonthlyCrates</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<artifactId>Utils</artifactId>
|
||||
<groupId>net.crankedpvp</groupId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,73 @@
|
||||
package net.crankedpvp.monthlycrates;
|
||||
|
||||
import lombok.Getter;
|
||||
import me.mattstudios.mf.base.CommandManager;
|
||||
import me.mattstudios.mf.base.components.TypeResult;
|
||||
import net.crankedpvp.monthlycrates.commands.MonthlyCrateCommand;
|
||||
import net.crankedpvp.monthlycrates.commands.ViewCrateOpenCommand;
|
||||
import net.crankedpvp.monthlycrates.listeners.MonthlyCrateListener;
|
||||
import net.crankedpvp.monthlycrates.managers.MonthlyCrateManager;
|
||||
import net.crankedpvp.monthlycrates.struct.LootType;
|
||||
import net.crankedpvp.monthlycrates.struct.MonthlyCrate;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class MonthlyCratesPlugin extends JavaPlugin {
|
||||
|
||||
@Getter
|
||||
private static MonthlyCratesPlugin instance;
|
||||
|
||||
@Getter
|
||||
private MonthlyCrateManager monthlyCrateManager;
|
||||
|
||||
@Getter
|
||||
private CommandManager commandManager;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
|
||||
instance = this;
|
||||
this.monthlyCrateManager = new MonthlyCrateManager(this);
|
||||
this.monthlyCrateManager.createDefaultConfig("-example");
|
||||
this.monthlyCrateManager.initializeAll();
|
||||
|
||||
this.commandManager = new CommandManager(this);
|
||||
this.registerCommandParameters();
|
||||
this.registerCommandCompletion();
|
||||
this.commandManager.register(new MonthlyCrateCommand(this));
|
||||
this.commandManager.register(new ViewCrateOpenCommand(this));
|
||||
this.registerListeners();
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void registerListeners() {
|
||||
PluginManager pluginManager = this.getServer().getPluginManager();
|
||||
pluginManager.registerEvents(new MonthlyCrateListener(this), this);
|
||||
}
|
||||
|
||||
|
||||
private void registerCommandParameters() {
|
||||
this.commandManager.getParameterHandler().register(LootType.class, argument -> {
|
||||
LootType lootType = LootType.valueOf(String.valueOf(argument).toUpperCase());
|
||||
return new TypeResult(lootType, argument);
|
||||
});
|
||||
}
|
||||
|
||||
public void registerCommandCompletion() {
|
||||
this.commandManager.getCompletionHandler().register("#monthlycrates", input -> this.monthlyCrateManager.getMonthlyCrates().values()
|
||||
.stream().map(MonthlyCrate::getId).collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
instance = null;
|
||||
this.monthlyCrateManager = null;
|
||||
this.commandManager = null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+231
@@ -0,0 +1,231 @@
|
||||
package net.crankedpvp.monthlycrates.commands;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
import net.crankedpvp.monthlycrates.MonthlyCratesPlugin;
|
||||
import net.crankedpvp.monthlycrates.inventory.AdminInventory;
|
||||
import net.crankedpvp.monthlycrates.managers.MonthlyCrateManager;
|
||||
import net.crankedpvp.monthlycrates.struct.LootType;
|
||||
import net.crankedpvp.monthlycrates.utils.Base64;
|
||||
import net.crankedpvp.monthlycrates.utils.Utils;
|
||||
import me.mattstudios.mf.annotations.*;
|
||||
import me.mattstudios.mf.base.CommandBase;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
@Command("monthlycrate")
|
||||
@Alias({"monthlycrates", "crankedcrate", "cc"})
|
||||
public class MonthlyCrateCommand extends CommandBase {
|
||||
|
||||
private final MonthlyCratesPlugin plugin;
|
||||
private final MonthlyCrateManager monthlyCrateManager;
|
||||
public MonthlyCrateCommand(MonthlyCratesPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
this.monthlyCrateManager = plugin.getMonthlyCrateManager();
|
||||
}
|
||||
|
||||
@Default
|
||||
@Permission("cranked.admin")
|
||||
public void defaultCommand(CommandSender commandSender) {
|
||||
Player player = (Player)commandSender;
|
||||
AdminInventory adminInventory = new AdminInventory(player);
|
||||
adminInventory.getUI().open(player);
|
||||
}
|
||||
|
||||
@SubCommand("list")
|
||||
@Permission("cranked.admin")
|
||||
public void listCommand(CommandSender commandSender) {
|
||||
Player player = (Player)commandSender;
|
||||
commandSender.sendMessage("");
|
||||
commandSender.sendMessage(Utils.fixColor("&bMonthly Crates"));
|
||||
this.monthlyCrateManager.getMonthlyCrateObjects().forEach(monthlyCrate -> commandSender.sendMessage(Utils.fixColor(" &b* &f" + monthlyCrate.getId())));
|
||||
player.playSound(player.getLocation(), Sound.FIREWORK_LAUNCH, 2.0F, 2.0F);
|
||||
}
|
||||
|
||||
@SubCommand("reload")
|
||||
@Permission("cranked.admin")
|
||||
public void reloadCommand(CommandSender commandSender, @Optional @Completion("#monthlycrates") String monthlycrate) {
|
||||
Player sender = (Player) commandSender;
|
||||
sender.playSound(sender.getLocation(), Sound.SUCCESSFUL_HIT, 2.0F, 2.0F);
|
||||
if (monthlycrate == null) {
|
||||
this.monthlyCrateManager.clearMonthlyCrateMap();
|
||||
this.monthlyCrateManager.initializeAll();
|
||||
sender.sendMessage(Utils.fixColor("&aYou have successfully reloaded all monthlycrates"));
|
||||
return;
|
||||
}
|
||||
|
||||
this.monthlyCrateManager.reloadMonthlyCrate(monthlycrate);
|
||||
sender.sendMessage(Utils.fixColor("&aYou have successfully reloaded the " + monthlycrate + " monthlycrate"));
|
||||
|
||||
}
|
||||
|
||||
@SubCommand("create")
|
||||
@Permission("cranked.admin")
|
||||
public void createCommand(CommandSender commandSender, String name) {
|
||||
Player sender = (Player)commandSender;
|
||||
if (this.monthlyCrateManager.isMonthlyCrate(name)) {
|
||||
sender.sendMessage(Utils.fixColor("&cThis is already a registered monthly crate"));
|
||||
sender.playSound(sender.getLocation(), Sound.ANVIL_LAND, 2.0F, 2.0F);
|
||||
return;
|
||||
}
|
||||
|
||||
this.monthlyCrateManager.createDefaultConfig(name);
|
||||
this.monthlyCrateManager.initialize(name);
|
||||
sender.sendMessage(Utils.fixColor("&aSuccessfully created the monthlycrate of name: " + name));
|
||||
sender.playSound(sender.getLocation(), Sound.SUCCESSFUL_HIT, 2.0F, 2.0F);
|
||||
}
|
||||
|
||||
@SubCommand("delete")
|
||||
@Permission("cranked.admin")
|
||||
public void deleteCommand(CommandSender commandSender, @Completion("#monthlycrates") String name) {
|
||||
Player player = (Player)commandSender;
|
||||
if (this.monthlyCrateManager.isMonthlyCrate(name)) {
|
||||
this.monthlyCrateManager.removeMonthlyCrate(name);
|
||||
this.monthlyCrateManager.deleteConfig(name);
|
||||
player.sendMessage(Utils.fixColor("&cYou have deleted the monthlycrate of name: " + name));
|
||||
player.playSound(player.getLocation(), Sound.ANVIL_BREAK, 2.0F, 2.0F);
|
||||
} else {
|
||||
player.sendMessage(Utils.fixColor("&cThat is not a valid monthlycrate"));
|
||||
player.playSound(player.getLocation(), Sound.ANVIL_LAND, 2.0F, 2.0F);
|
||||
}
|
||||
}
|
||||
|
||||
@SubCommand("addreward")
|
||||
@Alias({"additem", "addloot"})
|
||||
@Permission("cranked.admin")
|
||||
@WrongUsage("/monthlycrate additem [monthlycrate] [chance] [loot-type]")
|
||||
public void addItemCommand(CommandSender commandSender, @Completion("#monthlycrates") final String name, final Double chance, @Completion("#enum") final LootType lootType) throws IOException {
|
||||
|
||||
Player player = (Player)commandSender;
|
||||
File file = new File(this.plugin.getDataFolder() + "/MonthlyCrates/" + name + ".yml");
|
||||
if(file.exists()) {
|
||||
YamlConfiguration yamlConfiguration = YamlConfiguration.loadConfiguration(file);
|
||||
String itemName = ChatColor.stripColor(player.getItemInHand().getItemMeta().getDisplayName()).replace(".", "").replace(" ", "");
|
||||
String toBase64 = Base64.serialize(player.getItemInHand());
|
||||
if(!yamlConfiguration.contains("rewards") || !yamlConfiguration.getConfigurationSection("rewards").getKeys(true).contains(itemName + player.getItemInHand().getAmount())) {
|
||||
yamlConfiguration.set("rewards." + itemName + player.getItemInHand().getAmount() + ".item", toBase64);
|
||||
yamlConfiguration.set("rewards." + itemName + player.getItemInHand().getAmount() + ".chance", chance);
|
||||
yamlConfiguration.set("rewards." + itemName + player.getItemInHand().getAmount() + ".loot-type", lootType.name());
|
||||
yamlConfiguration.set("rewards." + itemName + player.getItemInHand().getAmount() + ".special-name", player.getItemInHand().getItemMeta().getDisplayName());
|
||||
yamlConfiguration.save(file);
|
||||
player.sendMessage(Utils.fixColor("&aAdded Item to monthlycrate: " + name));
|
||||
player.playSound(player.getLocation(), Sound.LEVEL_UP, 2.0F, 2.0F);
|
||||
this.monthlyCrateManager.reloadMonthlyCrate(name);
|
||||
} else {
|
||||
player.sendMessage(Utils.fixColor("&cThis item is already in the monhtlycrate"));
|
||||
player.playSound(player.getLocation(), Sound.ANVIL_LAND, 2.0F, 2.0F);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@SubCommand("removereward")
|
||||
@Alias({"removeitem", "removeloot"})
|
||||
@Permission("cranked.admin")
|
||||
@WrongUsage("/monthlycrate removeitem [monthlycrate]")
|
||||
public void removeItemCommand(CommandSender commandSender, @Completion("#monthlycrates") String name) {
|
||||
Player player = (Player)commandSender;
|
||||
ItemStack itemStack = player.getItemInHand();
|
||||
File file = new File(this.plugin.getDataFolder() + "/MonthlyCrates/" + name + ".yml");
|
||||
if (file.exists()) {
|
||||
YamlConfiguration yamlConfiguration = YamlConfiguration.loadConfiguration(file);
|
||||
String itemName = ChatColor.stripColor(itemStack.getItemMeta().getDisplayName()).replace(".", "").replace(" ", "");
|
||||
if (!yamlConfiguration.contains("rewards")) {
|
||||
player.sendMessage(Utils.fixColor("&cThis monthlycrate does not have this reward in"));
|
||||
player.playSound(player.getLocation(), Sound.ANVIL_LAND, 2.0F, 2.0F);
|
||||
return;
|
||||
}
|
||||
if (yamlConfiguration.getConfigurationSection("rewards").getKeys(true).contains(itemName + itemStack.getAmount())) {
|
||||
yamlConfiguration.set("rewards." + itemName + itemStack.getAmount(), null);
|
||||
yamlConfiguration.save(file);
|
||||
player.sendMessage(Utils.fixColor("&aSuccessfully removed item from monthlycrate"));
|
||||
player.playSound(player.getLocation(), Sound.CHICKEN_EGG_POP, 2.0F, 2.0F);
|
||||
this.monthlyCrateManager.reloadMonthlyCrate(name);
|
||||
} else {
|
||||
player.sendMessage(Utils.fixColor("&cThis monthlycrate does not have this reward in"));
|
||||
player.playSound(player.getLocation(), Sound.ANVIL_LAND, 2.0F, 2.0F);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@SubCommand("give")
|
||||
@Permission("cranked.admin")
|
||||
public void giveCommand(CommandSender commandSender, @Completion("#players") final Player player, @Completion("#monthlycrates") final String name, Integer amount) {
|
||||
if (!player.isOnline()) {
|
||||
commandSender.sendMessage(Utils.fixColor("&cThat player is not online"));
|
||||
if(commandSender instanceof Player)
|
||||
((Player)commandSender).playSound(player.getLocation(), Sound.ANVIL_LAND, 2.0F, 2.0F);
|
||||
return;
|
||||
}
|
||||
if (!this.monthlyCrateManager.isMonthlyCrate(name)) {
|
||||
commandSender.sendMessage(Utils.fixColor("&cThe crate by name " + name + " is not a valid crate"));
|
||||
if(commandSender instanceof Player)
|
||||
((Player)commandSender).playSound(player.getLocation(), Sound.ANVIL_LAND, 2.0F, 2.0F);
|
||||
return;
|
||||
}
|
||||
amount = (amount <= 0) ? 1 : amount;
|
||||
player.getInventory().addItem(this.monthlyCrateManager.getMonthlyCrate(name).createMonthlyCrate(amount, player));
|
||||
commandSender.sendMessage(Utils.fixColor("&cGiven monthlycrate to " + player.getName()));
|
||||
player.sendMessage(Utils.fixColor("&cReceived the " + name + " monthlycrate"));
|
||||
player.playSound(player.getLocation(), Sound.ITEM_PICKUP, 2.0F, 2.0F);
|
||||
}
|
||||
|
||||
@SubCommand("disable")
|
||||
@Permission("cranked.admin")
|
||||
public void disableCommand(CommandSender commandSender, @Completion("#monthlycrates") String name) {
|
||||
if (!this.monthlyCrateManager.isMonthlyCrate(name))
|
||||
return;
|
||||
File file = new File(this.plugin.getDataFolder() + "/MonthlyCrates/" + name + ".yml");
|
||||
if (file.renameTo(new File(this.plugin.getDataFolder() + "/MonthlyCrates/-" + name + ".yml"))) {
|
||||
this.monthlyCrateManager.removeMonthlyCrate(name);
|
||||
commandSender.sendMessage(Utils.fixColor("&cDisabled monthlycrate: " + name));
|
||||
Player player = (Player)commandSender;
|
||||
player.playSound(player.getLocation(), Sound.BAT_TAKEOFF, 2.0F, 2.0F);
|
||||
}
|
||||
}
|
||||
|
||||
@SubCommand("enable")
|
||||
@Permission("cranked.admin")
|
||||
public void enableCommand(CommandSender commandSender, String name) {
|
||||
if (this.monthlyCrateManager.isMonthlyCrate(name))
|
||||
return;
|
||||
File file = new File(this.plugin.getDataFolder() + "/MonthlyCrates/-" + name + ".yml");
|
||||
if (file.renameTo(new File(this.plugin.getDataFolder() + "/MonthlyCrates/" + name + ".yml"))) {
|
||||
this.monthlyCrateManager.initialize(name);
|
||||
commandSender.sendMessage(Utils.fixColor("&aEnabled monthlycrate: " + name));
|
||||
Player player = (Player)commandSender;
|
||||
player.playSound(player.getLocation(), Sound.SUCCESSFUL_HIT, 2.0F, 2.0F);
|
||||
}
|
||||
}
|
||||
|
||||
@SubCommand("help")
|
||||
@Permission("cranked.admin")
|
||||
public void helpMessage(CommandSender commandSender) {
|
||||
commandSender.sendMessage(Utils.fixColor("&a-------[ &2MonthlyCrates &a]-------"));
|
||||
commandSender.sendMessage(Utils.fixColor("&a[] &8- &7Optional argument"));
|
||||
commandSender.sendMessage(Utils.fixColor("&2<> &8- &7Required argument"));
|
||||
commandSender.sendMessage("");
|
||||
commandSender.sendMessage(Utils.fixColor(" &2* &a/mc create <name>"));
|
||||
commandSender.sendMessage(Utils.fixColor(" &2* &a/mc delete <name>"));
|
||||
commandSender.sendMessage(Utils.fixColor(" &2* &a/mc enable <name>"));
|
||||
commandSender.sendMessage(Utils.fixColor(" &2* &a/mc disable <name>"));
|
||||
commandSender.sendMessage(Utils.fixColor(" &2* &a/mc additem <monthlycrate> <chance>"));
|
||||
commandSender.sendMessage(Utils.fixColor(" &2* &a/mc removeitem <monthlycrate>"));
|
||||
commandSender.sendMessage(Utils.fixColor(" &2* &a/mc reload [monthlycrate]"));
|
||||
commandSender.sendMessage(Utils.fixColor(" &2* &a/mc list"));
|
||||
commandSender.sendMessage(Utils.fixColor(" &2* &a/mc give <player> <monthlycrate> <amount>"));
|
||||
commandSender.sendMessage(Utils.fixColor("&a-------[ &2MonthlyCrates &a]-------"));
|
||||
if(commandSender instanceof Player) {
|
||||
Player player = (Player) commandSender;
|
||||
player.playSound(player.getLocation(), Sound.LEVEL_UP, 2.0F, 2.0F);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package net.crankedpvp.monthlycrates.commands;
|
||||
|
||||
import me.mattstudios.mf.annotations.Command;
|
||||
import me.mattstudios.mf.annotations.Default;
|
||||
import me.mattstudios.mf.base.CommandBase;
|
||||
import net.crankedpvp.crankedutils.utils.Arrow;
|
||||
import net.crankedpvp.crankedutils.utils.CC;
|
||||
import net.crankedpvp.crankedutils.utils.SoundUtils;
|
||||
import net.crankedpvp.monthlycrates.MonthlyCratesPlugin;
|
||||
import net.crankedpvp.monthlycrates.inventory.MonthlyCrateInventory;
|
||||
import net.crankedpvp.monthlycrates.managers.MonthlyCrateManager;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Command("viewcrateopen")
|
||||
public class ViewCrateOpenCommand extends CommandBase {
|
||||
|
||||
private final MonthlyCratesPlugin plugin;
|
||||
public ViewCrateOpenCommand(MonthlyCratesPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Default
|
||||
public void defaultCommand(final Player player, String crateID) {
|
||||
MonthlyCrateManager monthlyCrateManager = this.plugin.getMonthlyCrateManager();
|
||||
if(!monthlyCrateManager.getMonthlyCrateBeingOpened().containsKey(UUID.fromString(crateID))) return;
|
||||
|
||||
MonthlyCrateInventory monthlyCrateInventory = monthlyCrateManager.getMonthlyCrateBeingOpened().get(UUID.fromString(crateID));
|
||||
if(!monthlyCrateInventory.isActive()) {
|
||||
player.sendMessage(CC.translate("&d&lMonthly Crates &8" + Arrow.arrow() + " &eThat monthly crate has finished opened"));
|
||||
SoundUtils.tryPlayErrorSound(player);
|
||||
return;
|
||||
}
|
||||
monthlyCrateManager.getMonthlyCrateBeingOpened().get(UUID.fromString(crateID)).getUI().open(player);
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package net.crankedpvp.monthlycrates.inventory;
|
||||
|
||||
import dev.triumphteam.gui.guis.GuiItem;
|
||||
import dev.triumphteam.gui.guis.PaginatedGui;
|
||||
import net.crankedpvp.monthlycrates.MonthlyCratesPlugin;
|
||||
import net.crankedpvp.monthlycrates.struct.MonthlyCrate;
|
||||
import net.crankedpvp.monthlycrates.utils.ItemBuilder;
|
||||
import net.crankedpvp.monthlycrates.utils.Utils;
|
||||
import org.bukkit.DyeColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class AdminInventory {
|
||||
private final PaginatedGui adminInventory;
|
||||
|
||||
public PaginatedGui getUI() {
|
||||
return this.adminInventory;
|
||||
}
|
||||
|
||||
Map<String, MonthlyCrate> monthlyCrateMap = MonthlyCratesPlugin.getInstance().getMonthlyCrateManager().getMonthlyCrates();
|
||||
|
||||
public AdminInventory(Player player) {
|
||||
this.adminInventory = new PaginatedGui(6, 36, "Cranked Monthly Crates");
|
||||
this.adminInventory.setDefaultClickAction(event -> event.setCancelled(true));
|
||||
this.adminInventory.getFiller().fillBottom(new GuiItem(createFillerItem()));
|
||||
ItemStack prevPage = new ItemBuilder(Material.ARROW, "&c<- Previous Page", "&7Right-Click&7 to go to the previous page.").colorize().build();
|
||||
ItemStack nextPage = new ItemBuilder(Material.ARROW, "&aNext Page ->", "&7Right-Click&7 to go to the next page.").colorize().build();
|
||||
this.adminInventory.setItem(6, 1, new GuiItem(prevPage, event -> {
|
||||
this.adminInventory.previous();
|
||||
event.setCancelled(true);
|
||||
}));
|
||||
this.adminInventory.setItem(6, 9, new GuiItem(nextPage, event -> {
|
||||
this.adminInventory.next();
|
||||
event.setCancelled(true);
|
||||
}));
|
||||
for (MonthlyCrate monthlyCrate : this.monthlyCrateMap.values()) {
|
||||
ItemStack item = monthlyCrate.createMonthlyCrate(1, player);
|
||||
this.adminInventory.addItem(new GuiItem(item, event -> {
|
||||
if(player.hasPermission("cranked.admin")) player.getInventory().addItem(item);
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
private ItemStack createFillerItem() {
|
||||
ItemStack item = new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.GRAY.getData());
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
meta.setDisplayName(Utils.fixColor("&cCranked&fPvP"));
|
||||
item.setItemMeta(meta);
|
||||
return item;
|
||||
}
|
||||
}
|
||||
+162
@@ -0,0 +1,162 @@
|
||||
package net.crankedpvp.monthlycrates.inventory;
|
||||
|
||||
import dev.triumphteam.gui.guis.Gui;
|
||||
import dev.triumphteam.gui.guis.GuiItem;
|
||||
import lombok.Getter;
|
||||
import net.crankedpvp.monthlycrates.MonthlyCratesPlugin;
|
||||
import net.crankedpvp.monthlycrates.struct.LootType;
|
||||
import net.crankedpvp.monthlycrates.struct.MonthlyCrate;
|
||||
import net.crankedpvp.monthlycrates.utils.Utils;
|
||||
import org.bukkit.DyeColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class MonthlyCrateInventory {
|
||||
|
||||
private final int amountOfRewards;
|
||||
|
||||
@Getter
|
||||
private boolean active = true;
|
||||
|
||||
private final MonthlyCratesPlugin plugin = MonthlyCratesPlugin.getInstance();
|
||||
|
||||
private final Gui adminInventory;
|
||||
|
||||
public Gui getUI() {
|
||||
return this.adminInventory;
|
||||
}
|
||||
|
||||
@Getter
|
||||
private final Map<Integer, Integer> taskBySlot = new HashMap<>();
|
||||
|
||||
private int spinningItems = 0;
|
||||
private int spunItems = 0;
|
||||
|
||||
private boolean openedMystery = false;
|
||||
|
||||
private final ItemStack[] preDefinedRewards;
|
||||
private final ItemStack preDefinedBonusReward;
|
||||
|
||||
public MonthlyCrateInventory(Player player, MonthlyCrate monthlyCrate) {
|
||||
this.amountOfRewards = monthlyCrate.getAmountOfRewards();
|
||||
this.preDefinedRewards = new ItemStack[monthlyCrate.getAmountOfRewards()];
|
||||
for(int i = 0; i < this.amountOfRewards; i++)
|
||||
this.preDefinedRewards[i] = (monthlyCrate.getRandomItemWithBlacklist(Collections.singletonList(LootType.BONUS)));
|
||||
this.preDefinedBonusReward = monthlyCrate.getRandomItemWithWhitelist(Collections.singletonList(LootType.BONUS));
|
||||
this.adminInventory = new Gui(6, "Monthly Crate: " + monthlyCrate.getId());
|
||||
this.adminInventory.setDefaultClickAction(event -> event.setCancelled(true));
|
||||
|
||||
this.adminInventory.setCloseGuiAction(inventoryCloseEvent -> {
|
||||
if(inventoryCloseEvent.getPlayer() != player) return;
|
||||
this.active = false;
|
||||
player.getInventory().addItem(preDefinedBonusReward);
|
||||
Arrays.stream(preDefinedRewards).forEach(reward -> player.getInventory().addItem(reward));
|
||||
|
||||
for(HumanEntity viewer : this.adminInventory.getInventory().getViewers()) {
|
||||
this.adminInventory.close(viewer);
|
||||
}
|
||||
});
|
||||
this.adminInventory.getFiller().fillBorder(new GuiItem(createFillerItem()));
|
||||
this.adminInventory.getFiller().fillBetweenPoints(5, 1, 5, 9, new GuiItem(createFillerItem()));
|
||||
this.adminInventory.getFiller().fill(new GuiItem(createMysteryItem(), event -> {
|
||||
if(event.getWhoClicked() != player) return;
|
||||
if(spinningItems >= this.amountOfRewards) return;
|
||||
updateItemTask(event.getSlot(), monthlyCrate, spinningItems, player);
|
||||
spinningItems++;
|
||||
}));
|
||||
|
||||
for(int i = 48; i < 51; i++) {
|
||||
this.adminInventory.updateItem(i, new GuiItem(createBlockedItem()));
|
||||
}
|
||||
}
|
||||
|
||||
private void allowOpeningOfBonusItems(MonthlyCrate monthlyCrate, Player player) {
|
||||
for(int i = 48; i < 51; i++) {
|
||||
int finalI = i;
|
||||
this.adminInventory.updateItem(i, new GuiItem(createMysteryBonusItem(), event -> {
|
||||
if(event.getWhoClicked() != player) return;
|
||||
if(!openedMystery) {
|
||||
this.adminInventory.updateItem(finalI, new GuiItem(this.preDefinedBonusReward));
|
||||
player.playSound(player.getLocation(), Sound.FIREWORK_LARGE_BLAST, 2.0F, 2.0F);
|
||||
plugin.getServer().getScheduler().runTaskLater(plugin, () -> {
|
||||
for(int j = 48; j < 51; j++) {
|
||||
if(j != finalI)
|
||||
this.adminInventory.updateItem(j, new GuiItem(monthlyCrate.getRandomItemWithWhitelist(Collections.singletonList(LootType.BONUS))));
|
||||
}
|
||||
}, 20L);
|
||||
plugin.getServer().getScheduler().runTaskLater(plugin, () -> {
|
||||
player.playSound(player.getLocation(), Sound.CHEST_CLOSE, 2.0F, 2.0F);
|
||||
player.closeInventory();
|
||||
}, 40L);
|
||||
}
|
||||
openedMystery = true;
|
||||
this.active = false;
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
private ItemStack createMysteryBonusItem() {
|
||||
ItemStack item = new ItemStack(Material.ENDER_CHEST);
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
meta.setDisplayName(Utils.fixColor("&d&l???"));
|
||||
item.setItemMeta(meta);
|
||||
return item;
|
||||
}
|
||||
|
||||
private void updateItemTask(int slot, MonthlyCrate monthlyCrate, int index, Player player) {
|
||||
AtomicInteger counter = new AtomicInteger();
|
||||
this.taskBySlot.put(slot, plugin.getServer().getScheduler().scheduleAsyncRepeatingTask(plugin, () -> {
|
||||
if(!player.getOpenInventory().getTopInventory().getTitle().contains("Monthly Crate")) plugin.getServer().getScheduler().cancelTask(this.taskBySlot.get(slot));;
|
||||
player.playSound(player.getLocation(), Sound.SUCCESSFUL_HIT, 1.0F, 1.0F);
|
||||
this.adminInventory.updateItem(slot, new GuiItem(monthlyCrate.getRandomItemWithBlacklist(Collections.singletonList(LootType.BONUS))));
|
||||
counter.getAndIncrement();
|
||||
if(counter.get() == 35) {
|
||||
plugin.getServer().getScheduler().cancelTask(this.taskBySlot.get(slot));
|
||||
this.plugin.getServer().getScheduler().runTaskLater(plugin, () -> this.adminInventory.updateItem(slot, new GuiItem(this.preDefinedRewards[index])), 5L);
|
||||
spunItems++;
|
||||
if(spunItems == this.amountOfRewards) {
|
||||
player.playSound(player.getLocation(), Sound.FIREWORK_LAUNCH, 2.0F, 2.0F);
|
||||
this.plugin.getServer().getScheduler().runTaskLater(plugin, () -> {
|
||||
allowOpeningOfBonusItems(monthlyCrate, player);
|
||||
player.playSound(player.getLocation(), Sound.CLICK, 1.0F, 1.0F);
|
||||
}, 20L);
|
||||
}
|
||||
}
|
||||
}, 0L, 3L));
|
||||
}
|
||||
|
||||
private ItemStack createBlockedItem() {
|
||||
ItemStack item = new ItemStack(Material.BARRIER);
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
meta.setDisplayName(Utils.fixColor("&c&lWAIT"));
|
||||
meta.setLore(Collections.singletonList(Utils.fixColor("&7Open " + (this.amountOfRewards-spinningItems) + " mystery items first")));
|
||||
item.setItemMeta(meta);
|
||||
return item;
|
||||
}
|
||||
|
||||
private ItemStack createMysteryItem() {
|
||||
ItemStack item = new ItemStack(Material.ENDER_CHEST);
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
meta.setDisplayName(Utils.fixColor("&f&l???"));
|
||||
item.setItemMeta(meta);
|
||||
return item;
|
||||
}
|
||||
|
||||
private ItemStack createFillerItem() {
|
||||
ItemStack item = new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.GRAY.getData());
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
meta.setDisplayName(Utils.fixColor("&cCranked&fPvP"));
|
||||
item.setItemMeta(meta);
|
||||
return item;
|
||||
}
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package net.crankedpvp.monthlycrates.listeners;
|
||||
|
||||
import de.tr7zw.nbtapi.NBTItem;
|
||||
import net.crankedpvp.crankedutils.utils.CC;
|
||||
import net.crankedpvp.crankedutils.utils.Clickable;
|
||||
import net.crankedpvp.monthlycrates.MonthlyCratesPlugin;
|
||||
import net.crankedpvp.monthlycrates.inventory.MonthlyCrateInventory;
|
||||
import net.crankedpvp.monthlycrates.managers.MonthlyCrateManager;
|
||||
import net.crankedpvp.monthlycrates.struct.MonthlyCrate;
|
||||
import net.crankedpvp.monthlycrates.utils.Utils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.Action;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class MonthlyCrateListener implements Listener {
|
||||
|
||||
private final MonthlyCratesPlugin plugin;
|
||||
public MonthlyCrateListener(MonthlyCratesPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerInteract(PlayerInteractEvent event) {
|
||||
if(event.getAction() == null) return;
|
||||
if(event.getAction() != Action.RIGHT_CLICK_BLOCK && event.getAction() != Action.RIGHT_CLICK_AIR) return;
|
||||
if(event.getItem() == null) return;
|
||||
|
||||
ItemStack itemStack = event.getItem();
|
||||
if(!MonthlyCrateManager.isMonthlyCrate(itemStack)) return;
|
||||
|
||||
event.setCancelled(true);
|
||||
NBTItem nbtItem = new NBTItem(itemStack);
|
||||
MonthlyCrate monthlyCrate = this.plugin.getMonthlyCrateManager().getMonthlyCrate(nbtItem.getString("monthlyCrateType"));
|
||||
|
||||
if(!Utils.hasAvaliableSlot(event.getPlayer(), 10)) {
|
||||
event.getPlayer().sendMessage(Utils.fixColor("&cYour inventory does not have enough open slots to open this"));
|
||||
event.getPlayer().playSound(event.getPlayer().getLocation(), Sound.LAVA_POP, 1.0F, 1.0F);
|
||||
return;
|
||||
}
|
||||
|
||||
int amount = itemStack.getAmount();
|
||||
if (amount <= 1) {
|
||||
event.getPlayer().setItemInHand(null);
|
||||
} else {
|
||||
itemStack.setAmount(amount - 1);
|
||||
event.getPlayer().setItemInHand(itemStack);
|
||||
}
|
||||
|
||||
MonthlyCrateInventory monthlyCrateInventory = new MonthlyCrateInventory(event.getPlayer(), monthlyCrate);
|
||||
monthlyCrateInventory.getUI().open(event.getPlayer());
|
||||
|
||||
UUID uuid = UUID.randomUUID();
|
||||
this.plugin.getMonthlyCrateManager().getMonthlyCrateBeingOpened().put(uuid, monthlyCrateInventory);
|
||||
|
||||
Clickable clickable = new Clickable("&f✧ &eClick to watch &6" + event.getPlayer().getName() + " &eopen a " + monthlyCrate.getDisplayName(), "&eClick to view &6" + event.getPlayer().getName() + "'s &emonthlycrate &f✧", "/viewcrateopen " + uuid);
|
||||
clickable.add(CC.translate("&eYou can purchase this monthlycrate at &fbuy.crankedpvp.org&e!"));
|
||||
for(Player onlinePlayer : Bukkit.getOnlinePlayers()) {
|
||||
onlinePlayer.sendMessage("");
|
||||
clickable.sendToPlayer(onlinePlayer);
|
||||
onlinePlayer.sendMessage("");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
+184
@@ -0,0 +1,184 @@
|
||||
package net.crankedpvp.monthlycrates.managers;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Maps;
|
||||
import de.tr7zw.nbtapi.NBTItem;
|
||||
import lombok.Getter;
|
||||
import net.crankedpvp.monthlycrates.MonthlyCratesPlugin;
|
||||
import net.crankedpvp.monthlycrates.inventory.MonthlyCrateInventory;
|
||||
import net.crankedpvp.monthlycrates.struct.LootType;
|
||||
import net.crankedpvp.monthlycrates.struct.MonthlyCrate;
|
||||
import net.crankedpvp.monthlycrates.struct.Reward;
|
||||
import net.crankedpvp.monthlycrates.utils.Base64;
|
||||
import org.apache.commons.lang.Validate;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public class MonthlyCrateManager {
|
||||
|
||||
private final MonthlyCratesPlugin plugin;
|
||||
public MonthlyCrateManager(MonthlyCratesPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
this.monthlyCrates = Maps.newConcurrentMap();
|
||||
this.monthlyCrateBeingOpened = Maps.newConcurrentMap();
|
||||
}
|
||||
|
||||
@Getter
|
||||
private final Map<String, MonthlyCrate> monthlyCrates;
|
||||
|
||||
@Getter
|
||||
private final Map<UUID, MonthlyCrateInventory> monthlyCrateBeingOpened;
|
||||
|
||||
public void initializeAll() {
|
||||
File[] files = (new File(this.plugin.getDataFolder(), "MonthlyCrates")).listFiles();
|
||||
assert files != null;
|
||||
Arrays.stream(files)
|
||||
.filter(file -> !file.getName().startsWith("-"))
|
||||
.filter(file -> file.getName().endsWith(".yml"))
|
||||
.forEach(file -> {
|
||||
try {
|
||||
initialize(file.getName().replace(".yml", ""));
|
||||
} catch (NullPointerException exception) {
|
||||
System.out.println("[MonthlyCrates] Failed to register monthlycrate: " + file.getName().replace(".yml", ""));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void initialize(String name) {
|
||||
File file = new File(this.plugin.getDataFolder(), "MonthlyCrates/" + name + ".yml");
|
||||
YamlConfiguration yamlConfiguration = new YamlConfiguration();
|
||||
try {
|
||||
yamlConfiguration.load(file);
|
||||
} catch (IOException |org.bukkit.configuration.InvalidConfigurationException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
String displayName = yamlConfiguration.getString("Item.Display.Name");
|
||||
List<String> displayLore = yamlConfiguration.getStringList("Item.Display.Lore");
|
||||
Material material = Material.valueOf(yamlConfiguration.getString("Item.Material").toUpperCase());
|
||||
boolean glowing = yamlConfiguration.getBoolean("Item.Glowing");
|
||||
List<Reward> rewards = getRewards(yamlConfiguration);
|
||||
int amountOfRewards = yamlConfiguration.contains("Reward.Amount") ? yamlConfiguration.getInt("Reward.Amount") : 0;
|
||||
MonthlyCrate monthlyCrate = new MonthlyCrate(name, displayName, displayLore, material, glowing, amountOfRewards, rewards);
|
||||
this.register(monthlyCrate);
|
||||
System.out.println("[MonthlyCrates] Registered monthlycrate: " + name);
|
||||
}
|
||||
|
||||
private List<Reward> getRewards(FileConfiguration fileConfiguration) {
|
||||
List<Reward> rewards = new ArrayList<>();
|
||||
if (fileConfiguration.getConfigurationSection("rewards") == null) return rewards;
|
||||
fileConfiguration.getConfigurationSection("rewards").getKeys(false).forEach(key -> {
|
||||
ItemStack itemStack = null;
|
||||
try {
|
||||
itemStack = Base64.deserialize(fileConfiguration.getConfigurationSection("rewards." + key).getString("item").replace("\r\n", ""));
|
||||
} catch (IOException|ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Reward reward = new Reward(itemStack, fileConfiguration.getConfigurationSection("rewards." + key).getDouble("chance"), LootType.valueOf(fileConfiguration.getConfigurationSection("rewards." + key).getString("loot-type").toUpperCase()), fileConfiguration.getConfigurationSection("rewards."+key).getString("special-name"));
|
||||
rewards.add(reward);
|
||||
});
|
||||
return rewards;
|
||||
}
|
||||
|
||||
public void createDefaultConfig(String name) {
|
||||
File file = new File(this.plugin.getDataFolder(), "MonthlyCrates/" + name + ".yml");
|
||||
if (!file.exists()) {
|
||||
file.getParentFile().mkdirs();
|
||||
try {
|
||||
file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
YamlConfiguration yamlConfiguration = new YamlConfiguration();
|
||||
yamlConfiguration.set("Item.Display.Name", "&f&ki&r&5✧&f&ki&r &f&lCRANKED CRATE: &6&lExample &f&ki&r&5✧&f&ki&r");
|
||||
yamlConfiguration.set("Item.Display.Lore", Arrays.asList("&7&oUnlocked by %player% from &f&nbuy.crankedpvp.org",
|
||||
"",
|
||||
"&f&lADMIN ITEMS",
|
||||
"%admin-rewards% %color-&f&l%",
|
||||
"",
|
||||
"&e&lCOSMETIC ITEMS",
|
||||
"%cosmetic-rewards% %color-&e&l%",
|
||||
"",
|
||||
"&6&lTREASURE ITEMS",
|
||||
"%treasure-rewards% %color-&6&l%",
|
||||
"",
|
||||
"&d&lBONUS ITEMS",
|
||||
"%bonus-rewards% %color-&d&l%"));
|
||||
yamlConfiguration.set("Item.Material", Material.ENDER_CHEST.name());
|
||||
yamlConfiguration.set("Item.Glowing", true);
|
||||
yamlConfiguration.set("Reward.Amount", 9);
|
||||
try {
|
||||
yamlConfiguration.save(file);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void reloadMonthlyCrate(String monthlycrate) {
|
||||
if (isMonthlyCrate(monthlycrate)) {
|
||||
removeMonthlyCrate(monthlycrate);
|
||||
initialize(monthlycrate);
|
||||
} else {
|
||||
System.out.println("[-] Failure reloading monthlycrate, " + monthlycrate);
|
||||
}
|
||||
}
|
||||
|
||||
public void register(MonthlyCrate monthlyCrate) {
|
||||
try {
|
||||
this.monthlyCrates.put(monthlyCrate.getId(), monthlyCrate);
|
||||
this.plugin.getLogger().info("Successfully registered monthlyCrate: " + monthlyCrate.getId());
|
||||
} catch (Exception var3) {
|
||||
System.err.println("&8(&c!&8) Failed to register monthlyCrate, " + monthlyCrate.getId());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isMonthlyCrate(String name) {
|
||||
return (getMonthlyCrate(name) != null);
|
||||
}
|
||||
|
||||
public MonthlyCrate getMonthlyCrate(String name) {
|
||||
Validate.notNull(name);
|
||||
Validate.notEmpty(name);
|
||||
return this.monthlyCrates.get(name);
|
||||
}
|
||||
|
||||
public void clearMonthlyCrateMap() {
|
||||
this.monthlyCrates.clear();
|
||||
}
|
||||
|
||||
public void removeMonthlyCrate(String monthlycrate) {
|
||||
this.monthlyCrates.remove(monthlycrate);
|
||||
}
|
||||
|
||||
public Collection<MonthlyCrate> getMonthlyCrateObjects() {
|
||||
return Collections.unmodifiableCollection(this.monthlyCrates.values());
|
||||
}
|
||||
|
||||
public MonthlyCrate select(Collection<MonthlyCrate> monthlyCrateCollection) {
|
||||
return (monthlyCrateCollection.size() > 0) ? ImmutableList.copyOf(monthlyCrateCollection).get(ThreadLocalRandom.current().nextInt(monthlyCrateCollection.size())) : null;
|
||||
}
|
||||
|
||||
public static boolean isMonthlyCrate(ItemStack itemStack) {
|
||||
if (itemStack == null || itemStack.getType() == Material.AIR) return false;
|
||||
NBTItem nbtItem = new NBTItem(itemStack);
|
||||
if (nbtItem.hasNBTData()) {
|
||||
if (nbtItem.hasKey("crankedCustomItem")) {
|
||||
return nbtItem.getString("crankedCustomItem").equalsIgnoreCase("monthlyCrate");
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void deleteConfig(String name) {
|
||||
File file = new File(this.plugin.getDataFolder(), "MonthlyCrates/" + name + ".yml");
|
||||
if (file.exists())
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package net.crankedpvp.monthlycrates.struct;
|
||||
|
||||
public enum LootType {
|
||||
|
||||
ADMIN,
|
||||
COSMETIC,
|
||||
TREASURE,
|
||||
BONUS,
|
||||
RARE,
|
||||
LEGENDARY
|
||||
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package net.crankedpvp.monthlycrates.struct;
|
||||
|
||||
import de.tr7zw.nbtapi.NBTItem;
|
||||
import lombok.Getter;
|
||||
import net.crankedpvp.crankedutils.utils.CC;
|
||||
import net.crankedpvp.crankedutils.utils.RandomUtils;
|
||||
import net.crankedpvp.monthlycrates.utils.ItemBuilder;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@Getter
|
||||
public class MonthlyCrate {
|
||||
|
||||
private final String id;
|
||||
private final String displayName;
|
||||
private final List<String> lore;
|
||||
private final Material material;
|
||||
private final boolean glowing;
|
||||
|
||||
private final int amountOfRewards;
|
||||
|
||||
private final List<Reward> rewards;
|
||||
|
||||
public MonthlyCrate(String id, String displayName, List<String> lore, Material material, boolean glowing, int amountOfRewards, List<Reward> rewards) {
|
||||
this.id = id;
|
||||
this.displayName = displayName;
|
||||
this.lore = lore;
|
||||
this.material = material;
|
||||
this.glowing = glowing;
|
||||
this.amountOfRewards = amountOfRewards;
|
||||
this.rewards = rewards;
|
||||
}
|
||||
|
||||
public MonthlyCrate(String id, String displayName, List<String> lore, Material material, boolean glowing, List<Reward> rewards) {
|
||||
this.id = id;
|
||||
this.displayName = displayName;
|
||||
this.lore = lore;
|
||||
this.material = material;
|
||||
this.glowing = glowing;
|
||||
this.amountOfRewards = 9;
|
||||
this.rewards = rewards;
|
||||
}
|
||||
|
||||
|
||||
public List<String> getLore(String playerName) {
|
||||
List<String> newLore = new ArrayList<>();
|
||||
for(String line : lore) {
|
||||
if(line.contains("rewards%")) {
|
||||
LootType lootType = LootType.valueOf(line.split("-")[0].toUpperCase().replace("%", ""));
|
||||
Stream<Reward> rewardStream = this.rewards.stream().filter(reward -> reward.getLootType() == lootType);
|
||||
rewardStream.forEach(reward -> {
|
||||
ItemStack itemStack = reward.getItemStack();
|
||||
//String amount = itemStack.getAmount() <= 1 ? "&f&l* " : "&f&l* " + itemStack.getAmount() + "x &r";
|
||||
String amount = "&f&l* " + itemStack.getAmount() + "x &r";
|
||||
amount = CC.translate(amount);
|
||||
if(reward.getGetSpecialName() == null || reward.getGetSpecialName().equals("null")) {
|
||||
newLore.add(amount + reward.getItemStack().getItemMeta().getDisplayName());
|
||||
} else {
|
||||
newLore.add(amount + reward.getGetSpecialName());
|
||||
}
|
||||
});
|
||||
} else {
|
||||
newLore.add(line.replace("%player%", playerName).replace("%amount%", String.valueOf(this.amountOfRewards)));
|
||||
}
|
||||
}
|
||||
return newLore;
|
||||
}
|
||||
|
||||
|
||||
public ItemStack createMonthlyCrate(int amount, Player player) {
|
||||
String playerName = (player == null) ? "Console" : player.getName();
|
||||
ItemBuilder itemBuilder = new ItemBuilder(material, amount, (short)0, displayName).setLore(getLore(playerName));
|
||||
if(glowing) itemBuilder.setGlowing();
|
||||
NBTItem nbtItem = new NBTItem(itemBuilder.colorize().build());
|
||||
nbtItem.setString("crankedCustomItem", "monthlyCrate");
|
||||
nbtItem.setString("monthlyCrateType", getId());
|
||||
return nbtItem.getItem();
|
||||
}
|
||||
|
||||
public ItemStack createMonthlyCrate(int amount) {
|
||||
return createMonthlyCrate(amount, null);
|
||||
}
|
||||
|
||||
public ItemStack createMonthlyCrate() {
|
||||
return createMonthlyCrate(1);
|
||||
}
|
||||
|
||||
public ItemStack getRandomItemWithBlacklist(List<LootType> blacklistedLootTypes) {
|
||||
Map<ItemStack, Double> rewards = new HashMap<>();
|
||||
this.rewards.stream()
|
||||
.filter(reward -> !blacklistedLootTypes.contains(reward.getLootType()))
|
||||
.forEach(reward -> rewards.put(reward.getItemStack(), reward.getChance()));
|
||||
return RandomUtils.getRandomItem(rewards);
|
||||
}
|
||||
|
||||
public ItemStack getRandomItemWithWhitelist(List<LootType> whitelistedLootTypes) {
|
||||
Map<ItemStack, Double> rewards = new HashMap<>();
|
||||
this.rewards.stream()
|
||||
.filter(reward -> whitelistedLootTypes.contains(reward.getLootType()))
|
||||
.forEach(reward -> rewards.put(reward.getItemStack(), reward.getChance()));
|
||||
return RandomUtils.getRandomItem(rewards);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package net.crankedpvp.monthlycrates.struct;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
public class Reward {
|
||||
|
||||
private final ItemStack itemStack;
|
||||
private final double chance;
|
||||
private final LootType lootType;
|
||||
private final String getSpecialName;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package net.crankedpvp.monthlycrates.utils;
|
||||
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.util.io.BukkitObjectInputStream;
|
||||
import org.bukkit.util.io.BukkitObjectOutputStream;
|
||||
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class Base64 {
|
||||
public static ItemStack deserialize(String base64) throws IOException, ClassNotFoundException {
|
||||
byte[] data = Base64Coder.decodeLines(base64);
|
||||
ByteArrayInputStream bytesIn = new ByteArrayInputStream(data);
|
||||
BukkitObjectInputStream in = new BukkitObjectInputStream(bytesIn);
|
||||
ItemStack itemStack = (ItemStack) in.readObject();
|
||||
in.close();
|
||||
return itemStack;
|
||||
}
|
||||
|
||||
public static String serialize(ItemStack itemStack) throws IOException {
|
||||
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
|
||||
BukkitObjectOutputStream out = new BukkitObjectOutputStream(bytesOut);
|
||||
out.writeObject(itemStack);
|
||||
out.flush();
|
||||
out.close();
|
||||
return Base64Coder.encodeLines(bytesOut.toByteArray());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,250 @@
|
||||
package net.crankedpvp.monthlycrates.utils;
|
||||
|
||||
|
||||
|
||||
import de.tr7zw.nbtapi.NBTItem;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Color;
|
||||
import org.bukkit.DyeColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.inventory.ItemFlag;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.inventory.meta.LeatherArmorMeta;
|
||||
import org.bukkit.inventory.meta.SkullMeta;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class ItemBuilder {
|
||||
|
||||
private ItemMeta itemMeta;
|
||||
private final ItemStack itemStack;
|
||||
|
||||
public ItemBuilder(String material) {
|
||||
this(Material.valueOf(material));
|
||||
}
|
||||
|
||||
public ItemBuilder(Material material) {
|
||||
this(material,1);
|
||||
}
|
||||
|
||||
public ItemBuilder(Material material, int amount) {
|
||||
this(new ItemStack(material, amount));
|
||||
}
|
||||
|
||||
public ItemBuilder(Material material, String name, String... lore) {
|
||||
this(material, 1, (short) 0, name, lore);
|
||||
}
|
||||
|
||||
public ItemBuilder(Material material, int amount, short durability) {
|
||||
this(new ItemStack(material, amount, durability));
|
||||
}
|
||||
|
||||
public ItemBuilder(Material material, int amount, short durability, String name, String... lore) {
|
||||
this(new ItemStack(material, amount, durability));
|
||||
setName(name);
|
||||
setLore(lore);
|
||||
}
|
||||
|
||||
public ItemBuilder(ItemBuilder itemBuilder) {
|
||||
this.itemStack = itemBuilder.itemStack;
|
||||
this.itemMeta = itemBuilder.itemMeta;
|
||||
}
|
||||
|
||||
public ItemBuilder(ConfigurationSection section) {
|
||||
this(new ItemBuilder(section.getString("name"))
|
||||
.setLore(section.getStringList("lore"))
|
||||
.setDurability((short) section.getInt("damage"))
|
||||
.setUnbreakable(section.getBoolean("unbreakable"))
|
||||
);
|
||||
}
|
||||
|
||||
public ItemBuilder(ItemStack itemStack) {
|
||||
this.itemStack = itemStack;
|
||||
this.itemMeta = itemStack.getItemMeta();
|
||||
}
|
||||
|
||||
public ItemBuilder clone() {
|
||||
return new ItemBuilder(itemStack);
|
||||
}
|
||||
|
||||
public ItemBuilder setMaterial(String material) {
|
||||
itemStack.setType(Material.valueOf(material));
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder setMaterial(Material material) {
|
||||
itemStack.setType(material);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder setDurability(short durability) {
|
||||
itemStack.setDurability(durability);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder setName(String name) {
|
||||
itemMeta.setDisplayName(name);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder addUnsafeEnchantment(Enchantment enchantment, int level) {
|
||||
itemStack.addUnsafeEnchantment(enchantment, level);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder removeEnchantment(Enchantment enchantment) {
|
||||
itemStack.removeEnchantment(enchantment);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder addEnchantment(Enchantment enchantment, int level) {
|
||||
itemMeta.addEnchant(enchantment, level,true);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder addEnchantments(Map<Enchantment, Integer> enchantments) {
|
||||
itemStack.addEnchantments(enchantments);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder setGlowing() {
|
||||
if (itemMeta.hasEnchants()) return this;
|
||||
itemMeta.addEnchant(Enchantment.DURABILITY,1,true);
|
||||
return addItemFlag(ItemFlag.HIDE_ENCHANTS);
|
||||
}
|
||||
|
||||
public ItemBuilder colorize() {
|
||||
colorizeName();
|
||||
return colorizeLore();
|
||||
}
|
||||
|
||||
public ItemBuilder colorizeLore() {
|
||||
if (itemMeta.hasLore()) {
|
||||
List<String> lore = getItemMeta().getLore();
|
||||
lore.replaceAll(Utils::fixColor);
|
||||
return setLore(lore);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder colorizeName() {
|
||||
if (itemMeta.hasDisplayName()) setName(Utils.fixColor(getItemMeta().getDisplayName()));
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder setUnbreakable(boolean unbreakable) {
|
||||
itemMeta.spigot().setUnbreakable(unbreakable);
|
||||
return this;
|
||||
}
|
||||
|
||||
public List<String> getLore() {
|
||||
return itemMeta.getLore();
|
||||
}
|
||||
|
||||
public ItemBuilder setLore(String... lore) {
|
||||
return setLore(Arrays.asList(lore));
|
||||
}
|
||||
|
||||
public ItemBuilder setLore(List<String> lore) {
|
||||
itemMeta.setLore(lore);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder removeLoreLine(String line) {
|
||||
List<String> lore = new ArrayList<>(itemMeta.getLore());
|
||||
lore.remove(line);
|
||||
itemMeta.setLore(lore);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder removeLoreLine(int index) {
|
||||
if (index < 0) return this;
|
||||
List<String> lore = new ArrayList<>(itemMeta.getLore());
|
||||
if (index > lore.size()) return this;
|
||||
lore.remove(index);
|
||||
itemMeta.setLore(lore);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder addLoreLine(String line) {
|
||||
List<String> lore = new ArrayList<>();
|
||||
if(itemMeta.hasLore()) lore = new ArrayList<>(itemMeta.getLore());
|
||||
lore.add(line);
|
||||
itemMeta.setLore(lore);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder addLoreLine(String line, int pos) {
|
||||
List<String> lore = new ArrayList<>(itemMeta.getLore());
|
||||
if (pos >= lore.size()) return this;
|
||||
lore.set(pos, line);
|
||||
itemMeta.setLore(lore);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder addItemFlags(List<String> itemFlags) {
|
||||
for (String itemFlagStr : itemFlags) {
|
||||
try {
|
||||
ItemFlag itemFlag = ItemFlag.valueOf(itemFlagStr);
|
||||
addItemFlag(itemFlag);
|
||||
} catch (IllegalArgumentException ignored) {}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder setItemFlags(List<ItemFlag> itemFlags) {
|
||||
itemMeta.addItemFlags(itemFlags.toArray(new ItemFlag[0]));
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder addItemFlag(ItemFlag itemFlag) {
|
||||
itemMeta.addItemFlags(itemFlag);
|
||||
return this;
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public ItemBuilder setColor(DyeColor color) {
|
||||
itemStack.setDurability(color.getData());
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder setLeatherArmorColor(Color color) {
|
||||
LeatherArmorMeta leatherArmorMeta = (LeatherArmorMeta) itemMeta;
|
||||
if (Bukkit.getItemFactory().isApplicable(leatherArmorMeta, itemStack)) {
|
||||
leatherArmorMeta.setColor(color);
|
||||
itemMeta = leatherArmorMeta;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemBuilder setSkullOwner(String owner) {
|
||||
SkullMeta skullMeta = (SkullMeta) itemMeta;
|
||||
if (Bukkit.getItemFactory().isApplicable(skullMeta, itemStack)) {
|
||||
skullMeta.setOwner(owner);
|
||||
itemMeta = skullMeta;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemMeta getItemMeta() {
|
||||
return itemMeta;
|
||||
}
|
||||
|
||||
public ItemStack build() {
|
||||
itemStack.setItemMeta(itemMeta);
|
||||
return itemStack;
|
||||
}
|
||||
|
||||
public ItemStack addNbt(String key, String string) {
|
||||
NBTItem nbtItem = new NBTItem(itemStack);
|
||||
nbtItem.setString(key, string);
|
||||
return nbtItem.getItem();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package net.crankedpvp.monthlycrates.utils;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
public class Utils {
|
||||
public static String fixColor(String string) {
|
||||
return ChatColor.translateAlternateColorCodes('&', string);
|
||||
}
|
||||
|
||||
public static boolean hasAvaliableSlot(Player player, int amount){
|
||||
int amountOpen = 0;
|
||||
Inventory inv = player.getInventory();
|
||||
for (ItemStack item : inv.getContents()) {
|
||||
if(item == null) {
|
||||
amountOpen += 1;
|
||||
}
|
||||
}
|
||||
return amountOpen >= amount;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
name: CrankedMonthlyCrates
|
||||
main: net.crankedpvp.monthlycrates.MonthlyCratesPlugin
|
||||
version: ${git.total.commit.count}.0
|
||||
description: "§fCommit ID: §a${git.commit.id.abbrev}"
|
||||
website: https://crankedpvp.net/
|
||||
depend:
|
||||
- CrankedUtils
|
||||
Reference in New Issue
Block a user