SteamWar/SpigotCore
Archiviert
13
0

Implementing AnvilGUI + optional CustomItems in SWListInv + Refactoring

Dieser Commit ist enthalten in:
Lixfel 2020-12-21 12:59:49 +01:00
Ursprung c69abf4470
Commit 50d3252276
11 geänderte Dateien mit 141 neuen und 224 gelöschten Zeilen

Datei anzeigen

@ -1,32 +0,0 @@
/*
This file is a part of the SteamWar software.
Copyright (C) 2020 SteamWar.de-Serverteam
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.inventory;
import org.bukkit.craftbukkit.v1_10_R1.inventory.CraftInventoryCustom;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
class SWInventory_10 {
private SWInventory_10(){}
static Inventory get(Player player, int size, String title){
return new CraftInventoryCustom(player, size, title);
}
}

Datei anzeigen

@ -1,32 +0,0 @@
/*
This file is a part of the SteamWar software.
Copyright (C) 2020 SteamWar.de-Serverteam
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.inventory;
import org.bukkit.craftbukkit.v1_12_R1.inventory.CraftInventoryCustom;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
class SWInventory_12 {
private SWInventory_12(){}
static Inventory get(Player player, int size, String title){
return new CraftInventoryCustom(player, size, title);
}
}

Datei anzeigen

@ -1,32 +0,0 @@
/*
This file is a part of the SteamWar software.
Copyright (C) 2020 SteamWar.de-Serverteam
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.inventory;
import org.bukkit.craftbukkit.v1_14_R1.inventory.CraftInventoryCustom;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
class SWInventory_14 {
private SWInventory_14() {}
static Inventory get(Player player, int size, String title){
return new CraftInventoryCustom(player, size, title);
}
}

Datei anzeigen

@ -1,32 +0,0 @@
/*
This file is a part of the SteamWar software.
Copyright (C) 2020 SteamWar.de-Serverteam
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.inventory;
import org.bukkit.craftbukkit.v1_15_R1.inventory.CraftInventoryCustom;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
class SWInventory_15 {
private SWInventory_15() {}
static Inventory get(Player player, int size, String title){
return new CraftInventoryCustom(player, size, title);
}
}

Datei anzeigen

@ -1,32 +0,0 @@
/*
This file is a part of the SteamWar software.
Copyright (C) 2020 SteamWar.de-Serverteam
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.inventory;
import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftInventoryCustom;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
class SWInventory_8 {
private SWInventory_8(){}
static Inventory get(Player player, int size, String title){
return new CraftInventoryCustom(player, size, title);
}
}

Datei anzeigen

@ -1,32 +0,0 @@
/*
This file is a part of the SteamWar software.
Copyright (C) 2020 SteamWar.de-Serverteam
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.inventory;
import org.bukkit.craftbukkit.v1_9_R2.inventory.CraftInventoryCustom;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
class SWInventory_9 {
private SWInventory_9(){}
static Inventory get(Player player, int size, String title){
return new CraftInventoryCustom(player, size, title);
}
}

Datei anzeigen

@ -14,6 +14,13 @@
<main.basedir>${project.basedir}/..</main.basedir>
</properties>
<repositories>
<repository>
<id>codemc-snapshots</id>
<url>https://repo.codemc.io/repository/maven-snapshots/</url>
</repository>
</repositories>
<artifactId>SpigotCore_Main</artifactId>
<version>2.0</version>
<packaging>jar</packaging>
@ -69,6 +76,12 @@
<scope>system</scope>
<systemPath>${main.basedir}/lib/ProtocolLib.jar</systemPath>
</dependency>
<dependency>
<groupId>net.wesjd</groupId>
<artifactId>anvilgui</artifactId>
<version>1.4.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>steamwar</groupId>
<artifactId>SpigotCore_API</artifactId>

Datei anzeigen

@ -0,0 +1,84 @@
/*
This file is a part of the SteamWar software.
Copyright (C) 2020 SteamWar.de-Serverteam
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.inventory;
import de.steamwar.core.Core;
import net.wesjd.anvilgui.AnvilGUI;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.List;
public class SWAnvilInv {
private final AnvilGUI.Builder builder;
private final Player player;
private Response callback;
public SWAnvilInv(Player p, String t){
builder = new AnvilGUI.Builder().plugin(Core.getInstance()).title(t).text("»").onComplete(this::onResult);
player = p;
}
public void setItem(Material m){
setItem(m, new ArrayList<>(), false);
}
public void setItem(Material m, byte meta){
setItem(m, meta, new ArrayList<>(), false);
}
public void setItem(Material m, List<String> lore, boolean e){
setItem(m, (byte)0, lore, e);
}
public void setItem(Material m, byte meta, List<String> lore, boolean e){
builder.itemLeft(new SWItem(m, meta, "»", lore, e, null).getItemStack());
}
public void setCallback(Response callback){
this.callback = callback;
}
public void addCloseCallback(Runnable callback){
builder.onClose(p -> callback.run());
}
public void open(){
builder.open(player);
}
private AnvilGUI.Response onResult(Player player, String s) {
if(s.startsWith("»"))
s = s.substring(1);
callback.onResult(s);
return AnvilGUI.Response.close();
}
public void onClose(Player player) {
player.sendMessage("Closing");
}
public static interface Response{
void onResult(String s);
}
}

Datei anzeigen

@ -36,32 +36,14 @@ import java.util.Map;
public class SWInventory implements Listener {
Player player;
private Map<Integer, InvCallback> callbacks;
Inventory inventory;
final Player player;
private final Map<Integer, InvCallback> callbacks = new HashMap<>();
final Inventory inventory;
public SWInventory(Player p, int size, String t){
callbacks = new HashMap<>();
player = p;
switch(Core.getVersion()){
case 8:
inventory = SWInventory_8.get(p, size, t);
break;
case 9:
inventory = SWInventory_9.get(p, size, t);
break;
case 10:
inventory = SWInventory_10.get(p, size, t);
break;
case 14:
inventory = SWInventory_14.get(p, size, t);
break;
case 15:
inventory = SWInventory_15.get(p, size, t);
break;
default:
inventory = SWInventory_12.get(p, size, t);
}
inventory = Bukkit.createInventory(p, size, t);
}
public SWInventory(Player p, int size, String t, Map<Integer, SWItem> items){
@ -106,7 +88,7 @@ public class SWInventory implements Listener {
}
@EventHandler
public void onInventoryClick(InventoryClickEvent e){
private void onInventoryClick(InventoryClickEvent e){
if(!player.equals(e.getWhoClicked()))
return;
@ -116,7 +98,7 @@ public class SWInventory implements Listener {
}
@EventHandler
public void onInventoryClose(InventoryCloseEvent e){
private void onInventoryClose(InventoryCloseEvent e){
if(!player.equals(e.getPlayer()))
return;

Datei anzeigen

@ -103,10 +103,18 @@ public class SWItem {
this(material, (byte)0, name, new ArrayList<>(), false, null);
}
public SWItem(Material material, String name, InvCallback c){
this(material, (byte)0, name, new ArrayList<>(), false, c);
}
public SWItem(Material material, byte meta, String name){
this(material, meta, name, new ArrayList<>(), false, null);
}
public SWItem(Material material, byte meta, String name, InvCallback c){
this(material, meta, name, new ArrayList<>(), false, c);
}
public SWItem(Material material, String name, List<String> lore, boolean enchanted, InvCallback c) {
this(material, (byte)0, name, lore, enchanted, c);
}

Datei anzeigen

@ -30,21 +30,29 @@ import java.util.*;
public class SWListInv<T> extends SWInventory {
private final List<SWListEntry<T>> elements;
private final Map<Integer, SWItem> customItems = new HashMap<>();
private ListCallback<T> callback;
private List<SWListEntry<T>> elements;
private int page;
private boolean opened = false;
public SWListInv(Player p, String t, List<SWListEntry<T>> l, ListCallback<T> c){
super(p, (l.size()>45) ? 54 : (l.size() + 9-l.size()%9), t);
this(p, t, true, l, c);
}
public SWListInv(Player p, String t, boolean dynamicSize, List<SWListEntry<T>> l, ListCallback<T> c){
super(p, dynamicSize ? dynamicSize(l.size()) : 54, t);
callback = c;
elements = l;
page = 0;
setCallback(-999, (ClickType click) -> player.closeInventory());
}
@Override
public void open(){
opened = true;
inventory.clear();
setCallback(-999, (ClickType click) -> player.closeInventory());
if(elements.size() > 54){
if(page != 0)
setItem(45, SWItem.getDye(10), (byte)10, "§eSeite zurück", (ClickType click) -> {
@ -75,17 +83,23 @@ public class SWListInv<T> extends SWInventory {
setCallback(ipage, (ClickType click) -> callback.clicked(click, elements.get(pos).getObject()));
i++;
}
for(Map.Entry<Integer, SWItem> customItem : customItems.entrySet())
setItem(customItem.getKey(), customItem.getValue());
super.open();
}
@Override
public void setItem(int pos, SWItem item){
super.setItem(pos, item);
if(!opened)
customItems.put(pos, item);
}
public void setCallback(ListCallback<T> c){
callback = c;
}
public interface ListCallback<T>{
void clicked(ClickType click, T element);
}
public static List<SWListEntry<UUID>> createPlayerList(UUID without){
List<SWListEntry<UUID>> onlinePlayers = new ArrayList<>();
for(Player player : Bukkit.getOnlinePlayers()){
@ -118,6 +132,14 @@ public class SWListInv<T> extends SWInventory {
return schemList;
}
private static int dynamicSize(int size){
return (size>45) ? 54 : (size + 9-size%9);
}
public interface ListCallback<T>{
void clicked(ClickType click, T element);
}
public static class SWListEntry<T>{
final SWItem item;
final T object;