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
+43
View File
@@ -0,0 +1,43 @@
<?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>Keys</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>
<dependency>
<artifactId>LootFilter</artifactId>
<groupId>net.crankedpvp</groupId>
</dependency>
<dependency>
<groupId>net.crankedpvp</groupId>
<artifactId>Leaderboards</artifactId>
</dependency>
<dependency>
<artifactId>LootTable</artifactId>
<groupId>net.crankedpvp</groupId>
</dependency>
<dependency>
<groupId>net.crankedpvp</groupId>
<artifactId>StorageUtilsShaded</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/../StorageUtils/target/StorageUtils-1.0-shaded.jar</systemPath>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,193 @@
package net.crankedpvp.cratekeys;
import com.google.common.collect.Lists;
import de.tr7zw.nbtapi.NBTCompound;
import de.tr7zw.nbtapi.NBTItem;
import net.crankedpvp.crankedutils.utils.PlayerUtils;
import net.crankedpvp.crankedutils.utils.items.ItemUtils;
import net.crankedpvp.cratekeys.data.CrateData;
import net.crankedpvp.cratekeys.enums.CrateKeyType;
import net.crankedpvp.cratekeys.items.CrateKeyItem;
import net.crankedpvp.cratekeys.managers.CrateDataManager;
import net.crankedpvp.lootfilter.LootFilterAPI;
import net.crankedpvp.loottable.LootTableAPI;
import net.crankedpvp.loottable.data.LootTable;
import net.crankedpvp.loottable.struct.LootTableType;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.util.List;
import java.util.Map;
public class CrankedCrateKeysAPI {
public static void addCrate(CrateKeyType type, Location location) {
FileConfiguration file = CrankedCrateKeysPlugin.getInstance().getConfig();
List<String> crates = file.contains("crates") ? file.getStringList("crates") : Lists.newArrayList();
crates.add(type.name() + ":" + toStringLocation(location));
file.set("crates", crates);
CrankedCrateKeysPlugin.getInstance().saveConfig();
CrankedCrateKeysPlugin.getInstance().reloadConfig();
}
public static void givePlayerKeys(Player player, CrateKeyType type, int amount) {
while (amount != 0) {
PlayerUtils.giveOrDropProtectedItem(player, CrateKeyItem.getItem(type), 60);
amount--;
}
}
public static void giveCrateKeyLoot(Player player, LootTableType type) {
LootFilterAPI.giveLootTableItem(player, type, LootTableAPI.getRandomLootItem(type));
/*
ItemStack item = LootTableAPI.getRandomLootItem(type);
//getRandomItem(CrankedCrateKeysPlugin.getInstance().getCrateLootManager().getCachedCrateLoot().get(type));
PlayerUtils.giveOrDropProtectedItem(player, item, 60);
*/
}
public static Double getTotalPercentageOfItems(Map<ItemStack, Double> items) {
double totalPercentage = 0.0D;
for (Map.Entry<ItemStack, Double> entry : items.entrySet())
totalPercentage += entry.getValue();
return totalPercentage;
}
/*
public static ItemStack getRandomItem(Map<ItemStack, Double> items) {
double totalPercentage = getTotalPercentageOfItems(items);
double find = ThreadLocalRandom.current().nextDouble(0.0D, totalPercentage);
double total = 0.0D;
for (Map.Entry<ItemStack, Double> entry : items.entrySet()) {
double value = entry.getValue();
if (value + total >= find)
return entry.getKey();
total += value;
}
return null;
}
*/
public static boolean isCrateKey(ItemStack item) {
if (ItemUtils.isAirOrNull(item))
return false;
NBTItem wrapper = new NBTItem(item);
if (!wrapper.hasKey("cosmicData"))
return false;
NBTCompound tag = wrapper.getCompound("cosmicData");
if (tag == null) return false;
return tag.hasKey("crate_type");
}
public static void removeCrate(Location location) {
FileConfiguration file = CrankedCrateKeysPlugin.getInstance().getConfig();
String loc = toStringLocation(location);
List<String> crates = file.contains("crates") ? file.getStringList("crates") : Lists.newArrayList();
for (int i = 0; i < crates.size(); i++) {
if (crates.get(i).split(":")[1].equals(loc)) {
crates.remove(i);
file.set("crates", crates);
CrankedCrateKeysPlugin.getInstance().saveConfig();
CrankedCrateKeysPlugin.getInstance().reloadConfig();
}
}
}
public static void clearCrates() {
CrateDataManager.clearCrates();
}
public static void deleteCrates() {
FileConfiguration file = CrankedCrateKeysPlugin.getInstance().getConfig();
file.set("crates", null);
CrankedCrateKeysPlugin.getInstance().saveConfig();
CrankedCrateKeysPlugin.getInstance().reloadConfig();
}
public static List<CrateData> getCrates() {
List<CrateData> crates = Lists.newArrayList();
FileConfiguration file = CrankedCrateKeysPlugin.getInstance().getConfig();
List<String> list = file.contains("crates") ? file.getStringList("crates") : Lists.newArrayList();
for (String data : list) {
String[] split = data.split(":");
crates.add(new CrateData(CrateKeyType.valueOf(split[0]), fromStringLocation(split[1])));
}
return crates;
}
public static CrateData findCrate(Location location) {
List<CrateData> crates = CrateDataManager.getCrates();
for (CrateData data : crates) {
if (data.getLocation().getX() == location.getX() &&
data.getLocation().getY() == location.getY() && data.getLocation().getZ() == location.getZ() &&
data.getLocation().getWorld().getName().equals(location.getWorld().getName()))
return data;
}
return null;
}
public static String toStringLocation(Location location) {
double x = location.getX();
double y = location.getY();
double z = location.getZ();
float yaw = location.getYaw();
float pitch = location.getPitch();
String world = location.getWorld().getName();
String result = world + " " + x + " " + y + " " + z + " " + yaw + " " + pitch;
return result;
}
public static Location fromStringLocation(String location) {
String[] list = location.split(" ");
World world = Bukkit.getWorld(list[0]);
double x = Double.parseDouble(list[1]);
double y = Double.parseDouble(list[2]);
double z = Double.parseDouble(list[3]);
float yaw = Float.parseFloat(list[4]);
float pitch = Float.parseFloat(list[5]);
Location l = new Location(world, x, y, z);
l.setPitch(pitch);
l.setYaw(yaw);
return l;
}
/*
public static void setItems(CrateType type, Map<ItemStack, Double> items) {
String key = type.name().toLowerCase() + ".items";
List<String> list = Lists.newArrayList();
for (Map.Entry<ItemStack, Double> entry : items.entrySet()) {
list.add(ItemStackSerializer.serialize(entry.getKey()) + ":" + entry.getValue());
}
CrankedCrateKeysPlugin.getInstance().getConfig().set(key, list);
CrankedCrateKeysPlugin.getInstance().saveConfig();
CrankedCrateKeysPlugin.getInstance().reloadConfig();
}
*/
/*
public static Map<ItemStack, Double> getItems(CrateType type) {
String key = type.name().toLowerCase() + ".items";
if (!CrankedCrateKeysPlugin.getInstance().getConfig().contains(key))
return Maps.newLinkedHashMap();
Map<ItemStack, Double> map = Maps.newLinkedHashMap();
List<String> items = CrankedCrateKeysPlugin.getInstance().getConfig().getStringList(key);
for (String item : items) {
String[] split = item.split(":");
map.put(ItemStackSerializer.deserialize(split[0]), Double.valueOf(split[1]));
}
return map;
}
*/
}
@@ -0,0 +1,66 @@
package net.crankedpvp.cratekeys;
import lombok.Getter;
import me.mattstudios.mf.base.CommandManager;
import me.mattstudios.mf.base.components.TypeResult;
import net.crankedpvp.cratekeys.commands.CommandAdminCrate;
import net.crankedpvp.cratekeys.enums.CrateKeyType;
import net.crankedpvp.cratekeys.listeners.CrateKeyInventoryListener;
import net.crankedpvp.cratekeys.listeners.CrateLogicListener;
import net.crankedpvp.cratekeys.managers.CrateDataManager;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
@Getter
public class CrankedCrateKeysPlugin extends JavaPlugin {
public static CrankedCrateKeysPlugin AstroCrateKeysPlugin;
public static FileConfiguration config;
private CommandManager commandManager;
public void onEnable() {
AstroCrateKeysPlugin = this;
registerListeners();
CrateDataManager.init();
config = getConfig();
this.commandManager = new CommandManager(this, true);
this.registerCommandParameters();
this.commandManager.register(new CommandAdminCrate());
}
public void onDisable() {
CrateDataManager.onDisable();
AstroCrateKeysPlugin = null;
}
private void registerListeners() {
registerListener(new CrateLogicListener());
registerListener(new CrateKeyInventoryListener());
}
public static void registerListener(Listener listener) {
Bukkit.getPluginManager().registerEvents(listener, (Plugin) AstroCrateKeysPlugin);
}
public static CrankedCrateKeysPlugin getInstance() {
return AstroCrateKeysPlugin;
}
private void registerCommandParameters() {
this.commandManager.getParameterHandler().register(CrateKeyType.class, argument -> {
CrateKeyType crateType = CrateKeyType.valueOf(String.valueOf(argument).toUpperCase());
return new TypeResult(crateType, argument);
});
}
}
@@ -0,0 +1,110 @@
package net.crankedpvp.cratekeys.commands;
import me.mattstudios.mf.annotations.*;
import me.mattstudios.mf.base.CommandBase;
import net.crankedpvp.crankedutils.utils.CC;
import net.crankedpvp.crankedutils.utils.PlayerUtils;
import net.crankedpvp.cratekeys.CrankedCrateKeysAPI;
import net.crankedpvp.cratekeys.enums.CrateKeyType;
import net.crankedpvp.cratekeys.items.CratePlacementItem;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
@Command("admincrate")
@Alias({"acrate","acrates", "admincrates"})
public class CommandAdminCrate extends CommandBase {
@Default
@Permission("cratekeys.admin")
public void defaultCommand(CommandSender commandSender) {
Player player = (Player) commandSender;
player.sendMessage("");
player.sendMessage(CC.RedB + "Admin Crate Commands");
//player.sendMessage(CC.Red + "/acrate additem <percentage> <rarity>");
//player.sendMessage(CC.Red + "/acrate removeitem <rarity>");
player.sendMessage(CC.Red + "/acrate givekey <rarity> <amount> [IGN]");
player.sendMessage(CC.Red + "/acrate givecrate <rarity>");
player.sendMessage("");
}
/*
@Permission("cratekeys.admin")
@SubCommand("additem")
public void addItem(CommandSender sender, @Completion("#range:0-100") Double chance, @Completion("#enum") CrateType type) {
Player player = ((Player)sender);
ItemStack item = player.getItemInHand().clone();
if (item == null || item.getType() == Material.AIR) {
player.sendMessage(CC.RedB + "(!) " + CC.Red + "You can't add air to a crate!");
return;
}
LootTableAPI.g
Map<ItemStack, Double> items = CrankedCrateKeysAPI.getItems(type);
items.put(item, chance);
CrankedCrateKeysAPI.setItems(type, items);
CrankedCrateKeysPlugin.getInstance().getCrateLootManager().getCachedCrateLoot().put(type, CrankedCrateKeysAPI.getItems(type));
player.sendMessage(CC.translate("&a&l(!)&a Added " + item.getItemMeta().getDisplayName() + " &ato " + type.getPrefixedName() + " &awith a &7" + chance + " &a% chance."));
}
@Permission("cratekeys.admin")
@SubCommand("removeitem")
public void removeItem(CommandSender sender, @Completion("#enum") CrateType type) {
Player player = ((Player)sender);
ItemStack item = player.getItemInHand().clone();
if (item == null || item.getType() == Material.AIR) {
player.sendMessage(CC.RedB + "(!) " + CC.Red + "You can't remove air from a crate!");
return;
}
Map<ItemStack, Double> items = CrankedCrateKeysAPI.getItems(type);
for (Map.Entry<ItemStack, Double> entry : items.entrySet()) {
if (entry.getKey().equals(item)) {
item = entry.getKey();
break;
}
}
if (!items.containsKey(item)) {
player.sendMessage(CC.RedB + "(!) " + CC.Red + "Could not find item!");
return;
}
items.remove(item);
CrankedCrateKeysAPI.setItems(type, items);
CrankedCrateKeysPlugin.getInstance().getCrateLootManager().getCachedCrateLoot().put(type, CrankedCrateKeysAPI.getItems(type));
player.sendMessage(CC.translate("&a&l(!)&a Removed " + item.getItemMeta().getDisplayName() + " &afrom " + type.getPrefixedName() + "&a."));
}
*/
@Permission("cratekeys.admin")
@SubCommand("givekey")
public void giveKey(CommandSender sender, @Completion("#enum") CrateKeyType type, Integer amount, @Optional Player forPlayer) {
if (amount == null) {
amount = 1;
}
if (forPlayer == null) {
if (sender instanceof Player) {
Player player = ((Player) sender);
CrankedCrateKeysAPI.givePlayerKeys(player, type, amount);
player.sendMessage(CC.translate("&a&l(!)&a You have received " + amount + " &a " + type.getPrefixedName() + " &acratekeys."));
}
} else {
CrankedCrateKeysAPI.givePlayerKeys(forPlayer, type, amount);
forPlayer.sendMessage(CC.translate("&a&l(!)&a You have received " + amount + " &a " + type.getPrefixedName() + " &acratekeys."));
}
}
@Permission("cratekeys.admin")
@SubCommand("givecrate")
public void giveCrate(Player sender, @Completion("#enum") CrateKeyType type) {
sender.sendMessage(CC.translate("&a&l(!)&a You have received a " +type.getName() + " Crate."));
ItemStack item = CratePlacementItem.getItem(type);
PlayerUtils.giveOrDropProtectedItem(sender, item, 60);
}
}
@@ -0,0 +1,23 @@
package net.crankedpvp.cratekeys.data;
import lombok.Getter;
import net.crankedpvp.cratekeys.enums.CrateKeyType;
import org.bukkit.Location;
public class CrateData {
private final CrateKeyType crate;
@Getter
private final Location location;
public CrateData(CrateKeyType crate, Location location) {
this.crate = crate;
this.location = location;
}
public CrateKeyType getCrateType() {
return this.crate;
}
}
@@ -0,0 +1,47 @@
package net.crankedpvp.cratekeys.enums;
import lombok.Getter;
import net.crankedpvp.crankedutils.utils.CC;
public enum CrateKeyType {
ABYSS_KEY("Abyss", "&c", ""),
MYTHIC_KEY("Mythic", "&e", ""),
SEASON_KEY("Winter", "&b", "&3");
@Getter
private final String name;
@Getter
private final String prefix;
private final String prefix2;
CrateKeyType(String name, String prefix, String prefix2) {
this.name = name;
this.prefix = CC.translate(prefix);
this.prefix2 = CC.translate(prefix2);
}
public String getPrefixedName() {
if (this == SEASON_KEY) {
StringBuilder prefix = new StringBuilder();
boolean color = true;
String currentColor;
for (String letter : this.getName().split("")) {
if (color) {
currentColor = this.prefix;
color = false;
} else {
currentColor = this.prefix2;
color = true;
}
currentColor += "&l";
prefix.append(currentColor).append(letter);
}
return CC.translate(prefix + "&r&l" + " &f&lCrate Key");
} else {
return CC.translate(getPrefix() + CC.Bold + getName() + " &f&lCrate Key");
}
}
}
@@ -0,0 +1,97 @@
package net.crankedpvp.cratekeys.inventory;
import com.google.common.collect.Lists;
import dev.triumphteam.gui.guis.GuiItem;
import dev.triumphteam.gui.guis.PaginatedGui;
import lombok.Getter;
import net.crankedpvp.crankedutils.utils.CC;
import net.crankedpvp.crankedutils.utils.GuiUtil;
import net.crankedpvp.cratekeys.enums.CrateKeyType;
import net.crankedpvp.loottable.LootTableAPI;
import net.crankedpvp.loottable.struct.LootTableType;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.List;
public class LootPreviewGUI {
@Getter
private final PaginatedGui cratePreviewInventory;
public LootPreviewGUI(CrateKeyType type) {
this.cratePreviewInventory = new PaginatedGui(6, 28, type.getName() + " Crate Loot Preview");
cratePreviewInventory.setDefaultClickAction(event -> event.setCancelled(true));
cratePreviewInventory.getFiller().fillBorder(new GuiItem(GuiUtil.createFillerItem()));
cratePreviewInventory.setItem(6, 1, new GuiItem(GuiUtil.createPreviousItem(), event -> {
cratePreviewInventory.previous();
event.setCancelled(true);
}));
cratePreviewInventory.setItem(6, 9, new GuiItem(GuiUtil.createNextItem(), event -> {
cratePreviewInventory.next();
event.setCancelled(true);
}));
cratePreviewInventory.setItem(1, 5, new GuiItem(createGUIItem()));
for(ItemStack itemStack : LootTableAPI.getAllLootFromLootTable(LootTableType.valueOf(type.name()))) {
this.cratePreviewInventory.addItem(new GuiItem(itemStack, event -> {
event.getWhoClicked().sendMessage(CC.Gray + "All items in this crate have a same chance to be pulled.");
if(event.getWhoClicked().isOp()) event.getWhoClicked().getInventory().addItem(itemStack);
}));
}
/*
for (Map.Entry<ItemStack, Double> entry : itemList.entrySet()) {
ItemStack item = entry.getKey().clone();
double chance = entry.getValue();
ItemBuilder itemBuilder = new ItemBuilder(item);
itemBuilder.addLoreLine("");
itemBuilder.addLoreLine(CC.translate("&c&l* &7Chance: &f" + chance + "%"));
itemBuilder.addLoreLine("");
this.expShopUI.addItem(new GuiItem(itemBuilder.build(), event -> {
ItemStack found = null;
if (player.isOp() && event.isShiftClick() && event.isRightClick()) {
for (ItemStack i : itemList.keySet()) {
if (i.getItemMeta().getDisplayName().equals(item.getItemMeta().getDisplayName())) {
found = i;
}
}
itemList.remove(found);
CrankedCrateKeysAPI.setItems(type, itemList);
CrankedCrateKeysPlugin.getInstance().getCrateLootManager().getCachedCrateLoot().put(type, CrankedCrateKeysAPI.getItems(type));
player.sendMessage(CC.translate("&a&l(!)&a Removed " + found.getItemMeta().getDisplayName() + "&f from the " + type.getPrefixedName() + "&a!"));
LootPreviewGUI lootPreviewGUI = new LootPreviewGUI(player, type);
lootPreviewGUI.getLootGUI().open(player);
}
}));
}
*/
}
public ItemStack createGUIItem() {
ItemStack item = new ItemStack(Material.BOOK);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(CC.translate("&6&lCrates Info"));
List<String> lore = Lists.newArrayList();
lore.add(CC.Gray + "Crate Keys can be unlocked from");
lore.add(CC.Gray + "killings mobs, /shards shop,");
lore.add(CC.Gray + "fighting bosses, koth, envoy");
lore.add(CC.Gray + "and many more other ways.");
meta.setLore(lore);
item.setItemMeta(meta);
return item;
}
}
@@ -0,0 +1,45 @@
package net.crankedpvp.cratekeys.items;
import de.tr7zw.nbtapi.NBTCompound;
import de.tr7zw.nbtapi.NBTItem;
import net.crankedpvp.crankedutils.utils.CC;
import net.crankedpvp.crankedutils.utils.items.ItemBuilder;
import net.crankedpvp.cratekeys.enums.CrateKeyType;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
public class CrateKeyItem {
public static ItemStack getItem(CrateKeyType type) {
if (type == null)
return null;
ItemBuilder item = new ItemBuilder(Material.TRIPWIRE_HOOK);
item.setName(type.getPrefixedName());
item.addLoreLine(CC.translate("&7Right-Click on a " + type.getPrefix() +type.getName() + " &7Crate to open."));
item.addLoreLine("");
item.addLoreLine(CC.translate(type.getPrefix() + CC.Bold +"(!) " + type.getPrefix() + "Type &f/warp crates " + type.getPrefix() + " to open this crate key."));
ItemStack built = item.build();
NBTItem wrapper = new NBTItem(built);
NBTCompound tag = wrapper.getOrCreateCompound("cosmicData");
tag.setString("crate_type", type.name());
if (type.name().contains("MUTATED")) {
tag.setString("tintColor", "-10237440");
}
return new ItemBuilder(wrapper.getItem()).setGlowing().build();
}
public static CrateKeyType getCrateType(ItemStack item) {
if (item == null || item.getType() == Material.AIR)
return null;
NBTItem wrapper = new NBTItem(item);
if (!wrapper.hasKey("cosmicData"))
return null;
NBTCompound tag = wrapper.getOrCreateCompound("cosmicData");
if (!tag.hasKey("crate_type"))
return null;
return CrateKeyType.valueOf(tag.getString("crate_type"));
}
}
@@ -0,0 +1,36 @@
package net.crankedpvp.cratekeys.items;
import de.tr7zw.nbtapi.NBTCompound;
import de.tr7zw.nbtapi.NBTItem;
import net.crankedpvp.crankedutils.utils.CC;
import net.crankedpvp.crankedutils.utils.items.ItemBuilder;
import net.crankedpvp.cratekeys.enums.CrateKeyType;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
public class CratePlacementItem {
public static ItemStack getItem(CrateKeyType type) {
if (type == null)
return null;
ItemBuilder item = new ItemBuilder(Material.CHEST);
item.setName(type.getPrefixedName() + " Crate");
item.addLoreLine(CC.Gray + "Right-Click to place crate.");
ItemStack built = item.build();
NBTItem wrapper = new NBTItem(built);
NBTCompound tag = wrapper.getOrCreateCompound("cosmicData");
tag.setString("crate_place_type", type.name());
return wrapper.getItem();
}
public static CrateKeyType getCrateType(ItemStack item) {
if (item == null || item.getType() == Material.AIR)
return null;
NBTItem wrapper = new NBTItem(item);
if (!wrapper.hasKey("cosmicData"))
return null;
NBTCompound tag = wrapper.getOrCreateCompound("cosmicData");
if (!tag.hasKey("crate_place_type"))
return null;
return CrateKeyType.valueOf(tag.getString("crate_place_type"));
}
}
@@ -0,0 +1,77 @@
package net.crankedpvp.cratekeys.listeners;
import net.crankedpvp.crankedutils.utils.CC;
import net.crankedpvp.crankedutils.utils.InventoryUtils;
import net.crankedpvp.cratekeys.CrankedCrateKeysAPI;
import net.crankedpvp.cratekeys.data.CrateData;
import net.crankedpvp.cratekeys.items.CrateKeyItem;
import net.crankedpvp.leaderboards.LeaderboardsAPI;
import net.crankedpvp.loottable.struct.LootTableType;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.block.Chest;
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;
public class CrateKeyInventoryListener implements Listener {
@EventHandler
public void onClick(PlayerInteractEvent event) {
if (event.getAction() != Action.RIGHT_CLICK_BLOCK)
return;
Player player = event.getPlayer();
Block block = event.getClickedBlock();
if ((block.getState() instanceof Chest)) {
Chest chest = (Chest) block.getState();
CrateData data = CrankedCrateKeysAPI.findCrate(chest.getLocation());
if (data == null) {
return;
}
event.setCancelled(true);
if (!CrankedCrateKeysAPI.isCrateKey(player.getItemInHand())) {
player.sendMessage(CC.translate("&c&l(!)&c Please hold a " + data.getCrateType().getPrefix() + CC.Bold +
data.getCrateType().getName() + "&f&l Crate Key&c to open this crate."));
return;
}
if (CrateKeyItem.getCrateType(player.getItemInHand()) != data.getCrateType()) {
player.sendMessage(CC.translate("&c&l(!)&c This is not the correct crate type. You need to click the " + CrateKeyItem.getCrateType(player.getItemInHand()).getPrefix() +
CC.Bold + CrateKeyItem.getCrateType(player.getItemInHand()).getName() + CC.Bold + "&f&l Crate&c to open this key."));
return;
}
if (!InventoryUtils.hasAvailableSlot(player)) {
player.sendMessage(CC.translate("&c&l(!)&c Your inventory is full. Clear some space before opening more keys!"));
return;
}
if (player.isSneaking()) {
int amountOfKeys = player.getItemInHand().getAmount();
while (amountOfKeys != 0) {
CrankedCrateKeysAPI.giveCrateKeyLoot(player, LootTableType.valueOf(data.getCrateType().name()));
amountOfKeys--;
}
LeaderboardsAPI.getPlayerStats(player.getUniqueId()).addCrateKeysOpened(amountOfKeys);
player.setItemInHand(null);
} else {
LeaderboardsAPI.getPlayerStats(player.getUniqueId()).addCrateKeysOpened(1);
CrankedCrateKeysAPI.giveCrateKeyLoot(player, LootTableType.valueOf(data.getCrateType().name()));
if (player.getItemInHand().getAmount() > 1) {
player.getItemInHand().setAmount(player.getItemInHand().getAmount() - 1);
} else {
player.setItemInHand(null);
}
}
player.playSound(player.getLocation(), Sound.LEVEL_UP, 1f, 1f);
}
}
}
@@ -0,0 +1,76 @@
package net.crankedpvp.cratekeys.listeners;
import net.crankedpvp.cratekeys.CrankedCrateKeysAPI;
import net.crankedpvp.cratekeys.data.CrateData;
import net.crankedpvp.cratekeys.enums.CrateKeyType;
import net.crankedpvp.cratekeys.inventory.LootPreviewGUI;
import net.crankedpvp.cratekeys.items.CratePlacementItem;
import net.crankedpvp.cratekeys.managers.CrateDataManager;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Chest;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
public class CrateLogicListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPlace(BlockPlaceEvent event) {
ItemStack item = event.getPlayer().getItemInHand();
CrateKeyType type = CratePlacementItem.getCrateType(item);
if (type == null)
return;
final Block block = event.getBlock();
CrateDataManager.newInstance(type, block.getLocation());
}
@EventHandler(priority = EventPriority.LOW)
public void placeKey(PlayerInteractEvent event) {
if(event.getItem() == null || event.getItem().getType() == Material.AIR) return;
if(event.getItem().getType() != Material.TRIPWIRE_HOOK) return;
if(CrankedCrateKeysAPI.isCrateKey(event.getItem())) {
event.setCancelled(true);
return;
}
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBreak(BlockBreakEvent event) {
Block block = event.getBlock();
if (!(block.getState() instanceof Chest))
return;
Chest chest = (Chest) block.getState();
CrateData data = CrankedCrateKeysAPI.findCrate(chest.getLocation());
if (data == null)
return;
CrateDataManager.removeInstance(data);
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onInteract(PlayerInteractEvent event) {
if (event.getAction() != Action.LEFT_CLICK_BLOCK)
return;
Player player = event.getPlayer();
Block block = event.getClickedBlock();
if (!(block.getState() instanceof Chest))
return;
Chest chest = (Chest) block.getState();
CrateData data = CrankedCrateKeysAPI.findCrate(chest.getLocation());
if (data == null)
return;
if (player.isSneaking() && player.isOp()) return;
event.setCancelled(true);
LootPreviewGUI lootPreviewGUI = new LootPreviewGUI(data.getCrateType());
lootPreviewGUI.getCratePreviewInventory().open(player);
}
}
@@ -0,0 +1,49 @@
package net.crankedpvp.cratekeys.managers;
import lombok.Getter;
import net.crankedpvp.cratekeys.CrankedCrateKeysAPI;
import net.crankedpvp.cratekeys.data.CrateData;
import net.crankedpvp.cratekeys.enums.CrateKeyType;
import org.bukkit.Location;
import org.bukkit.Material;
import java.util.List;
public class CrateDataManager {
@Getter
private static List<CrateData> crates;
public static void init() {
crates = CrankedCrateKeysAPI.getCrates();
}
public static void onDisable() {
CrankedCrateKeysAPI.deleteCrates();
crates.forEach(crate -> CrankedCrateKeysAPI.addCrate(crate.getCrateType(), crate.getLocation()));
for (int i = crates.size() - 1; i >= 0; i--) {
CrateData data = crates.get(i);
if (data.getLocation().getBlock().getType() != Material.CHEST)
crates.remove(i);
}
}
public static void clearCrates() {
crates.clear();
}
public static void newInstance(CrateKeyType type, Location location) {
CrateData data = new CrateData(type, location);
crates.add(data);
}
public static void removeInstance(CrateData crate) {
for (int i = 0; i < crates.size(); i++) {
CrateData data = crates.get(i);
if (data.equals(crate)) {
crates.remove(i);
return;
}
}
}
}
View File
+7
View File
@@ -0,0 +1,7 @@
name: CrankedCrateKeys
main: net.crankedpvp.cratekeys.CrankedCrateKeysPlugin
version: ${git.total.commit.count}.0
description: "§fCommit ID: §a${git.commit.id.abbrev}"
website: https://crankedpvp.net/
depend:
- CrankedUtils