Initial commit

This commit is contained in:
Scifi9902
2026-10-12 13:57:05 -03:00
commit 4bde3b176a
2576 changed files with 212659 additions and 0 deletions
+28
View File
@@ -0,0 +1,28 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>net.crankedpvp</groupId>
<artifactId>ProjectCranked</artifactId>
<version>1.0</version>
</parent>
<groupId>net.crankedpvp.hypeboxes</groupId>
<artifactId>HypeBoxes</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>net.crankedpvp</groupId>
<artifactId>Utils</artifactId>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,81 @@
package net.crankedpvp.hypeboxes;
import lombok.Getter;
import me.mattstudios.mf.base.CommandManager;
import net.crankedpvp.hypeboxes.commands.HypeBoxCommand;
import net.crankedpvp.hypeboxes.commands.ViewBattleOpenCommand;
import net.crankedpvp.hypeboxes.commands.ViewHypeboxOpenCommand;
import net.crankedpvp.hypeboxes.inventory.HypeBoxAnimationInventory;
import net.crankedpvp.hypeboxes.inventory.HypeboxBattleInventory;
import net.crankedpvp.hypeboxes.language.FileHandler;
import net.crankedpvp.hypeboxes.listeners.HypeboxOpenListener;
import net.crankedpvp.hypeboxes.listeners.HypeboxPreviewListener;
import net.crankedpvp.hypeboxes.struct.HypeBoxManager;
import net.crankedpvp.hypeboxes.struct.HypeBoxRegister;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class HypeboxesPlugin extends JavaPlugin {
@Getter
private static HypeboxesPlugin instance;
@Getter
private FileHandler fileHandler;
@Getter
private HypeBoxRegister hypeBoxRegister;
@Getter
private HypeBoxManager hypeBoxManager;
@Getter
private CommandManager commandManager;
@Getter
private Map<UUID, HypeBoxAnimationInventory> hypeboxesBeingOpened;
@Getter
private Map<UUID, HypeboxBattleInventory> hypeboxBattles;
@Override
public void onEnable() {
instance = this;
this.fileHandler = new FileHandler(this);
this.fileHandler.load();
this.hypeBoxRegister = new HypeBoxRegister(this);
this.hypeBoxManager = new HypeBoxManager(this);
this.hypeBoxRegister.clearHypeboxMap();
this.hypeBoxManager.createDefaultConfig("-example");
this.hypeBoxManager.initializeAll();
this.commandManager = new CommandManager(this);
this.registerCommandCompletion();
this.commandManager.register(new HypeBoxCommand(this));
this.commandManager.register(new ViewHypeboxOpenCommand(this));
this.commandManager.register(new ViewBattleOpenCommand(this));
this.hypeboxesBeingOpened = new HashMap<>();
this.hypeboxBattles = new HashMap<>();
this.registerListeners();
}
private void registerListeners() {
PluginManager pluginManager = this.getServer().getPluginManager();
pluginManager.registerEvents(new HypeboxOpenListener(this), this);
pluginManager.registerEvents(new HypeboxPreviewListener(this), this);
}
public void registerCommandCompletion() {
this.commandManager.getCompletionHandler().register("#hypeboxes", input -> this.hypeBoxRegister.getHypeboxesAsIDs());
}
}
@@ -0,0 +1,313 @@
package net.crankedpvp.hypeboxes.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.hypeboxes.HypeboxesPlugin;
import net.crankedpvp.hypeboxes.inventory.AdminHypeboxInventory;
import net.crankedpvp.hypeboxes.inventory.HypeboxPreBattleInventory;
import net.crankedpvp.hypeboxes.language.Language;
import net.crankedpvp.hypeboxes.struct.HypeBox;
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("hypebox")
@Alias({"hb"})
public class HypeBoxCommand extends CommandBase {
private final HypeboxesPlugin plugin;
public HypeBoxCommand(HypeboxesPlugin 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;
}
}
@SubCommand("help")
@WrongUsage("&eUsage: /hb 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")
@Permission("cranked.admin")
@WrongUsage("&eUsage: /hb create <name> [amount of rewards]")
public void createCommand(CommandSender commandSender, String lootbox) {
Player player = (Player) commandSender;
if(plugin.getHypeBoxRegister().isHypebox(lootbox)) {
player.sendMessage(Language.PREFIX.getMessage() + Language.ALREADY_HYPEBOX.getMessage().replace("%lootbox%", lootbox));
player.playSound(player.getLocation(), Sound.ANVIL_LAND, 2.0F, 2.0F);
return;
}
if(player.getItemInHand() == null) {
plugin.getHypeBoxManager().createHypebox(lootbox);
} else {
plugin.getHypeBoxManager().createHypeboxFromItemStack(player.getItemInHand(), lootbox);
}
player.sendMessage(Language.PREFIX.getMessage() + Language.CREATED_HYPEBOX.getMessage().replace("%lootbox%", lootbox));
player.playSound(player.getLocation(), Sound.SUCCESSFUL_HIT, 2.0F, 2.0F);
plugin.registerCommandCompletion();
}
@SubCommand("delete")
@Permission("cranked.admin")
@WrongUsage("&eUsage: /hb delete <lootbox>")
public void deleteCommand(CommandSender commandSender, @Completion("#hypeboxes") String lootbox) {
Player player = (Player) commandSender;
if(!plugin.getHypeBoxRegister().isHypebox(lootbox)) {
player.sendMessage(Language.PREFIX.getMessage() + Language.HYPEBOX_INVALID.getMessage().replace("%lootbox%", lootbox));
player.playSound(player.getLocation(), Sound.ANVIL_LAND, 2.0F, 2.0F);
return;
}
player.sendMessage(Language.PREFIX.getMessage() + Language.DELETED_HYPEBOX.getMessage().replace("%lootbox%", lootbox));
player.playSound(player.getLocation(), Sound.LAVA_POP, 2.0F, 2.0F);
plugin.getHypeBoxManager().deleteConfig(lootbox);
plugin.getHypeBoxRegister().removeHypebox(lootbox);
}
@SubCommand("disable")
@Permission("cranked.admin")
@WrongUsage("&eUsage: /hb disable <lootbox>")
public void disableCommand(CommandSender commandSender, @Completion("#hypeboxes") String lootbox) {
Player player = (Player) commandSender;
if (!this.plugin.getHypeBoxRegister().isHypebox(lootbox)) {
player.sendMessage(Language.PREFIX.getMessage() + Language.HYPEBOX_INVALID.getMessage().replace("%lootbox%", lootbox));
player.playSound(player.getLocation(), Sound.ANVIL_LAND, 2.0F, 2.0F);
return;
}
File file = new File(this.plugin.getDataFolder() + "/Hypeboxes/" + lootbox + ".yml");
if (file.renameTo(new File(this.plugin.getDataFolder() + "/Hypeboxes/-" + lootbox + ".yml"))) {
this.plugin.getHypeBoxRegister().removeHypebox(lootbox);
player.sendMessage(Language.PREFIX.getMessage() + Language.HYPEBOX_DISABLED.getMessage().replace("%lootbox%", lootbox));
player.playSound(player.getLocation(), Sound.BAT_TAKEOFF, 2.0F, 2.0F);
}
}
@SubCommand("enable")
@Permission("cranked.admin")
@WrongUsage("&eUsage: /hb enable <lootbox>")
public void enableCommand(CommandSender commandSender, @Completion("#hypeboxes") String lootbox) {
Player player = (Player) commandSender;
if (this.plugin.getHypeBoxRegister().isHypebox(lootbox)) {
player.sendMessage(Language.PREFIX.getMessage() + Language.ALREADY_HYPEBOX.getMessage().replace("%lootbox%", lootbox));
player.playSound(player.getLocation(), Sound.ANVIL_LAND, 2.0F, 2.0F);
return;
}
File file = new File(this.plugin.getDataFolder() + "/Hypeboxes/-" + lootbox + ".yml");
if (file.renameTo(new File(this.plugin.getDataFolder() + "/Hypeboxes/" + lootbox + ".yml"))) {
this.plugin.getHypeBoxManager().initialize(lootbox);
player.sendMessage(Language.PREFIX.getMessage() + Language.HYPEBOX_ENABLED.getMessage().replace("%lootbox%", lootbox));
player.playSound(player.getLocation(), Sound.SUCCESSFUL_HIT, 2.0F, 2.0F);
}
}
@SubCommand("reload")
@Permission("cranked.admin")
@WrongUsage("&eUsage: /hb reload [lootbox]")
public void reloadCommand(CommandSender commandSender, @Optional @Completion("#hypeboxes") 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.getHypeBoxRegister().clearHypeboxMap();
this.plugin.getHypeBoxManager().initializeAll();
this.plugin.getFileHandler().load();
return;
}
if (this.plugin.getHypeBoxRegister().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.getHypeBoxRegister().removeHypebox(lootbox);
this.plugin.getHypeBoxManager().initialize(lootbox);
return;
}
player.sendMessage(Language.HYPEBOX_INVALID.getMessage().replace("%lootbox%", lootbox));
player.playSound(player.getLocation(), Sound.ANVIL_LAND, 2.0F, 2.0F);
}
@SubCommand("list")
@Permission("cranked.admin")
@WrongUsage("&eUsage: /hb list")
public void listCommand(CommandSender commandSender) {
Player player = (Player) commandSender;
AdminHypeboxInventory adminLootboxInventory = new AdminHypeboxInventory(player, this.plugin);
adminLootboxInventory.getUI().open(player);
player.playSound(player.getLocation(), Sound.CHEST_OPEN, 2.0F, 2.0F);
}
@SubCommand("give")
@Permission("cranked.admin")
@WrongUsage("&eUsage: /hb give <player> <lootbox> [amount]")
public void giveCommand(CommandSender commandSender, @Completion("#players")Player player, @Completion("#hypeboxes") 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.getHypeBoxRegister().isHypebox(lootbox)) {
commandSender.sendMessage(Language.PREFIX.getMessage() + Language.HYPEBOX_INVALID.getMessage().replace("%lootbox%", lootbox));
//sender.playSound(player.getLocation(), Sound.ANVIL_LAND, 2.0F, 2.0F);
return;
}
this.plugin.getHypeBoxManager().giveHypebox(player, this.plugin.getHypeBoxRegister().getHypebox(lootbox), amount);
commandSender.sendMessage(Language.PREFIX.getMessage() + Language.HYPEBOX_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.HYPEBOX_RECEIVED.getMessage().replace("%lootbox%", CC.translate(this.plugin.getHypeBoxRegister().getHypebox(lootbox).getDisplayName())));
}
@SubCommand("giveall")
@Permission("cranked.admin")
@WrongUsage("&eUsage: /hb giveall <lootbox> [amount]")
public void giveCommand(CommandSender commandSender, @Completion("#hypeboxes") String lootbox, @Optional Integer amount) {
if(amount == null) amount = 1;
if(!this.plugin.getHypeBoxRegister().isHypebox(lootbox)) {
commandSender.sendMessage(Language.PREFIX.getMessage() + Language.HYPEBOX_INVALID.getMessage().replace("%lootbox%", lootbox));
SoundUtils.tryPlayErrorSound(commandSender);
return;
}
commandSender.sendMessage(Language.PREFIX.getMessage() + Language.HYPEBOX_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.getHypeBoxManager().giveHypebox(p, this.plugin.getHypeBoxRegister().getHypebox(lootbox), amount);
p.sendMessage(Language.PREFIX.getMessage() + Language.HYPEBOX_RECEIVED.getMessage().replace("%lootbox%", CC.translate(this.plugin.getHypeBoxRegister().getHypebox(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: /hb addreward <lootbox> <chance> <value>")
public void addItemCommand(CommandSender commandSender, @Completion("#hypeboxes") String lootbox, Double chance, Double value) {
this.plugin.getHypeBoxManager().addItem((Player)commandSender, lootbox, chance, value);
}
@SubCommand("removereward")
@Alias({"removeitem", "removeloot"})
@Permission("cranked.admin")
public void removeItemCommand(CommandSender commandSender, @Completion("#hypeboxes") String lootbox) {
Player player = (Player)commandSender;
this.plugin.getHypeBoxManager().removeItemFromHypebox(player, player.getItemInHand(), lootbox);
}
@SubCommand("clearrewardsinallhypeboxes")
@Permission("*")
public void clear(CommandSender commandSender) {
if(commandSender instanceof Player) return;
this.plugin.getHypeBoxRegister().getLootboxes().forEach((lootboxName, lootbox) -> {
lootbox.getRewards().clear();
File file = new File(this.plugin.getDataFolder() + "/Hypeboxes/" + 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("/hb half <lootbox> [left/right]")
@Permission("cranked.admin")
public void giveHalf(final Player player, String lootbox, @Optional String half) {
if(!this.plugin.getHypeBoxRegister().isHypebox(lootbox)) {
player.sendMessage(Language.PREFIX.getMessage() + Language.HYPEBOX_INVALID.getMessage().replace("%lootbox%", lootbox));
SoundUtils.tryPlayErrorSound(player);
return;
}
HypeBox lootboxObject = this.plugin.getHypeBoxRegister().getHypebox(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));
}
@SubCommand("battle")
public void battle(final Player player) {
HypeboxPreBattleInventory hypeboxPreBattleInventory = new HypeboxPreBattleInventory(player);
hypeboxPreBattleInventory.getUI().open(player);
}
private void sendAdminHelpMessage(Player player) {
player.sendMessage("");
player.sendMessage(CC.translate("&cCranked&fPvP &8| &cHypeboxes"));
player.sendMessage(CC.translate("&c[] &8- &fOptional Argument"));
player.sendMessage(CC.translate("&4<> &8- &fRequired Argument"));
player.sendMessage("");
player.sendMessage(CC.translate(" &c* &f/hb create <name>"));
player.sendMessage(CC.translate(" &c* &f/hb delete <name>"));
player.sendMessage(CC.translate(" &c* &f/hb enable <name>"));
player.sendMessage(CC.translate(" &c* &f/hb disable <name>"));
player.sendMessage(CC.translate(" &c* &f/hb additem <name> <chance> <value>"));
player.sendMessage(CC.translate(" &c* &f/hb removeitem <name>"));
player.sendMessage(CC.translate(" &c* &f/hb reload [lootbox]"));
player.sendMessage(CC.translate(" &c* &f/hb list"));
player.sendMessage(CC.translate(" &c* &f/hb give <player> <lootbox> [amount]"));
player.sendMessage(CC.translate(" &c* &f/hb giveall <lootbox> [amount]"));
player.sendMessage("");
}
}
@@ -0,0 +1,35 @@
package net.crankedpvp.hypeboxes.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.hypeboxes.HypeboxesPlugin;
import net.crankedpvp.hypeboxes.inventory.HypeboxBattleInventory;
import org.bukkit.entity.Player;
import java.util.UUID;
@Command("viewbattleopen")
public class ViewBattleOpenCommand extends CommandBase {
private final HypeboxesPlugin plugin;
public ViewBattleOpenCommand(HypeboxesPlugin plugin) {
this.plugin = plugin;
}
@Default
public void defaultCommand(final Player player, String crateID) {
if(!this.plugin.getHypeboxBattles().containsKey(UUID.fromString(crateID))) return;
HypeboxBattleInventory hypeboxBattleInventory = this.plugin.getHypeboxBattles().get(UUID.fromString(crateID));
if(!hypeboxBattleInventory.isActive()) {
player.sendMessage(CC.translate("&4&lHypebox Battles &8" + Arrow.arrow() + " &eThis hype box battle has finished being opened"));
SoundUtils.tryPlayErrorSound(player);
return;
}
this.plugin.getHypeboxBattles().get(UUID.fromString(crateID)).getUI().open(player);
}
}
@@ -0,0 +1,35 @@
package net.crankedpvp.hypeboxes.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.hypeboxes.HypeboxesPlugin;
import net.crankedpvp.hypeboxes.inventory.HypeBoxAnimationInventory;
import org.bukkit.entity.Player;
import java.util.UUID;
@Command("viewhypeboxopen")
public class ViewHypeboxOpenCommand extends CommandBase {
private final HypeboxesPlugin plugin;
public ViewHypeboxOpenCommand(HypeboxesPlugin plugin) {
this.plugin = plugin;
}
@Default
public void defaultCommand(final Player player, String crateID) {
if(!this.plugin.getHypeboxesBeingOpened().containsKey(UUID.fromString(crateID))) return;
HypeBoxAnimationInventory hypeBoxAnimationInventory = this.plugin.getHypeboxesBeingOpened().get(UUID.fromString(crateID));
if(!hypeBoxAnimationInventory.isActive()) {
player.sendMessage(CC.translate("&d&lHype Boxes &8" + Arrow.arrow() + " &eThis hype box has finished being opened"));
SoundUtils.tryPlayErrorSound(player);
return;
}
this.plugin.getHypeboxesBeingOpened().get(UUID.fromString(crateID)).getUI().open(player);
}
}
@@ -0,0 +1,5 @@
package net.crankedpvp.hypeboxes.interfaces;
public interface ActiveInventory {
boolean isActive();
}
@@ -0,0 +1,61 @@
package net.crankedpvp.hypeboxes.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.hypeboxes.HypeboxesPlugin;
import net.crankedpvp.hypeboxes.language.Language;
import net.crankedpvp.hypeboxes.struct.HypeBox;
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;
public class AdminHypeboxInventory {
private final PaginatedGui lootboxMenu;
public PaginatedGui getUI() {
return this.lootboxMenu;
}
public AdminHypeboxInventory(Player player, HypeboxesPlugin plugin) {
Map<String, HypeBox> lootboxObjectMap = plugin.getHypeBoxRegister().getSortedHypeboxes();
this.lootboxMenu = new PaginatedGui(6, 28, "Cranked Hypeboxes");
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 &cHypeboxes")).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);
}));
lootboxObjectMap.entrySet().stream()
.filter(Objects::nonNull)
.forEach(lootbox -> {
ItemStack itemStack = lootbox.getValue().createHypeBox(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() + " hypebox"));
player.playSound(player.getLocation(), Sound.SUCCESSFUL_HIT, 2.0F, 2.0F);
}
}));
});
}
}
@@ -0,0 +1,122 @@
package net.crankedpvp.hypeboxes.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.hypeboxes.HypeboxesPlugin;
import net.crankedpvp.hypeboxes.interfaces.ActiveInventory;
import net.crankedpvp.hypeboxes.struct.HypeBox;
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 implements ActiveInventory {
private BukkitTask spinningTask;
private int amountOfSpins = 0;
private ItemStack receivingItem;
private final ItemStack[] itemStacks = new ItemStack[9];
private final Player player;
private boolean active = true;
private final Gui hypeBoxAnimationInventory;
public Gui getUI() {
return this.hypeBoxAnimationInventory;
}
public HypeBoxAnimationInventory(Player player, HypeBox hypebox) {
this.player = player;
this.amountOfSpins = 0;
this.receivingItem = hypebox.pickRandomReward();
this.hypeBoxAnimationInventory = new Gui(3, CC.translate(hypebox.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(!inventoryCloseEvent.getPlayer().equals(player)) return;
active = false;
if(this.spinningTask != null) this.spinningTask.cancel();
player.getInventory().addItem(receivingItem);
if(hypebox.doesBroadcast())Bukkit.broadcastMessage(CC.translate("&d&lHype Boxes &8" + Arrow.arrow() + " &f" + LuckpermUtils.getDisplayName(player) + " &fhas just opened a " + hypebox.getDisplayName() + "&r &fand received:"));
if(hypebox.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 = hypebox.pickRandomReward();
this.itemStacks[i] = itemStack;
this.hypeBoxAnimationInventory.setItem(2, i+1, new GuiItem(itemStack));
}
runTask(hypebox);
}
public void shiftAllItems(HypeBox hypeBox) {
this.amountOfSpins++;
ItemStack[] itemStacks = this.itemStacks.clone();
System.arraycopy(itemStacks, 1, this.itemStacks, 0, 8);
this.itemStacks[8] = hypeBox.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);
}
public void check(HypeBox hypeBox) {
shiftAllItems(hypeBox);
if(this.amountOfSpins >= 50) {
this.spinningTask.cancel();
this.receivingItem = this.itemStacks[4];
this.player.playSound(player.getLocation(), Sound.NOTE_PLING, 2.0f, 2.0f);
} else if(this.amountOfSpins == 47) {
this.spinningTask.cancel();
this.spinningTask = Bukkit.getScheduler().runTaskTimer(HypeboxesPlugin.getInstance(), () -> check(hypeBox), 0L, 7L);
} else if(this.amountOfSpins == 43) {
this.spinningTask.cancel();
this.spinningTask = Bukkit.getScheduler().runTaskTimer(HypeboxesPlugin.getInstance(), () -> check(hypeBox), 0L, 5L);
} else if(this.amountOfSpins == 35) {
this.spinningTask.cancel();
this.spinningTask = Bukkit.getScheduler().runTaskTimer(HypeboxesPlugin.getInstance(), () -> check(hypeBox), 0L, 3L);
}
}
public void runTask(HypeBox hypeBox) {
this.spinningTask = Bukkit.getScheduler().runTaskTimer(HypeboxesPlugin.getInstance(), () -> check(hypeBox), 3L, 3L);
}
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();
}
@Override
public boolean isActive() {
return this.active;
}
}
@@ -0,0 +1,303 @@
package net.crankedpvp.hypeboxes.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.hypeboxes.HypeboxesPlugin;
import net.crankedpvp.hypeboxes.interfaces.ActiveInventory;
import net.crankedpvp.hypeboxes.struct.HypeBox;
import net.crankedpvp.hypeboxes.struct.HypeBoxReward;
import net.crankedpvp.hypeboxes.struct.battle.BattleInfo;
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;
import java.text.NumberFormat;
import java.util.*;
public class HypeboxBattleInventory implements ActiveInventory {
private final Gui hypeboxBattleInventory;
public Gui getUI() {
return this.hypeboxBattleInventory;
}
private int amountOfHypeboxes = 0;
private final Map<String, Integer> hypeboxAmounts;
private final Map<String, Integer> toGive;
private final Player player;
private final BattleInfo playerWins = new BattleInfo();
private final BattleInfo botWins = new BattleInfo();
private boolean complete = false;
private BukkitTask roller;
private int count = 0;
private boolean active = true;
private LinkedList<HypeBoxReward> rightQueue;
private LinkedList<HypeBoxReward> leftQueue;
public HypeboxBattleInventory(Player player, Map<String, Integer> hypeboxAmounts) {
this.player = player;
this.amountOfHypeboxes = getAmountFromMap(hypeboxAmounts);
this.hypeboxAmounts = hypeboxAmounts;
this.toGive = new HashMap<>(hypeboxAmounts);
this.hypeboxBattleInventory = new Gui(6, "Hypebox Battle Inventory");
this.hypeboxBattleInventory.setDefaultClickAction(event -> event.setCancelled(true));
this.hypeboxBattleInventory.setItem(Arrays.asList(0, 1, 3, 5, 7, 8, 9, 10, 16, 17, 27, 28, 29, 30, 31, 32, 33, 34, 35, 40, 49), new GuiItem(createFillerItem(DyeColor.GRAY)));
this.hypeboxBattleInventory.setItem(Arrays.asList(4, 12, 14, 22), new GuiItem(createFillerItem(DyeColor.LIME)));
this.hypeboxBattleInventory.setItem(Arrays.asList(11, 15), new GuiItem(new ItemBuilder(Material.HOPPER, "&dRolling...").colorize().build()));
this.updatePlayerHead();
this.updateBotHead();
this.rollNextBox();
}
private int getAmountFromMap(Map<String, Integer> hypeboxAmounts) {
int amt = 0;
for (String s : hypeboxAmounts.keySet()) {
if(hypeboxAmounts.get(s) > 0) amt += hypeboxAmounts.get(s);
}
return amt;
}
private void handleBattleCompletion(Player player) {
if (playerWins.getValue() == botWins.getValue()) {
handleDraw(player);
} else if (playerWins.getValue() > botWins.getValue()) {
handlePlayerWin(player);
} else {
handlePlayerLoss(player);
}
}
private void handleDraw(Player player) {
Bukkit.broadcastMessage(CC.translate("&4&lHype Box Battles &8" + Arrow.arrow() + "&f" + LuckpermUtils.getDisplayName(player) + " &8(&e$" + this.playerWins.getValue() + "&8) &fhas &b&lDREW &fwith the &cCranked Bot " + "&8(&e$" + this.botWins.getValue() + "&8) &fin a hype box battle &8(&4" + this.amountOfHypeboxes * 2 + " Hypeboxes&8)"));
Bukkit.broadcastMessage(CC.translate("&fVisit &cbuy.crankedpvp.net &fto purchase hype boxes! &7(/hypebox battle)"));
toGive.forEach((lb, amt) -> player.getInventory().addItem(HypeboxesPlugin.getInstance().getHypeBoxRegister().getHypebox(lb).createHypeBox(amt)));
}
private void handlePlayerWin(Player player) {
Bukkit.broadcastMessage(CC.translate("&4&lHype Box Battles &8" + Arrow.arrow() + "&f" + LuckpermUtils.getDisplayName(player) + " &8(&e$" + this.playerWins.getValue() + "&8) &fhas &a&lWON &fagainst the &cCranked Bot " + "&8(&e$" + this.botWins.getValue() + "&8) &fin a hype box battle &8(&4" + this.amountOfHypeboxes * 2 + " Hypeboxes&8)"));
playerWins.getHypeBoxRewardList().forEach(item -> {
Bukkit.broadcastMessage(CC.translate(" &f• &l" + item.getItemStack().getAmount() + "x " + item.getItemStack().getItemMeta().getDisplayName() + " &8(&e" + formatAsCurrency(item.getValue()) + "&8)"));
player.getInventory().addItem(item.getItemStack());
});
botWins.getHypeBoxRewardList().forEach(item -> {
Bukkit.broadcastMessage(CC.translate(" &f• &l" + item.getItemStack().getAmount() + "x " + item.getItemStack().getItemMeta().getDisplayName() + " &8(&e" + formatAsCurrency(item.getValue()) + "&8)"));
player.getInventory().addItem(item.getItemStack());
});
Bukkit.broadcastMessage(CC.translate("&fVisit &cbuy.crankedpvp.net &fto purchase hype boxes! &7(/hypebox battle)"));
}
private void handlePlayerLoss(Player player) {
Bukkit.broadcastMessage(CC.translate("&4&lHype Box Battles &8" + Arrow.arrow() + "&f" + LuckpermUtils.getDisplayName(player) + " &8(&e$" + this.playerWins.getValue() + "&8) &fhas &c&lLOST &fagainst the &cCranked Bot " + "&8(&e$" + this.botWins.getValue() + "&8) &fin a hype box battle &8(&4" + this.amountOfHypeboxes * 2 + " Hypeboxes&8)"));
playerWins.getHypeBoxRewardList().forEach(item -> Bukkit.broadcastMessage(CC.translate(" &f• &l" + item.getItemStack().getAmount() + "x " + item.getItemStack().getItemMeta().getDisplayName() + " &8(&e" + formatAsCurrency(item.getValue()) + "&8)")));
botWins.getHypeBoxRewardList().forEach(item -> Bukkit.broadcastMessage(CC.translate(" &f• &l" + item.getItemStack().getAmount() + "x " + item.getItemStack().getItemMeta().getDisplayName() + " &8(&e" + formatAsCurrency(item.getValue()) + "&8)")));
Bukkit.broadcastMessage(CC.translate("&fVisit &cbuy.crankedpvp.net &fto purchase hype boxes! &7(/hypebox battle)"));
}
private void addPlayerItem(HypeBoxReward reward) {
if(count <= 4) {
this.hypeboxBattleInventory.updateItem(count + 35, new GuiItem(reward.buildWithInfo()));
} else {
this.hypeboxBattleInventory.updateItem(count + 40, new GuiItem(reward.buildWithInfo()));
}
}
private void addBotItem(HypeBoxReward reward) {
if(count <= 4) {
this.hypeboxBattleInventory.updateItem(count + 40, new GuiItem(reward.buildWithInfo()));
} else {
this.hypeboxBattleInventory.updateItem(count + 45, new GuiItem(reward.buildWithInfo()));
}
}
private int spins = 0;
private void rollNextBox() {
HypeBox currentBox = getNextBox();
if (currentBox == null) {
this.complete = true;
Bukkit.getScheduler().runTaskLater(HypeboxesPlugin.getInstance(), () -> {
if(this.active) {
this.active = false;
handleBattleCompletion(player);
}
}, 20L);
return;
}
updateCurrentBox(currentBox);
rightQueue = new LinkedList<>();
for (int i = 0; i < 4; i++) {
rightQueue.addFirst(currentBox.pickRandomRewardAsReward());
}
leftQueue = new LinkedList<>();
for (int i = 0; i < 4; i++) {
leftQueue.addFirst(currentBox.pickRandomRewardAsReward());
}
roller = Bukkit.getScheduler().runTaskTimer(HypeboxesPlugin.getInstance(), () -> {
performRoll(currentBox);
}, 0L, 3L);
}
private void updateCurrentBox(HypeBox currentBox) {
this.hypeboxBattleInventory.updateItem(13, new GuiItem(currentBox.createHypeBox(amountOfHypeboxes)));
}
private void performRoll(HypeBox currentBox) {
rollRight(currentBox);
rollLeft(currentBox);
this.hypeboxBattleInventory.update();
spins++;
playRollSound();
if (spins >= 40) {
endRoll();
}
}
private void playRollSound() {
player.playSound(player.getLocation(), Sound.SUCCESSFUL_HIT, 1f, 1f);
}
private void endRoll() {
Bukkit.getScheduler().cancelTask(roller.getTaskId());
HypeBoxReward playerReward = leftQueue.get(1);
HypeBoxReward botReward = rightQueue.get(1);
if(count == 8) {
resetCount();
}
count++;
playerWins.addItem(playerReward);
addPlayerItem(playerReward);
updatePlayerHead();
botWins.addItem(botReward);
addBotItem(botReward);
updateBotHead();
spins = 0;
player.playSound(player.getLocation(), Sound.NOTE_PLING, 1.0f, 1.0f);
scheduleNextRoll();
}
private void resetCount() {
count = 0;
for (Integer i : Arrays.asList(36, 37, 38, 39, 41, 42, 43, 44, 45, 46, 47, 48, 50, 51, 52, 53)) {
this.hypeboxBattleInventory.updateItem(i, new GuiItem(new ItemStack(Material.AIR)));
}
}
private void scheduleNextRoll() {
Bukkit.getScheduler().runTaskLater(HypeboxesPlugin.getInstance(), this::rollNextBox, 35L);
}
private void rollRight(HypeBox currentBox) {
rightQueue.removeLast();
HypeBoxReward hypeBoxReward = currentBox.pickRandomRewardAsReward();
rightQueue.addFirst(hypeBoxReward);
this.hypeboxBattleInventory.updateItem(23, buildWithLore(rightQueue.get(0)));
this.hypeboxBattleInventory.updateItem(24, buildWithLore(rightQueue.get(1)));
this.hypeboxBattleInventory.updateItem(25, buildWithLore(rightQueue.get(2)));
this.hypeboxBattleInventory.updateItem(26, buildWithLore(rightQueue.get(3)));
}
private void rollLeft(HypeBox currentBox) {
HypeBoxReward hypeBoxReward = currentBox.pickRandomRewardAsReward();
leftQueue.removeFirst();
leftQueue.addLast(hypeBoxReward);
this.hypeboxBattleInventory.updateItem(18, buildWithLore(leftQueue.get(3)));
this.hypeboxBattleInventory.updateItem(19, buildWithLore(leftQueue.get(2)));
this.hypeboxBattleInventory.updateItem(20, buildWithLore(leftQueue.get(1)));
this.hypeboxBattleInventory.updateItem(21, buildWithLore(leftQueue.get(0)));
}
private ItemStack buildWithLore(HypeBoxReward reward) {
ItemBuilder itemBuilder = new ItemBuilder(reward.getItemStack().clone());
itemBuilder.addLore("", "&fChance: &e" + reward.getChance() + "%", "&fValue: &e" + formatAsCurrency(reward.getValue()));
return itemBuilder.colorize().build();
}
private HypeBox getNextBox() {
HashMap<String, Integer> test = new HashMap<>(this.hypeboxAmounts);
for (Map.Entry<String, Integer> stringIntegerEntry : test.entrySet()) {
if(stringIntegerEntry.getValue() > 0) {
HypeBox hypeBox = HypeboxesPlugin.getInstance().getHypeBoxRegister().getHypebox(stringIntegerEntry.getKey());
this.hypeboxAmounts.put(stringIntegerEntry.getKey(), stringIntegerEntry.getValue() - 1);
return hypeBox;
} else {
this.hypeboxAmounts.remove(stringIntegerEntry.getKey());
}
}
return null;
}
private void updateBotHead() {
ItemBuilder itemBuilder = new ItemBuilder(SkullUtils.getSkull("eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvNjJjYWYxMGQzZjc4ZDQ0MTBlYmJhZjYyZDNhZjU3NGI2ZTdlYTNkZDg0MjQ4NzEyYzJhNzdmMTM1MWYyYmFjNCJ9fX0="));
itemBuilder.setName("&f&l<&4&lBot&f&l> &4Cranked's Statistics");
itemBuilder.setLore("", "&eRewards Value", " &f* &e" + formatAsCurrency(botWins.getValue()), "", "&eRewards Won");
for (HypeBoxReward hypeBoxReward : botWins.getHypeBoxRewardList()) {
itemBuilder.addLore(" &f* " + hypeBoxReward.getItemStack().getItemMeta().getDisplayName());
}
this.hypeboxBattleInventory.updateItem(1, 7, new GuiItem(itemBuilder.colorize().build()));
}
private void updatePlayerHead() {
ItemBuilder itemBuilder = new ItemBuilder(SkullUtils.getSkullFromPlayer(player));
itemBuilder.setName("&7" + LuckpermUtils.getDisplayName(player) + "'s Statistics");
itemBuilder.setLore("", "&eRewards Value", " &f* &e$" + playerWins.getValue(), "", "&eRewards Won");
for (HypeBoxReward hypeBoxReward : playerWins.getHypeBoxRewardList()) {
itemBuilder.addLore(" &f* " + hypeBoxReward.getItemStack().getItemMeta().getDisplayName());
}
this.hypeboxBattleInventory.updateItem(1, 3, new GuiItem(itemBuilder.colorize().build()));
}
private ItemStack createFillerItem(DyeColor dyeColor) {
ItemStack item = new ItemStack(Material.STAINED_GLASS_PANE, 1, dyeColor.getData());
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(CC.translate(" "));
item.setItemMeta(meta);
return item;
}
public static String formatAsCurrency(double amount) {
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(Locale.US);
return currencyFormatter.format(amount);
}
@Override
public boolean isActive() {
return active;
}
}
@@ -0,0 +1,165 @@
package net.crankedpvp.hypeboxes.inventory;
import dev.triumphteam.gui.guis.Gui;
import dev.triumphteam.gui.guis.GuiItem;
import net.crankedpvp.crankedutils.utils.CC;
import net.crankedpvp.crankedutils.utils.Clickable;
import net.crankedpvp.crankedutils.utils.LuckpermUtils;
import net.crankedpvp.crankedutils.utils.items.ItemBuilder;
import net.crankedpvp.hypeboxes.HypeboxesPlugin;
import net.crankedpvp.hypeboxes.struct.HypeBox;
import net.crankedpvp.hypeboxes.struct.HypeBoxManager;
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;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class HypeboxPreBattleInventory {
private final Gui preBattleInv;
public Gui getUI() {
return this.preBattleInv;
}
private final Map<String, Integer> hypeboxAmounts = new HashMap<>();
private boolean isSpinning = false;
private final Player player;
public HypeboxPreBattleInventory(Player player) {
this.player = player;
this.preBattleInv = new Gui(3, "Hypebox Battles");
this.preBattleInv.setDefaultTopClickAction(event -> event.setCancelled(true));
this.preBattleInv.setDefaultClickAction(event -> {
ItemStack clickedItem = event.getCurrentItem();
if (clickedItem != null) {
if(HypeBoxManager.isHypebox(clickedItem)) {
HypeBox hypeBox = HypeBoxManager.getHypeBoxFromItemStack(clickedItem);
int amount = clickedItem.getAmount();
event.setCurrentItem(null);
addToAmount(hypeBox, amount);
updateSlot();
} else {
event.setCancelled(true);
}
}
});
this.preBattleInv.setItem(Arrays.asList(1, 11, 19, 7, 15, 25), new GuiItem(createFillerItem(DyeColor.RED)));
this.preBattleInv.setItem(2, 5, new GuiItem(createEmptySlot(), event -> {
tryOpenHypeboxBattle(player);
}));
this.preBattleInv.setCloseGuiAction(event -> {
if(!isSpinning) {
hypeboxAmounts.forEach((hb, amt) -> {
ItemStack itemStack = HypeboxesPlugin.getInstance().getHypeBoxRegister().getHypebox(hb).createHypeBox(amt);
player.getInventory().addItem(itemStack);
player.sendMessage(CC.translate("&aYour hype boxes have been refunded"));
});
}
});
}
private void tryOpenHypeboxBattle(Player player) {
int amt = 0;
for (String s : this.hypeboxAmounts.keySet()) {
if(this.hypeboxAmounts.get(s) > 0) amt += this.hypeboxAmounts.get(s);
}
if(amt < 3) {
player.sendMessage(CC.translate("&cYou must open a minimum of 3 hypeboxes at once"));
player.closeInventory();
return;
}
/*
if(amt > 8) {
player.sendMessage(CC.translate("&cYou can only open 8 hypeboxes at a time"));
player.closeInventory();
return;
}
*/
for (String s : this.hypeboxAmounts.keySet()) {
if(this.hypeboxAmounts.get(s) > 0) {
this.isSpinning = true;
player.closeInventory();
HypeboxBattleInventory hypeboxBattleInventory = new HypeboxBattleInventory(player, hypeboxAmounts);
hypeboxBattleInventory.getUI().open(player);
UUID uuid = UUID.randomUUID();
HypeboxesPlugin.getInstance().getHypeboxBattles().put(uuid, hypeboxBattleInventory);
Clickable clickable = new Clickable("&f✧ &eClick to watch &c" + LuckpermUtils.getDisplayName(player) + " &eplay against the &cCranked Bot &ein a &4&lHype Box Battle", "&eClick to view &6" + player.getName() + "'s &ehypebox battle &f✧", "/viewbattleopen " + uuid);
clickable.add(CC.translate("\n&eYou can purchase hypeboxes at &cbuy.crankedpvp.net&e!"));
for(Player onlinePlayer : Bukkit.getOnlinePlayers()) {
onlinePlayer.sendMessage("");
clickable.sendToPlayer(onlinePlayer);
onlinePlayer.sendMessage("");
}
return;
}
}
player.sendMessage(CC.translate("&cYou can't do this battle, contact an admin to find out why"));
}
private void updateSlot() {
if(hypeboxAmounts.isEmpty()) {
this.preBattleInv.setItem(2, 5, new GuiItem(createEmptySlot(), event -> {
tryOpenHypeboxBattle(player);
}));
} else {
this.preBattleInv.setItem(2, 5, new GuiItem(createReadySlot(), event -> {
tryOpenHypeboxBattle(player);
}));
}
this.preBattleInv.update();
}
private ItemStack createEmptySlot() {
return new ItemBuilder(Material.BARRIER, "&c&lInsert Hypebox", "&7Click a hypebox in your inventory",
"&7to insert it into the battle.").colorize().build();
}
private ItemStack createReadySlot() {
ItemBuilder itemBuilder = new ItemBuilder(Material.SLIME_BALL, "&a&lInsert Hypebox", "&7Click a hypebox in your inventory",
"&7to insert it into the battle.", "", "&fCurrent Hypeboxes:");
for (String s : hypeboxAmounts.keySet()) {
itemBuilder.addLore(" &f* " + hypeboxAmounts.get(s) + "x " + s + " Hypeboxes");
}
itemBuilder.addLore("", "&a&lClick &fto enter the hypebox battle");
return itemBuilder.colorize().build();
}
private ItemStack createFillerItem(DyeColor dyeColor) {
ItemStack item = new ItemStack(Material.STAINED_GLASS_PANE, 1, dyeColor.getData());
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(CC.translate(" "));
item.setItemMeta(meta);
return item;
}
private void addToAmount(HypeBox hypeBox, int amount) {
String id = hypeBox.getId();
if(this.hypeboxAmounts.containsKey(id)) {
this.hypeboxAmounts.put(id, this.hypeboxAmounts.get(id) + amount);
} else {
this.hypeboxAmounts.put(id, amount);
}
}
}
@@ -0,0 +1,50 @@
package net.crankedpvp.hypeboxes.inventory;
import dev.triumphteam.gui.guis.Gui;
import dev.triumphteam.gui.guis.GuiItem;
import net.crankedpvp.crankedutils.utils.CC;
import net.crankedpvp.crankedutils.utils.GuiUtil;
import net.crankedpvp.hypeboxes.HypeboxesPlugin;
import net.crankedpvp.hypeboxes.language.Language;
import net.crankedpvp.hypeboxes.struct.HypeBox;
import net.crankedpvp.hypeboxes.struct.HypeBoxReward;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
public class PreviewLootboxInventory {
private final Gui previewInventory;
public Gui getUI() {
return this.previewInventory;
}
public PreviewLootboxInventory(Player player, String name, HypeboxesPlugin plugin) {
this.previewInventory = new Gui(3, "Preview Hypebox");
this.previewInventory.setDefaultClickAction(event -> event.setCancelled(true));
this.previewInventory.getFiller().fillBorder(new GuiItem(GuiUtil.createFillerItem()));
HypeBox hypeBox = plugin.getHypeBoxRegister().getHypebox(name);
for (HypeBoxReward reward : hypeBox.getRewards()) {
ItemStack item = reward.getItemStack();
this.previewInventory.addItem(new GuiItem(reward.buildWithInfo(), event -> {
if (player.hasPermission("cranked.admin")) {
if (event.isShiftClick() && event.isRightClick()) {
plugin.getHypeBoxManager().removeItemFromHypebox(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,97 @@
package net.crankedpvp.hypeboxes.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 {
HYPEBOX_LORE(),
HYPEBOX_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.hypeboxes.language;
import net.crankedpvp.hypeboxes.HypeboxesPlugin;
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 HypeboxesPlugin plugin;
public FileHandler(HypeboxesPlugin 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.hypeboxes.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_HYPEBOX,
CREATED_HYPEBOX,
DELETED_HYPEBOX,
HYPEBOX_DISABLED,
HYPEBOX_ENABLED,
HYPEBOX_INVALID,
RELOADED_ALL,
RELOADED_SINGULAR,
HYPEBOX_RECEIVED,
HYPEBOX_GIVEN,
HYPEBOX_ADDITEM,
HYPEBOX_ITEM_ALREADY_IN,
HYPEBOX_ITEM_NOT_FOUND,
HYPEBOX_REMOVE_ITEM,
PREVIEW_LOOT,
HYPEBOX_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,73 @@
package net.crankedpvp.hypeboxes.listeners;
import de.tr7zw.nbtapi.NBTItem;
import net.crankedpvp.crankedutils.utils.CC;
import net.crankedpvp.crankedutils.utils.Clickable;
import net.crankedpvp.crankedutils.utils.InventoryUtils;
import net.crankedpvp.crankedutils.utils.LuckpermUtils;
import net.crankedpvp.hypeboxes.HypeboxesPlugin;
import net.crankedpvp.hypeboxes.inventory.HypeBoxAnimationInventory;
import net.crankedpvp.hypeboxes.language.Language;
import net.crankedpvp.hypeboxes.struct.HypeBox;
import net.crankedpvp.hypeboxes.struct.HypeBoxManager;
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 HypeboxOpenListener implements Listener {
private final HypeboxesPlugin plugin;
public HypeboxOpenListener(HypeboxesPlugin 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(!HypeBoxManager.isHypebox(itemStack)) return;
event.setCancelled(true);
final NBTItem nbtItem = new NBTItem(itemStack);
final HypeBox hypeBox = plugin.getHypeBoxRegister().getHypebox(nbtItem.getOrCreateCompound("cosmicData").getString("hypeboxID"));
if(hypeBox == null) return;
if(hypeBox.getRewards().isEmpty()) return;
if(!InventoryUtils.hasAvaliableSlot(event.getPlayer(), 1)) {
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);
HypeBoxAnimationInventory hypeboxAnimationInventory = new HypeBoxAnimationInventory(event.getPlayer(), hypeBox);
hypeboxAnimationInventory.getUI().open(event.getPlayer());
UUID uuid = UUID.randomUUID();
this.plugin.getHypeboxesBeingOpened().put(uuid, hypeboxAnimationInventory);
Clickable clickable = new Clickable("&f✧ &eClick to watch &6" + LuckpermUtils.getDisplayName(event.getPlayer()) + " &eopen a " + hypeBox.getDisplayName(), "&eClick to view &6" + event.getPlayer().getName() + "'s &ehypebox &f✧", "/viewhypeboxopen " + uuid);
clickable.add(CC.translate("\n&eYou can purchase this hypebox at &fbuy.crankedpvp.org&e!"));
for(Player onlinePlayer : Bukkit.getOnlinePlayers()) {
onlinePlayer.sendMessage("");
clickable.sendToPlayer(onlinePlayer);
onlinePlayer.sendMessage("");
}
}
}
}
@@ -0,0 +1,39 @@
package net.crankedpvp.hypeboxes.listeners;
import de.tr7zw.nbtapi.NBTItem;
import net.crankedpvp.crankedutils.utils.StringUtils;
import net.crankedpvp.hypeboxes.HypeboxesPlugin;
import net.crankedpvp.hypeboxes.inventory.PreviewLootboxInventory;
import net.crankedpvp.hypeboxes.language.Language;
import net.crankedpvp.hypeboxes.struct.HypeBoxManager;
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 HypeboxPreviewListener implements Listener {
private final HypeboxesPlugin plugin;
public HypeboxPreviewListener(HypeboxesPlugin 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 (HypeBoxManager.isHypebox(event.getItem())) {
event.setCancelled(true);
final NBTItem nbtItem = new NBTItem(event.getItem());
final String name = nbtItem.getCompound("cosmicData").getString("hypeboxID");
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,106 @@
package net.crankedpvp.hypeboxes.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.SortUtils;
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.*;
import java.util.concurrent.ThreadLocalRandom;
@Getter @AllArgsConstructor
public class HypeBox {
private final String id;
private final String displayName;
private final List<String> lore;
private final Material material;
private final boolean glowing;
private final boolean broadcastRewards;
private final String skullValue;
private final List<HypeBoxReward> rewards;
public ItemStack createHypeBox(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", "hypebox");
nbtItem.getCompound("cosmicData").setString("hypeboxID", 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 HypeBoxReward pickRandomRewardAsReward() {
double totalChance = 0.0;
for (HypeBoxReward reward : rewards) {
totalChance += reward.getChance();
}
Random random = new Random();
double randomValue = random.nextDouble() * totalChance;
double cumulativeChance = 0.0;
for (HypeBoxReward reward : rewards) {
cumulativeChance += reward.getChance();
if (randomValue <= cumulativeChance) {
return reward;
}
}
throw new IllegalStateException("No reward was selected. Check if the list of rewards and their chances are set up correctly.");
}
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,263 @@
package net.crankedpvp.hypeboxes.struct;
import de.tr7zw.nbtapi.NBTItem;
import net.crankedpvp.crankedutils.utils.InventoryUtils;
import net.crankedpvp.crankedutils.utils.StringUtils;
import net.crankedpvp.hypeboxes.HypeboxesPlugin;
import net.crankedpvp.hypeboxes.language.Config;
import net.crankedpvp.hypeboxes.language.Language;
import net.crankedpvp.hypeboxes.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 HypeBoxManager {
private final HypeboxesPlugin plugin;
public HypeBoxManager(HypeboxesPlugin plugin) {
this.plugin = plugin;
}
public static boolean isHypebox(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("hypeboxID");
}
public static HypeBox getHypeBoxFromItemStack(ItemStack itemStack) {
if (itemStack == null || itemStack.getType() == Material.AIR) return null;
NBTItem nbtItem = new NBTItem(itemStack);
if(!nbtItem.getOrCreateCompound("cosmicData").hasKey("customItem")) return null;
return HypeboxesPlugin.getInstance().getHypeBoxRegister().getHypebox(Objects.requireNonNull(nbtItem.getCompound("cosmicData")).getString("hypeboxID"));
}
public static boolean isHypeboxHalfGenerator(ItemStack itemStack) {
if (itemStack == null || itemStack.getType() == Material.AIR) return false;
NBTItem nbtItem = new NBTItem(itemStack);
return (nbtItem.hasKey("leftOrRightHalf"));
}
public static boolean isHypeboxHalf(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 doesHypeboxBroadcast(ItemStack itemStack) {
if(!isHypebox(itemStack)) return false;
if (itemStack.getType() == Material.AIR) return false;
NBTItem nbtItem = new NBTItem(itemStack);
return nbtItem.getBoolean("broadcast");
}
public void createDefaultConfig(String name) {
File file = new File(this.plugin.getDataFolder(), "Hypeboxes/" + 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&lHypebox");
yamlConfiguration.set("Item.Display.Lore", Config.HYPEBOX_LORE.getValuesAsList());
yamlConfiguration.set("Item.Material", "" + Config.HYPEBOX_MATERIAL.getValue());
yamlConfiguration.set("Item.Glowing", true);
yamlConfiguration.set("Misc.Broadcast.Rewards", true);
try {
yamlConfiguration.save(file);
} catch (IOException e) {
e.printStackTrace();
}
}
public void createDefaultConfigFromItemStack(ItemStack itemStack, String name) {
File file = new File(this.plugin.getDataFolder(), "Hypeboxes/" + 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.Lore", itemMeta.getLore());
yamlConfiguration.set("Item.Material", "" + itemStack.getType().name());
yamlConfiguration.set("Item.Glowing", itemMeta.hasEnchants());
yamlConfiguration.set("Misc.Broadcast.Rewards", true);
try {
yamlConfiguration.save(file);
} catch (IOException e) {
e.printStackTrace();
}
}
public void createHypebox(String lootbox) {
createDefaultConfig(lootbox);
this.initialize(lootbox);
}
public void createHypeboxFromItemStack(ItemStack itemStack, String lootbox) {
createDefaultConfigFromItemStack(itemStack, lootbox);
this.initialize(lootbox);
}
public void deleteConfig(String name) {
File file = new File(this.plugin.getDataFolder(), "Hypeboxes/" + name + ".yml");
if (file.exists())
file.delete();
}
public void reloadHypebox(String hypebox) {
if (this.plugin.getHypeBoxRegister().isHypebox(hypebox)) {
this.plugin.getHypeBoxRegister().removeHypebox(hypebox);
initialize(hypebox);
} else {
System.out.println("[-] Failure reloading hypebox, " + hypebox);
}
}
public void initializeAll() {
File dataFolder = new File(this.plugin.getDataFolder(), "Hypeboxes");
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 hypebox: " + file.getName().replace(".yml", ""));
}
});
}
public void initialize(String fileName) {
File file = new File(this.plugin.getDataFolder(), "Hypeboxes/" + 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> lore = yamlConfiguration.getStringList("Item.Display.Lore");
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");
boolean broadcastRewards = yamlConfiguration.contains("Misc.Broadcast.Rewards") && yamlConfiguration.getBoolean("Misc.Broadcast.Rewards");
List<HypeBoxReward> rewards = getRewards(yamlConfiguration);
HypeBox lootbox = new HypeBox(file.getName().replace(".yml", ""), name, lore, material, glowing, broadcastRewards, skullValue, rewards);
this.plugin.getHypeBoxRegister().register(lootbox);
}
private List<HypeBoxReward> getRewards(FileConfiguration fileConfiguration) {
List<HypeBoxReward> 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();
}
HypeBoxReward reward = new HypeBoxReward(itemStack, fileConfiguration.getConfigurationSection("rewards." + key).getDouble("chance"), fileConfiguration.getConfigurationSection("rewards." + key).getDouble("value"));
rewards.add(reward);
});
return rewards;
}
public void addItem(Player player, String lootbox, double chance, double value) {
try {
File file = new File(this.plugin.getDataFolder() + "/Hypeboxes/" + 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 + ".value", value);
yamlConfiguration.save(file);
player.sendMessage(Language.PREFIX.getMessage() + Language.HYPEBOX_ADDITEM.getMessage().replace("%lootbox%", lootbox).replace("%item%", player.getItemInHand().getItemMeta().getDisplayName()));
player.playSound(player.getLocation(), Sound.LEVEL_UP, 2.0F, 2.0F);
reloadHypebox(lootbox);
} else {
player.sendMessage(Language.PREFIX.getMessage() + Language.HYPEBOX_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 removeItemFromHypebox(Player player, ItemStack itemStack, String lootbox) {
try {
File file = new File(this.plugin.getDataFolder() + "/Hypeboxes/" + 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.HYPEBOX_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.HYPEBOX_REMOVE_ITEM.getMessage().replace("%lootbox%", lootbox).replace("%item%", itemStack.getItemMeta().getDisplayName()));
player.playSound(player.getLocation(), Sound.CHICKEN_EGG_POP, 2.0F, 2.0F);
reloadHypebox(lootbox);
} else {
player.sendMessage(Language.PREFIX.getMessage() + Language.HYPEBOX_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 giveHypebox(Player player, HypeBox lootbox, int amount) {
InventoryUtils.giveItemToPlayer(player, lootbox.createHypeBox(amount));
}
public void giveHypebox(Player player, HypeBox lootbox) {
giveHypebox(player, lootbox, 1);
}
public ItemStack getHypebox(HypeBox lootbox) {
return getHypebox(lootbox, 1);
}
public ItemStack getHypebox(HypeBox lootbox, int amount) {
return lootbox.createHypeBox(amount);
}
}
@@ -0,0 +1,88 @@
package net.crankedpvp.hypeboxes.struct;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;
import lombok.Getter;
import net.crankedpvp.hypeboxes.HypeboxesPlugin;
import org.apache.commons.lang.Validate;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
public class HypeBoxRegister {
private final HypeboxesPlugin plugin;
@Getter
private final Map<String, HypeBox> lootboxes;
public HypeBoxRegister(HypeboxesPlugin plugin) {
this.lootboxes = Maps.newConcurrentMap();
this.plugin = plugin;
}
public Map<String, HypeBox> getSortedHypeboxes() {
return sort(this.lootboxes);
}
public Map<String, HypeBox> sort(Map<String, HypeBox> map) {
List<Map.Entry<String, HypeBox>> list = new ArrayList<>(map.entrySet());
list.sort(Map.Entry.comparingByKey());
Collections.reverse(list);
Map<String, HypeBox> result = new LinkedHashMap<>();
for (int i = list.size() - 1; i >= 0; i--) {
Map.Entry<String, HypeBox> entry = list.get(i);
result.put(entry.getKey(), entry.getValue());
}
return result;
}
public void register(HypeBox hypebox) {
try {
this.lootboxes.put(hypebox.getId(), hypebox);
this.plugin.getLogger().info("Successfully registered hypebox: " + hypebox.getId());
} catch (Exception var3) {
System.err.println("&8(&c!&8) Failed to register hypebox, " + hypebox.getId());
}
}
public boolean isHypebox(String name) {
return (getHypebox(name) != null);
}
public HypeBox getHypebox(String name) {
Validate.notNull(name);
Validate.notEmpty(name);
return this.lootboxes.get(name);
}
public void clearHypeboxMap() {
this.lootboxes.clear();
}
public void removeHypebox(String lootbox) {
this.lootboxes.remove(lootbox);
}
public Collection<HypeBox> getHypeboxObjects() {
return Collections.unmodifiableCollection(this.lootboxes.values());
}
public List<HypeBox> getHypeboxesAsList() {
return new ArrayList<>(this.lootboxes.values());
}
public List<String> getHypeboxesAsIDs() {
return getHypeboxesAsList().stream()
.map(HypeBox::getId)
.collect(Collectors.toList());
}
public HypeBox select(Collection<HypeBox> lootboxCollection) {
return (lootboxCollection.size() > 0) ? ImmutableList.copyOf(lootboxCollection).get(ThreadLocalRandom.current().nextInt(lootboxCollection.size())) : null;
}
}
@@ -0,0 +1,22 @@
package net.crankedpvp.hypeboxes.struct;
import lombok.AllArgsConstructor;
import lombok.Getter;
import net.crankedpvp.crankedutils.utils.items.ItemBuilder;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
@AllArgsConstructor
@Getter
public class HypeBoxReward {
private final ItemStack itemStack;
private final double chance;
private final double value;
public ItemStack buildWithInfo() {
ItemBuilder itemBuilder = new ItemBuilder(itemStack.clone());
itemBuilder.addLore("", "&fChance: &e" + chance + "%", "&fValue: &e$" + value);
return itemBuilder.colorize().build();
}
}
@@ -0,0 +1,29 @@
package net.crankedpvp.hypeboxes.struct.battle;
import lombok.AllArgsConstructor;
import lombok.Getter;
import net.crankedpvp.hypeboxes.struct.HypeBoxReward;
import java.util.ArrayList;
import java.util.List;
@Getter @AllArgsConstructor
public class BattleInfo {
private List<HypeBoxReward> hypeBoxRewardList;
public BattleInfo() {
this.hypeBoxRewardList = new ArrayList<>();
}
public void addItem(HypeBoxReward hypeBoxReward) {
this.hypeBoxRewardList.add(hypeBoxReward);
}
public double getValue() {
double value = 0;
for (HypeBoxReward hypeBoxReward : hypeBoxRewardList) {
value += hypeBoxReward.getValue();
}
return value;
}
}
@@ -0,0 +1,30 @@
package net.crankedpvp.hypeboxes.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());
}
}
+6
View File
@@ -0,0 +1,6 @@
HYPEBOX_MATERIAL: CHEST
HYPEBOX_LORE:
- "&7Right-Click to receive a"
- "&7random reward"
- ""
- "&4&l(!) &7Obtained from &cbuy.crankedpvp.org &7(/buy)"
+18
View File
@@ -0,0 +1,18 @@
PREFIX: "&cCranked&fPvP &8| "
FULL_INVENTORY: "&cYour inventory does not have enough slots to complete this action"
ALREADY_HYPEBOX: "&cA lootbox named %lootbox% is already registered"
CREATED_HYPEBOX: "&aYou have created the lootbox %lootbox%"
DELETED_HYPEBOX: "&aYou have deleted the lootbox %lootbox%"
HYPEBOX_INVALID: "&cThe lootbox with named %lootbox% could not be found"
HYPEBOX_DISABLED: "&aThe lootbox named %lootbox% has been disabled"
HYPEBOX_ENABLED: "&aThe lootbox named %lootbox% has been enabled"
RELOADED_ALL: "&aYou have reloaded ALL lootboxes"
RELOADED_SINGULAR: "&aYou have reloaded the %lootbox% lootbox"
HYPEBOX_RECEIVED: "&aYou have received the %lootbox% lootbox"
HYPEBOX_GIVEN: "&aYou have given the %lootbox% lootbox to %player%"
HYPEBOX_ADDITEM: "&aAdded %item%&r&a to %lootbox% lootbox"
HYPEBOX_ITEM_ALREADY_IN: "&cThis item is already in the %lootbox% lootbox"
HYPEBOX_ITEM_NOT_FOUND: "&cThis item is not in the %lootbox% lootbox"
HYPEBOX_REMOVE_ITEM: "&cDeleted %item%&r&c from %lootbox% lootbox"
HYPEBOX_OPENED: "&aYou have opened the %lootbox% lootbox"
PREVIEW_LOOT: "&aYou are now previewing the loot of the %lootbox% lootbox"
+7
View File
@@ -0,0 +1,7 @@
name: CrankedHypeBoxes
main: net.crankedpvp.hypeboxes.HypeboxesPlugin
version: ${git.total.commit.count}.0
description: "§fCommit ID: §a${git.commit.id.abbrev}"
website: https://crankedpvp.net/
depend:
- CrankedUtils