SteamWar/SpigotCore
Archiviert
13
0

Adding GUI-Lists + modifying API + Added Schems by SchemsByType

Dieser Commit ist enthalten in:
Lixfel 2019-06-05 15:42:41 +02:00
Ursprung 3efacade25
Commit 42e2dbcfac
5 geänderte Dateien mit 144 neuen und 20 gelöschten Zeilen

Datei anzeigen

@ -1,6 +1,8 @@
package de.steamwar.inventory;
import org.bukkit.event.inventory.ClickType;
public interface InvCallback {
public void clicked();
public void clicked(ClickType click);
}

Datei anzeigen

@ -19,7 +19,7 @@ import java.util.*;
public class SWInventory implements Listener, Inventory {
private Player player;
protected Player player;
private Map<Integer, InvCallback> callbacks;
private int inventorySize;
private ItemStack[] contents;
@ -31,7 +31,6 @@ public class SWInventory implements Listener, Inventory {
player = p;
inventorySize = size;
contents = new ItemStack[inventorySize];
Bukkit.getPluginManager().registerEvents(this, Core.getInstance());
}
public SWInventory(Player p, int size, String t, Map<Integer, SWItem> items){
@ -72,21 +71,28 @@ public class SWInventory implements Listener, Inventory {
public void open(){
player.openInventory(this);
Bukkit.getPluginManager().registerEvents(this, Core.getInstance());
}
@EventHandler
public void onInventoryClick(InventoryClickEvent e){
if(!player.equals(e.getWhoClicked()))
return;
e.setCancelled(true);
if(callbacks.containsKey(e.getRawSlot()))
callbacks.get(e.getSlot()).clicked();
callbacks.get(e.getSlot()).clicked(e.getClick());
}
@EventHandler
public void onInventoryClose(InventoryCloseEvent e){
if(!player.equals(e.getPlayer()))
return;
InventoryClickEvent.getHandlerList().unregister(this);
InventoryCloseEvent.getHandlerList().unregister(this);
if(callbacks.containsKey(-1))
callbacks.get(-1).clicked();
callbacks.get(-1).clicked(null);
}
@ -209,12 +215,14 @@ public class SWInventory implements Listener, Inventory {
@Override
public void clear(int index) {
contents[index] = null;
callbacks.remove(index);
}
@Override
public void clear() {
contents = new ItemStack[contents.length];
callbacks.clear();
}
@Override

Datei anzeigen

@ -5,6 +5,7 @@ import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.ArrayList;
import java.util.List;
public class SWItem {
@ -18,6 +19,10 @@ public class SWItem {
itemMeta = itemStack.getItemMeta();
}
public SWItem(Material material, String name){
this(material, (byte)0, name, new ArrayList<>(), false, null);
}
public SWItem(Material material, String name, List<String> lore, boolean enchanted, InvCallback c){
this(material, (byte)0, name, lore, enchanted, c);
}

Datei anzeigen

@ -0,0 +1,115 @@
package de.steamwar.inventory;
import de.warking.hunjy.MySQL.Schematic;
import de.warking.hunjy.MySQL.SchematicType;
import javafx.util.Pair;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class SWListInv<T> extends SWInventory {
private ListCallback<T> callback;
private List<Pair<SWItem, T>> elements;
private int page;
public SWListInv(Player p, String t, ListCallback<T> c, List<Pair<SWItem, T>> l) {
super(p, (l.size()>45) ? 54 : (l.size() + 9-l.size()%9), t);
callback = c;
elements = l;
page = 0;
}
@Override
public void open(){
clear();
setCallback(-999, (ClickType click) -> player.closeInventory());
if(elements.size() > 54){
//TODO: Shift & Right-Click-Features
if(page != 0)
setItem(45, Material.INK_SACK, (byte)10, "§eSeite zurück", (ClickType click) -> {
page--;
open();
});
else
setItem(45, Material.INK_SACK, (byte)8, "§7Seite zurück", (ClickType click) -> {});
if(page < elements.size()%45)
setItem(53, Material.INK_SACK, (byte)10, "§eSeite vor", (ClickType click) -> {
page++;
open();
});
else
setItem(53, Material.INK_SACK, (byte)8, "§7Seite vor", (ClickType click) -> {});
}
int ipageLimit = elements.size() - page*45;
if(ipageLimit > 45 && elements.size() > 54){
ipageLimit = 45;
}
int i = page*45;
for(int ipage=0; ipage < ipageLimit; ipage++ ){
SWItem e = elements.get(i).getKey();
final int pos = i;
setItem(ipage, e);
setCallback(ipage, (ClickType click) -> callback.clicked(click, elements.get(pos).getValue()));
i++;
}
super.open();
}
public void setCallback(ListCallback<T> c){
callback = c;
}
public interface ListCallback<T>{
public void clicked(ClickType click, T element);
}
public static List<Pair<SWItem, UUID>> createPlayerList(Player without){
List<Pair<SWItem, UUID>> onlinePlayers = new ArrayList<>();
for(Player player : Bukkit.getOnlinePlayers()){
if(without != null && player.getUniqueId().equals(without.getUniqueId()))
continue;
SWItem p = new SWItem();
ItemStack head = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
SkullMeta headmeta = (SkullMeta) head.getItemMeta();
headmeta.setOwningPlayer(player);
headmeta.setDisplayName(player.getName());
head.setItemMeta(headmeta);
p.setItemStack(head);
onlinePlayers.add(new Pair<>(p, player.getUniqueId()));
}
return onlinePlayers;
}
public static List<Pair<SWItem, Schematic>> getSchemList(Player p, SchematicType type){
List<Pair<SWItem, Schematic>> schemList = new ArrayList<>();
List<Schematic> schems;
if(type == null)
schems = Schematic.getSchemsAccessibleByUser(p.getUniqueId());
else
schems = Schematic.getSchemsOfType(p.getUniqueId(), type);
for(Schematic s : schems){
Material m;
if(s.getItem().isEmpty())
m = Material.CAULDRON_ITEM;
else
m = Material.valueOf(s.getItem());
SWItem item = new SWItem(m,"§e" + s.getSchemName());
schemList.add(new Pair<>(item, s));
}
return schemList;
}
}

Datei anzeigen

@ -95,19 +95,13 @@ public class Schematic {
}
public static List<Schematic> getSchemsOfType(int schemOwner, SchematicType schemType){
try{
ResultSet schematic = sql.select("SELECT * FROM Schematic WHERE SchemOwner = '" + schemOwner + "' AND SchemType = '" + schemType.name() + "'");
List<Schematic> schematics = new ArrayList<>();
while(schematic.next()){
String schemName = schematic.getString("SchemName");
String item = schematic.getString("Item");
schematics.add(new Schematic(schemName, schemOwner, item, schemType, false));
}
return schematics;
}catch(SQLException e){
e.printStackTrace();
}
return null;
//Unsauber, dafür auch geaddede Schematics dabei
List<Schematic> schems = getSchemsAccessibleByUser(schemOwner);
if(schems != null)
for(int i = schems.size()-1; i >= 0; i--)
if(!schems.get(i).getSchemType().equals(schemType))
schems.remove(i);
return schems;
}
public static List<Schematic> getAllSchemsOfType(SchematicType schemType){