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
@@ -0,0 +1,64 @@
package net.crankedpvp.customblocks;
import lombok.Getter;
import me.mattstudios.mf.base.CommandManager;
import net.crankedpvp.customblocks.commands.CustomBlockCommand;
import net.crankedpvp.customblocks.data.CustomBlockManager;
import net.crankedpvp.customblocks.listeners.CustomBlockBreakListener;
import net.crankedpvp.customblocks.listeners.CustomBlockListener;
import net.crankedpvp.customblocks.listeners.CustomBlockPlaceListener;
import net.crankedpvp.customblocks.struct.CustomBlock;
import net.crankedpvp.customblocks.struct.typeadapters.CustomBlockDeserializer;
import net.crankedpvp.customblocks.tasks.TickAbleBlocksRunnable;
import net.crankedpvp.customblocks.utils.config.ConfigManager;
import net.crankedpvp.storageutils.StorageUtils;
import net.crankedpvp.storageutils.gson.Gson;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
@Getter
public class CustomBlocks extends JavaPlugin {
@Getter
public static CustomBlocks instance;
private CustomBlockManager customBlockManager;
private CommandManager commandManager;
private ConfigManager configManager;
private Gson gson;
public void onEnable() {
instance = this;
this.gson = StorageUtils.getInstance()
.getGson()
.newBuilder()
.registerTypeAdapter(CustomBlock.class, new CustomBlockDeserializer()).setLenient()
.create();
this.commandManager = new CommandManager(this, true);
commandManager.register(new CustomBlockCommand());
this.customBlockManager = new CustomBlockManager(this);
customBlockManager.loadAll();
Bukkit.getScheduler().scheduleAsyncRepeatingTask(this, ()-> {
customBlockManager.saveAll();
}, 40L, 15*60*20L);
this.configManager = new ConfigManager(this);
configManager.setupFiles();
registerListeners();
registerTasks();
}
public void onDisable() {
customBlockManager.saveAll();
instance = null;
}
public void registerListeners() {
getServer().getPluginManager().registerEvents(new CustomBlockListener(this), this);
getServer().getPluginManager().registerEvents(new CustomBlockBreakListener(this), this);
getServer().getPluginManager().registerEvents(new CustomBlockPlaceListener(this), this);
}
public void registerTasks() {
Bukkit.getScheduler().scheduleAsyncRepeatingTask(this, new TickAbleBlocksRunnable(), 40L, 20L);
}
}
@@ -0,0 +1,67 @@
package net.crankedpvp.customblocks;
import de.tr7zw.nbtapi.NBTCompound;
import de.tr7zw.nbtapi.NBTItem;
import net.crankedpvp.customblocks.data.CustomBlockManager;
import net.crankedpvp.customblocks.struct.CustomBlock;
import net.crankedpvp.customblocks.struct.CustomBlockType;
import net.crankedpvp.customblocks.utils.ChunkUtil;
import net.crankedpvp.crankedutils.utils.LocationUtils;
import net.crankedpvp.crankedutils.utils.items.ItemUtils;
import net.crankedpvp.storageutils.utils.LocationDeserializer;
import net.crankedpvp.storageutils.utils.LocationSerializer;
import org.bukkit.Location;
import org.bukkit.inventory.ItemStack;
public class CustomBlocksAPI {
private static CustomBlockManager customBlockManager = CustomBlocks.getInstance().getCustomBlockManager();
public static boolean isCustomBlockItem(ItemStack itemStack) {
if (ItemUtils.isAirOrNull(itemStack)) return false;
NBTItem nbtItem = new NBTItem(itemStack);
return nbtItem.hasKey("cosmicType") && nbtItem.getString("cosmicType").equals("custom_block");
}
public static int getLevelFromItem(ItemStack itemStack) {
if (isCustomBlockItem(itemStack)) {
NBTItem nbtItem = new NBTItem(itemStack);
NBTCompound cosmicData = nbtItem.getOrCreateCompound("cosmicData");
NBTCompound customBlockData = cosmicData.getOrCreateCompound("customBlockData");
return customBlockData.getInteger("level");
}
return 1;
}
public static CustomBlockType getTypeFromItem(ItemStack itemStack) {
if (isCustomBlockItem(itemStack)) {
NBTItem nbtItem = new NBTItem(itemStack);
NBTCompound cosmicData = nbtItem.getOrCreateCompound("cosmicData");
NBTCompound customBlockData = cosmicData.getOrCreateCompound("customBlockData");
return CustomBlockType.valueOf(customBlockData.getString("type"));
}
return null;
}
public static CustomBlock getCustomBlockData(Location location) {
if (isCustomBlock(location)) {
return customBlockManager.getCustomBlockData(location);
}
return null;
}
public static boolean isCustomBlock(Location location) {
return customBlockManager.containsKey(LocationSerializer.serialize(location));
}
public static boolean chunkContainsCustomBlock(Location location, CustomBlockType type) {
for (String key : customBlockManager.getCacheKeys()) {
Location loc = LocationDeserializer.deserialize(key);
CustomBlock data = customBlockManager.getCustomBlockData(loc);
if (ChunkUtil.isLocationAInSameChunkAsLocationB(loc, location) && type == data.getType()) {
return true;
}
}
return false;
}
}
@@ -0,0 +1,41 @@
package net.crankedpvp.customblocks.commands;
import me.mattstudios.mf.annotations.*;
import me.mattstudios.mf.base.CommandBase;
import net.crankedpvp.customblocks.CustomBlocks;
import net.crankedpvp.customblocks.guis.CustomBlocksGui;
import net.crankedpvp.customblocks.struct.CustomBlockType;
import net.crankedpvp.crankedutils.utils.CC;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@Command("customblocks")
@Alias({"cblocks", "customblock"})
public class CustomBlockCommand extends CommandBase {
@Default
public void onDefault(CommandSender player) {
player.sendMessage(CC.translate("&e&l(!)&e Opening the Custom Blocks Menu..."));
CustomBlocksGui customBlocksGui = null;
try {
customBlocksGui = new CustomBlocksGui((Player) player);
} catch (IllegalAccessException | InstantiationException e) {
e.printStackTrace();
}
customBlocksGui.getGui().open((Player) player);
}
@SubCommand("give")
@Permission("cranked.admin")
public void onGive(Player player, String type, Integer level) {
player.getInventory().addItem(CustomBlockType.valueOf(type).build(level));
player.sendMessage(CC.Green + "Received Custom Block!");
}
@SubCommand("reload")
@Permission("cranked.admin")
public void onReload(Player player) {
CustomBlocks.getInstance().getConfigManager().reloadFiles();
player.sendMessage(CC.Green + "Reloaded Custom Blocks");
}
}
@@ -0,0 +1,23 @@
package net.crankedpvp.customblocks.data;
import net.crankedpvp.customblocks.CustomBlocks;
import net.crankedpvp.customblocks.struct.CustomBlock;
import net.crankedpvp.crankedutils.utils.LocationUtils;
import net.crankedpvp.storageutils.utils.LocationDeserializer;
import net.crankedpvp.storageutils.utils.LocationSerializer;
import net.crankedpvp.storageutils.utils.SaveLoadManager;
import org.bukkit.Location;
public class CustomBlockManager extends SaveLoadManager<CustomBlock> {
public CustomBlockManager(CustomBlocks plugin) {
super(plugin, CustomBlocks.getInstance().getGson(), "/data", CustomBlock.class);
}
public CustomBlock getCustomBlockData(Location location) {
return getFromCache(LocationSerializer.serialize(location));
}
}
@@ -0,0 +1,127 @@
package net.crankedpvp.customblocks.guis;
import dev.triumphteam.gui.guis.GuiItem;
import dev.triumphteam.gui.guis.PaginatedGui;
import lombok.Getter;
import net.crankedpvp.customblocks.struct.impl.AutoGrinder;
import net.crankedpvp.customblocks.utils.TimeUtil;
import net.crankedpvp.crankedutils.utils.CC;
import net.crankedpvp.crankedutils.utils.EconomyUtils;
import net.crankedpvp.crankedutils.utils.NumberUtils;
import net.crankedpvp.crankedutils.utils.PlayerUtils;
import net.crankedpvp.crankedutils.utils.items.ItemBuilder;
import net.crankedpvp.cratekeys.enums.CrateKeyType;
import net.crankedpvp.cratekeys.items.CrateKeyItem;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.util.EnumMap;
@Getter
public class AutoGrinderGui {
private PaginatedGui gui;
private static final ItemStack SEASON_KEY_ITEM = CrateKeyItem.getItem(CrateKeyType.SEASON_KEY);
private static final ItemStack ABYSS_KEY_ITEM = CrateKeyItem.getItem(CrateKeyType.ABYSS_KEY);
private static final ItemStack MYTHIC_KEY_ITEM = CrateKeyItem.getItem(CrateKeyType.MYTHIC_KEY);
private static final EnumMap<CrateKeyType, ItemStack> crateKeyTypeItemStackEnumMap = new EnumMap<CrateKeyType, ItemStack>(CrateKeyType.class);
public AutoGrinderGui(Player player, AutoGrinder autoGrinder) {
crateKeyTypeItemStackEnumMap.put(CrateKeyType.SEASON_KEY, SEASON_KEY_ITEM);
crateKeyTypeItemStackEnumMap.put(CrateKeyType.ABYSS_KEY, ABYSS_KEY_ITEM);
crateKeyTypeItemStackEnumMap.put(CrateKeyType.MYTHIC_KEY, MYTHIC_KEY_ITEM);
this.gui = new PaginatedGui(3, 27, "Key Grinder Menu");
this.gui.setItem(1, 2, getKeyClaimItem(autoGrinder, CrateKeyType.SEASON_KEY));
this.gui.setItem(2, 2, getKeyClaimItem(autoGrinder, CrateKeyType.ABYSS_KEY));
this.gui.setItem(3, 2, getKeyClaimItem(autoGrinder, CrateKeyType.MYTHIC_KEY));
this.gui.setItem(2, 4, getStatusItem(autoGrinder));
this.gui.setItem(2, 6, getExpClaimItem(autoGrinder));
this.gui.setItem(2, 8, getUpgradeItem(autoGrinder));
this.gui.setDefaultClickAction(event -> event.setCancelled(true));
}
public GuiItem getKeyClaimItem(AutoGrinder autoGrinder, CrateKeyType type) {
ItemStack item = new ItemBuilder(Material.GOLD_NUGGET)
.setName(type.getPrefixedName() + " &7(&f" + NumberUtils.formatMoney(autoGrinder.getKeys().getOrDefault(type, 0)) + "&7/" + autoGrinder.getMaxKeys(autoGrinder.getLevel()) + "&7)")
.addLoreLine("&7Click to claim the " + type.getName() + " keys in this key grinder!")
.colorize()
.build();
return new GuiItem(item, event -> {
if (autoGrinder.getKeys().getOrDefault(type, 0) == 0) return;
while (event.getWhoClicked().getInventory().firstEmpty() != -1 && autoGrinder.getKeys().get(type) != 0) {
autoGrinder.removeKey(type);
PlayerUtils.giveOrDropProtectedItem((Player) event.getWhoClicked(), CrateKeyItem.getItem(type), 60);
}
this.gui.updateItem(event.getSlot(), getKeyClaimItem(autoGrinder, type));
});
}
public GuiItem getStatusItem(AutoGrinder autoGrinder) {
ItemStack activateButton = new ItemBuilder(Material.STONE_BUTTON, CC.translate("&e&lActivate Key Grinder"))
.addLoreLine("")
.addLoreLine(CC.translate("&eExpires in: &f" + ((autoGrinder.getLastActivated() + 3600000) - System.currentTimeMillis() <= 0 ? "✗" : TimeUtil.fancy((autoGrinder.getLastActivated() + 3600000) - System.currentTimeMillis()))))
.addLoreLine(CC.translate("&eStatus: " + (autoGrinder.isActive() ? "&a&lACTIVE" : "&c&lINACTIVE")))
.addLoreLine(CC.translate("&ePrice: &2$&a" + NumberUtils.formatMoney(autoGrinder.getActivationPrice())))
.addLoreLine("")
.addLoreLine(CC.translate("&7Click to activate this key grinder"))
.build();
return new GuiItem(activateButton, event -> {
if (EconomyUtils.hasBalance((OfflinePlayer) event.getWhoClicked(), autoGrinder.getActivationPrice())) {
autoGrinder.activate((Player) event.getWhoClicked());
EconomyUtils.withdrawBalance((OfflinePlayer) event.getWhoClicked(), autoGrinder.getActivationPrice());
event.getWhoClicked().sendMessage(CC.translate("&c&l- " + NumberUtils.formatMoney(autoGrinder.getActivationPrice())));
} else {
event.getWhoClicked().sendMessage(CC.translate("&c&l(!)&c You do not have enough money to activate this key grinder!"));
}
this.gui.updateItem(2, 4, getStatusItem(autoGrinder));
});
}
public GuiItem getExpClaimItem(AutoGrinder autoGrinder) {
ItemStack item = new ItemBuilder(Material.EXP_BOTTLE)
.setName("&a&lEXP &7(" + NumberUtils.formatMoney(autoGrinder.getExp()) + "&7)")
.addLoreLine("&7Click to claim the exp in this key grinder!")
.colorize()
.build();
return new GuiItem(item, event -> {
if (autoGrinder.getExp() == 0) return;
((Player) event.getWhoClicked()).giveExp(autoGrinder.getExp());
event.getWhoClicked().sendMessage(CC.translate("&a&l+ " + NumberUtils.formatMoney(autoGrinder.getExp()) + " EXP"));
autoGrinder.setExp(0);
this.gui.updateItem(2, 6, getExpClaimItem(autoGrinder));
});
}
public GuiItem getUpgradeItem(AutoGrinder autoGrinder) {
ItemStack item = new ItemBuilder(Material.EMERALD)
.setName("&a&lUpgrade Key Grinder")
.addLoreLine("")
.addLoreLine("&aCurrent Level: &f" + autoGrinder.getLevel() + "&7/" + autoGrinder.getMaxLevel())
.addLoreLine(autoGrinder.getLevel() == autoGrinder.getMaxLevel() ? "&aUpgrade Price: &c" + "MAX" : "&aUpgrade Price: &f$" + NumberUtils.formatMoney(autoGrinder.getUpgradePrice(autoGrinder.getLevel())))
.addLoreLine("")
.addLoreLine("&7Click to upgrade the key grinder!")
.colorize()
.build();
return new GuiItem(item, event -> {
if (autoGrinder.getLevel() == autoGrinder.getMaxLevel()) return;
if (!EconomyUtils.hasBalance((OfflinePlayer) event.getWhoClicked(), autoGrinder.getUpgradePrice(autoGrinder.getLevel()))) {
event.getWhoClicked().sendMessage(CC.translate("&c&l(!)&c You do not have enough money to upgrade your key grinder! ($" + NumberUtils.formatMoney(autoGrinder.getUpgradePrice(autoGrinder.getLevel())) + ")"));
return;
}
EconomyUtils.withdrawBalance((OfflinePlayer) event.getWhoClicked(), autoGrinder.getUpgradePrice(autoGrinder.getLevel()));
event.getWhoClicked().sendMessage(CC.translate("&c&l- " + NumberUtils.formatMoney(autoGrinder.getUpgradePrice(autoGrinder.getLevel()))));
autoGrinder.setLevel(autoGrinder.getLevel() + 1);
this.gui.updateItem(2, 8, getUpgradeItem(autoGrinder));
});
}
}
@@ -0,0 +1,168 @@
package net.crankedpvp.customblocks.guis;
import dev.triumphteam.gui.guis.GuiItem;
import dev.triumphteam.gui.guis.PaginatedGui;
import lombok.Getter;
import net.crankedpvp.crankedcore.utils.ExpBottleAPI;
import net.crankedpvp.crankedcore.utils.MoneyNotesAPI;
import net.crankedpvp.cratekeys.CrankedCrateKeysAPI;
import net.crankedpvp.customblocks.struct.CustomBlock;
import net.crankedpvp.customblocks.utils.ChunkUtil;
import net.crankedpvp.customblocks.utils.CrateKeyAPI;
import net.crankedpvp.crankedutils.utils.CC;
import net.crankedpvp.crankedutils.utils.EconomyUtils;
import net.crankedpvp.crankedutils.utils.items.ItemBuilder;
import net.crankedpvp.cratekeys.enums.CrateKeyType;
import net.crankedpvp.lootfilter.LootFilterAPI;
import net.crankedpvp.loottable.LootTableAPI;
import net.crankedpvp.loottable.struct.LootTableType;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Chest;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Getter
public class CrateKeyOpenerGUI {
private final PaginatedGui customBlockGui;
public CrateKeyOpenerGUI(CustomBlock customBlock, Player player) {
player.sendMessage(CC.translate("&a&l(!)&a Opening the Personal CrateKey Opener!"));
player.playSound(player.getLocation(), Sound.CHEST_OPEN, 5, 5);
this.customBlockGui = new PaginatedGui(3, 27, "CrateKey Custom Block Menu");
HashMap<CrateKeyType, Integer> keys = CrateKeyAPI.getCrateKeysInInventory(player.getInventory());
int tickets = 0;
for (Integer amount : keys.values()) {
tickets += amount;
}
ItemStack confirmButton = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 5);
ItemMeta meta = confirmButton.getItemMeta();
meta.setDisplayName(CC.translate("&a&lConfirm Button"));
List<String> lore = new ArrayList<>();
lore.add(CC.translate("&7&nRight-Click&7 to open &f" + tickets + "&7 Cratekeys"));
meta.setLore(lore);
confirmButton.setItemMeta(meta);
int finalTickets = tickets;
this.customBlockGui.setItem(2, 3, new GuiItem(customBlock.getType().build(0)));
customBlockGui.setItem(2, 5, new GuiItem(confirmButton, event -> {
if (finalTickets == 0) {
player.sendMessage(CC.translate("&c&l(!)&c You have no cratekeys in your inventory!"));
return;
}
Location nearbyChest = ChunkUtil.getClosestChestLocation(customBlock.getLocation());
if (nearbyChest != null) {
Chest chest = (Chest) nearbyChest.getBlock().getState();
if (!ChunkUtil.isEmpty(chest.getInventory())) {
player.sendMessage(CC.translate("&c&l(!)&c Please empty the chest before opening new cratekeys!"));
return;
}
for (Map.Entry<CrateKeyType, Integer> crate : keys.entrySet()) {
CrateKeyType type = crate.getKey();
Integer amount = crate.getValue();
while (amount != 0) {
if (!ChunkUtil.isEmpty(chest.getInventory())) {
Location newNearbyChest = ChunkUtil.getClosestChestLocation(customBlock.getLocation());
if (newNearbyChest != null) {
chest = (Chest) newNearbyChest.getBlock().getState();
}
}
ItemStack itemStack = LootTableAPI.getRandomLootItem(LootTableType.valueOf(type.name()));
if (!LootFilterAPI.isFiltered(player, LootTableType.valueOf(type.name()), itemStack)) {
if (MoneyNotesAPI.isMoneyNote(itemStack)) {
double moneyNoteVal = MoneyNotesAPI.getMoneyAmount(itemStack);
EconomyUtils.depositBalance(player, moneyNoteVal);
player.playSound(player.getLocation(), Sound.ORB_PICKUP, 1.0f, 1.0f);
player.sendMessage(ChatColor.GREEN.toString() + ChatColor.BOLD.toString() + "+ $" + moneyNoteVal);
} else if (ExpBottleAPI.isXpBottle(itemStack)) {
int xpValue = ExpBottleAPI.getXpAmount(itemStack);
int exp = (player.getTotalExperience() + xpValue);
player.setTotalExperience(0);
player.setExp(0);
player.setLevel(0);
player.giveExp(exp);
player.playSound(player.getLocation(), Sound.ORB_PICKUP, 1.0f, 1.0f);
player.sendMessage(ChatColor.GREEN.toString() + ChatColor.BOLD.toString() + "+ " + xpValue + " xp");
} else {
chest.getInventory().addItem(itemStack);
}
}
amount--;
}
}
} else {
player.sendMessage(CC.translate("&c&l(!)&c There are no nearby empty chests to fill with loot"));
return;
}
//remove slots from player inv
Inventory inventory = player.getInventory();
for (ItemStack itemStack : inventory) {
if (CrankedCrateKeysAPI.isCrateKey(itemStack)) {
inventory.remove(itemStack);
}
}
player.getInventory().setContents(inventory.getContents());
player.closeInventory();
player.playSound(player.getLocation(), Sound.VILLAGER_YES, 5, 5);
}));
customBlockGui.getFiller().fillBorder(new GuiItem(new ItemBuilder(new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 7)).setName(CC.translate("&7")).build()));
customBlockGui.setDefaultClickAction(event -> event.setCancelled(true));
ItemStack clearButton = new ItemBuilder(Material.HOPPER, CC.translate("&c&lClear Chests"))
.addLoreLine(CC.translate("&7Click here to clear all nearby chests."))
.build();
customBlockGui.setItem(2, 7, new GuiItem(clearButton, event -> {
int counter = 0;
for (Location location : ChunkUtil.getNearbyChests(customBlock.getLocation())) {
Chest chest = (Chest) location.getBlock().getState();
if (!ChunkUtil.isEmpty(chest.getInventory())) {
chest.getInventory().clear();
counter++;
}
}
if (counter == 0) {
player.sendMessage(CC.translate("&c&l(!)&c There are no nearby chests to clear."));
return;
}
player.closeInventory();
player.playSound(player.getLocation(), Sound.VILLAGER_YES, 5, 5);
player.sendMessage(CC.translate("&c&l(!)&c Cleared " + counter + " nearby chests."));
}));
}
}
@@ -0,0 +1,30 @@
package net.crankedpvp.customblocks.guis;
import dev.triumphteam.gui.guis.GuiItem;
import dev.triumphteam.gui.guis.PaginatedGui;
import lombok.Getter;
import net.crankedpvp.crankedutils.utils.PlayerUtils;
import net.crankedpvp.customblocks.struct.CustomBlockType;
import net.crankedpvp.customblocks.struct.abilities.LevelAble;
import org.bukkit.entity.Player;
@Getter
public class CustomBlocksGui {
private final PaginatedGui gui;
public CustomBlocksGui(Player player) throws IllegalAccessException, InstantiationException {
this.gui = new PaginatedGui(3, 27, "Cranked CustomBlocks");
this.gui.setDefaultClickAction(event -> event.setCancelled(true));
for (CustomBlockType type : CustomBlockType.values()) {
this.gui.addItem(new GuiItem(type.getCustomBlockClass().newInstance() instanceof LevelAble ? type.build(1) : type.build(0), event -> {
if (player.isOp()) {
try {
PlayerUtils.giveOrDropProtectedItem(player, type.getCustomBlockClass().newInstance() instanceof LevelAble ? type.build(1) : type.build(0), 60);
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
}
}));
}
}
}
@@ -0,0 +1,168 @@
package net.crankedpvp.customblocks.guis;
import dev.triumphteam.gui.guis.GuiItem;
import dev.triumphteam.gui.guis.PaginatedGui;
import lombok.Getter;
import net.crankedpvp.crankedcore.utils.ExpBottleAPI;
import net.crankedpvp.crankedcore.utils.MoneyNotesAPI;
import net.crankedpvp.crankedutils.utils.CC;
import net.crankedpvp.crankedutils.utils.EconomyUtils;
import net.crankedpvp.crankedutils.utils.items.ItemBuilder;
import net.crankedpvp.customblocks.struct.CustomBlock;
import net.crankedpvp.customblocks.utils.ChunkUtil;
import net.crankedpvp.customblocks.utils.SlotbotsAPI;
import net.crankedpvp.lootfilter.LootFilterAPI;
import net.crankedpvp.loottable.LootTableAPI;
import net.crankedpvp.loottable.data.LootTable;
import net.crankedpvp.loottable.struct.LootTableType;
import net.crankedpvp.slotbot.SlotBotAPI;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Chest;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.ArrayList;
import java.util.List;
@Getter
public class SlotBotOpenerGUI {
private final PaginatedGui customBlockGui;
public SlotBotOpenerGUI(CustomBlock customBlock, Player p) {
p.sendMessage(CC.translate("&a&l(!)&a Opening the Personal Slotbot Opener!"));
p.playSound(p.getLocation(), Sound.CHEST_OPEN, 5, 5);
this.customBlockGui = new PaginatedGui(3, 27, "Slotbot Custom Block Menu");
int tickets = SlotbotsAPI.getSlotsInInv(p);
ItemStack confirmButton = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 5);
ItemMeta meta = confirmButton.getItemMeta();
meta.setDisplayName(CC.translate("&a&lConfirm Button"));
List<String> lore = new ArrayList<>();
lore.add(CC.translate("&7&nRight-Click&7 to open &f" + tickets + "&7 Slotbot Tickets"));
meta.setLore(lore);
confirmButton.setItemMeta(meta);
this.customBlockGui.setItem(2, 3, new GuiItem(customBlock.getType().build(0)));
customBlockGui.setItem(2, 5, new GuiItem(confirmButton, event -> {
if (tickets == 0) {
p.sendMessage(CC.translate("&c&l(!)&c You have no slots in your inventory!"));
return;
}
int a = tickets;
Location nearbyChest = ChunkUtil.getClosestChestLocation(customBlock.getLocation());
if (nearbyChest != null) {
Chest chest = (Chest) nearbyChest.getBlock().getState();
if (!ChunkUtil.isEmpty(chest.getInventory())) {
p.sendMessage(CC.translate("&c&l(!)&c Please empty the chest before opening new slotbots!"));
return;
}
//remove slots from player inv
Inventory inventory = p.getInventory();
for (ItemStack itemStack : inventory) {
if (SlotBotAPI.isSlotBot(itemStack)) {
inventory.remove(itemStack);
}
}
p.getInventory().setContents(inventory.getContents());
p.closeInventory();
p.playSound(p.getLocation(), Sound.VILLAGER_YES, 5, 5);
while (a != 0) {
if (!ChunkUtil.isEmpty(chest.getInventory())) {
Location newNearbyChest = ChunkUtil.getClosestChestLocation(chest.getLocation());
if (newNearbyChest != null) {
chest = (Chest) newNearbyChest.getBlock().getState();
}
}
final ItemStack itemStack = LootTableAPI.getRandomLootItem(LootTableType.SLOTBOT);
if (!LootFilterAPI.isFiltered(p, LootTableType.SLOTBOT, itemStack)) {
if (MoneyNotesAPI.isMoneyNote(itemStack)) {
double moneyNoteVal = MoneyNotesAPI.getMoneyAmount(itemStack);
EconomyUtils.depositBalance(p, moneyNoteVal);
p.playSound(p.getLocation(), Sound.ORB_PICKUP, 1.0f, 1.0f);
p.sendMessage(ChatColor.GREEN.toString() + ChatColor.BOLD.toString() + "+ $" + moneyNoteVal);
} else if (ExpBottleAPI.isXpBottle(itemStack)) {
int xpValue = ExpBottleAPI.getXpAmount(itemStack);
int exp = (p.getTotalExperience() + xpValue);
p.setTotalExperience(0);
p.setExp(0);
p.setLevel(0);
p.giveExp(exp);
p.playSound(p.getLocation(), Sound.ORB_PICKUP, 1.0f, 1.0f);
p.sendMessage(ChatColor.GREEN.toString() + ChatColor.BOLD.toString() + "+ " + xpValue + " xp");
} else {
if (itemStack == null) continue;
chest.getInventory().addItem(itemStack);
}
}
a--;
}
} else {
p.sendMessage(CC.translate("&c&l(!)&c There are no nearby empty chests to fill with loot"));
}
}));
customBlockGui.getFiller().fillBorder(new GuiItem(new ItemBuilder(new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 7)).setName(CC.translate("&7")).build()));
customBlockGui.setDefaultClickAction(event -> event.setCancelled(true));
ItemStack clearButton = new ItemBuilder(Material.HOPPER, CC.translate("&c&lClear Chests"))
.addLoreLine(CC.translate("&7Click here to clear all nearby chests."))
.build();
customBlockGui.setItem(2, 7, new GuiItem(clearButton, event -> {
int counter = 0;
for (Location location : ChunkUtil.getNearbyChests(customBlock.getLocation())) {
Chest chest = (Chest) location.getBlock().getState();
if (!ChunkUtil.isEmpty(chest.getInventory())) {
chest.getInventory().clear();
counter++;
}
}
if (counter == 0) {
p.sendMessage(CC.translate("&c&l(!)&c There are no nearby chests to clear."));
return;
}
p.closeInventory();
p.playSound(p.getLocation(), Sound.VILLAGER_YES, 5, 5);
p.sendMessage(CC.translate("&c&l(!)&c Cleared " + counter + " nearby chests."));
}));
}
}
@@ -0,0 +1,75 @@
package net.crankedpvp.customblocks.listeners;
import net.crankedpvp.customblocks.CustomBlocks;
import net.crankedpvp.customblocks.CustomBlocksAPI;
import net.crankedpvp.customblocks.data.CustomBlockManager;
import net.crankedpvp.customblocks.struct.CustomBlock;
import net.crankedpvp.customblocks.struct.CustomBlockType;
import net.crankedpvp.customblocks.struct.abilities.LevelAble;
import net.crankedpvp.crankedutils.utils.CC;
import net.crankedpvp.crankedutils.utils.EconomyUtils;
import net.crankedpvp.crankedutils.utils.NumberUtils;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import java.util.List;
public class CustomBlockBreakListener implements Listener {
private CustomBlockManager customBlockManager;
private CustomBlocks customBlocks;
public CustomBlockBreakListener(CustomBlocks plugin) {
this.customBlocks = plugin;
this.customBlockManager = plugin.getCustomBlockManager();
}
@EventHandler(priority = EventPriority.HIGH)
public void onBreak(BlockBreakEvent event) {
if (event.isCancelled()) return;
if (CustomBlocksAPI.isCustomBlock(event.getBlock().getLocation())) {
CustomBlock customBlockData = CustomBlocksAPI.getCustomBlockData(event.getBlock().getLocation());
CustomBlockType type = customBlockData.getType();
if (!EconomyUtils.hasBalance(event.getPlayer(), type.getMinePrice())) {
event.getPlayer().sendMessage(CC.translate("&c&l(!)&c You do not have enough money to mine the " + type.getDisplayName() + " Custom Block!"));
event.setCancelled(true);
return;
}
int level = 0;
if (customBlockData instanceof LevelAble) {
level = ((LevelAble) customBlockData).getLevel();
}
event.setCancelled(true);
event.getBlock().setType(Material.AIR);
event.getBlock().getLocation().getWorld().dropItemNaturally(event.getBlock().getLocation(), customBlockData.getType().build(level));
EconomyUtils.withdrawBalance(event.getPlayer(), type.getMinePrice());
event.getPlayer().sendMessage(CC.translate("&c&l- $" + NumberUtils.formatMoney(type.getMinePrice())));
customBlockManager.removeFromCache(customBlockData);
}
}
@EventHandler
public void onExplode(EntityExplodeEvent event) {
List<Block> blockList = event.blockList();
for (Block block : blockList) {
if (block.getType() != Material.NOTE_BLOCK) continue;
if (CustomBlocksAPI.isCustomBlock(block.getLocation())) {
block.getDrops().clear();
CustomBlock customBlockData = CustomBlocksAPI.getCustomBlockData(block.getLocation());
block.setType(Material.AIR);
int level = 0;
if (customBlockData instanceof LevelAble) {
level = ((LevelAble) customBlockData).getLevel();
}
block.getLocation().getWorld().dropItemNaturally(block.getLocation(), customBlockData.getType().build(level));
customBlockManager.removeFromCache(customBlockData);
}
}
}
}
@@ -0,0 +1,29 @@
package net.crankedpvp.customblocks.listeners;
import net.crankedpvp.customblocks.CustomBlocks;
import net.crankedpvp.customblocks.data.CustomBlockManager;
import net.crankedpvp.customblocks.struct.CustomBlock;
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.player.PlayerInteractEvent;
public class CustomBlockListener implements Listener {
private CustomBlockManager customBlockManager;
private CustomBlocks customBlocks;
public CustomBlockListener(CustomBlocks plugin) {
this.customBlocks = plugin;
this.customBlockManager = plugin.getCustomBlockManager();
}
@EventHandler(priority = EventPriority.HIGH)
public void onInteract(PlayerInteractEvent event) {
if (event.getAction() == Action.RIGHT_CLICK_BLOCK && !event.isCancelled() && customBlockManager.getCustomBlockData(event.getClickedBlock().getLocation()) != null) {
event.setCancelled(true);
CustomBlock customBlock = customBlockManager.getCustomBlockData(event.getClickedBlock().getLocation());
customBlock.onClick(event.getPlayer());
}
}
}
@@ -0,0 +1,65 @@
package net.crankedpvp.customblocks.listeners;
import net.crankedpvp.customblocks.CustomBlocks;
import net.crankedpvp.customblocks.CustomBlocksAPI;
import net.crankedpvp.customblocks.data.CustomBlockManager;
import net.crankedpvp.customblocks.struct.CustomBlock;
import net.crankedpvp.customblocks.struct.CustomBlockType;
import net.crankedpvp.customblocks.struct.abilities.LevelAble;
import net.crankedpvp.crankedutils.utils.CC;
import net.crankedpvp.crankedutils.utils.EconomyUtils;
import net.crankedpvp.crankedutils.utils.FactionUtils;
import net.crankedpvp.crankedutils.utils.NumberUtils;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.inventory.ItemStack;
public class CustomBlockPlaceListener implements Listener {
private CustomBlockManager customBlockManager;
private CustomBlocks customBlocks;
public CustomBlockPlaceListener(CustomBlocks plugin) {
this.customBlocks = plugin;
this.customBlockManager = plugin.getCustomBlockManager();
}
@EventHandler(priority = EventPriority.HIGH)
public void onPlace(BlockPlaceEvent event) throws IllegalAccessException, InstantiationException {
ItemStack item = event.getItemInHand();
if (event.isCancelled()) return;
if (CustomBlocksAPI.isCustomBlockItem(item)) {
CustomBlockType type = CustomBlocksAPI.getTypeFromItem(item);
if (FactionUtils.isWilderness(event.getBlock().getLocation())) {
event.getPlayer().sendMessage(CC.translate("&c&l(!)&c You cannot place custom blocks in wilderness!"));
event.setCancelled(true);
return;
}
if (!EconomyUtils.hasBalance(event.getPlayer(), type.getPlacePrice())) {
event.getPlayer().sendMessage(CC.translate("&c&l(!)&c You do not have enough money to place the " + type.getDisplayName() + " Custom Block!"));
event.setCancelled(true);
return;
}
if (CustomBlocksAPI.chunkContainsCustomBlock(event.getBlock().getLocation(), type)) {
event.getPlayer().sendMessage(CC.translate("&c&l(!)&c This chunk already contains a " + type.getDisplayName() + " Custom Block!"));
event.setCancelled(true);
return;
}
event.getPlayer().sendMessage(CC.translate("&c&l- $" + NumberUtils.formatMoney(type.getPlacePrice())));
EconomyUtils.withdrawBalance(event.getPlayer(), type.getPlacePrice());
CustomBlock customBlock = type.getCustomBlockClass().newInstance();
customBlock.setLocation(event.getBlock().getLocation());
if (customBlock instanceof LevelAble) {
((LevelAble) customBlock).setLevel(CustomBlocksAPI.getLevelFromItem(item));
}
customBlockManager.addToCache(customBlock);
}
}
}
@@ -0,0 +1,36 @@
package net.crankedpvp.customblocks.struct;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import net.crankedpvp.crankedutils.utils.LocationUtils;
import net.crankedpvp.storageutils.utils.KeyToSave;
import net.crankedpvp.storageutils.utils.LocationDeserializer;
import net.crankedpvp.storageutils.utils.LocationSerializer;
import org.bukkit.Location;
import org.bukkit.entity.Player;
@Getter
@RequiredArgsConstructor
@Setter
public abstract class CustomBlock implements KeyToSave {
private Location location;
private final CustomBlockType type;
@Override
public String getKey() {
return LocationSerializer.serialize(location);
}
@Override
public void setKey(String key) {
this.location = LocationDeserializer.deserialize(key);
}
public void onClick(Player clicker) {
}
}
@@ -0,0 +1,84 @@
package net.crankedpvp.customblocks.struct;
import de.tr7zw.nbtapi.NBTCompound;
import de.tr7zw.nbtapi.NBTItem;
import lombok.AllArgsConstructor;
import lombok.Getter;
import net.crankedpvp.customblocks.struct.impl.*;
import net.crankedpvp.crankedutils.utils.CC;
import net.crankedpvp.crankedutils.utils.NumberUtils;
import net.crankedpvp.crankedutils.utils.items.ItemBuilder;
import net.crankedpvp.crankedutils.utils.items.SkullUtils;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import java.util.Arrays;
import java.util.List;
@Getter
@AllArgsConstructor
public enum CustomBlockType {
SLOTBOT_OPENER("Roll Tickets Opener", ChatColor.GOLD, 250000, 250000, Arrays.asList(
CC.translate("&7Roll all your tickets"),
CC.translate("&7at once using the roll bot opener."),
"",
CC.translate("&7Place chests around the block"),
CC.translate("&7to collect all the loot from the bot.")
), SlotOpener.class, "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZGUxYzJhNWM0ZDRjMzVmYzM3NTQyOTVmMzljMzMzZWY2NzhiZDJlZGFkOWM4OGYyZDE2ODBmMTQ5NjgyIn19fQ=="),
KEY_OPENER("Key Opener", ChatColor.AQUA, 250000, 250000, Arrays.asList(
CC.translate("&7Open all your cratekeys"),
CC.translate("&7at once using the key opener."),
"",
CC.translate("&7Place chests around the block"),
CC.translate("&7to collect all the loot from the keys.")
), KeyOpener.class, "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZjk4YmM2M2YwNWY2Mzc4YmYyOWVmMTBlM2Q4MmFjYjNjZWI3M2E3MjBiZjgwZjMwYmM1NzZkMGFkOGM0MGNmYiJ9fX0="),
AUTO_GRINDER("Key Grinder", ChatColor.YELLOW, 250000, 250000, Arrays.asList(
CC.translate("&7Automatically grinds crate keys"),
CC.translate("&7while being placed and activated."),
"",
CC.translate("&7Dug up from the deep depths of"),
CC.translate("&7the gold mines, this custom block is"),
CC.translate("&7essential for grinding Keys.")
), AutoGrinder.class, "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvYjIwNjU3ZTI0YjU2ZTFiMmY4ZmMyMTlkYTFkZTc4OGMwYzI0ZjM2Mzg4YjFhNDA5ZDBjZDJkOGRiYTQ0YWEzYiJ9fX0=");
private final String displayName;
private final ChatColor color;
private final double minePrice;
private final double placePrice;
private final List<String> lore;
private final Class<? extends CustomBlock> customBlockClass;
private final String skin;
public ItemStack build(int level) {
String title = level != 0 ? getColor() + "&l" + getDisplayName() + " &7(&fLVL " + level + "&7)": getColor() + "&l" + getDisplayName();
ItemStack block = new ItemBuilder(SkullUtils.getSkull(getSkin()))
.setName(title)
.addLoreLine(getColor() + "Price to Place: &f$" + NumberUtils.formatMoney(getPlacePrice()))
.addLoreLine(getColor() + "Price to Mine: &f$" + NumberUtils.formatMoney(getMinePrice()))
.addLoreLine("")
.addLore(getLore())
.addLoreLine("")
.addLoreLine("&7Place custom block to use!")
.colorize()
.build();
NBTItem nbtItem = new NBTItem(block);
NBTCompound cosmicData = nbtItem.getOrCreateCompound("cosmicData");
NBTCompound cosmicData2 = cosmicData.getOrCreateCompound("customBlockData");
nbtItem.setString("cosmicType", "custom_block");
if (level != 0) {
cosmicData2.setInteger("level", level);
}
cosmicData2.setString("type", name());
//cosmicData.setNBTBase("customBlockData", cosmicData2.get());
//nbtItem.setNBTBase("cosmicData", cosmicData.get());
return nbtItem.getItem();
}
}
@@ -0,0 +1,11 @@
package net.crankedpvp.customblocks.struct.abilities;
import org.bukkit.entity.Player;
public interface ActivateAble {
boolean isActive();
void activate(Player player);
double getActivationPrice();
void setLastActivated(long lastActivated);
}
@@ -0,0 +1,7 @@
package net.crankedpvp.customblocks.struct.abilities;
public interface LevelAble {
int getMaxLevel();
int getLevel();
void setLevel(int lvl);
}
@@ -0,0 +1,5 @@
package net.crankedpvp.customblocks.struct.abilities;
public interface TickAble {
void onTick();
}
@@ -0,0 +1,147 @@
package net.crankedpvp.customblocks.struct.impl;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
import net.crankedpvp.customblocks.CustomBlocks;
import net.crankedpvp.customblocks.guis.AutoGrinderGui;
import net.crankedpvp.customblocks.struct.CustomBlock;
import net.crankedpvp.customblocks.struct.CustomBlockType;
import net.crankedpvp.customblocks.struct.abilities.ActivateAble;
import net.crankedpvp.customblocks.struct.abilities.LevelAble;
import net.crankedpvp.customblocks.struct.abilities.TickAble;
import net.crankedpvp.customblocks.utils.config.ConfigManager;
import net.crankedpvp.cratekeys.enums.CrateKeyType;
import org.bukkit.entity.Player;
import java.util.HashMap;
import java.util.Map;
@Getter
@Setter
public class AutoGrinder extends CustomBlock implements LevelAble, TickAble, ActivateAble {
private Integer exp;
private Map<CrateKeyType, Integer> keys;
private Integer level;
private Long lastActivated;
public AutoGrinder() {
super(CustomBlockType.AUTO_GRINDER);
this.level = 0;
this.exp = 0;
this.keys = new HashMap<>();
this.lastActivated = 0L;
}
@SneakyThrows
@Override
public void onClick(Player clicker) {
AutoGrinderGui autoGrinderGui = new AutoGrinderGui(clicker, this);
autoGrinderGui.getGui().open(clicker);
}
@Override
public int getMaxLevel() {
return 5;
}
@Override
public int getLevel() {
return level;
}
@Override
public void setLevel(int lvl) {
this.level = lvl;
}
@Override
public void onTick() {
if (!isActive()) return;
double commonKeyChance = CustomBlocks.getInstance().getConfigManager().getConfig(ConfigManager.ConfigFile.SETTINGS).getDouble("commonKeyChance") * 0.01 * level;
double rareKeyChance = CustomBlocks.getInstance().getConfigManager().getConfig(ConfigManager.ConfigFile.SETTINGS).getDouble("rareKeyChance") * 0.01 * level;
double mythicKeyChance = CustomBlocks.getInstance().getConfigManager().getConfig(ConfigManager.ConfigFile.SETTINGS).getDouble("mythicKeyChance") * 0.01 * level;
int expPer = CustomBlocks.getInstance().getConfigManager().getConfig(ConfigManager.ConfigFile.SETTINGS).getInt("expPer") * level;
exp += expPer;
if (Math.random() < mythicKeyChance) {
if (keys.getOrDefault(CrateKeyType.MYTHIC_KEY, 0) >= getMaxKeys(level)) return;
addKey(CrateKeyType.MYTHIC_KEY);
} else if (Math.random() < rareKeyChance) {
if (keys.getOrDefault(CrateKeyType.ABYSS_KEY, 0) >= getMaxKeys(level)) return;
addKey(CrateKeyType.ABYSS_KEY);
} else if (Math.random() < commonKeyChance) {
if (keys.getOrDefault(CrateKeyType.SEASON_KEY, 0) >= getMaxKeys(level)) return;
addKey(CrateKeyType.SEASON_KEY);
}
}
@Override
public boolean isActive() {
return System.currentTimeMillis() < lastActivated + 3600000;
}
@Override
public void activate(Player player) {
this.lastActivated = System.currentTimeMillis();
}
@Override
public double getActivationPrice() {
return CustomBlocks.getInstance().getConfigManager().getConfig(ConfigManager.ConfigFile.SETTINGS).getInt("autoGrinderActivationPrice");
}
@Override
public void setLastActivated(long lastActivated) {
this.lastActivated = lastActivated;
}
public void addKey(CrateKeyType type) {
if (!keys.containsKey(type)) {
keys.put(type, 1);
} else {
int current = keys.get(type);
keys.put(type, current + 1);
}
}
public void removeKey(CrateKeyType type) {
keys.put(type, keys.get(type)-1);
}
public double getUpgradePrice(int currentLevel) {
switch (currentLevel) {
case 1:
return 50000000;
case 2:
return 100000000;
case 3:
return 250000000;
case 4:
return 500000000;
case 5:
return 1000000000;
}
return 5000000;
}
public int getMaxKeys(int level) {
switch (level) {
case 1:
return 512;
case 2:
return 1024;
case 3:
return 2048;
case 4:
return 3456;
case 5:
return 5504;
}
return 512;
}
}
@@ -0,0 +1,25 @@
package net.crankedpvp.customblocks.struct.impl;
import net.crankedpvp.customblocks.guis.CrateKeyOpenerGUI;
import net.crankedpvp.customblocks.struct.CustomBlock;
import net.crankedpvp.customblocks.struct.CustomBlockType;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.List;
public class KeyOpener extends CustomBlock {
private List<ItemStack> items;
public KeyOpener() {
super(CustomBlockType.KEY_OPENER);
this.items = new ArrayList<>();
}
@Override
public void onClick(Player clicker) {
CrateKeyOpenerGUI crateKeyOpenerGUI = new CrateKeyOpenerGUI(this, clicker);
crateKeyOpenerGUI.getCustomBlockGui().open(clicker);
}
}
@@ -0,0 +1,20 @@
package net.crankedpvp.customblocks.struct.impl;
import net.crankedpvp.customblocks.guis.SlotBotOpenerGUI;
import net.crankedpvp.customblocks.struct.CustomBlock;
import net.crankedpvp.customblocks.struct.CustomBlockType;
import org.bukkit.entity.Player;
public class SlotOpener extends CustomBlock {
public SlotOpener() {
super(CustomBlockType.SLOTBOT_OPENER);
}
@Override
public void onClick(Player clicker) {
SlotBotOpenerGUI slotBotOpenerGUI = new SlotBotOpenerGUI(this, clicker);
slotBotOpenerGUI.getCustomBlockGui().open(clicker);
}
}
@@ -0,0 +1,39 @@
package net.crankedpvp.customblocks.struct.typeadapters;
import lombok.SneakyThrows;
import net.crankedpvp.customblocks.struct.CustomBlock;
import net.crankedpvp.customblocks.struct.CustomBlockType;
import net.crankedpvp.storageutils.gson.*;
import java.lang.reflect.Type;
public class CustomBlockDeserializer implements JsonDeserializer<CustomBlock> {
@SneakyThrows
@Override
public CustomBlock deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException {
JsonObject jsonObject = jsonElement.getAsJsonObject();
CustomBlockType cType = CustomBlockType.valueOf(jsonObject.get("type").getAsString());
//CustomBlock customBlock = cType.getCustomBlockClass().newInstance();
//customBlock.setLocation(LocationUtils.getLocationFromString(jsonObject.get("location").getAsString(), false));
/*if (customBlock instanceof LevelAble) {
((LevelAble) customBlock).setLevel(jsonObject.get("level").getAsInt());
}
if (customBlock instanceof ActivateAble) {
((ActivateAble) customBlock).setLastActivated(jsonObject.get("lastActivated").getAsLong());
}
if (customBlock instanceof AutoGrinder) {
((AutoGrinder) customBlock).setGrinderKeys(jsonObject.get("grinderKeys").getAsInt());
((AutoGrinder) customBlock).setExp(jsonObject.get("exp").getAsInt());
}*/
return context.deserialize(jsonElement, cType.getCustomBlockClass());
}
}
@@ -0,0 +1,17 @@
package net.crankedpvp.customblocks.tasks;
import net.crankedpvp.customblocks.CustomBlocks;
import net.crankedpvp.customblocks.struct.CustomBlock;
import net.crankedpvp.customblocks.struct.abilities.TickAble;
import org.bukkit.scheduler.BukkitRunnable;
public class TickAbleBlocksRunnable extends BukkitRunnable {
@Override
public void run() {
for (CustomBlock customBlock : CustomBlocks.getInstance().getCustomBlockManager().getCacheValues()) {
if (customBlock instanceof TickAble) {
((TickAble) customBlock).onTick();
}
}
}
}
@@ -0,0 +1,116 @@
package net.crankedpvp.customblocks.utils;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Chest;
import org.bukkit.craftbukkit.v1_8_R3.CraftChunk;
import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftEntity;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.List;
public class ChunkUtil {
public static boolean isLocationAInSameChunkAsLocationB(Location aLocation, Location bLocation) {
double aX = Math.floor(aLocation.getBlockX() >> 4);
double aZ = Math.floor(aLocation.getBlockZ() >> 4);
double bX = Math.floor(bLocation.getBlockX() >> 4);
double bZ = Math.floor(bLocation.getBlockZ() >> 4);
return aX == bX && aZ == bZ;
}
public static Chunk getIfLoaded(int chunkX, int chunkZ, World bWorld) {
net.minecraft.server.v1_8_R3.World world = ((CraftWorld) bWorld).getHandle();
net.minecraft.server.v1_8_R3.Chunk theChunk = world.getChunkIfLoaded(chunkX, chunkZ);
return (theChunk == null) ? null : theChunk.bukkitChunk;
}
public static List<CraftEntity> getEntities(Chunk chunk) {
net.minecraft.server.v1_8_R3.Chunk nmsChunk = ((CraftChunk) chunk).getHandle();
List<CraftEntity> toReturn = new ArrayList<>();
if (nmsChunk == null) return toReturn;
for (List<net.minecraft.server.v1_8_R3.Entity> entities : nmsChunk.entitySlices) {
for (net.minecraft.server.v1_8_R3.Entity entity : entities) {
if (entity.getBukkitEntity() == null) continue;
toReturn.add(entity.getBukkitEntity());
}
}
return toReturn;
}
public static boolean isEmpty(Inventory inv) {
for (ItemStack item : inv.getContents()) {
if (item != null) {
return false;
}
}
return true;
}
public static List<Location> getNearbyChests(Location location) {
List<Location> chests = new ArrayList<>();
if (location.clone().add(-1, 0, 0).getBlock().getType() == Material.CHEST || location.clone().add(-1, 0, 0).getBlock().getType() == Material.TRAPPED_CHEST) {
Chest chest = (Chest) location.clone().add(-1, 0, 0).getBlock().getState();
chests.add(chest.getLocation());
}
if (location.clone().add(0, 0, -1).getBlock().getType() == Material.CHEST || location.clone().add(0, 0, -1).getBlock().getType() == Material.TRAPPED_CHEST) {
Chest chest = (Chest) location.clone().add(0, 0, -1).getBlock().getState();
chests.add(chest.getLocation());
}
if (location.clone().add(1, 0, 0).getBlock().getType() == Material.CHEST || location.clone().add(1, 0, 0).getBlock().getType() == Material.TRAPPED_CHEST) {
Chest chest = (Chest) location.clone().add(1, 0, 0).getBlock().getState();
chests.add(chest.getLocation());
}
if (location.clone().add(0, 0, 1).getBlock().getType() == Material.CHEST || location.clone().add(0, 0, 1).getBlock().getType() == Material.TRAPPED_CHEST) {
Chest chest = (Chest) location.clone().add(0, 0, 1).getBlock().getState();
chests.add(chest.getLocation());
}
return chests;
}
public static Location getClosestChestLocation(Location location) {
if (location.clone().add(-1, 0, 0).getBlock().getType() == Material.CHEST || location.clone().add(-1, 0, 0).getBlock().getType() == Material.TRAPPED_CHEST) {
Chest chest = (Chest) location.clone().add(-1, 0, 0).getBlock().getState();
if (isEmpty(chest.getInventory())) {
return location.clone().add(-1, 0, 0);
}
}
if (location.clone().add(0, 0, -1).getBlock().getType() == Material.CHEST || location.clone().add(0, 0, -1).getBlock().getType() == Material.TRAPPED_CHEST) {
Chest chest = (Chest) location.clone().add(0, 0, -1).getBlock().getState();
if (isEmpty(chest.getInventory())) {
return location.clone().add(0, 0, -1);
}
}
if (location.clone().add(1, 0, 0).getBlock().getType() == Material.CHEST || location.clone().add(1, 0, 0).getBlock().getType() == Material.TRAPPED_CHEST) {
Chest chest = (Chest) location.clone().add(1, 0, 0).getBlock().getState();
if (isEmpty(chest.getInventory())) {
return location.clone().add(1, 0, 0);
}
}
if (location.clone().add(0, 0, 1).getBlock().getType() == Material.CHEST || location.clone().add(0, 0, 1).getBlock().getType() == Material.TRAPPED_CHEST) {
Chest chest = (Chest) location.clone().add(0, 0, 1).getBlock().getState();
if (isEmpty(chest.getInventory())) {
return location.clone().add(0, 0, 1);
}
}
return null;
}
}
@@ -0,0 +1,31 @@
package net.crankedpvp.customblocks.utils;
import net.crankedpvp.crankedutils.utils.items.ItemUtils;
import net.crankedpvp.cratekeys.CrankedCrateKeysAPI;
import net.crankedpvp.cratekeys.enums.CrateKeyType;
import net.crankedpvp.cratekeys.items.CrateKeyItem;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import java.util.HashMap;
public class CrateKeyAPI {
public static HashMap<CrateKeyType, Integer> getCrateKeysInInventory(Inventory inventory) {
HashMap<CrateKeyType, Integer> keys = new HashMap<>();
for (ItemStack itemStack : inventory) {
if (ItemUtils.isAirOrNull(itemStack)) continue;
if (CrankedCrateKeysAPI.isCrateKey(itemStack)) {
CrateKeyType crateType = CrateKeyItem.getCrateType(itemStack);
if (keys.containsKey(crateType)) {
int amount = keys.get(crateType);
keys.put(crateType, amount + itemStack.getAmount());
} else {
keys.put(crateType, itemStack.getAmount());
}
}
}
return keys;
}
}
@@ -0,0 +1,20 @@
package net.crankedpvp.customblocks.utils;
import net.crankedpvp.cratekeys.CrankedCrateKeysAPI;
import net.crankedpvp.cratekeys.enums.CrateKeyType;
import net.crankedpvp.cratekeys.items.CrateKeyItem;
import net.crankedpvp.slotbot.SlotBotAPI;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
public class SlotbotsAPI {
public static int getSlotsInInv(Player player) {
int slots =0;
for (ItemStack itemStack : player.getInventory()) {
if (SlotBotAPI.isSlotBot(itemStack)) {
slots += itemStack.getAmount();
}
}
return slots;
}
}
@@ -0,0 +1,125 @@
package net.crankedpvp.customblocks.utils;
import java.sql.Timestamp;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public final class TimeUtil {
private TimeUtil() {
throw new RuntimeException("Cannot instantiate a utility class.");
}
public static Timestamp addDuration(long duration) {
return truncateTimestamp(new Timestamp(System.currentTimeMillis() + duration));
}
public static Timestamp truncateTimestamp(Timestamp timestamp) {
if (timestamp.toLocalDateTime().getYear() > 2037) {
timestamp.setYear(2037);
}
return timestamp;
}
public static Timestamp addDuration(Timestamp timestamp) {
return truncateTimestamp(new Timestamp(System.currentTimeMillis() + timestamp.getTime()));
}
public static Timestamp fromMillis(long millis) {
return new Timestamp(millis);
}
public static Timestamp getCurrentTimestamp() {
return new Timestamp(System.currentTimeMillis());
}
public static long getCurrentMillis() {
return System.currentTimeMillis();
}
public static String fancy(long seconds) {
seconds = seconds/1000;
int day = (int) TimeUnit.SECONDS.toDays(seconds);
long hours = TimeUnit.SECONDS.toHours(seconds) - (day * 24);
long minute = TimeUnit.SECONDS.toMinutes(seconds) - (TimeUnit.SECONDS.toHours(seconds) * 60);
long second = TimeUnit.SECONDS.toSeconds(seconds) - (TimeUnit.SECONDS.toMinutes(seconds) * 60);
return hours + "h " + minute + "m " + second + "s ";
}
public static String millisToRoundedTime(long millis) {
millis += 1L; // for good shit men
long seconds = millis / 1000L;
long minutes = seconds / 60L;
long hours = minutes / 60L;
long days = hours / 24L;
long weeks = days / 7L;
long months = weeks / 4L;
long years = months / 12L;
if (years > 0) {
return years + " year" + (years == 1 ? "" : "s");
} else if (months > 0) {
return months + " month" + (months == 1 ? "" : "s");
} else if (weeks > 0) {
return weeks + " week" + (weeks == 1 ? "" : "s");
} else if (days > 0) {
return days + " day" + (days == 1 ? "" : "s");
} else if (hours > 0) {
return hours + " hour" + (hours == 1 ? "" : "s");
} else if (minutes > 0) {
return minutes + " minute" + (minutes == 1 ? "" : "s");
} else {
return seconds + " second" + (seconds == 1 ? "" : "s");
}
}
public static long parseTime(String time) {
long totalTime = 0L;
boolean found = false;
Matcher matcher = Pattern.compile("\\d+\\D+").matcher(time);
while (matcher.find()) {
String s = matcher.group();
Long value = Long.parseLong(s.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)")[0]);
String type = s.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)")[1];
switch (type) {
case "s":
totalTime += value;
found = true;
break;
case "m":
totalTime += value * 60;
found = true;
break;
case "h":
totalTime += value * 60 * 60;
found = true;
break;
case "d":
totalTime += value * 60 * 60 * 24;
found = true;
break;
case "w":
totalTime += value * 60 * 60 * 24 * 7;
found = true;
break;
case "M":
totalTime += value * 60 * 60 * 24 * 30;
found = true;
break;
case "y":
totalTime += value * 60 * 60 * 24 * 365;
found = true;
break;
}
}
return !found ? -1 : totalTime * 1000;
}
}
@@ -0,0 +1,108 @@
package net.crankedpvp.customblocks.utils.config;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class AbstractConfig {
private final JavaPlugin plugin;
private final String path;
private FileConfiguration config;
private final File configFile;
public AbstractConfig(JavaPlugin plugin, String path) {
this.plugin = plugin;
this.path = path;
this.configFile = new File(plugin.getDataFolder(), path);
}
public boolean setupConfigFile() {
boolean copyDefaults = !configFile.exists();
this.saveDefaultConfig();
config = YamlConfiguration.loadConfiguration(configFile);
if (copyDefaults) this.copyDefaults();
boolean result = this.saveConfig();
if (result) this.loadConfigFromDisk();
return result;
}
public boolean saveConfig() {
try {
config.save(configFile);
return true;
} catch (IOException e) {
plugin.getLogger().severe("Could not save config file for " + path + "!");
e.printStackTrace();
}
return false;
}
public void saveDefaultConfig() {
if (!configFile.exists()) {
InputStream defConfig = plugin.getResource(path);
if (defConfig != null) {
plugin.saveResource(path,false);
} else {
try {
configFile.getParentFile().mkdirs();
configFile.createNewFile();
} catch (IOException e) {
plugin.getLogger().severe("Could not create config file for " + path + "!");
e.printStackTrace();
}
}
}
}
public boolean reloadConfig() {
boolean copyDefaults = !configFile.exists();
this.saveDefaultConfig();
config = YamlConfiguration.loadConfiguration(configFile);
if (copyDefaults) this.copyDefaults();
this.saveConfig();
return this.loadConfigFromDisk();
}
public boolean loadConfigFromDisk() {
try {
config.load(configFile);
return true;
} catch (IOException | InvalidConfigurationException e) {
plugin.getLogger().severe("Could not load config for " + path + "!");
e.printStackTrace();
}
return false;
}
public void copyDefaults() {
InputStream defConfig = plugin.getResource(path);
if (defConfig != null) {
config.setDefaults(YamlConfiguration.loadConfiguration(new InputStreamReader(defConfig)));
}
config.options().copyDefaults(true);
}
public FileConfiguration getConfig() {
return config;
}
public String getPath() {
return path;
}
}
@@ -0,0 +1,51 @@
package net.crankedpvp.customblocks.utils.config;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.HashMap;
import java.util.Map;
public class ConfigManager {
@Getter
private final Map<ConfigFile, AbstractConfig> configs = new HashMap<>();
public ConfigManager(JavaPlugin plugin) {
for (ConfigFile configFile : ConfigFile.values()) {
AbstractConfig abstractConfig = new AbstractConfig(plugin, configFile.getFileName());
configs.put(configFile, abstractConfig);
}
}
public void setupFiles() {
configs.values().forEach(AbstractConfig::setupConfigFile);
}
public void reloadFiles() {
configs.values().forEach(AbstractConfig::reloadConfig);
}
public AbstractConfig getAbstractConfig(ConfigFile configFile) {
return configs.get(configFile);
}
public FileConfiguration getConfig(ConfigFile configFile) {
return getAbstractConfig(configFile).getConfig();
}
@Getter
@RequiredArgsConstructor
public enum ConfigFile {
SETTINGS("settings.yml"),
;
private final String fileName;
}
}