Refactor Navigator command; add bungee and player kit data packages
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
package com.softwarerat.API.bungee;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
import com.google.common.io.ByteStreams;
|
||||
|
||||
public class PlayerServerConnector {
|
||||
|
||||
JavaPlugin javaPlugin;
|
||||
|
||||
public PlayerServerConnector(JavaPlugin javaPlugin) {
|
||||
this.javaPlugin = javaPlugin;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void sendPlayer(Player player, String serverName) {
|
||||
if (player.hasPermission("group.Content")) {
|
||||
// player.sendMessage("hasperm");
|
||||
ByteArrayDataOutput out = ByteStreams.newDataOutput();
|
||||
out.writeUTF("Connect");
|
||||
out.writeUTF(serverName);
|
||||
|
||||
|
||||
player.sendPluginMessage(javaPlugin, "BungeeCord", out.toByteArray());
|
||||
// player.sendMessage("sucess");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -9,20 +9,21 @@ import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import com.softwarerat.API.builder.SpigotInventory;
|
||||
import com.softwarerat.API.builder.SpigotItem;
|
||||
import com.softwarerat.API.builder.SpigotUI;
|
||||
import com.softwarerat.API.bungee.PlayerServerConnector;
|
||||
|
||||
//Default Navigator
|
||||
public class Navigator implements CommandExecutor {
|
||||
JavaPlugin javaPlugin;
|
||||
|
||||
public Navigator(JavaPlugin javaPlugin) {
|
||||
PlayerServerConnector playerServerConnector;
|
||||
public Navigator(JavaPlugin javaPlugin, PlayerServerConnector playerServerConnector) {
|
||||
this.playerServerConnector = playerServerConnector;
|
||||
this.javaPlugin = javaPlugin;
|
||||
}
|
||||
|
||||
@SuppressWarnings("null")
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) {
|
||||
Player player = Bukkit.getPlayer(commandSender.getName());
|
||||
@@ -30,30 +31,19 @@ public class Navigator implements CommandExecutor {
|
||||
|
||||
|
||||
playerInventory.setSlot(new SpigotItem(Material.COMPASS).setName("Navigator").build(), 4, (click) -> openServerCreatorMenu(click.getPlayer()));
|
||||
player.sendMessage("give ITem");
|
||||
player.sendMessage("give Item");
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public void sendPlayer(Player player){
|
||||
if (player.hasPermission("group.Content")) {
|
||||
// player.sendMessage("hasperm");
|
||||
ByteArrayDataOutput out = ByteStreams.newDataOutput();
|
||||
out.writeUTF("Connect");
|
||||
out.writeUTF("build-1");
|
||||
|
||||
|
||||
player.sendPluginMessage(javaPlugin, "BungeeCord", out.toByteArray());
|
||||
// player.sendMessage("sucess");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("null")
|
||||
public void openServerCreatorMenu(Player player) {
|
||||
int rows = 6;
|
||||
SpigotUI gui = new SpigotUI(player, rows, "Server Auswählen",javaPlugin);
|
||||
gui.openInventory(player);
|
||||
gui.setSlot(new SpigotItem( Material.GRASS_BLOCK).setName("Bauserver").build(),10, (click) -> sendPlayer(player));
|
||||
gui.setSlot(new SpigotItem( Material.GRASS_BLOCK).setName("Bauserver").build(),10, (click) -> playerServerConnector.sendPlayer(player, "build-1"));
|
||||
// gui.setRunOnClose(() -> {});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
package com.softwarerat.API.data.player.kit;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemFlag;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.util.io.BukkitObjectInputStream;
|
||||
import org.bukkit.util.io.BukkitObjectOutputStream;
|
||||
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
|
||||
|
||||
public class KitAPI {
|
||||
private final HashMap<String, ItemStack[]> kits;
|
||||
|
||||
private final ConcurrentHashMap<UUID, String> playerKit;
|
||||
|
||||
private final ArrayList<Player> playersWithKits;
|
||||
|
||||
|
||||
public KitAPI() {
|
||||
this.kits = (HashMap)new HashMap<>();
|
||||
this.playerKit = new ConcurrentHashMap<>();
|
||||
this.playersWithKits = new ArrayList<>();
|
||||
}
|
||||
|
||||
public ItemStack[] loadKit(String base64) {
|
||||
System.out.println("Loading Kit with data: " + base64);
|
||||
try {
|
||||
return itemStackArrayFromBase64(base64);
|
||||
} catch (IOException iOException) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public ConcurrentHashMap<UUID, String> getPlayerKit() {
|
||||
return this.playerKit;
|
||||
}
|
||||
|
||||
public void giveKit(Player player, String base64) {
|
||||
player.getInventory().clear();
|
||||
System.out.println(this.playerKit);
|
||||
ItemStack[] kit = loadKit(base64);
|
||||
for (int i = 0; i < kit.length; i++) {
|
||||
ItemStack itemStack = kit[i];
|
||||
if (itemStack != null) {
|
||||
ItemMeta meta = itemStack.getItemMeta();
|
||||
assert meta != null;
|
||||
meta.setUnbreakable(true);
|
||||
meta.addItemFlags(new ItemFlag[] { ItemFlag.HIDE_ATTRIBUTES });
|
||||
itemStack.setItemMeta(meta);
|
||||
if (itemStack.getType().equals(Material.LEATHER_HELMET) || itemStack
|
||||
.getType().equals(Material.CHAINMAIL_HELMET) || itemStack
|
||||
.getType().equals(Material.IRON_HELMET) || itemStack
|
||||
.getType().equals(Material.GOLDEN_HELMET) || itemStack
|
||||
.getType().equals(Material.NETHERITE_HELMET) || itemStack
|
||||
.getType().equals(Material.DIAMOND_HELMET)) {
|
||||
player.getInventory().setHelmet(itemStack);
|
||||
} else if (itemStack.getType().equals(Material.LEATHER_CHESTPLATE) || itemStack
|
||||
.getType().equals(Material.CHAINMAIL_CHESTPLATE) || itemStack
|
||||
.getType().equals(Material.IRON_CHESTPLATE) || itemStack
|
||||
.getType().equals(Material.NETHERITE_CHESTPLATE) || itemStack
|
||||
.getType().equals(Material.GOLDEN_CHESTPLATE) || itemStack
|
||||
.getType().equals(Material.DIAMOND_CHESTPLATE)) {
|
||||
player.getInventory().setChestplate(itemStack);
|
||||
} else if (itemStack.getType().equals(Material.LEATHER_LEGGINGS) || itemStack
|
||||
.getType().equals(Material.CHAINMAIL_LEGGINGS) || itemStack
|
||||
.getType().equals(Material.IRON_LEGGINGS) || itemStack
|
||||
.getType().equals(Material.GOLDEN_LEGGINGS) || itemStack
|
||||
.getType().equals(Material.NETHERITE_LEGGINGS) || itemStack
|
||||
.getType().equals(Material.DIAMOND_LEGGINGS)) {
|
||||
player.getInventory().setLeggings(itemStack);
|
||||
} else if (itemStack.getType().equals(Material.LEATHER_BOOTS) || itemStack
|
||||
.getType().equals(Material.CHAINMAIL_BOOTS) || itemStack
|
||||
.getType().equals(Material.IRON_BOOTS) || itemStack
|
||||
.getType().equals(Material.GOLDEN_BOOTS) || itemStack
|
||||
.getType().equals(Material.NETHERITE_BOOTS) || itemStack
|
||||
.getType().equals(Material.DIAMOND_BOOTS)) {
|
||||
player.getInventory().setBoots(itemStack);
|
||||
} else {
|
||||
player.getInventory().setItem(i, itemStack);
|
||||
}
|
||||
}
|
||||
}
|
||||
this.playersWithKits.add(player);
|
||||
}
|
||||
|
||||
public String itemStackArrayToBase64(ItemStack[] items) throws IllegalStateException {
|
||||
try {
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);
|
||||
dataOutput.writeInt(items.length);
|
||||
for (int i = 0; i < items.length; i++)
|
||||
dataOutput.writeObject(items[i]);
|
||||
dataOutput.close();
|
||||
return Base64Coder.encodeLines(outputStream.toByteArray());
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException("Unable to save item stacks.", e);
|
||||
}
|
||||
}
|
||||
|
||||
public ItemStack[] itemStackArrayFromBase64(String data) throws IOException {
|
||||
try {
|
||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data));
|
||||
BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
|
||||
ItemStack[] items = new ItemStack[dataInput.readInt()];
|
||||
for (int i = 0; i < items.length; i++)
|
||||
items[i] = (ItemStack)dataInput.readObject();
|
||||
dataInput.close();
|
||||
return items;
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new IOException("Unable to decode class type.", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void saveKit(Player player, String name) {
|
||||
String kitBase64 = itemStackArrayToBase64(player.getInventory().getContents());
|
||||
|
||||
|
||||
this.kits.put(name, player.getInventory().getContents());
|
||||
}
|
||||
|
||||
|
||||
public HashMap<String,ItemStack[]> getKits() {
|
||||
return this.kits;
|
||||
}
|
||||
|
||||
|
||||
public ArrayList<Player> getPlayersWithKits() {
|
||||
return this.playersWithKits;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.softwarerat.API.data.player.kit;
|
||||
|
||||
|
||||
public class PlayerKit {
|
||||
String kitName;
|
||||
String kitDescription;
|
||||
String kitBase64;
|
||||
public String getKitName() {
|
||||
return kitName;
|
||||
}
|
||||
public void setKitName(String kitName) {
|
||||
this.kitName = kitName;
|
||||
}
|
||||
public String getKitDescription() {
|
||||
return kitDescription;
|
||||
}
|
||||
public void setKitDescription(String kitDescription) {
|
||||
this.kitDescription = kitDescription;
|
||||
}
|
||||
public String getKitBase64() {
|
||||
return kitBase64;
|
||||
}
|
||||
public void setKitBase64(String kitBase64) {
|
||||
this.kitBase64 = kitBase64;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user