Rewrite reward inventories; resolves #226; resolves #257; resolves #261; resolves #322

This commit is contained in:
Daniel Saukel 2017-10-05 13:21:20 +02:00
parent 3dad23c216
commit 29c7d5126d
13 changed files with 361 additions and 262 deletions

View File

@ -51,7 +51,7 @@ If you want to learn how to use DungeonsXL step by step, please have a look at t
DungeonsXL works with 1.7.8 and higher. However, support for 1.12 / 1.11.x / 1.10.x / 1.9.x has a higher priority than support for 1.8.x and lower. See [here](../../wiki/legacy-support) for detailed information. Some cosmetic features require the Spigot API and will therefore not work with CraftBukkit.
Older versions of DungeonsXL support versions since Minecraft 1.3.x, but of course, they are completely unsupported.
* [1.7.8-1.12](../../tree/master)
* [1.7.8-1.12.2](../../tree/master)
* [1.7.5](../../tree/50f772d14281bfe278dba2559d1758cc459c1a30)
* [1.7.2](../../tree/eccf82b7335dfb0723e3cd37a57df1a968ea7842)
* [1.6.4](../../tree/780145cf783ea76fe1bfee04cf89216bd4f92e1d)

View File

@ -39,12 +39,12 @@ import io.github.dre2n.dungeonsxl.player.DGroup;
import io.github.dre2n.dungeonsxl.player.DPermissions;
import io.github.dre2n.dungeonsxl.player.DPlayerCache;
import io.github.dre2n.dungeonsxl.requirement.RequirementTypeCache;
import io.github.dre2n.dungeonsxl.reward.DLootInventory;
import io.github.dre2n.dungeonsxl.reward.RewardTypeCache;
import io.github.dre2n.dungeonsxl.sign.DSignTypeCache;
import io.github.dre2n.dungeonsxl.sign.SignScripts;
import io.github.dre2n.dungeonsxl.sign.SignScriptCache;
import io.github.dre2n.dungeonsxl.trigger.TriggerTypeCache;
import io.github.dre2n.dungeonsxl.util.NoReload;
import io.github.dre2n.dungeonsxl.util.PageGUICache;
import io.github.dre2n.dungeonsxl.world.DWorldCache;
import java.io.File;
import java.util.List;
@ -92,10 +92,10 @@ public class DungeonsXL extends DREPlugin {
private DClasses dClasses;
private DLootTableCache dLootTables;
private DMobTypeCache dMobTypes;
private SignScripts signScripts;
private SignScriptCache signScripts;
private DWorldCache dWorlds;
private PageGUICache pageGUIs;
private CopyOnWriteArrayList<DLootInventory> dLootInventories = new CopyOnWriteArrayList<>();
private CopyOnWriteArrayList<Game> games = new CopyOnWriteArrayList<>();
private CopyOnWriteArrayList<DGroup> dGroups = new CopyOnWriteArrayList<>();
@ -142,8 +142,6 @@ public class DungeonsXL extends DREPlugin {
dPlayer.leave();
}
// Delete all Data
dLootInventories.clear();
dGroups.clear();
// Delete DWorlds
@ -245,6 +243,7 @@ public class DungeonsXL extends DREPlugin {
loadDMobTypes(MOBS);
loadSignScripts(SIGNS);
loadDCommandCache();
loadPageGUICache();
}
// Save and load
@ -342,7 +341,7 @@ public class DungeonsXL extends DREPlugin {
}
/**
* load / reload a new instance of DSignTypes
* load / reload a new instance of DSignTypeCache
*/
public void loadDSigns() {
dSigns = new DSignTypeCache();
@ -356,7 +355,7 @@ public class DungeonsXL extends DREPlugin {
}
/**
* load / reload a new instance of GameTypes
* load / reload a new instance of GameTypeCache
*/
public void loadGameTypes() {
gameTypes = new GameTypeCache();
@ -370,7 +369,7 @@ public class DungeonsXL extends DREPlugin {
}
/**
* load / reload a new instance of RequirementTypes
* load / reload a new instance of RequirementTypeCache
*/
public void loadRequirementTypes() {
requirementTypes = new RequirementTypeCache();
@ -384,7 +383,7 @@ public class DungeonsXL extends DREPlugin {
}
/**
* load / reload a new instance of RewardTypes
* load / reload a new instance of RewardTypeCache
*/
public void loadRewardTypes() {
rewardTypes = new RewardTypeCache();
@ -398,157 +397,165 @@ public class DungeonsXL extends DREPlugin {
}
/**
* load / reload a new instance of TriggerTypes
* load / reload a new instance of TriggerTypeCache
*/
public void loadTriggers() {
triggers = new TriggerTypeCache();
}
/**
* @return the loaded instance of Dungeons
* @return the loaded instance of DungeonCache
*/
public DungeonCache getDungeons() {
return dungeons;
}
/**
* load / reload a new instance of Dungeons
* load / reload a new instance of DungeonCache
*/
public void loadDungeons(File file) {
dungeons = new DungeonCache(file);
}
/**
* @return the loaded instance of GlobalProtections
* @return the loaded instance of GlobalProtectionCache
*/
public GlobalProtectionCache getGlobalProtections() {
return protections;
}
/**
* load / reload a new instance of GlobalProtections
* load / reload a new instance of GlobalProtectionCache
*/
public void loadGlobalProtections() {
protections = new GlobalProtectionCache();
}
/**
* @return the loaded instance of ExternalMobProviders
* @return the loaded instance of ExternalMobProviderCache
*/
public ExternalMobProviderCache getExternalMobProviders() {
return dMobProviders;
}
/**
* load / reload a new instance of ExternalMobProviders
* load / reload a new instance of ExternalMobProviderCache
*/
public void loadExternalMobProviders() {
dMobProviders = new ExternalMobProviderCache();
}
/**
* @return the loaded instance of DPlayers
* @return the loaded instance of DPlayerCache
*/
public DPlayerCache getDPlayers() {
return dPlayers;
}
/**
* load / reload a new instance of DPlayers
* load / reload a new instance of DPlayerCache
*/
public void loadDPlayers() {
dPlayers = new DPlayerCache();
}
/**
* @return the loaded instance of Announcers
* @return the loaded instance of AnnouncerCache
*/
public AnnouncerCache getAnnouncers() {
return announcers;
}
/**
* load / reload a new instance of Announcers
* load / reload a new instance of AnnouncerCache
*/
public void loadAnnouncers(File file) {
announcers = new AnnouncerCache(file);
}
/**
* @return the loaded instance of DClasses
* @return the loaded instance of DClasseCache
*/
public DClasses getDClasses() {
return dClasses;
}
/**
* load / reload a new instance of DClasses
* load / reload a new instance of DClasseCache
*/
public void loadDClasses(File file) {
dClasses = new DClasses(file);
}
/**
* @return the loaded instance of DLootTables
* @return the loaded instance of DLootTableCache
*/
public DLootTableCache getDLootTables() {
return dLootTables;
}
/**
* load / reload a new instance of DLootTables
* load / reload a new instance of DLootTableCache
*/
public void loadDLootTables(File file) {
dLootTables = new DLootTableCache(file);
}
/**
* @return the loaded instance of DMobTypes
* @return the loaded instance of DMobTypeCache
*/
public DMobTypeCache getDMobTypes() {
return dMobTypes;
}
/**
* load / reload a new instance of DMobTypes
* load / reload a new instance of DMobTypeCache
*/
public void loadDMobTypes(File file) {
dMobTypes = new DMobTypeCache(file);
}
/**
* @return the loaded instance of SignScripts
* @return the loaded instance of SignScriptCache
*/
public SignScripts getSignScripts() {
public SignScriptCache getSignScripts() {
return signScripts;
}
/**
* load / reload a new instance of SignScripts
* load / reload a new instance of SignScriptCache
*/
public void loadSignScripts(File file) {
signScripts = new SignScripts(file);
signScripts = new SignScriptCache(file);
}
/**
* @return the loaded instance of DWorlds
* @return the loaded instance of DWorldCache
*/
public DWorldCache getDWorlds() {
return dWorlds;
}
/**
* load / reload a new instance of DWorlds
* load / reload a new instance of DWorldCache
*/
public void loadDWorlds(File folder) {
dWorlds = new DWorldCache(MAPS);
}
public PageGUICache getPageGUICache() {
return pageGUIs;
}
/**
* @return the dLootInventories
* load / reload a new instance of PageGUICache
*/
public List<DLootInventory> getDLootInventories() {
return dLootInventories;
public void loadPageGUICache() {
if (pageGUIs != null) {
HandlerList.unregisterAll(pageGUIs);
}
pageGUIs = new PageGUICache();
manager.registerEvents(pageGUIs, this);
}
/**

View File

@ -54,7 +54,6 @@ public class DCommandCache extends DRECommandCache {
public static DRECommand RELOAD = CompatibilityHandler.getInstance().isSpigot() ? new ReloadCommand() : new ReloadCommandNoSpigot();
public static RenameCommand RENAME = new RenameCommand();
public static ResourcePackCommand RESOURCE_PACK = new ResourcePackCommand();
public static RewardsCommand REWARDS = new RewardsCommand();
public static SaveCommand SAVE = new SaveCommand();
public static StatusCommand STATUS = new StatusCommand();
public static TestCommand TEST = new TestCommand();
@ -85,7 +84,6 @@ public class DCommandCache extends DRECommandCache {
RELOAD,
RENAME,
RESOURCE_PACK,
REWARDS,
SAVE,
STATUS,
TEST,

View File

@ -1,64 +0,0 @@
/*
* Copyright (C) 2012-2017 Frank Baumann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.github.dre2n.dungeonsxl.command;
import io.github.dre2n.commons.chat.MessageUtil;
import io.github.dre2n.commons.command.DRECommand;
import io.github.dre2n.dungeonsxl.DungeonsXL;
import io.github.dre2n.dungeonsxl.config.DMessage;
import io.github.dre2n.dungeonsxl.player.DGlobalPlayer;
import io.github.dre2n.dungeonsxl.player.DPermissions;
import io.github.dre2n.dungeonsxl.reward.DLootInventory;
import java.util.List;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
/**
* @author Daniel Saukel
*/
public class RewardsCommand extends DRECommand {
public RewardsCommand() {
setCommand("rewards");
setMinArgs(0);
setMaxArgs(0);
setHelp(DMessage.HELP_CMD_REWARDS.getMessage());
setPermission(DPermissions.REWARDS.getNode());
setPlayerCommand(true);
}
@Override
public void onExecute(String[] args, CommandSender sender) {
Player player = (Player) sender;
DGlobalPlayer dGlobalPlayer = DungeonsXL.getInstance().getDPlayers().getByPlayer(player);
if (!dGlobalPlayer.hasRewardItemsLeft()) {
MessageUtil.sendMessage(player, DMessage.ERROR_NO_REWARDS_LEFT.getMessage());
return;
}
List<ItemStack> rewardItems = dGlobalPlayer.getRewardItems();
List<ItemStack> rewards = rewardItems.subList(0, rewardItems.size() > 54 ? 53 : rewardItems.size());
new DLootInventory(player, rewards.toArray(new ItemStack[54]));
rewardItems.removeAll(rewards);
if (rewardItems.isEmpty()) {
dGlobalPlayer.setRewardItems(null);
}
}
}

View File

@ -102,7 +102,6 @@ public enum DMessage implements Message {
ERROR_REQUIREMENTS("Error_Requirements", "&4You don't fulfill the requirements for this dungeon!"),
ERROR_SIGN_WRONG_FORMAT("Error_SignWrongFormat", "&4The sign is not written correctly!"),
ERROR_TOO_MANY_INSTANCES("Error_TooManyInstances", "&4There are currently too many maps instantiated. Try it again in a few minutes!"),
ERROR_TOO_MANY_REWARDS("Error_TooManyRewards", "&6You won too many rewards to show all of them in one inventory. Use &4/dxl rewards &6to receive the other ones."),
ERROR_TOO_MANY_TUTORIALS("Error_TooManyTutorials", "&4There are currently too many tutorials running. Try it again in a few minutes!"),
ERROR_TUTORIAL_NOT_EXIST("Error_TutorialNotExist", "&4Tutorial dungeon does not exist!"),
HELP_CMD_BREAK("Help_Cmd_Break", "/dxl break - Break a block protected by DungeonsXL"),
@ -176,8 +175,10 @@ public enum DMessage implements Message {
LOG_NEW_MAP("Log_NewDungeon", "&6Creating new map."),
LOG_NEW_PLAYER_DATA("Log_NewPlayerData", "&6A new player data file has been created and saved as &v1."),
LOG_WORLD_GENERATION_FINISHED("Log_WorldGenerationFinished", "&6World generation finished!"),
MISC_NEXT_PAGE("Misc_NextPage", "&6&lNEXT PAGE"),
MISC_NO("Misc_No", "&4[ NO ]"),
MISC_OKAY("Misc_Okay", "&a[ OK ]"),
MISC_PREVIOUS_PAGE("Misc_PreviousPage", "&6&lPREVIOUS PAGE"),
MISC_UNLIMITED("Misc_Unlimited", "unlimited"),
MISC_YES("Misc_Yes", "&a[ YES ]"),
PLAYER_BLOCK_INFO("Player_BlockInfo", "&6Block ID: &2&v1"),

View File

@ -29,8 +29,8 @@ import io.github.dre2n.dungeonsxl.global.DPortal;
import io.github.dre2n.dungeonsxl.world.DGameWorld;
import io.github.dre2n.dungeonsxl.world.DResourceWorld;
import java.io.File;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
@ -58,7 +58,7 @@ public class DGlobalPlayer implements PlayerWrapper {
private ItemStack[] respawnInventory;
private ItemStack[] respawnArmor;
private CopyOnWriteArrayList<ItemStack> rewardItems;
private List<ItemStack> rewardItems;
public DGlobalPlayer(Player player) {
this(player, false);
@ -276,7 +276,7 @@ public class DGlobalPlayer implements PlayerWrapper {
/**
* @return the reward items
*/
public CopyOnWriteArrayList<ItemStack> getRewardItems() {
public List<ItemStack> getRewardItems() {
return rewardItems;
}
@ -291,7 +291,7 @@ public class DGlobalPlayer implements PlayerWrapper {
* @param rewardItems
* the reward items to set
*/
public void setRewardItems(CopyOnWriteArrayList<ItemStack> rewardItems) {
public void setRewardItems(List<ItemStack> rewardItems) {
this.rewardItems = rewardItems;
}

View File

@ -1,127 +0,0 @@
/*
* Copyright (C) 2012-2017 Frank Baumann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.github.dre2n.dungeonsxl.reward;
import io.github.dre2n.dungeonsxl.DungeonsXL;
import io.github.dre2n.dungeonsxl.config.DMessage;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack;
/**
* @author Frank Baumann, Daniel Saukel
*/
public class DLootInventory {
private Inventory inventory;
private InventoryView inventoryView;
private Player player;
public DLootInventory(Player player, ItemStack[] itemStacks) {
DungeonsXL.getInstance().getDLootInventories().add(this);
inventory = Bukkit.createInventory(player, 54, ChatColor.translateAlternateColorCodes('&', DMessage.PLAYER_TREASURES.getMessage()));
for (ItemStack itemStack : itemStacks) {
if (itemStack != null) {
inventory.addItem(itemStack);
}
}
this.player = player;
}
/* Getters and setters */
/**
* @return the inventory
*/
public Inventory getInventory() {
return inventory;
}
/**
* @param inventory
* the inventory to set
*/
public void setInventory(Inventory inventory) {
this.inventory = inventory;
}
/**
* @return the inventoryView
*/
public InventoryView getInventoryView() {
return inventoryView;
}
/**
* @param inventoryView
* the inventoryView to set
*/
public void setInventoryView(InventoryView inventoryView) {
this.inventoryView = inventoryView;
}
/**
* @return the player
*/
public Player getPlayer() {
return player;
}
/**
* @param player
* the player to set
*/
public void setPlayer(Player player) {
this.player = player;
}
private long time;
/**
* @return the time
*/
public long getTime() {
return time;
}
/**
* @param time
* the time to set
*/
public void setTime(long time) {
this.time = time;
}
/* Statics */
/**
* @param player
* the player whose DLootIntentory will be returned
*/
public static DLootInventory getByPlayer(Player player) {
for (DLootInventory inventory : DungeonsXL.getInstance().getDLootInventories()) {
if (inventory.player == player) {
return inventory;
}
}
return null;
}
}

View File

@ -16,12 +16,9 @@
*/
package io.github.dre2n.dungeonsxl.reward;
import io.github.dre2n.commons.chat.MessageUtil;
import io.github.dre2n.dungeonsxl.config.DMessage;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
@ -38,16 +35,16 @@ public class ItemReward extends Reward {
/**
* @return the reward items
*/
public ItemStack[] getItems() {
return items.toArray(new ItemStack[items.size()]);
public List<ItemStack> getItems() {
return items;
}
/**
* @param items
* the reward items to set
*/
public void setItems(ItemStack[] items) {
this.items = Arrays.asList(items);
public void setItems(List<ItemStack> items) {
this.items = items;
}
/**
@ -74,14 +71,7 @@ public class ItemReward extends Reward {
/* Actions */
@Override
public void giveTo(Player player) {
if (items.size() <= 54) {
new DLootInventory(player, getItems());
} else {
new DLootInventory(player, items.subList(0, 54).toArray(new ItemStack[54]));
plugin.getDPlayers().getByPlayer(player).setRewardItems(new CopyOnWriteArrayList<>(items.subList(54, items.size())));
MessageUtil.sendMessage(player, DMessage.ERROR_TOO_MANY_REWARDS.getMessage());
}
plugin.getDPlayers().getByPlayer(player).setRewardItems(items);
}
}

View File

@ -17,7 +17,10 @@
package io.github.dre2n.dungeonsxl.reward;
import io.github.dre2n.dungeonsxl.DungeonsXL;
import io.github.dre2n.dungeonsxl.config.DMessage;
import io.github.dre2n.dungeonsxl.player.DGlobalPlayer;
import io.github.dre2n.dungeonsxl.player.DPermissions;
import io.github.dre2n.dungeonsxl.util.PageGUI;
import io.github.dre2n.dungeonsxl.world.DEditWorld;
import io.github.dre2n.dungeonsxl.world.DGameWorld;
import io.github.dre2n.dungeonsxl.world.block.RewardChest;
@ -27,7 +30,6 @@ import org.bukkit.block.Chest;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.PlayerMoveEvent;
@ -40,8 +42,8 @@ import org.bukkit.inventory.ItemStack;
public class RewardListener implements Listener {
DungeonsXL plugin = DungeonsXL.getInstance();
@EventHandler
/*@EventHandler
public void onInventoryClose(InventoryCloseEvent event) {
if (!(event.getPlayer() instanceof Player)) {
return;
@ -49,7 +51,7 @@ public class RewardListener implements Listener {
Player player = (Player) event.getPlayer();
for (DLootInventory inventory : plugin.getDLootInventories()) {
if (event.getView() != inventory.getInventoryView()) {
if (PageGUI.getByInventory() != inventory.getInventory()) {
continue;
}
@ -65,8 +67,7 @@ public class RewardListener implements Listener {
plugin.getDLootInventories().remove(inventory);
}
}
}*/
@EventHandler
public void onInventoryOpen(InventoryOpenEvent event) {
if (!(event.getPlayer() instanceof Player)) {
@ -107,15 +108,21 @@ public class RewardListener implements Listener {
@EventHandler
public void onPlayerMove(PlayerMoveEvent event) {
Player player = event.getPlayer();
DGlobalPlayer dPlayer = plugin.getDPlayers().getByPlayer(player);
if (plugin.getDWorlds().getInstanceByWorld(player.getWorld()) != null) {
return;
}
DLootInventory inventory = DLootInventory.getByPlayer(player);
if (inventory != null && player.getLocation().getBlock().getRelative(0, 1, 0).getType() != Material.PORTAL && player.getLocation().getBlock().getRelative(0, -1, 0).getType() != Material.PORTAL
if (dPlayer.hasRewardItemsLeft() && player.getLocation().getBlock().getRelative(0, 1, 0).getType() != Material.PORTAL && player.getLocation().getBlock().getRelative(0, -1, 0).getType() != Material.PORTAL
&& player.getLocation().getBlock().getRelative(1, 0, 0).getType() != Material.PORTAL && player.getLocation().getBlock().getRelative(-1, 0, 0).getType() != Material.PORTAL
&& player.getLocation().getBlock().getRelative(0, 0, 1).getType() != Material.PORTAL && player.getLocation().getBlock().getRelative(0, 0, -1).getType() != Material.PORTAL) {
inventory.setInventoryView(player.openInventory(inventory.getInventory()));
inventory.setTime(System.currentTimeMillis());
PageGUI lootInventory = new PageGUI(DMessage.PLAYER_TREASURES.getMessage(), true);
for (ItemStack item : dPlayer.getRewardItems()) {
if (item != null) {
lootInventory.addButton(item);
}
}
lootInventory.open(player);
dPlayer.setRewardItems(null);
}
}

View File

@ -26,11 +26,11 @@ import java.util.List;
*
* @author Daniel Saukel
*/
public class SignScripts {
public class SignScriptCache {
private List<SignScript> scripts = new ArrayList<>();
public SignScripts(File file) {
public SignScriptCache(File file) {
if (file.isDirectory()) {
for (File script : FileUtil.getFilesForFolder(file)) {
scripts.add(new SignScript(script));

View File

@ -0,0 +1,68 @@
/*
* Copyright (C) 2017 Daniel Saukel
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.github.dre2n.dungeonsxl.util;
import io.github.dre2n.commons.item.ItemUtil;
import io.github.dre2n.dungeonsxl.config.DMessage;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionType;
/**
* @author Daniel Saukel
*/
public class GUIButton {
/* Raw skulls */
public static final ItemStack SKULL = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
public static final ItemStack LEFT = ItemUtil.setSkullOwner(SKULL, "69b9a08d-4e89-4878-8be8-551caeacbf2a", "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvM2ViZjkwNzQ5NGE5MzVlOTU1YmZjYWRhYjgxYmVhZmI5MGZiOWJlNDljNzAyNmJhOTdkNzk4ZDVmMWEyMyJ9fX0=");
public static final ItemStack RIGHT = ItemUtil.setSkullOwner(SKULL, "15f49744-9b61-46af-b1c3-71c6261a0d0e", "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvMWI2ZjFhMjViNmJjMTk5OTQ2NDcyYWVkYjM3MDUyMjU4NGZmNmY0ZTgzMjIxZTU5NDZiZDJlNDFiNWNhMTNiIn19fQ==");
/* GUI buttons */
public static final ItemStack NEXT_PAGE = setDisplayName(RIGHT, DMessage.MISC_NEXT_PAGE.getMessage());
public static final ItemStack PREVIOUS_PAGE = setDisplayName(LEFT, DMessage.MISC_PREVIOUS_PAGE.getMessage());
public static final ItemStack PLACEHOLDER = setDisplayName(new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 15), ChatColor.RESET.toString());
/* Blank items that show meta stuff by default */
public static final ItemStack GUI_SWORD = new ItemStack(Material.IRON_SWORD);
public static final ItemStack GUI_WATER_BOTTLE = new ItemStack(Material.POTION);
static {
ItemMeta swordMeta = GUI_SWORD.getItemMeta();
swordMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
GUI_SWORD.setItemMeta(swordMeta);
PotionMeta watMeta = (PotionMeta) GUI_WATER_BOTTLE.getItemMeta();
watMeta.setBasePotionData(new PotionData(PotionType.WATER));
watMeta.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS);
GUI_WATER_BOTTLE.setItemMeta(watMeta);
}
public static ItemStack setDisplayName(ItemStack itemStack, String name) {
itemStack = itemStack.clone();
ItemMeta meta = itemStack.getItemMeta();
meta.setDisplayName(name);
itemStack.setItemMeta(meta);
return itemStack;
}
}

View File

@ -0,0 +1,152 @@
/*
* Copyright (C) 2017 Daniel Saukel
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.github.dre2n.dungeonsxl.util;
import io.github.dre2n.dungeonsxl.DungeonsXL;
import static io.github.dre2n.dungeonsxl.util.GUIButton.*;
import java.util.Stack;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
/**
* @author Daniel Saukel
*/
public class PageGUI {
private String title;
private boolean allowStealing;
private Stack<Inventory> pages = new Stack<>();
public PageGUI(String title) {
this(title, false);
}
public PageGUI(String title, boolean allowStealing) {
this.title = title;
this.allowStealing = allowStealing;
newPage();
DungeonsXL.getInstance().getPageGUICache().guis.add(this);
}
public Inventory newPage() {
Inventory gui = Bukkit.createInventory(null, 54, title);
gui.setItem(45, PREVIOUS_PAGE);
gui.setItem(46, PLACEHOLDER);
gui.setItem(47, PLACEHOLDER);
gui.setItem(48, PLACEHOLDER);
gui.setItem(49, PLACEHOLDER);
gui.setItem(50, PLACEHOLDER);
gui.setItem(51, PLACEHOLDER);
gui.setItem(52, PLACEHOLDER);
gui.setItem(53, NEXT_PAGE);
pages.add(gui);
return gui;
}
public void addButton(ItemStack button) {
if (hasSpace(pages.peek())) {
pages.peek().addItem(button);
} else {
newPage().addItem(button);
}
}
public String getTitle() {
return title;
}
public boolean isStealingAllowed() {
return allowStealing;
}
public void setStealingAllowed(boolean allowed) {
allowStealing = allowed;
}
public Stack<Inventory> getPages() {
return pages;
}
public void open(HumanEntity player) {
player.openInventory(pages.get(0));
}
public void open(HumanEntity player, int page) {
if (pages.size() - 1 >= page && page >= 0) {
player.openInventory(pages.get(page));
}
}
public void clear() {
pages.clear();
newPage();
}
/* Statics */
public static boolean hasSpace(Inventory inventory) {
for (ItemStack stack : inventory.getContents()) {
if (stack == null || stack.getType() == Material.AIR) {
return true;
}
}
return false;
}
public static void playSound(InventoryClickEvent event) {
if (event.getSlot() == -999) {
return;
}
HumanEntity human = event.getWhoClicked();
if (!(human instanceof Player)) {
return;
}
ItemStack clicked = event.getCurrentItem();
if (clicked == null || clicked.getType() == Material.AIR) {
return;
}
if (clicked.getType() == Material.BARRIER) {
((Player) human).playSound(human.getLocation(), Sound.BLOCK_ANVIL_PLACE, 1, 1);
} else if (clicked != null && !clicked.equals(PLACEHOLDER)) {
((Player) human).playSound(human.getLocation(), Sound.UI_BUTTON_CLICK, 1, 1);
}
}
public static boolean isPageGUI(Inventory inventory) {
return inventory != null && inventory.getSize() == 54 && PREVIOUS_PAGE.equals(inventory.getItem(45)) && PLACEHOLDER.equals(inventory.getItem(49));
}
public static PageGUI getByInventory(Inventory inventory) {
for (PageGUI gui : DungeonsXL.getInstance().getPageGUICache().guis) {
for (Inventory page : gui.pages) {
if (page.getTitle().equals(inventory.getTitle())) {
return gui;
}
}
}
return null;
}
}

View File

@ -0,0 +1,67 @@
/*
* Copyright (C) 2017 Daniel Saukel
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.github.dre2n.dungeonsxl.util;
import static io.github.dre2n.dungeonsxl.util.GUIButton.*;
import java.util.HashSet;
import java.util.Set;
import org.bukkit.Material;
import org.bukkit.entity.HumanEntity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
/**
* @author Daniel Saukel
*/
public class PageGUICache implements Listener {
Set<PageGUI> guis = new HashSet<>();
@EventHandler
public void onClick(InventoryClickEvent event) {
ItemStack button = event.getCurrentItem();
if (button == null || button.getType() == Material.AIR) {
return;
}
Inventory inventory = event.getInventory();
PageGUI gui = PageGUI.getByInventory(inventory);
if (gui == null) {
return;
}
int index = gui.getPages().indexOf(inventory);
HumanEntity player = event.getWhoClicked();
if (button.equals(PLACEHOLDER)) {
event.setCancelled(true);
} else if (button.equals(NEXT_PAGE)) {
event.setCancelled(true);
PageGUI.playSound(event);
gui.open(player, index + 1);
} else if (button.equals(PREVIOUS_PAGE)) {
event.setCancelled(true);
PageGUI.playSound(event);
gui.open(player, index - 1);
} else if (!gui.isStealingAllowed()) {
event.setCancelled(true);
PageGUI.playSound(event);
}
}
}