Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #1 #14

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,21 @@
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.configuration.Configuration;
import org.bukkit.configuration.file.FileConfiguration;
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.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.inventory.InventoryMoveItemEvent;
import org.bukkit.event.inventory.*;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitTask;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.*;
import java.util.stream.Collectors;

public class BukkitListener implements Listener {
Expand Down Expand Up @@ -65,13 +59,24 @@ public void onClick(InventoryClickEvent event) {
Player player = (Player) event.getWhoClicked();

BukkitVaultRegistry registry = (BukkitVaultRegistry) plugin.getRegistry();
ItemStack item = event.getCurrentItem();
ArrayList<ItemStack> items = new ArrayList<>();
items.add(event.getCurrentItem());
Inventory inventory = event.getInventory();

if (inventory != null && item != null && isBlacklistEnabled()) {
if (!permission.canBypassBlacklist(player) && getBlacklisted().contains(item.getType()) && registry.isVault(inventory)) {
player.sendMessage(plugin.getLanguage().get(Lang.BLACKLISTED_ITEM));
event.setCancelled(true);
if(event.getClick() == ClickType.NUMBER_KEY) {
items.add(player.getInventory().getItem(event.getHotbarButton()));
}

if (inventory != null && isBlacklistEnabled()) {
if(permission.canBypassBlacklist(player)) return;
if(!registry.isVault(inventory)) return;

for(ItemStack item : items) {
if (item != null && getBlacklisted().contains(item.getType())) {
player.sendMessage(plugin.getLanguage().get(Lang.BLACKLISTED_ITEM));
event.setCancelled(true);
return;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.github.dig.endervaults.bukkit.ui.icon;

import com.github.dig.endervaults.api.selector.SelectorMode;
import com.github.dig.endervaults.api.vault.Vault;
import de.tr7zw.changeme.nbtapi.NBTItem;
import org.bukkit.Material;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

public class SelectIconBuilder {


public static NBTItem fromMaterial(Vault vault, Material material) {
return fromMaterial(vault, material, SelectorMode.STATIC);
}

public static NBTItem fromMaterial(Vault vault, Material material, SelectorMode selectorMode) {
ItemStack item = new ItemStack(material, 1);
ItemMeta meta = item.getItemMeta();
meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES,
ItemFlag.HIDE_UNBREAKABLE,
ItemFlag.HIDE_ENCHANTS,
ItemFlag.HIDE_DESTROYS,
ItemFlag.HIDE_POTION_EFFECTS,
ItemFlag.HIDE_PLACED_ON);
item.setItemMeta(meta);

NBTItem nbtItem = new NBTItem(item);
nbtItem.setBoolean(SelectIconConstants.NBT_ICON_ITEM, true);
nbtItem.setString(SelectIconConstants.NBT_ICON_ID, vault.getId().toString());
nbtItem.setString(SelectIconConstants.NBT_ICON_OWNER_UUID, vault.getOwner().toString());
nbtItem.setString(SelectIconConstants.NBT_ICON_SELECTOR_MODE, selectorMode.name());
return nbtItem;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ public class SelectIconConstants {
public final static String NBT_ICON_ITEM = "vaulticonitem";
public final static String NBT_ICON_ID = "vaulticonid";
public final static String NBT_ICON_OWNER_UUID = "vaulticonowneruuid";

public final static String NBT_ICON_SELECTOR_MODE = "vaulticonselectormode";
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.dig.endervaults.api.VaultPluginProvider;
import com.github.dig.endervaults.api.lang.Lang;
import com.github.dig.endervaults.api.selector.SelectorMode;
import com.github.dig.endervaults.api.vault.Vault;
import com.github.dig.endervaults.bukkit.EVBukkitPlugin;
import de.tr7zw.changeme.nbtapi.NBTItem;
Expand Down Expand Up @@ -35,36 +36,35 @@ public SelectIconInventory(Vault vault) {
}

private void init() {
int slot;
Material material;
for (String matName : configuration.getStringList("selector.select-icon.items")) {
Material material;
try {
material = Material.valueOf(matName);
} catch (IllegalArgumentException e) {
log.log(Level.SEVERE, "[EnderVaults] Unable to find material " + matName + ", skipping...", e);
continue;
}

ItemStack item = new ItemStack(material, 1);
ItemMeta meta = item.getItemMeta();
meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES,
ItemFlag.HIDE_UNBREAKABLE,
ItemFlag.HIDE_ENCHANTS,
ItemFlag.HIDE_DESTROYS,
ItemFlag.HIDE_POTION_EFFECTS,
ItemFlag.HIDE_PLACED_ON);
item.setItemMeta(meta);

NBTItem nbtItem = new NBTItem(item);
nbtItem.setBoolean(SelectIconConstants.NBT_ICON_ITEM, true);
nbtItem.setString(SelectIconConstants.NBT_ICON_ID, vault.getId().toString());
nbtItem.setString(SelectIconConstants.NBT_ICON_OWNER_UUID, vault.getOwner().toString());

int slot = inventory.firstEmpty();
if (slot > -1) {
inventory.setItem(inventory.firstEmpty(), nbtItem.getItem());
} else {
log.log(Level.INFO, "[EnderVaults] Unable to find available spot to put item in, please reconfigure select icon settings.");
slot = inventory.firstEmpty();
addItemAt(SelectIconBuilder.fromMaterial(vault, material, SelectorMode.STATIC), slot);
}
for (String matName : configuration.getStringList("selector.select-icon.pane_fill_item")) {
try {
material = Material.valueOf(matName);
} catch (IllegalArgumentException e) {
log.log(Level.SEVERE, "[EnderVaults] Unable to find material " + matName + ", skipping...", e);
continue;
}
slot = inventory.firstEmpty();
addItemAt(SelectIconBuilder.fromMaterial(vault, material, SelectorMode.PANE_BY_FILL), slot);
}
}

private void addItemAt(NBTItem nbtItem, int slot) {
if (slot > -1) {
inventory.setItem(inventory.firstEmpty(), nbtItem.getItem());
} else {
log.log(Level.INFO, "[EnderVaults] Unable to find available spot to put item in, please reconfigure select icon settings.");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.github.dig.endervaults.bukkit.ui.icon;

import com.github.dig.endervaults.api.VaultPluginProvider;
import com.github.dig.endervaults.api.selector.SelectorMode;
import com.github.dig.endervaults.api.vault.Vault;
import com.github.dig.endervaults.api.vault.VaultRegistry;
import com.github.dig.endervaults.api.vault.metadata.VaultDefaultMetadata;
import com.github.dig.endervaults.bukkit.ui.selector.SelectorInventory;
Expand Down Expand Up @@ -33,8 +35,18 @@ public void onClick(InventoryClickEvent event) {
event.setCancelled(true);
UUID vaultID = UUID.fromString(nbtItem.getString(SelectIconConstants.NBT_ICON_ID));
UUID vaultOwnerUUID = UUID.fromString(nbtItem.getString(SelectIconConstants.NBT_ICON_OWNER_UUID));
SelectorMode selectorMode = SelectorMode.valueOf(nbtItem.getString(SelectIconConstants.NBT_ICON_SELECTOR_MODE));

registry.get(vaultOwnerUUID, vaultID).ifPresent(vault -> vault.getMetadata().put(VaultDefaultMetadata.ICON.getKey(), item.getType().toString()));
switch(selectorMode) {
case STATIC: {
registry.get(vaultOwnerUUID, vaultID).ifPresent(vault -> vault.getMetadata().put(VaultDefaultMetadata.ICON.getKey(), item.getType().toString()));
break;
}
case PANE_BY_FILL: {
registry.get(vaultOwnerUUID, vaultID).ifPresent(vault -> vault.getMetadata().remove(VaultDefaultMetadata.ICON.getKey()));
break;
}
}
new SelectorInventory(vaultOwnerUUID, 1).launchFor(player);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
Expand All @@ -28,7 +29,7 @@
import java.util.stream.Collectors;

@Log
public class SelectorInventory {
public class SelectorInventory implements InventoryHolder {

private final EVBukkitPlugin plugin = (EVBukkitPlugin) VaultPluginProvider.getPlugin();
private final VaultRegistry registry = plugin.getRegistry();
Expand All @@ -49,7 +50,7 @@ public SelectorInventory(UUID ownerUUID, int page) {
this.page = page;
this.mode = SelectorMode.valueOf(
configuration.getString("selector.design-mode", SelectorMode.PANE_BY_FILL.toString()));
this.inventory = Bukkit.createInventory(null, size,
this.inventory = Bukkit.createInventory(this, size,
plugin.getLanguage().get(Lang.VAULT_SELECTOR_TITLE));

init();
Expand All @@ -62,7 +63,7 @@ public SelectorInventory(UUID ownerUUID, int page, String title) {
this.ownerUUID = ownerUUID;
this.page = page;
this.mode = SelectorMode.valueOf(configuration.getString("selector.design-mode", SelectorMode.PANE_BY_FILL.toString()));
this.inventory = Bukkit.createInventory(null, size, title);
this.inventory = Bukkit.createInventory(this, size, title);

init();
}
Expand Down Expand Up @@ -256,4 +257,9 @@ private ItemStack createLockedItem() {
public void launchFor(Player player) {
player.openInventory(inventory);
}

@Override
public Inventory getInventory() {
return this.inventory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.inventory.InventoryInteractEvent;
import org.bukkit.event.inventory.InventoryMoveItemEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
Expand All @@ -38,6 +40,10 @@ public void onClick(InventoryClickEvent event) {
ItemStack item = event.getCurrentItem();
ClickType type = event.getClick();

if (event.getView().getTopInventory().getHolder() instanceof SelectorInventory) {
event.setCancelled(true);
}

if (inventory != null && item != null && item.getType() != Material.AIR) {
NBTItem nbtItem = new NBTItem(item);
if (nbtItem.hasKey(SelectorConstants.NBT_VAULT_ITEM)) {
Expand Down Expand Up @@ -93,4 +99,11 @@ public void onMove(InventoryMoveItemEvent event) {
}
}
}

@EventHandler(priority = EventPriority.HIGH)
public void onDrag(InventoryDragEvent event) {
if (event.getView().getTopInventory().getHolder() instanceof SelectorInventory) {
event.setCancelled(true);
}
}
}
4 changes: 3 additions & 1 deletion bukkit/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ selector:
- ARROW
- APPLE
- WHEAT
# Item designs, how the items look etc.
# Item designs, how the items look etc.
pane_fill_item:
- WHITE_STAINED_GLASS_PANE
template:
# Unlocked vault item
unlocked:
Expand Down