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>Lootboxes</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,71 @@
|
||||
package net.crankedpvp.lootboxes;
|
||||
|
||||
import lombok.Getter;
|
||||
import me.mattstudios.mf.base.CommandManager;
|
||||
import net.crankedpvp.lootboxes.commands.LootboxCommand;
|
||||
import net.crankedpvp.lootboxes.language.FileHandler;
|
||||
import net.crankedpvp.lootboxes.listeners.CombineListener;
|
||||
import net.crankedpvp.lootboxes.listeners.OpenLootboxListener;
|
||||
import net.crankedpvp.lootboxes.listeners.OpenSidePickerListener;
|
||||
import net.crankedpvp.lootboxes.listeners.PreviewLootboxListener;
|
||||
import net.crankedpvp.lootboxes.struct.LootboxManager;
|
||||
import net.crankedpvp.lootboxes.struct.LootboxRegister;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public class CrankedLootboxes extends JavaPlugin {
|
||||
|
||||
@Getter
|
||||
private static CrankedLootboxes instance;
|
||||
|
||||
@Getter
|
||||
private LootboxRegister lootboxRegister;
|
||||
|
||||
@Getter
|
||||
private LootboxManager lootboxManager;
|
||||
|
||||
|
||||
@Getter
|
||||
private CommandManager commandManager;
|
||||
|
||||
@Getter
|
||||
private FileHandler fileHandler;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
|
||||
instance = this;
|
||||
|
||||
|
||||
this.fileHandler = new FileHandler(this);
|
||||
this.fileHandler.load();
|
||||
|
||||
this.lootboxRegister = new LootboxRegister(this);
|
||||
this.lootboxManager = new LootboxManager(this);
|
||||
|
||||
|
||||
this.lootboxRegister.clearLootboxMap();
|
||||
this.lootboxManager.createDefaultConfig("-example", 6);
|
||||
this.lootboxManager.initializeAll();
|
||||
|
||||
this.registerListeners();
|
||||
|
||||
this.commandManager = new CommandManager(this);
|
||||
this.registerCommandCompletion();
|
||||
this.commandManager.register(new LootboxCommand(this));
|
||||
|
||||
}
|
||||
|
||||
private void registerListeners() {
|
||||
PluginManager pluginManager = this.getServer().getPluginManager();
|
||||
pluginManager.registerEvents(new OpenLootboxListener(this), this);
|
||||
pluginManager.registerEvents(new PreviewLootboxListener(this), this);
|
||||
pluginManager.registerEvents(new CombineListener(this), this);
|
||||
pluginManager.registerEvents(new OpenSidePickerListener(this), this);
|
||||
}
|
||||
|
||||
public void registerCommandCompletion() {
|
||||
this.commandManager.getCompletionHandler().register("#lootboxes", input -> this.lootboxRegister.getLootboxesAsIDs());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,353 @@
|
||||
package net.crankedpvp.lootboxes.commands;
|
||||
|
||||
import me.mattstudios.mf.annotations.*;
|
||||
import me.mattstudios.mf.base.CommandBase;
|
||||
import net.crankedpvp.crankedutils.utils.CC;
|
||||
import net.crankedpvp.crankedutils.utils.SoundUtils;
|
||||
import net.crankedpvp.lootboxes.CrankedLootboxes;
|
||||
import net.crankedpvp.lootboxes.inventory.AdminLootboxInventory;
|
||||
import net.crankedpvp.lootboxes.inventory.CurrentLootboxInventory;
|
||||
import net.crankedpvp.lootboxes.language.Language;
|
||||
import net.crankedpvp.lootboxes.struct.Lootbox;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
@Command("lootbox")
|
||||
@Alias({"lootboxes", "lb", "crankedlootbox", "mc"})
|
||||
public class LootboxCommand extends CommandBase {
|
||||
|
||||
private final CrankedLootboxes plugin;
|
||||
public LootboxCommand(CrankedLootboxes plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Default
|
||||
public void defaultCommand(CommandSender commandSender) {
|
||||
Player player = (Player) commandSender;
|
||||
|
||||
if(player.hasPermission("cranked.admin")) {
|
||||
sendAdminHelpMessage(player);
|
||||
player.playSound(player.getLocation(), Sound.SUCCESSFUL_HIT, 2.0F, 2.0F);
|
||||
return;
|
||||
}
|
||||
|
||||
CurrentLootboxInventory currentLootboxInventory = new CurrentLootboxInventory(player, plugin);
|
||||
currentLootboxInventory.getUI().open(player);
|
||||
player.playSound(player.getLocation(), Sound.CHEST_OPEN, 2.0F, 2.0F);
|
||||
}
|
||||
|
||||
@SubCommand("help")
|
||||
@Alias("support")
|
||||
@WrongUsage("&eUsage: /lootbox help")
|
||||
@Permission("cranked.admin")
|
||||
public void helpCommand(CommandSender commandSender) {
|
||||
Player player = (Player) commandSender;
|
||||
|
||||
player.playSound(player.getLocation(), Sound.SUCCESSFUL_HIT, 2.0F, 2.0F);
|
||||
sendAdminHelpMessage(player);
|
||||
}
|
||||
|
||||
@SubCommand("create")
|
||||
@Alias({"make", "new"})
|
||||
@Permission("cranked.admin")
|
||||
@WrongUsage("&eUsage: /lootbox create <name> [amount of rewards]")
|
||||
public void createCommand(CommandSender commandSender, String lootbox, @Optional Integer amountOfRewards) {
|
||||
Player player = (Player) commandSender;
|
||||
if(amountOfRewards == null) amountOfRewards = 1;
|
||||
if(plugin.getLootboxRegister().isLootbox(lootbox)) {
|
||||
player.sendMessage(Language.PREFIX.getMessage() + Language.ALREADY_LOOTBOX.getMessage().replace("%lootbox%", lootbox));
|
||||
player.playSound(player.getLocation(), Sound.ANVIL_LAND, 2.0F, 2.0F);
|
||||
return;
|
||||
}
|
||||
|
||||
if(player.getItemInHand() == null) {
|
||||
plugin.getLootboxManager().createLootbox(lootbox, amountOfRewards);
|
||||
} else {
|
||||
plugin.getLootboxManager().createLootboxFromItemStack(player.getItemInHand(), lootbox, amountOfRewards);
|
||||
}
|
||||
player.sendMessage(Language.PREFIX.getMessage() + Language.CREATED_LOOTBOX.getMessage().replace("%lootbox%", lootbox));
|
||||
player.playSound(player.getLocation(), Sound.SUCCESSFUL_HIT, 2.0F, 2.0F);
|
||||
plugin.registerCommandCompletion();
|
||||
}
|
||||
|
||||
@SubCommand("delete")
|
||||
@Alias({"del"})
|
||||
@Permission("cranked.admin")
|
||||
@WrongUsage("&eUsage: /lootbox delete <lootbox>")
|
||||
public void deleteCommand(CommandSender commandSender, @Completion("#lootboxes") String lootbox) {
|
||||
Player player = (Player) commandSender;
|
||||
if(!plugin.getLootboxRegister().isLootbox(lootbox)) {
|
||||
player.sendMessage(Language.PREFIX.getMessage() + Language.LOOTBOX_INVALID.getMessage().replace("%lootbox%", lootbox));
|
||||
player.playSound(player.getLocation(), Sound.ANVIL_LAND, 2.0F, 2.0F);
|
||||
return;
|
||||
}
|
||||
|
||||
player.sendMessage(Language.PREFIX.getMessage() + Language.DELETED_LOOTBOX.getMessage().replace("%lootbox%", lootbox));
|
||||
player.playSound(player.getLocation(), Sound.LAVA_POP, 2.0F, 2.0F);
|
||||
plugin.getLootboxManager().deleteConfig(lootbox);
|
||||
plugin.getLootboxRegister().removeLootbox(lootbox);
|
||||
}
|
||||
|
||||
@SubCommand("disable")
|
||||
@Alias("unload")
|
||||
@Permission("cranked.admin")
|
||||
@WrongUsage("&eUsage: /lootbox disable <lootbox>")
|
||||
public void disableCommand(CommandSender commandSender, @Completion("#lootboxes") String lootbox) {
|
||||
Player player = (Player) commandSender;
|
||||
|
||||
if (!this.plugin.getLootboxRegister().isLootbox(lootbox)) {
|
||||
player.sendMessage(Language.PREFIX.getMessage() + Language.LOOTBOX_INVALID.getMessage().replace("%lootbox%", lootbox));
|
||||
player.playSound(player.getLocation(), Sound.ANVIL_LAND, 2.0F, 2.0F);
|
||||
return;
|
||||
}
|
||||
|
||||
File file = new File(this.plugin.getDataFolder() + "/Lootboxes/" + lootbox + ".yml");
|
||||
if (file.renameTo(new File(this.plugin.getDataFolder() + "/Lootboxes/-" + lootbox + ".yml"))) {
|
||||
this.plugin.getLootboxRegister().removeLootbox(lootbox);
|
||||
player.sendMessage(Language.PREFIX.getMessage() + Language.LOOTBOX_DISABLED.getMessage().replace("%lootbox%", lootbox));
|
||||
player.playSound(player.getLocation(), Sound.BAT_TAKEOFF, 2.0F, 2.0F);
|
||||
}
|
||||
}
|
||||
|
||||
@SubCommand("enable")
|
||||
@Alias("load")
|
||||
@Permission("cranked.admin")
|
||||
@WrongUsage("&eUsage: /lootbox enable <lootbox>")
|
||||
public void enableCommand(CommandSender commandSender, @Completion("#lootboxes") String lootbox) {
|
||||
Player player = (Player) commandSender;
|
||||
|
||||
if (this.plugin.getLootboxRegister().isLootbox(lootbox)) {
|
||||
player.sendMessage(Language.PREFIX.getMessage() + Language.ALREADY_LOOTBOX.getMessage().replace("%lootbox%", lootbox));
|
||||
player.playSound(player.getLocation(), Sound.ANVIL_LAND, 2.0F, 2.0F);
|
||||
return;
|
||||
}
|
||||
File file = new File(this.plugin.getDataFolder() + "/Lootboxes/-" + lootbox + ".yml");
|
||||
if (file.renameTo(new File(this.plugin.getDataFolder() + "/Lootboxes/" + lootbox + ".yml"))) {
|
||||
this.plugin.getLootboxManager().initialize(lootbox);
|
||||
player.sendMessage(Language.PREFIX.getMessage() + Language.LOOTBOX_ENABLED.getMessage().replace("%lootbox%", lootbox));
|
||||
player.playSound(player.getLocation(), Sound.SUCCESSFUL_HIT, 2.0F, 2.0F);
|
||||
}
|
||||
}
|
||||
|
||||
@SubCommand("reload")
|
||||
@Alias("restart")
|
||||
@Permission("cranked.admin")
|
||||
@WrongUsage("&eUsage: /lootbox reload [lootbox]")
|
||||
public void reloadCommand(CommandSender commandSender, @Optional @Completion("#lootboxes") String lootbox) {
|
||||
Player player = (Player) commandSender;
|
||||
|
||||
if(lootbox == null) {
|
||||
player.sendMessage(Language.PREFIX.getMessage() + Language.RELOADED_ALL.getMessage());
|
||||
player.playSound(player.getLocation(), Sound.SUCCESSFUL_HIT, 2.0F, 2.0F);
|
||||
this.plugin.getLootboxRegister().clearLootboxMap();
|
||||
this.plugin.getLootboxManager().initializeAll();
|
||||
this.plugin.getFileHandler().load();
|
||||
return;
|
||||
}
|
||||
if (this.plugin.getLootboxRegister().getLootboxes().containsKey(lootbox)) {
|
||||
player.sendMessage(Language.PREFIX.getMessage() + Language.RELOADED_SINGULAR.getMessage().replace("%lootbox%", lootbox));
|
||||
player.playSound(player.getLocation(), Sound.SUCCESSFUL_HIT, 2.0F, 2.0F);
|
||||
this.plugin.getLootboxRegister().removeLootbox(lootbox);
|
||||
this.plugin.getLootboxManager().initialize(lootbox);
|
||||
return;
|
||||
}
|
||||
player.sendMessage(Language.LOOTBOX_INVALID.getMessage().replace("%lootbox%", lootbox));
|
||||
player.playSound(player.getLocation(), Sound.ANVIL_LAND, 2.0F, 2.0F);
|
||||
}
|
||||
|
||||
@SubCommand("list")
|
||||
@Alias({"all", "view"})
|
||||
@Permission("cranked.admin")
|
||||
@WrongUsage("&eUsage: /lootbox list")
|
||||
public void listCommand(CommandSender commandSender) {
|
||||
Player player = (Player) commandSender;
|
||||
|
||||
AdminLootboxInventory adminLootboxInventory = new AdminLootboxInventory(player, this.plugin, false);
|
||||
adminLootboxInventory.getUI().open(player);
|
||||
player.playSound(player.getLocation(), Sound.CHEST_OPEN, 2.0F, 2.0F);
|
||||
}
|
||||
|
||||
@SubCommand("viewweekly")
|
||||
@Alias({"weekly", "weeklylootbox"})
|
||||
@WrongUsage("&eUsage: /lootbox weekly")
|
||||
public void viewWeeklyCommand(CommandSender commandSender) {
|
||||
Player player = (Player) commandSender;
|
||||
CurrentLootboxInventory currentLootboxInventory = new CurrentLootboxInventory(player, plugin);
|
||||
currentLootboxInventory.getUI().open(player);
|
||||
player.playSound(player.getLocation(), Sound.CHEST_OPEN, 2.0F, 2.0F);
|
||||
}
|
||||
|
||||
@SubCommand("setweekly")
|
||||
@Alias({"setweekly", "setweeklylootbox"})
|
||||
@WrongUsage("&eUsage: /lootbox setweekly <lootbox>")
|
||||
@Permission("cranked.admin")
|
||||
public void setWeeklyCommand(CommandSender commandSender, @Completion("#lootboxes") String lootbox) {
|
||||
Player player = (Player) commandSender;
|
||||
|
||||
if (!this.plugin.getLootboxRegister().isLootbox(lootbox)) {
|
||||
player.sendMessage(Language.PREFIX.getMessage() + Language.LOOTBOX_INVALID.getMessage().replace("%lootbox%", lootbox));
|
||||
player.playSound(player.getLocation(), Sound.ANVIL_LAND, 2.0F, 2.0F);
|
||||
return;
|
||||
}
|
||||
|
||||
this.plugin.getLootboxRegister().setWeeklyLootbox(this.plugin.getLootboxRegister().getLootbox(lootbox));
|
||||
player.sendMessage(Language.PREFIX.getMessage() + CC.translate("&aSuccessfully set weekly lootbox to " + lootbox));
|
||||
player.playSound(player.getLocation(), Sound.SUCCESSFUL_HIT, 2.0F, 2.0F);
|
||||
}
|
||||
|
||||
@SubCommand("give")
|
||||
@Alias("send")
|
||||
@Permission("cranked.admin")
|
||||
@WrongUsage("&eUsage: /lootbox give <player> <lootbox> [amount]")
|
||||
public void giveCommand(CommandSender commandSender, @Completion("#players")Player player, @Completion("#lootboxes") String lootbox, @Optional Integer amount) {
|
||||
if(amount == null) amount = 1;
|
||||
|
||||
if(!player.isOnline()) {
|
||||
player.sendMessage(Language.PREFIX.getMessage() + CC.translate("&cThat player is not online"));
|
||||
player.playSound(player.getLocation(), Sound.ANVIL_LAND, 2.0F, 2.0F);
|
||||
return;
|
||||
}
|
||||
|
||||
if(!this.plugin.getLootboxRegister().isLootbox(lootbox)) {
|
||||
commandSender.sendMessage(Language.PREFIX.getMessage() + Language.LOOTBOX_INVALID.getMessage().replace("%lootbox%", lootbox));
|
||||
//sender.playSound(player.getLocation(), Sound.ANVIL_LAND, 2.0F, 2.0F);
|
||||
return;
|
||||
}
|
||||
|
||||
this.plugin.getLootboxManager().giveLootbox(player, this.plugin.getLootboxRegister().getLootbox(lootbox), amount);
|
||||
commandSender.sendMessage(Language.PREFIX.getMessage() + Language.LOOTBOX_GIVEN.getMessage().replace("%lootbox%", lootbox));
|
||||
//sender.playSound(sender.getLocation(), Sound.SUCCESSFUL_HIT, 2.0F, 2.0F);
|
||||
//player.playSound(sender.getLocation(), Sound.SUCCESSFUL_HIT, 2.0F, 2.0F);
|
||||
player.sendMessage(Language.PREFIX.getMessage() + Language.LOOTBOX_RECEIVED.getMessage().replace("%lootbox%", CC.translate(this.plugin.getLootboxRegister().getLootbox(lootbox).getDisplayName())));
|
||||
}
|
||||
|
||||
@SubCommand("giveall")
|
||||
@Alias("sendall")
|
||||
@Permission("cranked.admin")
|
||||
@WrongUsage("&eUsage: /lootbox giveall <lootbox> [amount]")
|
||||
public void giveCommand(CommandSender commandSender, @Completion("#lootboxes") String lootbox, @Optional Integer amount) {
|
||||
if(amount == null) amount = 1;
|
||||
|
||||
if(!this.plugin.getLootboxRegister().isLootbox(lootbox)) {
|
||||
commandSender.sendMessage(Language.PREFIX.getMessage() + Language.LOOTBOX_INVALID.getMessage().replace("%lootbox%", lootbox));
|
||||
SoundUtils.tryPlayErrorSound(commandSender);
|
||||
return;
|
||||
}
|
||||
|
||||
commandSender.sendMessage(Language.PREFIX.getMessage() + Language.LOOTBOX_GIVEN.getMessage().replace("%lootbox%", lootbox));
|
||||
SoundUtils.tryPlaySuccessSound(commandSender);
|
||||
|
||||
final Map<String, Integer> ipMap = new HashMap<>();
|
||||
for (final Player p : Bukkit.getOnlinePlayers()) {
|
||||
if(commandSender instanceof Player) {
|
||||
if(p.getUniqueId().toString().equals(((Player) commandSender).getUniqueId().toString())) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
final String ip = p.getAddress().getAddress().toString();
|
||||
if (ipMap.containsKey(ip) && ipMap.get(ip) > 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
this.plugin.getLootboxManager().giveLootbox(p, this.plugin.getLootboxRegister().getLootbox(lootbox), amount);
|
||||
p.sendMessage(Language.PREFIX.getMessage() + Language.LOOTBOX_RECEIVED.getMessage().replace("%lootbox%", CC.translate(this.plugin.getLootboxRegister().getLootbox(lootbox).getDisplayName())));
|
||||
|
||||
p.playSound(p.getLocation(), Sound.SUCCESSFUL_HIT, 2.0F, 2.0F);
|
||||
ipMap.put(ip, ipMap.getOrDefault(ip, 0) + 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@SubCommand("addreward")
|
||||
@Alias({"additem", "addloot"})
|
||||
@Permission("cranked.admin")
|
||||
@WrongUsage("&eUsage: /lootbox addreward <lootbox> <chance> [jackpot]")
|
||||
public void addItemCommand(CommandSender commandSender, @Completion("#lootboxes") String lootbox, Double chance, @Optional Boolean jackpot) {
|
||||
if (jackpot == null)
|
||||
jackpot = false;
|
||||
this.plugin.getLootboxManager().addItem((Player)commandSender, lootbox, chance, jackpot);
|
||||
}
|
||||
|
||||
@SubCommand("removereward")
|
||||
@Alias({"removeitem", "removeloot"})
|
||||
@Permission("cranked.admin")
|
||||
public void removeItemCommand(CommandSender commandSender, @Completion("#lootboxes") String lootbox) {
|
||||
Player player = (Player)commandSender;
|
||||
this.plugin.getLootboxManager().removeItemFromLootbox(player, player.getItemInHand(), lootbox);
|
||||
}
|
||||
|
||||
@SubCommand("clearrewardsinalllootboxes")
|
||||
@Permission("*")
|
||||
public void clear(CommandSender commandSender) {
|
||||
if(commandSender instanceof Player) return;
|
||||
this.plugin.getLootboxRegister().getLootboxes().forEach((lootboxName, lootbox) -> {
|
||||
lootbox.getRewards().clear();
|
||||
|
||||
File file = new File(this.plugin.getDataFolder() + "/Lootboxes/" + lootboxName + ".yml");
|
||||
if (file.exists()) {
|
||||
YamlConfiguration yamlConfiguration = YamlConfiguration.loadConfiguration(file);
|
||||
yamlConfiguration.set("rewards", null);
|
||||
try {
|
||||
yamlConfiguration.save(file);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
commandSender.sendMessage(CC.translate("&aSuccessfully cleared all lootbox rewards"));
|
||||
|
||||
}
|
||||
|
||||
@SubCommand("half")
|
||||
@Alias({"leftorright", "halfgen", "randomhalf"})
|
||||
@WrongUsage("/lb half <lootbox> [left/right]")
|
||||
@Permission("cranked.admin")
|
||||
public void giveHalf(final Player player, String lootbox, @Optional String half) {
|
||||
|
||||
if(!this.plugin.getLootboxRegister().isLootbox(lootbox)) {
|
||||
player.sendMessage(Language.PREFIX.getMessage() + Language.LOOTBOX_INVALID.getMessage().replace("%lootbox%", lootbox));
|
||||
SoundUtils.tryPlayErrorSound(player);
|
||||
return;
|
||||
}
|
||||
|
||||
Lootbox lootboxObject = this.plugin.getLootboxRegister().getLootbox(lootbox);
|
||||
|
||||
if(half == null) {
|
||||
player.getInventory().addItem(lootboxObject.buildLeftOrRightHalf());
|
||||
return;
|
||||
}
|
||||
half = half.toLowerCase(Locale.ROOT).equals("left") ? "left" : "right";
|
||||
player.getInventory().addItem(lootboxObject.buildHalf(half));
|
||||
player.sendMessage(CC.translate("&aGiven " + half + " half for " + lootbox));
|
||||
}
|
||||
|
||||
private void sendAdminHelpMessage(Player player) {
|
||||
player.sendMessage("");
|
||||
player.sendMessage(CC.translate("&cCranked&fPvP &8| &cLootboxes"));
|
||||
player.sendMessage(CC.translate("&c[] &8- &fOptional Argument"));
|
||||
player.sendMessage(CC.translate("&4<> &8- &fRequired Argument"));
|
||||
player.sendMessage("");
|
||||
player.sendMessage(CC.translate(" &c* &f/lootbox create <name> [amount of rewards]"));
|
||||
player.sendMessage(CC.translate(" &c* &f/lootbox delete <name>"));
|
||||
player.sendMessage(CC.translate(" &c* &f/lootbox enable <name>"));
|
||||
player.sendMessage(CC.translate(" &c* &f/lootbox disable <name>"));
|
||||
player.sendMessage(CC.translate(" &c* &f/lootbox additem <name> <chance> [jackpot]"));
|
||||
player.sendMessage(CC.translate(" &c* &f/lootbox removeitem <name>"));
|
||||
player.sendMessage(CC.translate(" &c* &f/lootbox reload [lootbox]"));
|
||||
player.sendMessage(CC.translate(" &c* &f/lootbox list"));
|
||||
player.sendMessage(CC.translate(" &c* &f/lootbox viewweekly"));
|
||||
player.sendMessage(CC.translate(" &c* &f/lootbox setweekly <lootbox>"));
|
||||
player.sendMessage(CC.translate(" &c* &f/lootbox give <player> <lootbox> [amount]"));
|
||||
player.sendMessage(CC.translate(" &c* &f/lootbox giveall <lootbox> [amount]"));
|
||||
player.sendMessage("");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package net.crankedpvp.lootboxes.inventory;
|
||||
|
||||
import dev.triumphteam.gui.guis.GuiItem;
|
||||
import dev.triumphteam.gui.guis.PaginatedGui;
|
||||
import net.crankedpvp.crankedutils.utils.CC;
|
||||
import net.crankedpvp.crankedutils.utils.GuiUtil;
|
||||
import net.crankedpvp.crankedutils.utils.items.ItemBuilder;
|
||||
import net.crankedpvp.lootboxes.CrankedLootboxes;
|
||||
import net.crankedpvp.lootboxes.language.Language;
|
||||
import net.crankedpvp.lootboxes.struct.Lootbox;
|
||||
import net.crankedpvp.lootboxes.struct.RewardType;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class AdminLootboxInventory {
|
||||
|
||||
private final PaginatedGui lootboxMenu;
|
||||
|
||||
public PaginatedGui getUI() {
|
||||
return this.lootboxMenu;
|
||||
}
|
||||
|
||||
|
||||
public AdminLootboxInventory(Player player, CrankedLootboxes plugin, boolean bundles) {
|
||||
|
||||
Map<String, Lootbox> lootboxObjectMap = plugin.getLootboxRegister().getSortedLootboxes();
|
||||
|
||||
this.lootboxMenu = new PaginatedGui(6, 28, "Cranked Lootboxes");
|
||||
this.lootboxMenu.setDefaultClickAction(event -> event.setCancelled(true));
|
||||
this.lootboxMenu.getFiller().fillBorder(new GuiItem(GuiUtil.createFillerItem()));
|
||||
ItemStack info = new ItemBuilder(Material.BOOK_AND_QUILL, CC.translate("&cCranked&fPvP &cLootboxes")).build();
|
||||
this.lootboxMenu.setItem(1, 5, new GuiItem(info));
|
||||
|
||||
this.lootboxMenu.setItem(6, 1, new GuiItem(GuiUtil.createPreviousItem(), event -> {
|
||||
this.lootboxMenu.previous();
|
||||
event.setCancelled(true);
|
||||
}));
|
||||
this.lootboxMenu.setItem(6, 9, new GuiItem(GuiUtil.createNextItem(), event -> {
|
||||
this.lootboxMenu.next();
|
||||
event.setCancelled(true);
|
||||
}));
|
||||
|
||||
setupSwitchItem(player, plugin, bundles);
|
||||
|
||||
lootboxObjectMap.entrySet().stream()
|
||||
.filter(Objects::nonNull)
|
||||
.filter(this.filterLootboxType(bundles))
|
||||
.forEach(lootbox -> {
|
||||
ItemStack itemStack = lootbox.getValue().createLootbox(1);
|
||||
this.lootboxMenu.addItem(new GuiItem(itemStack, event -> {
|
||||
if(player.hasPermission("cranked.admin")) {
|
||||
player.getInventory().addItem(itemStack);
|
||||
player.sendMessage(CC.translate(Language.PREFIX.getMessage() + "&aYou have been given a " + lootbox.getKey() + " lootbox"));
|
||||
player.playSound(player.getLocation(), Sound.SUCCESSFUL_HIT, 2.0F, 2.0F);
|
||||
}
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
private Predicate<? super Map.Entry<String, Lootbox>> filterLootboxType(boolean bundles) {
|
||||
if(bundles) return lootbox -> lootbox.getValue().getRewardType() == RewardType.BUNDLE;
|
||||
return lootbox -> lootbox.getValue().getRewardType() != RewardType.BUNDLE;
|
||||
}
|
||||
|
||||
|
||||
private void setupSwitchItem(Player player, CrankedLootboxes plugin, boolean bundle) {
|
||||
ItemBuilder itemBuilder = bundle ? new ItemBuilder(Material.STICK, "&f&lSwitch to &b&lLootboxes") : new ItemBuilder(Material.BLAZE_ROD, "&f&lSwitch to &c&lBundles");
|
||||
this.lootboxMenu.setItem(1, 9, new GuiItem(itemBuilder.colorize().build(), event -> {
|
||||
player.closeInventory();
|
||||
AdminLootboxInventory adminLootboxInventory = new AdminLootboxInventory(player, plugin, !bundle);
|
||||
adminLootboxInventory.getUI().open(player);
|
||||
}));
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package net.crankedpvp.lootboxes.inventory;
|
||||
|
||||
import dev.triumphteam.gui.guis.Gui;
|
||||
import dev.triumphteam.gui.guis.GuiItem;
|
||||
import net.crankedpvp.crankedutils.utils.CC;
|
||||
import net.crankedpvp.lootboxes.CrankedLootboxes;
|
||||
import org.bukkit.DyeColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
public class CurrentLootboxInventory {
|
||||
|
||||
private CrankedLootboxes plugin;
|
||||
private final Gui currentLootboxInventory;
|
||||
|
||||
public Gui getUI() {
|
||||
return this.currentLootboxInventory;
|
||||
}
|
||||
|
||||
public CurrentLootboxInventory(Player player, CrankedLootboxes plugin) {
|
||||
this.plugin = plugin;
|
||||
this.currentLootboxInventory = new Gui(6, "Cranked Weekly Lootbox");
|
||||
this.currentLootboxInventory.setDefaultClickAction(event -> event.setCancelled(true));
|
||||
this.currentLootboxInventory.getFiller().fill(new GuiItem(createFillerItem()));
|
||||
ItemStack lootbox = this.plugin.getLootboxRegister().getWeeklyLootbox() == null ? this.plugin.getLootboxRegister().select(this.plugin.getLootboxRegister().getLootboxObjects()).createLootbox(1) : this.plugin.getLootboxRegister().getWeeklyLootbox().createLootbox(1);
|
||||
this.currentLootboxInventory.setItem(3, 5, new GuiItem(lootbox));
|
||||
}
|
||||
|
||||
private ItemStack createFillerItem() {
|
||||
ItemStack item = new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.GRAY.getData());
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
meta.setDisplayName(CC.translate(""));
|
||||
item.setItemMeta(meta);
|
||||
return item;
|
||||
}
|
||||
|
||||
}
|
||||
+143
@@ -0,0 +1,143 @@
|
||||
package net.crankedpvp.lootboxes.inventory;
|
||||
|
||||
import dev.triumphteam.gui.guis.Gui;
|
||||
import dev.triumphteam.gui.guis.GuiItem;
|
||||
import net.crankedpvp.crankedutils.utils.Arrow;
|
||||
import net.crankedpvp.crankedutils.utils.CC;
|
||||
import net.crankedpvp.crankedutils.utils.LuckpermUtils;
|
||||
import net.crankedpvp.crankedutils.utils.items.ItemBuilder;
|
||||
import net.crankedpvp.lootboxes.CrankedLootboxes;
|
||||
import net.crankedpvp.lootboxes.struct.Lootbox;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.DyeColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
|
||||
public class HypeboxAnimationInventory {
|
||||
|
||||
private BukkitTask fastAnimationTask;
|
||||
private BukkitTask slowerAnimationTask;
|
||||
private BukkitTask slowAnimationTask;
|
||||
|
||||
private int amountOfSpins = 0;
|
||||
|
||||
private ItemStack receivingItem;
|
||||
|
||||
private final ItemStack[] itemStacks = new ItemStack[9];
|
||||
|
||||
private final Player player;
|
||||
|
||||
|
||||
private final Gui hypeBoxAnimationInventory;
|
||||
|
||||
public Gui getUI() {
|
||||
return this.hypeBoxAnimationInventory;
|
||||
}
|
||||
|
||||
public HypeboxAnimationInventory(Player player, Lootbox lootbox) {
|
||||
this.player = player;
|
||||
this.amountOfSpins = 0;
|
||||
this.receivingItem = lootbox.pickRandomReward();
|
||||
this.hypeBoxAnimationInventory = new Gui(3, CC.translate(lootbox.getDisplayName()));
|
||||
this.hypeBoxAnimationInventory.setDefaultClickAction(event -> event.setCancelled(true));
|
||||
this.hypeBoxAnimationInventory.getFiller().fill(new GuiItem(createFillerItem()));
|
||||
this.hypeBoxAnimationInventory.setItem(1, 5, new GuiItem(this.createWinningFillerItem()));
|
||||
this.hypeBoxAnimationInventory.setItem(3, 5, new GuiItem(this.createWinningFillerItem()));
|
||||
this.hypeBoxAnimationInventory.setCloseGuiAction(inventoryCloseEvent -> {
|
||||
if(this.fastAnimationTask != null) this.fastAnimationTask.cancel();
|
||||
if(this.slowerAnimationTask != null) this.slowerAnimationTask.cancel();
|
||||
if(this.slowAnimationTask != null) this.slowAnimationTask.cancel();
|
||||
player.getInventory().addItem(receivingItem);
|
||||
if(lootbox.doesBroadcast())Bukkit.broadcastMessage(CC.translate("&d&lHype Boxes &8" + Arrow.arrow() + " &f" + LuckpermUtils.getDisplayName(player) + " &fhas just opened a " + lootbox.getDisplayName() + "&r &fand received:"));
|
||||
if(lootbox.doesBroadcast())Bukkit.broadcastMessage(CC.translate(" &f• &l" + receivingItem.getAmount() + "x &r" + (receivingItem.hasItemMeta() ? receivingItem.getItemMeta().getDisplayName() : receivingItem.getType().name())));
|
||||
|
||||
});
|
||||
|
||||
for(int i = 0; i < 9; i++) {
|
||||
ItemStack itemStack = lootbox.pickRandomReward();
|
||||
this.itemStacks[i] = itemStack;
|
||||
this.hypeBoxAnimationInventory.setItem(2, i+1, new GuiItem(itemStack));
|
||||
}
|
||||
|
||||
this.fastAnimationTask = Bukkit.getScheduler().runTaskTimer(CrankedLootboxes.getInstance(), () -> shiftAllItemsToTheLeftOnceAtHighSpeed(lootbox), 3L, 3L);
|
||||
}
|
||||
|
||||
public void shiftAllItemsToTheLeftOnceAtHighSpeed(Lootbox lootbox) {
|
||||
this.amountOfSpins++;
|
||||
ItemStack[] itemStacks = this.itemStacks.clone();
|
||||
for(int i = 0; i < 8; i++) {
|
||||
this.itemStacks[i] = itemStacks[i+1];
|
||||
}
|
||||
this.itemStacks[8] = lootbox.pickRandomReward();
|
||||
|
||||
for(int i = 0; i < 9; i++) {
|
||||
this.hypeBoxAnimationInventory.updateItem(2, i+1, new GuiItem(this.itemStacks[i]));
|
||||
}
|
||||
this.player.updateInventory();
|
||||
this.player.playSound(player.getLocation(), Sound.SUCCESSFUL_HIT, 1.0f, 2.0f);
|
||||
|
||||
if(this.amountOfSpins == 40) {
|
||||
this.amountOfSpins = 0;
|
||||
this.fastAnimationTask.cancel();
|
||||
this.slowerAnimationTask = Bukkit.getScheduler().runTaskTimer(CrankedLootboxes.getInstance(), () -> shiftAllItemsToTheLeftOnceAtSlowerSpeed(lootbox), 0L, 7L);
|
||||
}
|
||||
}
|
||||
|
||||
public void shiftAllItemsToTheLeftOnceAtSlowerSpeed(Lootbox lootbox) {
|
||||
this.amountOfSpins++;
|
||||
ItemStack[] itemStacks = this.itemStacks.clone();
|
||||
for(int i = 0; i < 8; i++) {
|
||||
this.itemStacks[i] = itemStacks[i+1];
|
||||
}
|
||||
this.itemStacks[8] = lootbox.pickRandomReward();
|
||||
|
||||
for(int i = 0; i < 9; i++) {
|
||||
this.hypeBoxAnimationInventory.updateItem(2, i+1, new GuiItem(this.itemStacks[i]));
|
||||
}
|
||||
this.player.updateInventory();
|
||||
this.player.playSound(player.getLocation(), Sound.SUCCESSFUL_HIT, 1.0f, 2.0f);
|
||||
|
||||
if(this.amountOfSpins == 10) {
|
||||
this.amountOfSpins = 0;
|
||||
this.slowerAnimationTask.cancel();
|
||||
this.slowAnimationTask = Bukkit.getScheduler().runTaskTimer(CrankedLootboxes.getInstance(), () -> shiftAllItemsToTheLeftOnceAtSlowSpeed(lootbox), 0L, 12L);
|
||||
}
|
||||
}
|
||||
|
||||
public void shiftAllItemsToTheLeftOnceAtSlowSpeed(Lootbox lootbox) {
|
||||
this.amountOfSpins++;
|
||||
ItemStack[] itemStacks = this.itemStacks.clone();
|
||||
for(int i = 0; i < 8; i++) {
|
||||
this.itemStacks[i] = itemStacks[i+1];
|
||||
}
|
||||
this.itemStacks[8] = lootbox.pickRandomReward();
|
||||
|
||||
for(int i = 0; i < 9; i++) {
|
||||
this.hypeBoxAnimationInventory.updateItem(2, i+1, new GuiItem(this.itemStacks[i]));
|
||||
}
|
||||
this.player.updateInventory();
|
||||
this.player.playSound(player.getLocation(), Sound.SUCCESSFUL_HIT, 1.0f, 2.0f);
|
||||
|
||||
if(this.amountOfSpins == 5) {
|
||||
this.slowAnimationTask.cancel();
|
||||
this.receivingItem = this.itemStacks[4];
|
||||
}
|
||||
}
|
||||
|
||||
private ItemStack createFillerItem() {
|
||||
ItemStack item = new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.GRAY.getData());
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
meta.setDisplayName(CC.translate(" "));
|
||||
item.setItemMeta(meta);
|
||||
return item;
|
||||
}
|
||||
|
||||
private ItemStack createWinningFillerItem() {
|
||||
return new ItemBuilder(Material.STAINED_GLASS_PANE, 1, DyeColor.BLACK.getData()).setName(" ").setGlowing().build();
|
||||
}
|
||||
|
||||
}
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
package net.crankedpvp.lootboxes.inventory;
|
||||
|
||||
import dev.triumphteam.gui.guis.Gui;
|
||||
import dev.triumphteam.gui.guis.GuiItem;
|
||||
import net.crankedpvp.crankedutils.utils.Arrow;
|
||||
import net.crankedpvp.crankedutils.utils.CC;
|
||||
import net.crankedpvp.crankedutils.utils.LuckpermUtils;
|
||||
import net.crankedpvp.crankedutils.utils.items.ItemBuilder;
|
||||
import net.crankedpvp.crankedutils.utils.items.SkullUtils;
|
||||
import net.crankedpvp.lootboxes.struct.Lootbox;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.DyeColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
public class LuckyBlockAnimationInventory {
|
||||
|
||||
private boolean hasOpened;
|
||||
|
||||
private final Gui luckyBlockInventory;
|
||||
|
||||
public Gui getUI() {
|
||||
return this.luckyBlockInventory;
|
||||
}
|
||||
|
||||
public LuckyBlockAnimationInventory(Player player, Lootbox lootbox) {
|
||||
this.luckyBlockInventory = new Gui(3, "Choose One...");
|
||||
this.luckyBlockInventory.setDefaultClickAction(event -> event.setCancelled(true));
|
||||
this.luckyBlockInventory.getFiller().fill(new GuiItem(createFillerItem()));
|
||||
this.luckyBlockInventory.setCloseGuiAction(inventoryCloseEvent -> {
|
||||
if(!this.hasOpened) {
|
||||
ItemStack itemStack = lootbox.pickRandomReward();
|
||||
player.getInventory().addItem(itemStack);
|
||||
if(lootbox.doesBroadcast())Bukkit.broadcastMessage(CC.translate("&d&lLucky Blocks &8" + Arrow.arrow() + LuckpermUtils.getDisplayName(player) + " &fhas just opened a " + lootbox.getDisplayName() + "&r &fand received:"));
|
||||
if(lootbox.doesBroadcast())Bukkit.broadcastMessage(CC.translate(" &f• &l" + itemStack.getAmount() + "x &r" + (itemStack.hasItemMeta() ? itemStack.getItemMeta().getDisplayName() : itemStack.getType().name())));
|
||||
}
|
||||
});
|
||||
|
||||
ItemStack one = lootbox.pickRandomReward();
|
||||
ItemStack two = lootbox.pickRandomReward();
|
||||
ItemStack three = lootbox.pickRandomReward();
|
||||
|
||||
this.luckyBlockInventory.updateItem(2, 3, new GuiItem(getLuckyBlock(lootbox.getSkullValue()), event -> {
|
||||
this.setItems(one, two, three);
|
||||
player.getInventory().addItem(one);
|
||||
if(lootbox.doesBroadcast())Bukkit.broadcastMessage(CC.translate("&d&lLucky Blocks &8" + Arrow.arrow() + " &f" + LuckpermUtils.getDisplayName(player) + " &fhas just opened a " + lootbox.getDisplayName() + "&r &fand received:"));
|
||||
if(lootbox.doesBroadcast())Bukkit.broadcastMessage(CC.translate(" &f• &l" + one.getAmount() + "x &r" + (one.hasItemMeta() ? one.getItemMeta().getDisplayName() : one.getType().name())));
|
||||
}));
|
||||
|
||||
this.luckyBlockInventory.updateItem(2, 5, new GuiItem(getLuckyBlock(lootbox.getSkullValue()), event -> {
|
||||
this.setItems(one, two, three);
|
||||
player.getInventory().addItem(two);
|
||||
if(lootbox.doesBroadcast())Bukkit.broadcastMessage(CC.translate("&d&lLucky Blocks &8" + Arrow.arrow() + " &f" + LuckpermUtils.getDisplayName(player) + " &fhas just opened a " + lootbox.getDisplayName() + "&r &fand received:"));
|
||||
if(lootbox.doesBroadcast())Bukkit.broadcastMessage(CC.translate(" &f• &l" + two.getAmount() + "x &r" + (two.hasItemMeta() ? two.getItemMeta().getDisplayName() : two.getType().name())));
|
||||
}));
|
||||
|
||||
this.luckyBlockInventory.updateItem(2, 7, new GuiItem(getLuckyBlock(lootbox.getSkullValue()), event -> {
|
||||
this.setItems(one, two, three);
|
||||
player.getInventory().addItem(three);
|
||||
if(lootbox.doesBroadcast())Bukkit.broadcastMessage(CC.translate("&d&lLucky Blocks &8" + Arrow.arrow() + " &f" + LuckpermUtils.getDisplayName(player) + " &fhas just opened a " + lootbox.getDisplayName() + "&r &fand received:"));
|
||||
if(lootbox.doesBroadcast())Bukkit.broadcastMessage(CC.translate(" &f• &l" + three.getAmount() + "x &r" + (three.hasItemMeta() ? three.getItemMeta().getDisplayName() : three.getType().name())));
|
||||
}));
|
||||
}
|
||||
|
||||
private void setItems(ItemStack one, ItemStack two, ItemStack three) {
|
||||
this.hasOpened = true;
|
||||
this.luckyBlockInventory.updateItem(2, 3, new GuiItem(one));
|
||||
this.luckyBlockInventory.updateItem(2, 5, new GuiItem(two));
|
||||
this.luckyBlockInventory.updateItem(2, 7, new GuiItem(three));
|
||||
}
|
||||
|
||||
private ItemStack getLuckyBlock(String skullURL) {
|
||||
ItemBuilder itemBuilder = new ItemBuilder(SkullUtils.getSkullFromSkin(skullURL));
|
||||
itemBuilder.setName("&c&l???");
|
||||
itemBuilder.setLore("&7Click here to receive a",
|
||||
"&7random lucky block reward");
|
||||
return itemBuilder.colorize().build();
|
||||
}
|
||||
|
||||
private ItemStack createFillerItem() {
|
||||
ItemStack item = new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.GRAY.getData());
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
meta.setDisplayName(CC.translate(""));
|
||||
item.setItemMeta(meta);
|
||||
return item;
|
||||
}
|
||||
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
package net.crankedpvp.lootboxes.inventory;
|
||||
|
||||
import dev.triumphteam.gui.guis.GuiItem;
|
||||
import dev.triumphteam.gui.guis.PaginatedGui;
|
||||
import net.crankedpvp.crankedutils.utils.CC;
|
||||
import net.crankedpvp.crankedutils.utils.GuiUtil;
|
||||
import net.crankedpvp.crankedutils.utils.items.ItemBuilder;
|
||||
import net.crankedpvp.lootboxes.CrankedLootboxes;
|
||||
import net.crankedpvp.lootboxes.language.Language;
|
||||
import net.crankedpvp.lootboxes.struct.Reward;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
public class PreviewLootboxInventory {
|
||||
private final PaginatedGui previewInventory;
|
||||
|
||||
public PaginatedGui getUI() {
|
||||
return this.previewInventory;
|
||||
}
|
||||
|
||||
public PreviewLootboxInventory(Player player, String name, CrankedLootboxes plugin) {
|
||||
this.previewInventory = new PaginatedGui(6, 28, "Cranked Preview Lootbox");
|
||||
this.previewInventory.setDefaultClickAction(event -> event.setCancelled(true));
|
||||
this.previewInventory.getFiller().fillBorder(new GuiItem(GuiUtil.createFillerItem()));
|
||||
ItemBuilder info = new ItemBuilder(Material.BOOK_AND_QUILL, "&aPreview Loot");
|
||||
if (player.hasPermission("cranked.admin")) {
|
||||
info.addLoreLine("&7Left-Click to add reward to your inventory");
|
||||
info.addLoreLine("&7Right-Click to remove a reward");
|
||||
}
|
||||
this.previewInventory.setItem(1, 5, new GuiItem(info.colorize().build()));
|
||||
this.previewInventory.setItem(1, 1, new GuiItem(GuiUtil.createExitItem(), event -> player.closeInventory()));
|
||||
this.previewInventory.setItem(1, 9, new GuiItem(GuiUtil.createExitItem(), event -> player.closeInventory()));
|
||||
this.previewInventory.setItem(6, 1, new GuiItem(GuiUtil.createPreviousItem(), event -> {
|
||||
this.previewInventory.previous();
|
||||
event.setCancelled(true);
|
||||
}));
|
||||
this.previewInventory.setItem(6, 9, new GuiItem(GuiUtil.createNextItem(), event -> {
|
||||
this.previewInventory.next();
|
||||
event.setCancelled(true);
|
||||
}));
|
||||
for (Reward reward : plugin.getLootboxRegister().getLootbox(name).getRewards()) {
|
||||
ItemStack item = reward.getItemStack();
|
||||
this.previewInventory.addItem(new GuiItem(item, event -> {
|
||||
if (player.hasPermission("cranked.admin")) {
|
||||
if (event.isShiftClick() && event.isRightClick()) {
|
||||
plugin.getLootboxManager().removeItemFromLootbox(player, item, name);
|
||||
ItemStack itemStack = item.clone();
|
||||
itemStack.setType(Material.BARRIER);
|
||||
this.previewInventory.updateItem(event.getSlot(), new GuiItem(itemStack));
|
||||
player.playSound(player.getLocation(), Sound.ANVIL_BREAK, 2.0F, 2.0F);
|
||||
}
|
||||
if (event.isLeftClick()) {
|
||||
player.getInventory().addItem(item);
|
||||
player.sendMessage(CC.translate(Language.PREFIX.getMessage() + "&a+ " + item.getItemMeta().getDisplayName()));
|
||||
player.playSound(player.getLocation(), Sound.SUCCESSFUL_HIT, 2.0F, 2.0F);
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package net.crankedpvp.lootboxes.language;
|
||||
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import net.crankedpvp.crankedutils.utils.CC;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.function.UnaryOperator;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public enum Config {
|
||||
LOOTBOX_HEADER(),
|
||||
LOOTBOX_FOOTER(),
|
||||
LOOTBOX_REWARD_TITLE(),
|
||||
LOOTBOX_REWARD_LINE(),
|
||||
LOOTBOX_JACKPOT_TITLE(),
|
||||
LOOTBOX_JACKPOT_LINE(),
|
||||
LOOTBOX_MATERIAL()
|
||||
|
||||
;
|
||||
|
||||
|
||||
|
||||
private String value;
|
||||
private ImmutableList<String> valueList;
|
||||
|
||||
public String getValue() {
|
||||
return this.value != null ? this.value : this.name();
|
||||
}
|
||||
|
||||
public String getValue(UnaryOperator<String> function) {
|
||||
return this.value != null ? function.apply(this.value) : this.name();
|
||||
}
|
||||
|
||||
public String getPath() {
|
||||
return this.name();
|
||||
}
|
||||
|
||||
public List<String> getValuesAsList() {
|
||||
return this.valueList;
|
||||
}
|
||||
|
||||
public List<String> getValuesAsList(String oldString, String newString) {
|
||||
List<String> list = new ArrayList<>(this.valueList);
|
||||
list.replaceAll(s -> s.replace(oldString, newString));
|
||||
return list;
|
||||
}
|
||||
|
||||
void setValue(String value) {
|
||||
this.value = CC.translate(value);
|
||||
|
||||
List<String> tempList = Arrays.stream(StringUtils.split(value, System.lineSeparator()))
|
||||
.map(CC::translate)
|
||||
.collect(Collectors.toList());
|
||||
this.valueList = ImmutableList.copyOf(tempList);
|
||||
}
|
||||
|
||||
void setValue(List<String> valueList) {
|
||||
this.value = CC.translate(valueList.parallelStream().collect(Collectors.joining(System.lineSeparator())));
|
||||
|
||||
List<String> tempList = valueList.stream()
|
||||
.map(CC::translate)
|
||||
.collect(Collectors.toList());
|
||||
this.valueList = ImmutableList.copyOf(tempList);
|
||||
}
|
||||
|
||||
public void send(CommandSender sender, UnaryOperator<String> function) {
|
||||
sender.sendMessage(CC.translate(this.getValue(function)));
|
||||
}
|
||||
|
||||
public void send(CommandSender sender) {
|
||||
sender.sendMessage(CC.translate(this.getValue()));
|
||||
}
|
||||
|
||||
|
||||
public void send(List<CommandSender> senders, UnaryOperator<String> function) {
|
||||
String msg = CC.translate(this.getValue(function));
|
||||
senders.forEach(sender -> sender.sendMessage(msg));
|
||||
}
|
||||
|
||||
public void sendPlayers(List<Player> senders, UnaryOperator<String> function) {
|
||||
String msg = CC.translate(this.getValue(function));
|
||||
senders.forEach(sender -> sender.sendMessage(msg));
|
||||
}
|
||||
|
||||
public void send(CommandSender ...senders) {
|
||||
String msg = CC.translate(this.getValue());
|
||||
Arrays.asList(senders).forEach(sender -> sender.sendMessage(msg));
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return CC.translate(this.getValue());
|
||||
}
|
||||
|
||||
public String getMessage(UnaryOperator<String> function) {
|
||||
return CC.translate(this.getValue(function));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
package net.crankedpvp.lootboxes.language;
|
||||
|
||||
import net.crankedpvp.lootboxes.CrankedLootboxes;
|
||||
import org.bukkit.configuration.InvalidConfigurationException;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
public final class FileHandler {
|
||||
|
||||
private final File languageFile;
|
||||
private final File configFile;
|
||||
private final FileConfiguration languageFileConfiguration;
|
||||
private final FileConfiguration configFileConfiguration;
|
||||
private final String languagePath;
|
||||
private final String configPath;
|
||||
private final CrankedLootboxes plugin;
|
||||
|
||||
public FileHandler(CrankedLootboxes plugin) {
|
||||
this.plugin = plugin;
|
||||
this.languagePath = "language.yml";
|
||||
languageFile = new File(plugin.getDataFolder(), languagePath);
|
||||
languageFileConfiguration = new YamlConfiguration();
|
||||
|
||||
this.configPath = "config.yml";
|
||||
configFile = new File(plugin.getDataFolder(), configPath);
|
||||
configFileConfiguration = new YamlConfiguration();
|
||||
}
|
||||
|
||||
public void load() {
|
||||
this.saveDefaultLanguage();
|
||||
this.loadLanguageFromDisk();
|
||||
this.copyLanguageDefaults();
|
||||
this.saveLanguage();
|
||||
this.loadLanguageFromDisk();
|
||||
this.loadLanguageMessages();
|
||||
|
||||
this.saveDefaultConfig();
|
||||
this.loadConfigFromDisk();
|
||||
this.copyConfigDefaults();
|
||||
this.saveConfig();
|
||||
this.loadConfigFromDisk();
|
||||
this.loadConfigMessages();
|
||||
}
|
||||
|
||||
private void loadLanguageMessages() {
|
||||
for (Language entry : Language.values()) {
|
||||
String path = entry.getPath();
|
||||
if (languageFileConfiguration.isList(path)) {
|
||||
entry.setValue(languageFileConfiguration.getStringList(path));
|
||||
continue;
|
||||
}
|
||||
entry.setValue(languageFileConfiguration.getString(path));
|
||||
}
|
||||
}
|
||||
|
||||
private void loadConfigMessages() {
|
||||
for (Config entry : Config.values()) {
|
||||
String path = entry.getPath();
|
||||
if (configFileConfiguration.isList(path)) {
|
||||
entry.setValue(configFileConfiguration.getStringList(path));
|
||||
continue;
|
||||
}
|
||||
entry.setValue(configFileConfiguration.getString(path));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void loadLanguageFromDisk() {
|
||||
try {
|
||||
languageFileConfiguration.load(languageFile);
|
||||
} catch (IOException | InvalidConfigurationException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void loadConfigFromDisk() {
|
||||
try {
|
||||
configFileConfiguration.load(configFile);
|
||||
} catch (IOException | InvalidConfigurationException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void saveLanguage() {
|
||||
try {
|
||||
languageFileConfiguration.save(languageFile);
|
||||
} catch (IOException e) {
|
||||
plugin.getLogger().severe("Could not save config file for " + languageFile.getPath() + "!");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void saveConfig() {
|
||||
try {
|
||||
configFileConfiguration.save(configFile);
|
||||
} catch (IOException e) {
|
||||
plugin.getLogger().severe("Could not save config file for " + configFile.getPath() + "!");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void copyLanguageDefaults() {
|
||||
InputStream defConfig = plugin.getResource(this.languagePath);
|
||||
if (defConfig != null) {
|
||||
languageFileConfiguration.setDefaults(YamlConfiguration.loadConfiguration(new InputStreamReader(defConfig)));
|
||||
}
|
||||
languageFileConfiguration.options().copyDefaults(true);
|
||||
}
|
||||
|
||||
private void copyConfigDefaults() {
|
||||
InputStream defConfig = plugin.getResource(this.configPath);
|
||||
if (defConfig != null) {
|
||||
configFileConfiguration.setDefaults(YamlConfiguration.loadConfiguration(new InputStreamReader(defConfig)));
|
||||
}
|
||||
configFileConfiguration.options().copyDefaults(true);
|
||||
}
|
||||
|
||||
private void saveDefaultLanguage() {
|
||||
if (!languageFile.exists()) {
|
||||
languageFile.getParentFile().mkdirs();
|
||||
plugin.saveResource(languageFile.getName(),false);
|
||||
}
|
||||
}
|
||||
|
||||
private void saveDefaultConfig() {
|
||||
if (!configFile.exists()) {
|
||||
configFile.getParentFile().mkdirs();
|
||||
plugin.saveResource(configFile.getName(),false);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
package net.crankedpvp.lootboxes.language;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import net.crankedpvp.crankedutils.utils.CC;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.function.UnaryOperator;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public enum Language {
|
||||
|
||||
PREFIX,
|
||||
FULL_INVENTORY,
|
||||
|
||||
|
||||
ALREADY_LOOTBOX,
|
||||
CREATED_LOOTBOX,
|
||||
DELETED_LOOTBOX,
|
||||
LOOTBOX_DISABLED,
|
||||
LOOTBOX_ENABLED,
|
||||
LOOTBOX_INVALID,
|
||||
RELOADED_ALL,
|
||||
RELOADED_SINGULAR,
|
||||
|
||||
LOOTBOX_RECEIVED,
|
||||
LOOTBOX_GIVEN,
|
||||
|
||||
LOOTBOX_ADDITEM,
|
||||
LOOTBOX_ITEM_ALREADY_IN,
|
||||
|
||||
LOOTBOX_ITEM_NOT_FOUND,
|
||||
LOOTBOX_REMOVE_ITEM,
|
||||
|
||||
PREVIEW_LOOT,
|
||||
LOOTBOX_OPENED,
|
||||
|
||||
|
||||
;
|
||||
|
||||
private String value;
|
||||
private ImmutableList<String> valueList;
|
||||
|
||||
public String getValue() {
|
||||
return this.value != null ? this.value : this.name();
|
||||
}
|
||||
|
||||
public String getValue(UnaryOperator<String> function) {
|
||||
return this.value != null ? function.apply(this.value) : this.name();
|
||||
}
|
||||
|
||||
public String getPath() {
|
||||
return this.name();
|
||||
}
|
||||
|
||||
public List<String> getValuesAsList() {
|
||||
return this.valueList;
|
||||
}
|
||||
|
||||
public List<String> getValuesAsList(String oldString, String newString) {
|
||||
List<String> list = new ArrayList<>(this.valueList);
|
||||
list.replaceAll(s -> s.replace(oldString, newString));
|
||||
return list;
|
||||
}
|
||||
|
||||
void setValue(String value) {
|
||||
this.value = CC.translate(value);
|
||||
|
||||
List<String> tempList = Arrays.stream(StringUtils.split(value, System.lineSeparator()))
|
||||
.map(CC::translate)
|
||||
.collect(Collectors.toList());
|
||||
this.valueList = ImmutableList.copyOf(tempList);
|
||||
}
|
||||
|
||||
void setValue(List<String> valueList) {
|
||||
this.value = CC.translate(valueList.parallelStream().collect(Collectors.joining(System.lineSeparator())));
|
||||
|
||||
List<String> tempList = valueList.stream()
|
||||
.map(CC::translate)
|
||||
.collect(Collectors.toList());
|
||||
this.valueList = ImmutableList.copyOf(tempList);
|
||||
}
|
||||
|
||||
public void send(CommandSender sender, UnaryOperator<String> function) {
|
||||
sender.sendMessage(CC.translate(this.getValue(function)));
|
||||
}
|
||||
|
||||
public void send(CommandSender sender) {
|
||||
sender.sendMessage(CC.translate(this.getValue()));
|
||||
}
|
||||
|
||||
|
||||
public void send(List<CommandSender> senders, UnaryOperator<String> function) {
|
||||
String msg = CC.translate(this.getValue(function));
|
||||
senders.forEach(sender -> sender.sendMessage(msg));
|
||||
}
|
||||
|
||||
public void sendPlayers(List<Player> senders, UnaryOperator<String> function) {
|
||||
String msg = CC.translate(this.getValue(function));
|
||||
senders.forEach(sender -> sender.sendMessage(msg));
|
||||
}
|
||||
|
||||
public void send(CommandSender ...senders) {
|
||||
String msg = CC.translate(this.getValue());
|
||||
Arrays.asList(senders).forEach(sender -> sender.sendMessage(msg));
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return CC.translate(this.getValue());
|
||||
}
|
||||
|
||||
public String getMessage(UnaryOperator<String> function) {
|
||||
return CC.translate(this.getValue(function));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package net.crankedpvp.lootboxes.listeners;
|
||||
|
||||
import de.tr7zw.nbtapi.NBTItem;
|
||||
import net.crankedpvp.crankedutils.utils.CC;
|
||||
import net.crankedpvp.crankedutils.utils.SoundUtils;
|
||||
import net.crankedpvp.lootboxes.CrankedLootboxes;
|
||||
import net.crankedpvp.lootboxes.struct.Lootbox;
|
||||
import net.crankedpvp.lootboxes.struct.LootboxManager;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
public class CombineListener implements Listener {
|
||||
|
||||
private final CrankedLootboxes plugin;
|
||||
public CombineListener(CrankedLootboxes plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void combine(InventoryClickEvent event) {
|
||||
if(event.getCurrentItem() == null || event.getCurrentItem().getType() == Material.AIR) return;
|
||||
if(event.getCursor() == null || event.getCursor().getType() == Material.AIR) return;
|
||||
|
||||
Player player = (Player) event.getWhoClicked();
|
||||
ItemStack clickedItem = event.getCurrentItem();
|
||||
ItemStack cursorItem = event.getCursor();
|
||||
|
||||
if(!LootboxManager.isLootboxHalf(clickedItem)) return;
|
||||
if(!LootboxManager.isLootboxHalf(cursorItem)) return;
|
||||
|
||||
if(clickedItem.getAmount() > 1 || cursorItem.getAmount() > 1) {
|
||||
player.sendMessage(CC.translate("&cYou cannot merge stacked half lootboxes"));
|
||||
SoundUtils.tryPlayErrorSound(player);
|
||||
return;
|
||||
}
|
||||
|
||||
NBTItem nbtClickedItem = new NBTItem(clickedItem);
|
||||
NBTItem nbtCursorItem = new NBTItem(cursorItem);
|
||||
|
||||
String lootbox = nbtClickedItem.getString("halfLootbox");
|
||||
if(!nbtCursorItem.getString("halfLootbox").equals(lootbox)) return;
|
||||
|
||||
Lootbox lootboxObject = CrankedLootboxes.getInstance().getLootboxRegister().getLootbox(lootbox);
|
||||
String side = nbtClickedItem.getString("side");
|
||||
if(nbtCursorItem.getString("side").equals(side)) return;
|
||||
|
||||
event.setCurrentItem(null);
|
||||
event.setCursor(null);
|
||||
player.getInventory().addItem(lootboxObject.createLootbox(1));
|
||||
player.playSound(player.getLocation(), Sound.ANVIL_USE, 2.0f, 2.0f);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package net.crankedpvp.lootboxes.listeners;
|
||||
|
||||
import de.tr7zw.nbtapi.NBTItem;
|
||||
import net.crankedpvp.crankedutils.utils.*;
|
||||
import net.crankedpvp.lootboxes.CrankedLootboxes;
|
||||
import net.crankedpvp.lootboxes.inventory.HypeboxAnimationInventory;
|
||||
import net.crankedpvp.lootboxes.inventory.LuckyBlockAnimationInventory;
|
||||
import net.crankedpvp.lootboxes.language.Language;
|
||||
import net.crankedpvp.lootboxes.struct.Lootbox;
|
||||
import net.crankedpvp.lootboxes.struct.LootboxManager;
|
||||
import net.crankedpvp.lootboxes.struct.RewardType;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Sound;
|
||||
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.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
public class OpenLootboxListener implements Listener {
|
||||
private final CrankedLootboxes plugin;
|
||||
public OpenLootboxListener(CrankedLootboxes plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onInteract(PlayerInteractEvent event) {
|
||||
if(event.getAction() == null) return;
|
||||
if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
|
||||
ItemStack itemStack = event.getItem();
|
||||
if(!LootboxManager.isLootbox(itemStack)) return;
|
||||
event.setCancelled(true);
|
||||
final NBTItem nbtItem = new NBTItem(itemStack);
|
||||
final Lootbox lootbox = plugin.getLootboxRegister().getLootbox(nbtItem.getOrCreateCompound("cosmicData").getString("lootboxID"));
|
||||
if(lootbox == null) return;
|
||||
if(lootbox.getRewards().isEmpty()) return;
|
||||
|
||||
int looper = (lootbox.getRewardType().equals(RewardType.LOOTBOX) || lootbox.getRewardType().equals(RewardType.BUNDLE)) ? (event.getPlayer().isSneaking() ? itemStack.getAmount() : 1) : 1;
|
||||
for (int i = 0; i < looper; i++) {
|
||||
if(!InventoryUtils.hasAvaliableSlot(event.getPlayer(), lootbox.getAmountOfRewards())) {
|
||||
event.getPlayer().sendMessage(Language.PREFIX.getMessage() + Language.FULL_INVENTORY.getMessage());
|
||||
event.getPlayer().playSound(event.getPlayer().getLocation(), Sound.LAVA_POP, 1.0F, 1.0F);
|
||||
return;
|
||||
}
|
||||
|
||||
final int amount = itemStack.getAmount();
|
||||
if (amount <= 1) {
|
||||
event.getPlayer().setItemInHand(null);
|
||||
} else {
|
||||
itemStack.setAmount(amount - 1);
|
||||
event.getPlayer().setItemInHand(itemStack);
|
||||
}
|
||||
|
||||
//event.getPlayer().sendMessage(Language.PREFIX.getMessage() + Language.LOOTBOX_OPENED.getMessage().replace("%lootbox%", StringUtils.toNiceString(lootbox.getId())));
|
||||
if(i == 0) event.getPlayer().playSound(event.getPlayer().getLocation(), Sound.SUCCESSFUL_HIT, 0.5F, 0.5F);
|
||||
|
||||
if(lootbox.getRewardType() == RewardType.HYPEBOX) {
|
||||
HypeboxAnimationInventory hypeboxAnimationInventory = new HypeboxAnimationInventory(event.getPlayer(), lootbox);
|
||||
hypeboxAnimationInventory.getUI().open(event.getPlayer());
|
||||
return;
|
||||
}
|
||||
if(lootbox.getRewardType() == RewardType.LUCKY_BLOCK) {
|
||||
LuckyBlockAnimationInventory luckyBlockAnimationInventory = new LuckyBlockAnimationInventory(event.getPlayer(), lootbox);
|
||||
luckyBlockAnimationInventory.getUI().open(event.getPlayer());
|
||||
return;
|
||||
}
|
||||
if(lootbox.getRewardType() == RewardType.BUNDLE) {
|
||||
if(lootbox.doesBroadcast()) Bukkit.broadcastMessage(CC.translate("&d&lBundles &8" + Arrow.arrow() + " &f" + LuckpermUtils.getDisplayName(event.getPlayer()) + " &fhas just opened a " + lootbox.getDisplayName() + "&r &fand received:"));
|
||||
lootbox.getRewards().forEach(reward -> {
|
||||
event.getPlayer().getInventory().addItem(reward.getItemStack());
|
||||
if(lootbox.doesBroadcast()) Bukkit.broadcastMessage(CC.translate(" &f• &l" + reward.getItemStack().getAmount() + "x &r" + (reward.getItemStack().hasItemMeta() ? reward.getItemStack().getItemMeta().getDisplayName() : reward.getItemStack().getType().name())));
|
||||
|
||||
});
|
||||
return;
|
||||
}
|
||||
if(lootbox.getRewardType() == RewardType.LOOTBOX) {
|
||||
|
||||
if(lootbox.doesBroadcast()) Bukkit.broadcastMessage(CC.translate("&d&lLootboxes &8" + Arrow.arrow() + " &r" + LuckpermUtils.getDisplayName(event.getPlayer()) + " &fhas just opened a " + lootbox.getDisplayName() + "&r &fand received:"));
|
||||
final Map<ItemStack, Double> rewards = new HashMap<>();
|
||||
lootbox.getRewards().stream()
|
||||
.filter(Objects::nonNull)
|
||||
.forEach(reward -> rewards.put(reward.getItemStack(), reward.getChance()));
|
||||
for(int rewardNum = 0; rewardNum < lootbox.getAmountOfRewards(); rewardNum++) {
|
||||
ItemStack reward = RandomUtils.getRandomItem(rewards);
|
||||
event.getPlayer().getInventory().addItem(reward);
|
||||
assert reward != null;
|
||||
if(lootbox.doesBroadcast()) Bukkit.broadcastMessage(CC.translate(" &f• &l" + reward.getAmount() + "x &r" + (reward.hasItemMeta() ? reward.getItemMeta().getDisplayName() : reward.getType().name())));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package net.crankedpvp.lootboxes.listeners;
|
||||
|
||||
import de.tr7zw.nbtapi.NBTItem;
|
||||
import net.crankedpvp.crankedutils.utils.InventoryUtils;
|
||||
import net.crankedpvp.lootboxes.CrankedLootboxes;
|
||||
import net.crankedpvp.lootboxes.language.Language;
|
||||
import net.crankedpvp.lootboxes.struct.Lootbox;
|
||||
import net.crankedpvp.lootboxes.struct.LootboxManager;
|
||||
import org.bukkit.Sound;
|
||||
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;
|
||||
|
||||
public class OpenSidePickerListener implements Listener {
|
||||
private final CrankedLootboxes plugin;
|
||||
public OpenSidePickerListener(CrankedLootboxes plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onInteract(PlayerInteractEvent event) {
|
||||
if(event.getAction() == null) return;
|
||||
if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
|
||||
ItemStack itemStack = event.getItem();
|
||||
if(!LootboxManager.isLootboxHalfGenerator(itemStack)) return;
|
||||
event.setCancelled(true);
|
||||
final NBTItem nbtItem = new NBTItem(itemStack);
|
||||
final Lootbox lootbox = plugin.getLootboxRegister().getLootbox(nbtItem.getString("leftOrRightHalf"));
|
||||
if(lootbox == null) return;
|
||||
if(!InventoryUtils.hasAvaliableSlot(event.getPlayer(), lootbox.getAmountOfRewards())) {
|
||||
event.getPlayer().sendMessage(Language.PREFIX.getMessage() + Language.FULL_INVENTORY.getMessage());
|
||||
event.getPlayer().playSound(event.getPlayer().getLocation(), Sound.LAVA_POP, 1.0F, 1.0F);
|
||||
return;
|
||||
}
|
||||
|
||||
final int amount = itemStack.getAmount();
|
||||
if (amount <= 1) {
|
||||
event.getPlayer().setItemInHand(null);
|
||||
} else {
|
||||
itemStack.setAmount(amount - 1);
|
||||
event.getPlayer().setItemInHand(itemStack);
|
||||
}
|
||||
|
||||
event.getPlayer().playSound(event.getPlayer().getLocation(), Sound.SUCCESSFUL_HIT, 0.5F, 0.5F);
|
||||
|
||||
if(Math.random() > 0.55) {
|
||||
event.getPlayer().getInventory().addItem(lootbox.buildHalf("left"));
|
||||
return;
|
||||
}
|
||||
event.getPlayer().getInventory().addItem(lootbox.buildHalf("right"));
|
||||
}
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package net.crankedpvp.lootboxes.listeners;
|
||||
|
||||
import de.tr7zw.nbtapi.NBTItem;
|
||||
import net.crankedpvp.crankedutils.utils.StringUtils;
|
||||
import net.crankedpvp.lootboxes.CrankedLootboxes;
|
||||
import net.crankedpvp.lootboxes.inventory.PreviewLootboxInventory;
|
||||
import net.crankedpvp.lootboxes.language.Language;
|
||||
import net.crankedpvp.lootboxes.struct.LootboxManager;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.Action;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
|
||||
public class PreviewLootboxListener implements Listener {
|
||||
|
||||
private final CrankedLootboxes plugin;
|
||||
public PreviewLootboxListener(CrankedLootboxes plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onInteract(PlayerInteractEvent event) {
|
||||
if(event.getPlayer() == null) return;
|
||||
if(event.getItem() == null) return;
|
||||
if(event.getAction() == Action.LEFT_CLICK_BLOCK || event.getAction() == Action.LEFT_CLICK_AIR) {
|
||||
if (LootboxManager.isLootbox(event.getItem())) {
|
||||
event.setCancelled(true);
|
||||
final NBTItem nbtItem = new NBTItem(event.getItem());
|
||||
final String name = nbtItem.getCompound("cosmicData").getString("lootboxID");
|
||||
PreviewLootboxInventory previewLootInventory = new PreviewLootboxInventory(event.getPlayer(), name, this.plugin);
|
||||
previewLootInventory.getUI().open(event.getPlayer());
|
||||
event.getPlayer().sendMessage(Language.PREFIX.getMessage() + Language.PREVIEW_LOOT.getMessage().replace("%lootbox%", StringUtils.toNiceString(name)));
|
||||
event.getPlayer().playSound(event.getPlayer().getLocation(), Sound.CHEST_OPEN, 1.0F, 2.0F);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package net.crankedpvp.lootboxes.struct;
|
||||
|
||||
import de.tr7zw.nbtapi.NBTItem;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import net.crankedpvp.crankedutils.utils.Arrow;
|
||||
import net.crankedpvp.crankedutils.utils.RandomUtils;
|
||||
import net.crankedpvp.crankedutils.utils.items.ItemBuilder;
|
||||
import net.crankedpvp.crankedutils.utils.items.SkullUtils;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@Getter @AllArgsConstructor
|
||||
public class Lootbox {
|
||||
|
||||
private final String id;
|
||||
private final String displayName;
|
||||
private final List<String> header;
|
||||
private final List<String> footer;
|
||||
private final String rewardTitle;
|
||||
private final String rewardLine;
|
||||
private final String jackpotTitle;
|
||||
private final String jackpotLine;
|
||||
private final Material material;
|
||||
private final boolean glowing;
|
||||
private final boolean viewRewards;
|
||||
private final boolean broadcastRewards;
|
||||
private final int amountOfRewards;
|
||||
private final RewardType rewardType;
|
||||
private final String skullValue;
|
||||
private final List<Reward> rewards;
|
||||
|
||||
public List<String> getLore() {
|
||||
List<String> lore = new ArrayList<>();
|
||||
if(!header.isEmpty()) lore.addAll(header);
|
||||
if(viewRewards) {
|
||||
if(rewards.stream().anyMatch(rewardObject -> !rewardObject.isJackpot())) {
|
||||
lore.add(rewardTitle.replace("%rewards%", String.valueOf(amountOfRewards)));
|
||||
for(Reward reward : rewards) {
|
||||
if(!reward.isJackpot()) {
|
||||
if(reward.getSpecialName() == null || reward.getSpecialName().equals("null")) {
|
||||
lore.add(rewardLine.replace("%amount%", String.valueOf(reward.getItemStack().getAmount())).replace("%name%", reward.getItemStack().getItemMeta().getDisplayName()));
|
||||
} else {
|
||||
lore.add(reward.getSpecialName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if(rewards.stream().anyMatch(Reward::isJackpot)) {
|
||||
if(rewards.stream().anyMatch(rewardObject -> !rewardObject.isJackpot())) lore.add("");
|
||||
lore.add(jackpotTitle);
|
||||
for(Reward reward : rewards) {
|
||||
if(reward.isJackpot()) {
|
||||
if(reward.getSpecialName() == null || reward.getSpecialName().equals("null")) {
|
||||
lore.add(jackpotLine.replace("%amount%", String.valueOf(reward.getItemStack().getAmount())).replace("%name%", reward.getItemStack().getItemMeta().getDisplayName()));
|
||||
} else {
|
||||
lore.add(reward.getSpecialName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
lore.addAll(footer);
|
||||
return lore;
|
||||
}
|
||||
|
||||
public ItemStack createLootbox(int amount) {
|
||||
ItemBuilder itemBuilder = this.skullValue.equals("???") ? new ItemBuilder(material) : new ItemBuilder(SkullUtils.getSkullFromSkin(this.skullValue));
|
||||
itemBuilder.setAmount(amount);
|
||||
itemBuilder.setName(this.displayName);
|
||||
itemBuilder.setLore(this.getLore()).colorize();
|
||||
if (glowing) itemBuilder.setGlowing();
|
||||
NBTItem nbtItem = new NBTItem(itemBuilder.build());
|
||||
nbtItem.setBoolean("broadcast", this.broadcastRewards);
|
||||
nbtItem.getOrCreateCompound("cosmicData").setString("customItem", "lootbox");
|
||||
nbtItem.getCompound("cosmicData").setString("lootboxID", id);
|
||||
ItemStack itemStack = nbtItem.getItem();
|
||||
if(!this.skullValue.equals("???")) {
|
||||
return SkullUtils.getSkull(itemStack, this.skullValue);
|
||||
}
|
||||
return nbtItem.getItem();
|
||||
}
|
||||
|
||||
public ItemStack buildHalf(String side) {
|
||||
String string = side.equals("left") ? "&b&lLeft Half (" + this.getDisplayName() + "&b&l)" : "&d&lRight Half (" + this.getDisplayName() + "&d&l)";
|
||||
String opposite = side.equals("left") ? "right" : "left";
|
||||
ItemBuilder itemBuilder = new ItemBuilder(Material.PRISMARINE_CRYSTALS, string, "&7Combine with the " + opposite + " half to",
|
||||
"&7create a full " + this.getDisplayName());
|
||||
NBTItem nbtItem = new NBTItem(itemBuilder.colorize().setGlowing().build());
|
||||
nbtItem.setString("halfLootbox", this.getId());
|
||||
nbtItem.setString("side", side);
|
||||
return nbtItem.getItem();
|
||||
|
||||
}
|
||||
|
||||
public ItemStack buildLeftOrRightHalf() {
|
||||
ItemBuilder itemBuilder = new ItemBuilder(Material.PAPER, "&b&lLEFT &f&l&nOR&r &d&lRIGHT &f&lHalf " + Arrow.arrow() + " " + this.getDisplayName(),
|
||||
"&7Right-Click this lootbox to receive",
|
||||
"&7either the &bleft &7or &dright&7 half",
|
||||
"&7of the " + this.getDisplayName() + "&r &7lootbox",
|
||||
"",
|
||||
"&4&l(!) &7Obtained from &cbuy.crankedpvp.org &7(/buy)",
|
||||
"",
|
||||
"&f&lRandom Loot &7(1 Item)",
|
||||
"&f&l* 1x &b&lLeft Half (" + this.getDisplayName() + "&b&l)",
|
||||
"&f&l* 1x &d&lRight Half (" + this.getDisplayName() + "&d&l)"
|
||||
);
|
||||
NBTItem nbtItem = new NBTItem(itemBuilder.colorize().setGlowing().build());
|
||||
nbtItem.setString("leftOrRightHalf", this.getId());
|
||||
return nbtItem.getItem();
|
||||
}
|
||||
|
||||
public ItemStack pickRandomReward() {
|
||||
final Map<ItemStack, Double> rewards = new HashMap<>();
|
||||
this.getRewards().stream()
|
||||
.filter(Objects::nonNull)
|
||||
.forEach(reward -> rewards.put(reward.getItemStack(), reward.getChance()));
|
||||
return RandomUtils.getRandomItem(rewards);
|
||||
}
|
||||
|
||||
public boolean doesBroadcast() {
|
||||
return this.broadcastRewards;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,287 @@
|
||||
package net.crankedpvp.lootboxes.struct;
|
||||
|
||||
import de.tr7zw.nbtapi.NBTItem;
|
||||
import net.crankedpvp.crankedutils.utils.InventoryUtils;
|
||||
import net.crankedpvp.crankedutils.utils.StringUtils;
|
||||
import net.crankedpvp.lootboxes.CrankedLootboxes;
|
||||
import net.crankedpvp.lootboxes.language.Config;
|
||||
import net.crankedpvp.lootboxes.language.Language;
|
||||
import net.crankedpvp.lootboxes.utils.ItemStackSerializer;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public class LootboxManager {
|
||||
|
||||
private final CrankedLootboxes plugin;
|
||||
public LootboxManager(CrankedLootboxes plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public static boolean isLootbox(ItemStack itemStack) {
|
||||
if (itemStack == null || itemStack.getType() == Material.AIR) return false;
|
||||
NBTItem nbtItem = new NBTItem(itemStack);
|
||||
if(!nbtItem.getOrCreateCompound("cosmicData").hasKey("customItem")) return false;
|
||||
return nbtItem.getCompound("cosmicData").hasKey("lootboxID");
|
||||
}
|
||||
|
||||
public static boolean isLootboxHalfGenerator(ItemStack itemStack) {
|
||||
if (itemStack == null || itemStack.getType() == Material.AIR) return false;
|
||||
NBTItem nbtItem = new NBTItem(itemStack);
|
||||
return (nbtItem.hasKey("leftOrRightHalf"));
|
||||
}
|
||||
|
||||
public static boolean isLootboxHalf(ItemStack itemStack) {
|
||||
if (itemStack == null || itemStack.getType() == Material.AIR) return false;
|
||||
NBTItem nbtItem = new NBTItem(itemStack);
|
||||
if(!nbtItem.hasKey("halfLootbox")) return false;
|
||||
return nbtItem.hasKey("side");
|
||||
}
|
||||
|
||||
public static boolean doesLootboxBroadcast(ItemStack itemStack) {
|
||||
if(!isLootbox(itemStack)) return false;
|
||||
if (itemStack.getType() == Material.AIR) return false;
|
||||
NBTItem nbtItem = new NBTItem(itemStack);
|
||||
return nbtItem.getBoolean("broadcast");
|
||||
}
|
||||
|
||||
public void createDefaultConfig(String name) {
|
||||
createDefaultConfig(name, 3);
|
||||
}
|
||||
|
||||
public void createDefaultConfig(String name, int amountOfRewards) {
|
||||
File file = new File(this.plugin.getDataFolder(), "Lootboxes/" + name + ".yml");
|
||||
if (!file.exists()) {
|
||||
file.getParentFile().mkdirs();
|
||||
try {
|
||||
file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
YamlConfiguration yamlConfiguration = new YamlConfiguration();
|
||||
String[] colors = new String[]{"&a", "&b", "&c", "&d", "&e", "&2", "&3", "&4", "&5", "&6", "&9"};
|
||||
String color = colors[ThreadLocalRandom.current().nextInt(1, 10)];
|
||||
yamlConfiguration.set("Item.Display.Name", "&f&ki&r " + color + "&l" + StringUtils.toNiceString(name) + " &f&ki&r &f&lLootbox");
|
||||
yamlConfiguration.set("Item.Display.Header", Config.LOOTBOX_HEADER.getValuesAsList());
|
||||
yamlConfiguration.set("Item.Display.Footer", Config.LOOTBOX_FOOTER.getValuesAsList());
|
||||
yamlConfiguration.set("Item.Reward.Title", "" + Config.LOOTBOX_REWARD_TITLE.getValue());
|
||||
yamlConfiguration.set("Item.Reward.Line", "" + Config.LOOTBOX_REWARD_LINE.getValue());
|
||||
yamlConfiguration.set("Item.Jackpot.Title", "" + Config.LOOTBOX_JACKPOT_TITLE.getValue());
|
||||
yamlConfiguration.set("Item.Jackpot.Line", "" + Config.LOOTBOX_JACKPOT_LINE.getValue());
|
||||
yamlConfiguration.set("Item.Material", "" + Config.LOOTBOX_MATERIAL.getValue());
|
||||
yamlConfiguration.set("Item.Glowing", true);
|
||||
yamlConfiguration.set("Misc.Reward.Amount", amountOfRewards);
|
||||
yamlConfiguration.set("Misc.Reward.Type", "LOOTBOX");
|
||||
yamlConfiguration.set("Misc.View.Rewards", true);
|
||||
yamlConfiguration.set("Misc.Broadcast.Rewards", false);
|
||||
try {
|
||||
yamlConfiguration.save(file);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void createDefaultConfigFromItemStack(ItemStack itemStack, String name, int amountOfRewards) {
|
||||
File file = new File(this.plugin.getDataFolder(), "Lootboxes/" + name + ".yml");
|
||||
if (!file.exists()) {
|
||||
file.getParentFile().mkdirs();
|
||||
try {
|
||||
file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
ItemMeta itemMeta = itemStack.getItemMeta();
|
||||
YamlConfiguration yamlConfiguration = new YamlConfiguration();
|
||||
yamlConfiguration.set("Item.Display.Name", itemMeta.getDisplayName());
|
||||
yamlConfiguration.set("Item.Display.Header", itemMeta.getLore());
|
||||
yamlConfiguration.set("Item.Display.Footer", new ArrayList<>());
|
||||
yamlConfiguration.set("Item.Reward.Title", "" + Config.LOOTBOX_REWARD_TITLE.getValue());
|
||||
yamlConfiguration.set("Item.Reward.Line", "" + Config.LOOTBOX_REWARD_LINE.getValue());
|
||||
yamlConfiguration.set("Item.Jackpot.Title", "" + Config.LOOTBOX_JACKPOT_TITLE.getValue());
|
||||
yamlConfiguration.set("Item.Jackpot.Line", "" + Config.LOOTBOX_JACKPOT_LINE.getValue());
|
||||
yamlConfiguration.set("Item.Material", "" + itemStack.getType().name());
|
||||
yamlConfiguration.set("Item.Glowing", itemMeta.hasEnchants());
|
||||
yamlConfiguration.set("Misc.Reward.Amount", amountOfRewards);
|
||||
yamlConfiguration.set("Misc.Reward.Type", "LOOTBOX");
|
||||
yamlConfiguration.set("Misc.View.Rewards", true);
|
||||
yamlConfiguration.set("Misc.Broadcast.Rewards", false);
|
||||
try {
|
||||
yamlConfiguration.save(file);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void createLootbox(String lootbox, Integer amountOfRewards) {
|
||||
createDefaultConfig(lootbox, amountOfRewards);
|
||||
this.initialize(lootbox);
|
||||
}
|
||||
|
||||
public void createLootboxFromItemStack(ItemStack itemStack, String lootbox, Integer amountOfRewards) {
|
||||
createDefaultConfigFromItemStack(itemStack, lootbox, amountOfRewards);
|
||||
this.initialize(lootbox);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void deleteConfig(String name) {
|
||||
File file = new File(this.plugin.getDataFolder(), "Lootboxes/" + name + ".yml");
|
||||
if (file.exists())
|
||||
file.delete();
|
||||
}
|
||||
|
||||
public void reloadLootbox(String lootbox) {
|
||||
if (this.plugin.getLootboxRegister().isLootbox(lootbox)) {
|
||||
this.plugin.getLootboxRegister().removeLootbox(lootbox);
|
||||
initialize(lootbox);
|
||||
} else {
|
||||
System.out.println("[-] Failure reloading lootbox, " + lootbox);
|
||||
}
|
||||
}
|
||||
|
||||
public void initializeAll() {
|
||||
File dataFolder = new File(this.plugin.getDataFolder(), "Lootboxes");
|
||||
Arrays.stream(Objects.requireNonNull(dataFolder.listFiles()))
|
||||
.filter(file -> !file.isDirectory())
|
||||
.filter(file -> !file.getName().startsWith("-"))
|
||||
.forEach(file -> {
|
||||
try {
|
||||
initialize(file.getName().replace(".yml", ""));
|
||||
} catch (NullPointerException exception) {
|
||||
System.out.println("[CrankedLootboxes] Failed to register lootbox: " + file.getName().replace(".yml", ""));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void initialize(String fileName) {
|
||||
File file = new File(this.plugin.getDataFolder(), "Lootboxes/" + fileName + ".yml");
|
||||
YamlConfiguration yamlConfiguration = new YamlConfiguration();
|
||||
try {
|
||||
yamlConfiguration.load(file);
|
||||
} catch (IOException |org.bukkit.configuration.InvalidConfigurationException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
String name = yamlConfiguration.getString("Item.Display.Name");
|
||||
List<String> header = yamlConfiguration.getStringList("Item.Display.Header");
|
||||
List<String> footer = yamlConfiguration.getStringList("Item.Display.Footer");
|
||||
String rewardTitle = yamlConfiguration.getString("Item.Reward.Title");
|
||||
String rewardLine = yamlConfiguration.getString("Item.Reward.Line");
|
||||
String jackpotTitle = yamlConfiguration.getString("Item.Jackpot.Title");
|
||||
String jackpotLine = yamlConfiguration.getString("Item.Jackpot.Line");
|
||||
String mater = yamlConfiguration.getString("Item.Material");
|
||||
Material material = mater.contains("SKULL") ? Material.SKULL_ITEM : Material.valueOf(mater.toUpperCase());
|
||||
String skullValue = mater.contains(":") ? mater.split(":")[1] : "???";
|
||||
boolean glowing = yamlConfiguration.getBoolean("Item.Glowing");
|
||||
int rewardAmount = yamlConfiguration.getInt("Misc.Reward.Amount");
|
||||
boolean viewRewards = yamlConfiguration.getBoolean("Misc.View.Rewards");
|
||||
boolean broadcastRewards = yamlConfiguration.contains("Misc.Broadcast.Rewards") && yamlConfiguration.getBoolean("Misc.Broadcast.Rewards");
|
||||
RewardType rewardType = RewardType.valueOf(yamlConfiguration.getString("Misc.Reward.Type").toUpperCase());
|
||||
List<Reward> rewards = getRewards(yamlConfiguration);
|
||||
if(rewardType == RewardType.BUNDLE) rewardAmount = rewards.size();
|
||||
if(rewardType == RewardType.LUCKY_BLOCK) rewardAmount = 1;
|
||||
Lootbox lootbox = new Lootbox(file.getName().replace(".yml", ""), name, header, footer, rewardTitle, rewardLine, jackpotTitle, jackpotLine, material, glowing, viewRewards, broadcastRewards, rewardAmount, rewardType, skullValue, rewards);
|
||||
this.plugin.getLootboxRegister().register(lootbox);
|
||||
}
|
||||
|
||||
private List<Reward> getRewards(FileConfiguration fileConfiguration) {
|
||||
List<Reward> rewards = new ArrayList<>();
|
||||
if (fileConfiguration.getConfigurationSection("rewards") != null)
|
||||
fileConfiguration.getConfigurationSection("rewards").getKeys(false).forEach(key -> {
|
||||
ItemStack itemStack = null;
|
||||
try {
|
||||
itemStack = ItemStackSerializer.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"), fileConfiguration.getConfigurationSection("rewards." + key).getBoolean("jackpot"), fileConfiguration.getConfigurationSection("rewards." + key).getString("special-name"));
|
||||
rewards.add(reward);
|
||||
});
|
||||
return rewards;
|
||||
}
|
||||
|
||||
public void addItem(Player player, String lootbox, double chance, boolean jackpot) {
|
||||
try {
|
||||
File file = new File(this.plugin.getDataFolder() + "/Lootboxes/" + lootbox + ".yml");
|
||||
if(file.exists()) {
|
||||
YamlConfiguration yamlConfiguration = YamlConfiguration.loadConfiguration(file);
|
||||
String itemName = ChatColor.stripColor(player.getItemInHand().getItemMeta().getDisplayName());
|
||||
String toBase64 = ItemStackSerializer.serialize(player.getItemInHand());
|
||||
if(!yamlConfiguration.contains("rewards") || !yamlConfiguration.getConfigurationSection("rewards").getKeys(true).contains(itemName + player.getItemInHand().getAmount())) {
|
||||
String name = itemName + player.getItemInHand().getAmount();
|
||||
yamlConfiguration.set("rewards." + name + ".item", toBase64);
|
||||
yamlConfiguration.set("rewards." + name + ".chance", chance);
|
||||
yamlConfiguration.set("rewards." + name + ".jackpot", jackpot);
|
||||
yamlConfiguration.set("rewards." + name + ".special-name", "&f&l* " + player.getItemInHand().getAmount() + "x " + player.getItemInHand().getItemMeta().getDisplayName());
|
||||
yamlConfiguration.save(file);
|
||||
player.sendMessage(Language.PREFIX.getMessage() + Language.LOOTBOX_ADDITEM.getMessage().replace("%lootbox%", lootbox).replace("%item%", player.getItemInHand().getItemMeta().getDisplayName()));
|
||||
player.playSound(player.getLocation(), Sound.LEVEL_UP, 2.0F, 2.0F);
|
||||
reloadLootbox(lootbox);
|
||||
} else {
|
||||
player.sendMessage(Language.PREFIX.getMessage() + Language.LOOTBOX_ITEM_ALREADY_IN.getMessage().replace("%lootbox%", lootbox));
|
||||
player.playSound(player.getLocation(), Sound.ANVIL_LAND, 2.0F, 2.0F);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void removeItemFromLootbox(Player player, ItemStack itemStack, String lootbox) {
|
||||
try {
|
||||
File file = new File(this.plugin.getDataFolder() + "/Lootboxes/" + lootbox + ".yml");
|
||||
if (file.exists()) {
|
||||
YamlConfiguration yamlConfiguration = YamlConfiguration.loadConfiguration(file);
|
||||
String itemName = ChatColor.stripColor(itemStack.getItemMeta().getDisplayName());
|
||||
if (!yamlConfiguration.contains("rewards")) {
|
||||
player.sendMessage(Language.PREFIX.getMessage() + Language.LOOTBOX_ITEM_NOT_FOUND.getMessage().replace("%lootbox%", lootbox));
|
||||
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(Language.PREFIX.getMessage() + Language.LOOTBOX_REMOVE_ITEM.getMessage().replace("%lootbox%", lootbox).replace("%item%", itemStack.getItemMeta().getDisplayName()));
|
||||
player.playSound(player.getLocation(), Sound.CHICKEN_EGG_POP, 2.0F, 2.0F);
|
||||
reloadLootbox(lootbox);
|
||||
} else {
|
||||
player.sendMessage(Language.PREFIX.getMessage() + Language.LOOTBOX_ITEM_NOT_FOUND.getMessage().replace("%lootbox%", lootbox));
|
||||
player.playSound(player.getLocation(), Sound.ANVIL_LAND, 2.0F, 2.0F);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void giveLootbox(Player player, Lootbox lootbox, int amount) {
|
||||
InventoryUtils.giveItemToPlayer(player, lootbox.createLootbox(amount));
|
||||
}
|
||||
|
||||
public void giveLootbox(Player player, Lootbox lootbox) {
|
||||
giveLootbox(player, lootbox, 1);
|
||||
}
|
||||
|
||||
public ItemStack getLootbox(Lootbox lootbox) {
|
||||
return getLootbox(lootbox, 1);
|
||||
}
|
||||
|
||||
public ItemStack getLootbox(Lootbox lootbox, int amount) {
|
||||
return lootbox.createLootbox(amount);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package net.crankedpvp.lootboxes.struct;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Maps;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import net.crankedpvp.lootboxes.CrankedLootboxes;
|
||||
import org.apache.commons.lang.Validate;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class LootboxRegister {
|
||||
private final CrankedLootboxes plugin;
|
||||
|
||||
@Getter
|
||||
private final Map<String, Lootbox> lootboxes;
|
||||
|
||||
public LootboxRegister(CrankedLootboxes plugin) {
|
||||
this.lootboxes = Maps.newConcurrentMap();
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public Map<String, Lootbox> getSortedLootboxes() {
|
||||
return sort(this.lootboxes);
|
||||
}
|
||||
|
||||
|
||||
public Map<String, Lootbox> sort(Map<String, Lootbox> map) {
|
||||
List<Map.Entry<String, Lootbox>> list = new ArrayList<>(map.entrySet());
|
||||
list.sort(Map.Entry.comparingByKey());
|
||||
Collections.reverse(list);
|
||||
Map<String, Lootbox> result = new LinkedHashMap<>();
|
||||
for (int i = list.size() - 1; i >= 0; i--) {
|
||||
Map.Entry<String, Lootbox> entry = list.get(i);
|
||||
result.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Getter @Setter
|
||||
private Lootbox weeklyLootbox;
|
||||
|
||||
|
||||
public void register(Lootbox lootbox) {
|
||||
try {
|
||||
this.lootboxes.put(lootbox.getId(), lootbox);
|
||||
this.plugin.getLogger().info("Successfully registered lootbox: " + lootbox.getId());
|
||||
} catch (Exception var3) {
|
||||
System.err.println("&8(&c!&8) Failed to register lootbox, " + lootbox.getId());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isLootbox(String name) {
|
||||
return (getLootbox(name) != null);
|
||||
}
|
||||
|
||||
public Lootbox getLootbox(String name) {
|
||||
Validate.notNull(name);
|
||||
Validate.notEmpty(name);
|
||||
return this.lootboxes.get(name);
|
||||
}
|
||||
|
||||
public void clearLootboxMap() {
|
||||
this.lootboxes.clear();
|
||||
}
|
||||
|
||||
public void removeLootbox(String lootbox) {
|
||||
this.lootboxes.remove(lootbox);
|
||||
}
|
||||
|
||||
public Collection<Lootbox> getLootboxObjects() {
|
||||
return Collections.unmodifiableCollection(this.lootboxes.values());
|
||||
}
|
||||
|
||||
public List<Lootbox> getLootboxesAsList() {
|
||||
return new ArrayList<>(this.lootboxes.values());
|
||||
}
|
||||
|
||||
public List<String> getLootboxesAsIDs() {
|
||||
return getLootboxesAsList().stream()
|
||||
.map(Lootbox::getId)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public Lootbox select(Collection<Lootbox> lootboxCollection) {
|
||||
return (lootboxCollection.size() > 0) ? ImmutableList.copyOf(lootboxCollection).get(ThreadLocalRandom.current().nextInt(lootboxCollection.size())) : null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package net.crankedpvp.lootboxes.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;
|
||||
@Getter
|
||||
private final boolean jackpot;
|
||||
private final String specialName;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package net.crankedpvp.lootboxes.struct;
|
||||
|
||||
public enum RewardType {
|
||||
BUNDLE, LUCKY_BLOCK, LOOTBOX, HYPEBOX
|
||||
;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package net.crankedpvp.lootboxes.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 ItemStackSerializer {
|
||||
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,15 @@
|
||||
LOOTBOX_MATERIAL: CHEST
|
||||
LOOTBOX_HEADER:
|
||||
- "&7Right-Click to receive some"
|
||||
- "&7the following rewards..."
|
||||
- ""
|
||||
- "&4&l(!) &7Obtained from &cbuy.crankedpvp.org &7(/buy)"
|
||||
- ""
|
||||
LOOTBOX_FOOTER:
|
||||
- ""
|
||||
- "&c&lNOTE: &fThis is a physical item"
|
||||
- "&fIt will not be refunded if lost"
|
||||
LOOTBOX_REWARD_TITLE: "&f&lRandom Loot (&7%rewards% Items&f&l)"
|
||||
LOOTBOX_REWARD_LINE: "&f&l* %amount%x %name%"
|
||||
LOOTBOX_JACKPOT_TITLE: "&f&lJackpot Loot"
|
||||
LOOTBOX_JACKPOT_LINE: " &f&l* %amount%x %name%"
|
||||
@@ -0,0 +1,18 @@
|
||||
PREFIX: "&cCranked&fPvP &8| "
|
||||
FULL_INVENTORY: "&cYour inventory does not have enough slots to complete this action"
|
||||
ALREADY_LOOTBOX: "&cA lootbox named %lootbox% is already registered"
|
||||
CREATED_LOOTBOX: "&aYou have created the lootbox %lootbox%"
|
||||
DELETED_LOOTBOX: "&aYou have deleted the lootbox %lootbox%"
|
||||
LOOTBOX_INVALID: "&cThe lootbox with named %lootbox% could not be found"
|
||||
LOOTBOX_DISABLED: "&aThe lootbox named %lootbox% has been disabled"
|
||||
LOOTBOX_ENABLED: "&aThe lootbox named %lootbox% has been enabled"
|
||||
RELOADED_ALL: "&aYou have reloaded ALL lootboxes"
|
||||
RELOADED_SINGULAR: "&aYou have reloaded the %lootbox% lootbox"
|
||||
LOOTBOX_RECEIVED: "&aYou have received the %lootbox% lootbox"
|
||||
LOOTBOX_GIVEN: "&aYou have given the %lootbox% lootbox to %player%"
|
||||
LOOTBOX_ADDITEM: "&aAdded %item%&r&a to %lootbox% lootbox"
|
||||
LOOTBOX_ITEM_ALREADY_IN: "&cThis item is already in the %lootbox% lootbox"
|
||||
LOOTBOX_ITEM_NOT_FOUND: "&cThis item is not in the %lootbox% lootbox"
|
||||
LOOTBOX_REMOVE_ITEM: "&cDeleted %item%&r&c from %lootbox% lootbox"
|
||||
LOOTBOX_OPENED: "&aYou have opened the %lootbox% lootbox"
|
||||
PREVIEW_LOOT: "&aYou are now previewing the loot of the %lootbox% lootbox"
|
||||
@@ -0,0 +1,7 @@
|
||||
name: CrankedLootboxes
|
||||
main: net.crankedpvp.lootboxes.CrankedLootboxes
|
||||
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