mirror of
https://github.com/rockyhawk64/CommandPanels.git
synced 2025-11-18 07:14:17 +01:00
PanelBuilder improvements
This commit is contained in:
parent
8a8ac6d6f8
commit
fb53205293
@ -31,13 +31,13 @@ import me.rockyhawk.commandpanels.generate.GenerateCommand;
|
||||
import me.rockyhawk.commandpanels.generate.GenUtils;
|
||||
import me.rockyhawk.commandpanels.generate.GenTabComplete;
|
||||
import me.rockyhawk.commandpanels.manager.refresh.PanelRefresher;
|
||||
import me.rockyhawk.commandpanels.manager.OpenEvent;
|
||||
import me.rockyhawk.commandpanels.manager.PlayerJoinEvent;
|
||||
import me.rockyhawk.commandpanels.interaction.input.PlayerInputUtils;
|
||||
import me.rockyhawk.commandpanels.versions.VersionManager;
|
||||
import me.rockyhawk.commandpanels.nbt.NBTManager;
|
||||
import me.rockyhawk.commandpanels.items.potions.ClassicPotionData;
|
||||
import me.rockyhawk.commandpanels.items.potions.LegacyPotionData;
|
||||
import me.rockyhawk.commandpanels.builder.OpenGUI;
|
||||
import me.rockyhawk.commandpanels.builder.PanelBuilder;
|
||||
import me.rockyhawk.commandpanels.manager.session.SessionHandler;
|
||||
import me.rockyhawk.commandpanels.manager.session.SessionUtils;
|
||||
import me.rockyhawk.commandpanels.openwithitem.HotbarItemLoader;
|
||||
@ -90,7 +90,6 @@ public class Context {
|
||||
public VersionManager version;
|
||||
|
||||
public SessionHandler openPanels;
|
||||
public OpenGUI createGUI;
|
||||
public HotbarItemLoader hotbar;
|
||||
public NBTManager nbt;
|
||||
|
||||
@ -138,7 +137,6 @@ public class Context {
|
||||
|
||||
openCommands = new OpenCommands(this);
|
||||
openPanels = new SessionHandler(this);
|
||||
createGUI = new OpenGUI(this);
|
||||
hotbar = new HotbarItemLoader(this);
|
||||
nbt = new NBTManager(this);
|
||||
|
||||
@ -198,7 +196,7 @@ public class Context {
|
||||
Bukkit.getServer().getPluginManager().registerEvents(new SessionUtils(this), plugin);
|
||||
Bukkit.getServer().getPluginManager().registerEvents(generator, plugin);
|
||||
Bukkit.getServer().getPluginManager().registerEvents(new DroppedItemHandler(this), plugin);
|
||||
Bukkit.getServer().getPluginManager().registerEvents(new OpenEvent(this), plugin);
|
||||
Bukkit.getServer().getPluginManager().registerEvents(new PlayerJoinEvent(this), plugin);
|
||||
if (Bukkit.getServer().getPluginManager().isPluginEnabled("floodgate")) {
|
||||
Bukkit.getServer().getPluginManager().registerEvents(new OpenFloodgateGUI(this), plugin);
|
||||
}
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
package me.rockyhawk.commandpanels.api;
|
||||
|
||||
import me.rockyhawk.commandpanels.CommandPanels;
|
||||
import me.rockyhawk.commandpanels.manager.PanelOpenType;
|
||||
import me.rockyhawk.commandpanels.builder.PanelBuilder;
|
||||
import me.rockyhawk.commandpanels.manager.session.PanelPosition;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
@ -115,7 +115,7 @@ public class Panel{
|
||||
|
||||
//this will make a preview of the inventory using a certain player on the top
|
||||
public Inventory getInventory(Player p){
|
||||
return plugin.ctx.createGUI.openGui(this,p,PanelPosition.Top, PanelOpenType.Return,0);
|
||||
return new PanelBuilder(plugin.ctx).getInv(this,p,PanelPosition.Top,0);
|
||||
}
|
||||
|
||||
//open the panel for the player
|
||||
|
||||
@ -20,21 +20,21 @@ public class PanelInterface {
|
||||
public void setPanel(Panel panel, PanelPosition position){
|
||||
switch(position){
|
||||
case Top:{
|
||||
if(panel == null && top != null){
|
||||
if(top != null){
|
||||
top.isOpen = false;
|
||||
}
|
||||
top = panel;
|
||||
return;
|
||||
}
|
||||
case Middle:{
|
||||
if(panel == null && middle != null){
|
||||
if(middle != null){
|
||||
middle.isOpen = false;
|
||||
}
|
||||
middle = panel;
|
||||
return;
|
||||
}
|
||||
case Bottom:{
|
||||
if(panel == null && bottom != null){
|
||||
if(bottom != null){
|
||||
bottom.isOpen = false;
|
||||
}
|
||||
bottom = panel;
|
||||
|
||||
90
src/me/rockyhawk/commandpanels/builder/ItemPlacer.java
Normal file
90
src/me/rockyhawk/commandpanels/builder/ItemPlacer.java
Normal file
@ -0,0 +1,90 @@
|
||||
package me.rockyhawk.commandpanels.builder;
|
||||
|
||||
import me.rockyhawk.commandpanels.api.Panel;
|
||||
import me.rockyhawk.commandpanels.manager.session.PanelPosition;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Objects;
|
||||
|
||||
public class ItemPlacer {
|
||||
private final PanelBuilder bld;
|
||||
|
||||
public ItemPlacer(PanelBuilder bld) {
|
||||
this.bld = bld;
|
||||
}
|
||||
|
||||
public void placeEmptyItems(Panel panel, Player p, PanelPosition pos, Inventory inv) {
|
||||
ConfigurationSection config = panel.getConfig();
|
||||
|
||||
if (!config.contains("empty")) return;
|
||||
|
||||
String materialName = bld.ctx.text.placeholdersNoColour(panel, pos, p, config.getString("empty"));
|
||||
if (materialName.equals("AIR")) return;
|
||||
|
||||
ItemStack empty;
|
||||
try {
|
||||
short id = config.contains("emptyID")
|
||||
? Short.parseShort(bld.ctx.text.placeholdersNoColour(panel, pos, p, config.getString("emptyID")))
|
||||
: 0;
|
||||
|
||||
if (config.contains("custom-item." + materialName)) {
|
||||
empty = bld.ctx.itemCreate.makeItemFromConfig(panel, pos, config.getConfigurationSection("custom-item." + materialName), p, true, true, true);
|
||||
} else {
|
||||
empty = new ItemStack(Objects.requireNonNull(Material.matchMaterial(materialName.toUpperCase())), 1, id);
|
||||
ItemMeta meta = empty.getItemMeta();
|
||||
if (meta != null) {
|
||||
meta.setDisplayName(" ");
|
||||
if (bld.ctx.version.isAtLeast("1.21.4")) {
|
||||
try {
|
||||
Method m = ItemMeta.class.getMethod("setHideTooltip", boolean.class);
|
||||
m.invoke(meta, true);
|
||||
} catch (Exception ignore) {}
|
||||
}
|
||||
empty.setItemMeta(meta);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < inv.getSize(); i++) {
|
||||
if(!bld.slotManager.takenSlots.contains(i)) {
|
||||
bld.slotManager.setItem(empty, i, inv, p, pos);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
bld.ctx.debug.send(e, p, bld.ctx);
|
||||
}
|
||||
}
|
||||
|
||||
public void placeDuplicates(Inventory inv, PanelPosition position, Player p, ConfigurationSection config, String itemKey, ItemStack item, String section) {
|
||||
if (!config.contains("item." + itemKey + section + ".duplicate")) return;
|
||||
|
||||
String[] duplicates = config.getString("item." + itemKey + section + ".duplicate").split(",");
|
||||
for (String d : duplicates) {
|
||||
try {
|
||||
if (d.contains("-")) {
|
||||
int start = Integer.parseInt(d.split("-")[0]);
|
||||
int end = Integer.parseInt(d.split("-")[1]);
|
||||
for (int i = start; i <= end; i++) {
|
||||
if (!config.contains("item." + i)) {
|
||||
bld.slotManager.setItem(item, i, inv, p, position);
|
||||
bld.slotManager.takenSlots.add(i);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
int slot = Integer.parseInt(d);
|
||||
if (!config.contains("item." + slot)) {
|
||||
bld.slotManager.setItem(item, slot, inv, p, position);
|
||||
bld.slotManager.takenSlots.add(slot);
|
||||
}
|
||||
}
|
||||
} catch (Exception ignored) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,278 +0,0 @@
|
||||
package me.rockyhawk.commandpanels.builder;
|
||||
|
||||
import me.rockyhawk.commandpanels.Context;
|
||||
import me.rockyhawk.commandpanels.api.Panel;
|
||||
import me.rockyhawk.commandpanels.builder.storagecontents.GetStorageContents;
|
||||
import me.rockyhawk.commandpanels.builder.storagecontents.GetStorageContentsLegacy;
|
||||
import me.rockyhawk.commandpanels.manager.PanelOpenType;
|
||||
import me.rockyhawk.commandpanels.manager.session.PanelPosition;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.inventory.InventoryType;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashSet;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
public class OpenGUI {
|
||||
Context ctx;
|
||||
public OpenGUI(Context pl) {
|
||||
this.ctx = pl;
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public Inventory openGui(Panel panel, Player p, PanelPosition position, PanelOpenType openType, int animateValue) {
|
||||
ConfigurationSection pconfig = panel.getConfig();
|
||||
|
||||
Inventory i;
|
||||
if(position == PanelPosition.Top) {
|
||||
String title = getTitle(p, pconfig, panel, position, animateValue);
|
||||
|
||||
if (isNumeric(pconfig.getString("rows"))) {
|
||||
i = Bukkit.createInventory(p, Integer.parseInt(pconfig.getString("rows")) * 9, title);
|
||||
} else {
|
||||
i = Bukkit.createInventory(p, InventoryType.valueOf(pconfig.getString("rows")), title);
|
||||
}
|
||||
}else{
|
||||
i = p.getInventory();
|
||||
//if middle or bottom position, old items need to be cleared
|
||||
for (int c = 0; getInvSize(i,position) > c; ++c) {
|
||||
if(pconfig.getConfigurationSection("item").getKeys(false).contains(String.valueOf(c))){
|
||||
continue;
|
||||
}
|
||||
setItem(null, c, i, p, position);
|
||||
}
|
||||
}
|
||||
|
||||
Set<String> itemList = pconfig.getConfigurationSection("item").getKeys(false);
|
||||
HashSet<Integer> takenSlots = new HashSet<>();
|
||||
for (String item : itemList) {
|
||||
String section;
|
||||
//openType needs to not be 3 so the editor won't include hasperm and hasvalue, etc items
|
||||
section = ctx.has.hasSection(panel,position,pconfig.getConfigurationSection("item." + item), p);
|
||||
//This section is for animations below here: VISUAL ONLY
|
||||
|
||||
//check for if there is animations inside the items section
|
||||
if (pconfig.contains("item." + item + section + ".animate" + animateValue)) {
|
||||
//check for if it contains the animate that has the animvatevalue
|
||||
if (pconfig.contains("item." + item + section + ".animate" + animateValue)) {
|
||||
section = section + ".animate" + animateValue;
|
||||
}
|
||||
}
|
||||
|
||||
ItemStack s = ctx.itemCreate.makeItemFromConfig(panel,position,Objects.requireNonNull(pconfig.getConfigurationSection("item." + item + section)), p, true, true, true);
|
||||
|
||||
//This is for CUSTOM ITEMS
|
||||
if(pconfig.contains("item." + item + section + ".itemType")) {
|
||||
//this is for contents in the itemType section
|
||||
if (pconfig.getStringList("item." + item + section + ".itemType").contains("placeable") && openType == PanelOpenType.Refresh) {
|
||||
//keep item the same, openType == 0 meaning panel is refreshing
|
||||
setItem(p.getOpenInventory().getItem(Integer.parseInt(ctx.text.placeholdersNoColour(panel,position,p,item))), item, i, p, position);
|
||||
takenSlots.add(Integer.parseInt(ctx.text.placeholdersNoColour(panel,position,p,item)));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
//place item into the GUI
|
||||
setItem(s,item,i,p,position);
|
||||
takenSlots.add(Integer.parseInt(ctx.text.placeholdersNoColour(panel,position,p,item)));
|
||||
//only place duplicate items in without the editor mode. These are merely visual and will not carry over commands
|
||||
if(pconfig.contains("item." + item + section + ".duplicate")) {
|
||||
try {
|
||||
String[] duplicateItems = pconfig.getString("item." + item + section + ".duplicate").split(",");
|
||||
for (String tempDupe : duplicateItems) {
|
||||
if (tempDupe.contains("-")) {
|
||||
//if there is multiple dupe items, convert numbers to ints
|
||||
int[] bothNumbers = new int[]{Integer.parseInt(tempDupe.split("-")[0]), Integer.parseInt(tempDupe.split("-")[1])};
|
||||
for(int n = bothNumbers[0]; n <= bothNumbers[1]; n++){
|
||||
try{
|
||||
if(!pconfig.contains("item." + n)){
|
||||
setItem(s,n,i,p,position);
|
||||
takenSlots.add(n);
|
||||
}
|
||||
}catch(NullPointerException ignore){
|
||||
setItem(s,n,i,p,position);
|
||||
takenSlots.add(n);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
//if there is only one dupe item
|
||||
try{
|
||||
if(!pconfig.contains("item." + Integer.parseInt(tempDupe))){
|
||||
setItem(s,Integer.parseInt(tempDupe),i,p,position);
|
||||
takenSlots.add(Integer.parseInt(tempDupe));
|
||||
}
|
||||
}catch(NullPointerException ignore){
|
||||
setItem(s,Integer.parseInt(tempDupe),i,p,position);
|
||||
takenSlots.add(Integer.parseInt(tempDupe));
|
||||
}
|
||||
}
|
||||
}
|
||||
}catch(NullPointerException nullp){
|
||||
ctx.debug.send(nullp,p, ctx);
|
||||
p.closeInventory();
|
||||
ctx.openPanels.closePanelForLoader(p.getName(),position);
|
||||
}
|
||||
}
|
||||
} catch (ArrayIndexOutOfBoundsException ignore) {}
|
||||
}
|
||||
if (pconfig.contains("empty") && !ctx.text.placeholdersNoColour(panel,position,p,pconfig.getString("empty")).equals("AIR")) {
|
||||
String emptyValue = ctx.text.placeholdersNoColour(panel,position,p,pconfig.getString("empty"));
|
||||
ItemStack empty;
|
||||
try {
|
||||
//emptyID for older versions of minecraft (might be deprecated later on)
|
||||
short id = 0;
|
||||
if(pconfig.contains("emptyID")){
|
||||
id = Short.parseShort(ctx.text.placeholdersNoColour(panel,position,p,pconfig.getString("emptyID")));
|
||||
}
|
||||
//either use custom item or just material type
|
||||
if(pconfig.contains("custom-item." + emptyValue)){
|
||||
empty = ctx.itemCreate.makeItemFromConfig(panel,position,pconfig.getConfigurationSection("custom-item." + emptyValue),p,true,true,true);
|
||||
}else{
|
||||
empty = new ItemStack(Objects.requireNonNull(Material.matchMaterial(emptyValue.toUpperCase())), 1,id);
|
||||
ItemMeta renamedMeta = empty.getItemMeta();
|
||||
assert renamedMeta != null;
|
||||
renamedMeta.setDisplayName(" ");
|
||||
//If 1.21.4+ then hide box on hover of empty slot
|
||||
if(ctx.version.isAtLeast("1.21.4")){
|
||||
try {
|
||||
// Check if the setHideTooltip method exists
|
||||
Method setHideTooltipMethod = ItemMeta.class.getMethod("setHideTooltip", boolean.class);
|
||||
|
||||
// Invoke it dynamically
|
||||
setHideTooltipMethod.invoke(renamedMeta, true);
|
||||
|
||||
} catch (NoSuchMethodException e) {
|
||||
// The method does not exist in older Spigot versions
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
empty.setItemMeta(renamedMeta);
|
||||
}
|
||||
if (empty.getType() != Material.AIR) {
|
||||
for (int c = 0; getInvSize(i,position) > c; ++c) {
|
||||
if (!takenSlots.contains(c)) {
|
||||
//place empty item
|
||||
setItem(empty,c,i,p,position);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IllegalArgumentException | NullPointerException var26) {
|
||||
ctx.debug.send(var26,p, ctx);
|
||||
}
|
||||
}
|
||||
if (openType == PanelOpenType.Normal) {
|
||||
//declare old panel closed
|
||||
if(ctx.openPanels.hasPanelOpen(p.getName(),position)){
|
||||
ctx.openPanels.getOpenPanel(p.getName(),position).isOpen = false;
|
||||
}
|
||||
//open new panel
|
||||
ctx.openPanels.skipPanelClose.add(p.getName());
|
||||
ctx.openPanels.openPanelForLoader(p.getName(),panel,position);
|
||||
//only if it needs to open the top inventory
|
||||
if(position == PanelPosition.Top) {
|
||||
p.openInventory(i);
|
||||
}
|
||||
ctx.openPanels.skipPanelClose.remove(p.getName());
|
||||
} else if (openType == PanelOpenType.Refresh) {
|
||||
//openType Refresh will just refresh the panel
|
||||
if(ctx.version.isAtLeast("1.21.5")){
|
||||
//Title refresh ability added in 1.20.5 api
|
||||
if(position == PanelPosition.Top) {
|
||||
p.getOpenInventory().setTitle(getTitle(p, pconfig, panel, position, animateValue));
|
||||
}
|
||||
}
|
||||
if(position == PanelPosition.Top) {
|
||||
setStorageContents(p, getStorageContents(i));
|
||||
}
|
||||
} else if (openType == PanelOpenType.Return) {
|
||||
//will return the inventory, not opening it at all
|
||||
return i;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
private int getInvSize(Inventory inv, PanelPosition position){
|
||||
if(position == PanelPosition.Top){
|
||||
return inv.getSize();
|
||||
}else if(position == PanelPosition.Middle){
|
||||
return 27;
|
||||
}else{
|
||||
return 9;
|
||||
}
|
||||
}
|
||||
|
||||
//allows for a string to be used instead of integer, for placeholders
|
||||
private void setItem(ItemStack item, String slotName, Inventory inv, Player p, PanelPosition position) throws ArrayIndexOutOfBoundsException{
|
||||
int slot = Integer.parseInt(ctx.text.placeholdersNoColour(null, position, p, slotName));
|
||||
setItem(item, slot, inv, p, position);
|
||||
}
|
||||
private void setItem(ItemStack item, int slot, Inventory inv, Player p, PanelPosition position) throws ArrayIndexOutOfBoundsException{
|
||||
if(position == PanelPosition.Top){
|
||||
inv.setItem(slot, item);
|
||||
}else if(position == PanelPosition.Middle){
|
||||
if(slot+9 < 36) {
|
||||
p.getInventory().setItem(slot + 9, item);
|
||||
}
|
||||
}else{
|
||||
if(slot < 9) {
|
||||
p.getInventory().setItem(slot, item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isNumeric(String strNum) {
|
||||
if (strNum == null) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
int unused = Integer.parseInt(strNum);
|
||||
} catch (NumberFormatException nfe) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private String getTitle(Player p, ConfigurationSection pconfig, Panel panel, PanelPosition position, Integer animateValue){
|
||||
String title;
|
||||
if(pconfig.contains("custom-title")) {
|
||||
//used for titles in the custom-title section, for has sections
|
||||
String section = ctx.has.hasSection(panel,position,pconfig.getConfigurationSection("custom-title"), p);
|
||||
|
||||
//check for if there is animations inside the custom-title section
|
||||
if (pconfig.contains("custom-title" + section + ".animate" + animateValue)) {
|
||||
section = section + ".animate" + animateValue;
|
||||
}
|
||||
|
||||
title = ctx.text.placeholders(panel, position, p, pconfig.getString("custom-title" + section + ".title"));
|
||||
}else {
|
||||
//regular inventory title
|
||||
title = ctx.text.placeholders(panel, position, p, pconfig.getString("title"));
|
||||
}
|
||||
return title;
|
||||
}
|
||||
|
||||
private ItemStack[] getStorageContents(Inventory inventory) {
|
||||
if (ctx.version.isBelow("1.13")) {
|
||||
return new GetStorageContentsLegacy().getStorageContents(inventory);
|
||||
} else {
|
||||
return new GetStorageContents().getStorageContents(inventory);
|
||||
}
|
||||
}
|
||||
|
||||
private void setStorageContents(Player player, ItemStack[] items) {
|
||||
if (ctx.version.isBelow("1.16")) {
|
||||
new GetStorageContentsLegacy().setStorageContents(player, items);
|
||||
} else {
|
||||
new GetStorageContents().setStorageContents(player, items);
|
||||
}
|
||||
}
|
||||
}
|
||||
51
src/me/rockyhawk/commandpanels/builder/PanelBuilder.java
Normal file
51
src/me/rockyhawk/commandpanels/builder/PanelBuilder.java
Normal file
@ -0,0 +1,51 @@
|
||||
package me.rockyhawk.commandpanels.builder;
|
||||
|
||||
import me.rockyhawk.commandpanels.Context;
|
||||
import me.rockyhawk.commandpanels.api.Panel;
|
||||
import me.rockyhawk.commandpanels.manager.session.PanelPosition;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
|
||||
public class PanelBuilder {
|
||||
protected final Context ctx;
|
||||
protected final PanelFactory factory;
|
||||
protected final SlotManager slotManager;
|
||||
protected final ItemPlacer itemPlacer;
|
||||
|
||||
public PanelBuilder(Context ctx) {
|
||||
this.ctx = ctx;
|
||||
this.factory = new PanelFactory(ctx);
|
||||
this.slotManager = new SlotManager(this);
|
||||
this.itemPlacer = new ItemPlacer(this);
|
||||
}
|
||||
|
||||
public void openInv(Panel panel, Player p, PanelPosition position, int animateValue) {
|
||||
Inventory inv = buildInv(panel, p, position, animateValue);
|
||||
if (position == PanelPosition.Top) {
|
||||
p.openInventory(inv);
|
||||
}
|
||||
ctx.openPanels.openPanelForLoader(p.getName(), panel, position);
|
||||
}
|
||||
|
||||
public void refreshInv(Panel panel, Player p, PanelPosition position, int animateValue) {
|
||||
Inventory inv = buildInv(panel, p, position, animateValue);
|
||||
if (ctx.version.isAtLeast("1.21.5") && position == PanelPosition.Top) {
|
||||
p.getOpenInventory().setTitle(TitleHandler.getTitle(ctx, panel, p, position, animateValue));
|
||||
}
|
||||
if (position == PanelPosition.Top) {
|
||||
slotManager.setStorageContents(p, slotManager.getStorageContents(inv));
|
||||
}
|
||||
}
|
||||
|
||||
public Inventory getInv(Panel panel, Player p, PanelPosition position, int animateValue) {
|
||||
return buildInv(panel, p, position, animateValue);
|
||||
}
|
||||
|
||||
private Inventory buildInv(Panel panel, Player p, PanelPosition position, int animateValue) {
|
||||
Inventory inv = factory.createInventory(panel, p, position, animateValue);
|
||||
slotManager.takenSlots.clear();
|
||||
slotManager.populateSlots(panel, p, position, animateValue, inv);
|
||||
itemPlacer.placeEmptyItems(panel, p, position, inv);
|
||||
return inv;
|
||||
}
|
||||
}
|
||||
35
src/me/rockyhawk/commandpanels/builder/PanelFactory.java
Normal file
35
src/me/rockyhawk/commandpanels/builder/PanelFactory.java
Normal file
@ -0,0 +1,35 @@
|
||||
package me.rockyhawk.commandpanels.builder;
|
||||
|
||||
import me.rockyhawk.commandpanels.Context;
|
||||
import me.rockyhawk.commandpanels.api.Panel;
|
||||
import me.rockyhawk.commandpanels.manager.session.PanelPosition;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.inventory.InventoryType;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
|
||||
public class PanelFactory {
|
||||
private final Context ctx;
|
||||
|
||||
public PanelFactory(Context ctx) {
|
||||
this.ctx = ctx;
|
||||
}
|
||||
|
||||
public Inventory createInventory(Panel panel, Player p, PanelPosition position, int animateValue) {
|
||||
ConfigurationSection config = panel.getConfig();
|
||||
if (position == PanelPosition.Top) {
|
||||
String title = TitleHandler.getTitle(ctx, panel, p, position, animateValue);
|
||||
String rows = config.getString("rows");
|
||||
|
||||
if (rows.matches("\\d+")) {
|
||||
return Bukkit.createInventory(p, Integer.parseInt(rows) * 9, title);
|
||||
} else {
|
||||
return Bukkit.createInventory(p, InventoryType.valueOf(rows), title);
|
||||
}
|
||||
} else {
|
||||
Inventory inv = p.getInventory();
|
||||
return inv;
|
||||
}
|
||||
}
|
||||
}
|
||||
75
src/me/rockyhawk/commandpanels/builder/SlotManager.java
Normal file
75
src/me/rockyhawk/commandpanels/builder/SlotManager.java
Normal file
@ -0,0 +1,75 @@
|
||||
package me.rockyhawk.commandpanels.builder;
|
||||
|
||||
import me.rockyhawk.commandpanels.api.Panel;
|
||||
import me.rockyhawk.commandpanels.builder.storagecontents.GetStorageContents;
|
||||
import me.rockyhawk.commandpanels.builder.storagecontents.GetStorageContentsLegacy;
|
||||
import me.rockyhawk.commandpanels.manager.session.PanelPosition;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class SlotManager {
|
||||
private final PanelBuilder bld;
|
||||
|
||||
protected HashSet<Integer> takenSlots = new HashSet<>();
|
||||
|
||||
public SlotManager(PanelBuilder bld) {
|
||||
this.bld = bld;
|
||||
}
|
||||
|
||||
public void populateSlots(Panel panel, Player p, PanelPosition position, int animateValue, Inventory inv) {
|
||||
ConfigurationSection config = panel.getConfig();
|
||||
Set<String> itemKeys = config.getConfigurationSection("item").getKeys(false);
|
||||
|
||||
for (String itemKey : itemKeys) {
|
||||
String section = bld.ctx.has.hasSection(panel, position, config.getConfigurationSection("item." + itemKey), p);
|
||||
|
||||
if (config.contains("item." + itemKey + section + ".animate" + animateValue)) {
|
||||
section += ".animate" + animateValue;
|
||||
}
|
||||
|
||||
ItemStack item = bld.ctx.itemCreate.makeItemFromConfig(panel, position, config.getConfigurationSection("item." + itemKey + section), p, true, true, true);
|
||||
int slot = Integer.parseInt(bld.ctx.text.placeholdersNoColour(panel, position, p, itemKey));
|
||||
setItem(item, slot, inv, p, position);
|
||||
takenSlots.add(slot);
|
||||
|
||||
// handle duplicates
|
||||
bld.itemPlacer.placeDuplicates(inv, position, p, config, itemKey, item, section);
|
||||
}
|
||||
}
|
||||
|
||||
protected void setItem(ItemStack item, int slot, Inventory inv, Player p, PanelPosition position) {
|
||||
if(position == PanelPosition.Top){
|
||||
inv.setItem(slot, item);
|
||||
}else if(position == PanelPosition.Middle){
|
||||
if(slot+9 < 36) {
|
||||
p.getInventory().setItem(slot + 9, item);
|
||||
}
|
||||
}else{
|
||||
if(slot < 9) {
|
||||
p.getInventory().setItem(slot, item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// For refresh compatibility
|
||||
protected void setStorageContents(Player player, ItemStack[] items) {
|
||||
if (bld.ctx.version.isBelow("1.16")) {
|
||||
new GetStorageContentsLegacy().setStorageContents(player, items);
|
||||
} else {
|
||||
new GetStorageContents().setStorageContents(player, items);
|
||||
}
|
||||
}
|
||||
|
||||
protected ItemStack[] getStorageContents(Inventory inventory) {
|
||||
if (bld.ctx.version.isBelow("1.13")) {
|
||||
return new GetStorageContentsLegacy().getStorageContents(inventory);
|
||||
} else {
|
||||
return new GetStorageContents().getStorageContents(inventory);
|
||||
}
|
||||
}
|
||||
}
|
||||
18
src/me/rockyhawk/commandpanels/builder/TitleHandler.java
Normal file
18
src/me/rockyhawk/commandpanels/builder/TitleHandler.java
Normal file
@ -0,0 +1,18 @@
|
||||
package me.rockyhawk.commandpanels.builder;
|
||||
|
||||
import me.rockyhawk.commandpanels.Context;
|
||||
import me.rockyhawk.commandpanels.api.Panel;
|
||||
import me.rockyhawk.commandpanels.manager.session.PanelPosition;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class TitleHandler {
|
||||
public static String getTitle(Context ctx, Panel panel, Player p, PanelPosition position, int animateValue) {
|
||||
ConfigurationSection config = panel.getConfig();
|
||||
if (config.contains("title.animate" + animateValue)) {
|
||||
return ctx.text.placeholders(panel, position, p, config.getString("title.animate" + animateValue));
|
||||
} else {
|
||||
return ctx.text.placeholders(panel, position, p, config.getString("title"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,7 +1,7 @@
|
||||
package me.rockyhawk.commandpanels.commands;
|
||||
|
||||
import me.rockyhawk.commandpanels.Context;
|
||||
import me.rockyhawk.commandpanels.manager.PanelOpenType;
|
||||
import me.rockyhawk.commandpanels.builder.PanelBuilder;
|
||||
import me.rockyhawk.commandpanels.manager.session.PanelPosition;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
@ -38,7 +38,7 @@ public class UpdateCommand implements CommandExecutor {
|
||||
if(args[1].equalsIgnoreCase("all")){
|
||||
for(PanelPosition papo : PanelPosition.values()){
|
||||
if(ctx.openPanels.hasPanelOpen(name, papo)) {
|
||||
ctx.createGUI.openGui(ctx.openPanels.getOpenPanel(name, papo), targetPlayer, papo, PanelOpenType.Refresh, 0);
|
||||
new PanelBuilder(ctx).refreshInv(ctx.openPanels.getOpenPanel(name, papo), targetPlayer, papo, 0);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@ -50,7 +50,7 @@ public class UpdateCommand implements CommandExecutor {
|
||||
}
|
||||
|
||||
if(ctx.openPanels.hasPanelOpen(name, pp)) {
|
||||
ctx.createGUI.openGui(ctx.openPanels.getOpenPanel(name, pp), targetPlayer, pp, PanelOpenType.Refresh, 0);
|
||||
new PanelBuilder(ctx).refreshInv(ctx.openPanels.getOpenPanel(name, pp), targetPlayer, pp, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -26,6 +26,7 @@ public class ClickHandler {
|
||||
if(e.getSlotType() == InventoryType.SlotType.OUTSIDE) return;
|
||||
|
||||
PanelPosition position = slotResolver.resolveSlotPosition(e, panel, p, clickedSlot);
|
||||
if(position == PanelPosition.Middle) clickedSlot -= 9;
|
||||
if (position == null) {
|
||||
e.setCancelled(true);
|
||||
return;
|
||||
|
||||
@ -23,7 +23,7 @@ public class ClickSlotResolver {
|
||||
|
||||
if (e.getSlotType() == InventoryType.SlotType.CONTAINER) {
|
||||
if (handler.ctx.openPanels.hasPanelOpen(p.getName(), PanelPosition.Middle)) {
|
||||
e.setCancelled(false);
|
||||
e.setCancelled(true);
|
||||
return PanelPosition.Middle;
|
||||
}
|
||||
} else {
|
||||
|
||||
@ -2,9 +2,9 @@ package me.rockyhawk.commandpanels.interaction.commands.tags;
|
||||
|
||||
import me.rockyhawk.commandpanels.Context;
|
||||
import me.rockyhawk.commandpanels.api.Panel;
|
||||
import me.rockyhawk.commandpanels.builder.PanelBuilder;
|
||||
import me.rockyhawk.commandpanels.manager.session.PanelPosition;
|
||||
import me.rockyhawk.commandpanels.interaction.commands.TagResolver;
|
||||
import me.rockyhawk.commandpanels.manager.PanelOpenType;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class RefreshTag implements TagResolver {
|
||||
@ -16,7 +16,7 @@ public class RefreshTag implements TagResolver {
|
||||
}
|
||||
|
||||
if (ctx.openPanels.hasPanelOpen(player.getName(), pos)) {
|
||||
ctx.createGUI.openGui(panel, player, pos, PanelOpenType.Refresh, 0);
|
||||
new PanelBuilder(ctx).refreshInv(panel, player, pos, 0);
|
||||
}
|
||||
if (ctx.inventorySaver.hasNormalInventory(player)) {
|
||||
ctx.hotbar.updateHotbarItems(player);
|
||||
|
||||
@ -1,9 +0,0 @@
|
||||
package me.rockyhawk.commandpanels.manager;
|
||||
|
||||
public enum PanelOpenType {
|
||||
Normal,
|
||||
Return,
|
||||
Refresh
|
||||
}
|
||||
|
||||
// After refactoring this should no longer be a necessary enum to keep
|
||||
@ -7,15 +7,14 @@ import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerChangedWorldEvent;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
|
||||
public class OpenEvent implements Listener {
|
||||
public class PlayerJoinEvent implements Listener {
|
||||
Context ctx;
|
||||
public OpenEvent(Context pl) {
|
||||
public PlayerJoinEvent(Context pl) {
|
||||
this.ctx = pl;
|
||||
}
|
||||
@EventHandler
|
||||
public void onWorldLogin(PlayerJoinEvent e){
|
||||
public void onWorldLogin(org.bukkit.event.player.PlayerJoinEvent e){
|
||||
if (!e.getPlayer().hasPlayedBefore() && ctx.configHandler.config.contains("open-on-first-login")) {
|
||||
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(ctx.plugin, () ->
|
||||
openOnJoin(e.getPlayer(), "open-on-first-login"), 40L); // 2 seconds delay
|
||||
@ -2,8 +2,8 @@ package me.rockyhawk.commandpanels.manager.open;
|
||||
|
||||
import me.rockyhawk.commandpanels.Context;
|
||||
import me.rockyhawk.commandpanels.api.Panel;
|
||||
import me.rockyhawk.commandpanels.builder.PanelBuilder;
|
||||
import me.rockyhawk.commandpanels.events.PanelOpenedEvent;
|
||||
import me.rockyhawk.commandpanels.manager.PanelOpenType;
|
||||
import me.rockyhawk.commandpanels.manager.session.PanelPosition;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
@ -40,7 +40,7 @@ public class OpenPanel {
|
||||
|
||||
boolean openForOtherUser = !(sender instanceof Player && sender == p);
|
||||
|
||||
if (!permission.hasPermission(sender, p, panel, openForOtherUser)) return;
|
||||
if (!permission.hasPermission(sender, p, panel, position, openForOtherUser)) return;
|
||||
|
||||
if (position != PanelPosition.Top && !ctx.openPanels.hasPanelOpen(p.getName(), PanelPosition.Top)) {
|
||||
sender.sendMessage(ctx.text.colour(ctx.tag + ChatColor.RED + "Cannot open a panel without a panel at the top already."));
|
||||
@ -58,7 +58,7 @@ public class OpenPanel {
|
||||
preloader.executePreLoad(panel, position, p);
|
||||
|
||||
try {
|
||||
ctx.createGUI.openGui(panel, p, position, PanelOpenType.Normal, 0);
|
||||
new PanelBuilder(ctx).openInv(panel, p, position, 0);
|
||||
commandExecutor.executeOpenCommands(panel, position, p);
|
||||
soundPlayer.playOpenSound(panel, p);
|
||||
|
||||
|
||||
@ -2,6 +2,7 @@ package me.rockyhawk.commandpanels.manager.open;
|
||||
|
||||
import me.rockyhawk.commandpanels.Context;
|
||||
import me.rockyhawk.commandpanels.api.Panel;
|
||||
import me.rockyhawk.commandpanels.manager.session.PanelPosition;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.entity.Player;
|
||||
@ -13,7 +14,7 @@ public class PermissionValidator {
|
||||
this.ctx = ctx;
|
||||
}
|
||||
|
||||
public boolean hasPermission(CommandSender sender, Player p, Panel panel, boolean openForOtherUser) {
|
||||
public boolean hasPermission(CommandSender sender, Player p, Panel panel, PanelPosition position, boolean openForOtherUser) {
|
||||
String permMessage = panel.getConfig().getString("custom-messages.perms");
|
||||
String defaultPerm = ctx.configHandler.config.getString("config.format.perms");
|
||||
|
||||
|
||||
@ -2,8 +2,8 @@ package me.rockyhawk.commandpanels.manager.refresh;
|
||||
|
||||
import me.rockyhawk.commandpanels.Context;
|
||||
import me.rockyhawk.commandpanels.api.Panel;
|
||||
import me.rockyhawk.commandpanels.builder.PanelBuilder;
|
||||
import me.rockyhawk.commandpanels.events.PanelOpenedEvent;
|
||||
import me.rockyhawk.commandpanels.manager.PanelOpenType;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
@ -64,6 +64,7 @@ public class PanelRefresher implements Listener {
|
||||
new BukkitRunnable(){
|
||||
int c = 0;
|
||||
int animatecount = 0;
|
||||
PanelBuilder builder = new PanelBuilder(ctx);
|
||||
@Override
|
||||
public void run() {
|
||||
//counter counts to refresh delay (in ticks) then restarts
|
||||
@ -94,7 +95,7 @@ public class PanelRefresher implements Listener {
|
||||
//reload the panel is debug is enabled (only personal debug)
|
||||
pn.setConfig(YamlConfiguration.loadConfiguration(pn.getFile()));
|
||||
}
|
||||
ctx.createGUI.openGui(pn, p,e.getPosition(), PanelOpenType.Refresh,animatecount);
|
||||
builder.refreshInv(pn, p,e.getPosition(),animatecount);
|
||||
} catch (Exception ex) {
|
||||
//error opening gui
|
||||
p.closeInventory();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user