bug fixe around serelization of items (armor and off hand slots, nbt tags error)
created command class, Class.java and ClassCommand.java for future of add of classes ig.
This commit is contained in:
unurled 2022-02-24 22:04:29 +01:00
parent 168eb600bc
commit 54f6377c10
29 changed files with 196 additions and 112 deletions

View file

@ -1,40 +0,0 @@
# To contribute improvements to CI/CD templates, please follow the Development guide at:
# https://docs.gitlab.com/ee/development/cicd/templates.html
# This specific template is located at:
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Gradle.gitlab-ci.yml
# This is the Gradle build system for JVM applications
# https://gradle.org/
# https://github.com/gradle/gradle
image: gradle:alpine
# Disable the Gradle daemon for Continuous Integration servers as correctness
# is usually a priority over speed in CI environments. Using a fresh
# runtime for each build is more reliable since the runtime is completely
# isolated from any previous builds.
variables:
GRADLE_OPTS: "-Dorg.gradle.daemon=false"
before_script:
- export GRADLE_USER_HOME=`pwd`/.gradle
build:
stage: build
script: gradle --build-cache assemble
cache:
key: "$CI_COMMIT_REF_NAME"
policy: push
paths:
- build
- .gradle
test:
stage: test
script: gradle check
cache:
key: "$CI_COMMIT_REF_NAME"
policy: pull
paths:
- build
- .gradle

View file

@ -10,6 +10,7 @@ import gq.unurled.raxen.utils.Reload;
import gq.unurled.raxen.utils.Vault;
import lombok.Getter;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
@ -20,7 +21,7 @@ public final class Raxen extends JavaPlugin {
private static final String prefix = ChatColor.AQUA + "Rx" + ChatColor.LIGHT_PURPLE + "> ";
@Getter
private static String version = "";
private static String version = "0.4.4";
private final PluginManager pm = getServer().getPluginManager();
private static Raxen plugin;
@ -83,6 +84,8 @@ public final class Raxen extends JavaPlugin {
registerEvents();
getServer().getConsoleSender().sendMessage(Component.text(prefix +"§aServer Started Successfully!"));
listenerManager.getServerPingEvent().setLoading(false);
}
private void registerCommands() {
@ -99,6 +102,7 @@ public final class Raxen extends JavaPlugin {
@Override
public void onDisable() {
listenerManager.getServerPingEvent().setLoading(true);
Reload.kickAll();
playerConfig.close();

View file

@ -2,7 +2,7 @@ package gq.unurled.raxen.commands.admin;
import de.tr7zw.nbtapi.NBTItem;
import gq.unurled.raxen.Raxen;
import gq.unurled.raxen.components.player.Attributes.Attributes;
import gq.unurled.raxen.components.player.attributes.Attributes;
import net.kyori.adventure.text.Component;
import org.bukkit.Material;
import org.bukkit.command.Command;

View file

@ -42,6 +42,8 @@ public class SpawnEntity implements TabExecutor {
EntityType type = EntityType.valueOf(args[0]);
Entity e = player.getWorld().spawnEntity(player.getLocation(), type, false);
setNameSpacedKeys(e, "&cName", 100, 100,0,50,0,100,0,100,0);
e.setCustomName(color(args[1]));
e.setCustomNameVisible(true);
return false;
}

View file

@ -0,0 +1,37 @@
package gq.unurled.raxen.commands.player;
import gq.unurled.raxen.Raxen;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import static gq.unurled.raxen.utils.Utils.error;
public class ClassCommand implements TabExecutor {
private Raxen main;
public ClassCommand(Raxen main) {
this.main = main;
}
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if(!(sender instanceof Player)) {
error("The console can't execute this Command!");
return true;
}
Player player = (Player) sender;
return false;
}
@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
return null;
}
}

View file

@ -1,7 +1,7 @@
package gq.unurled.raxen.commands.player;
import gq.unurled.raxen.Raxen;
import gq.unurled.raxen.components.player.Attributes.Attributes;
import gq.unurled.raxen.components.player.attributes.Attributes;
import net.kyori.adventure.text.Component;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;

View file

@ -1,7 +1,7 @@
package gq.unurled.raxen.components.entity;
import gq.unurled.raxen.Raxen;
import gq.unurled.raxen.components.player.Attributes.Attribute;
import gq.unurled.raxen.components.player.attributes.Attribute;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.persistence.PersistentDataContainer;

View file

@ -1,7 +1,6 @@
package gq.unurled.raxen.components.entity;
import gq.unurled.raxen.Raxen;
import gq.unurled.raxen.components.player.Attributes.Attribute;
import org.bukkit.entity.LivingEntity;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;

View file

@ -216,6 +216,7 @@ public class ItemListGui implements Listener {
//add to stash
//}
player.getInventory().addItem(e.getCurrentItem());
gq.unurled.raxen.utils.Skills.updateSkills(main, player);
}
}
}

View file

@ -1,6 +1,6 @@
package gq.unurled.raxen.components.player;
import gq.unurled.raxen.components.player.Storages.Inventory;
import gq.unurled.raxen.components.player.storages.Inventory;
public class Inventories {
private Inventory inv;

View file

@ -2,10 +2,10 @@ package gq.unurled.raxen.components.player;
import gq.unurled.raxen.Raxen;
import gq.unurled.raxen.components.entity.EntityNamespacedKey;
import gq.unurled.raxen.components.player.Attributes.Attribute;
import gq.unurled.raxen.components.player.Attributes.Attributes;
import gq.unurled.raxen.components.player.Storages.EnderChest;
import gq.unurled.raxen.components.player.Storages.Inventory;
import gq.unurled.raxen.components.player.attributes.Attribute;
import gq.unurled.raxen.components.player.attributes.Attributes;
import gq.unurled.raxen.components.player.storages.EnderChest;
import gq.unurled.raxen.components.player.storages.Inventory;
import gq.unurled.raxen.utils.Items;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
@ -69,7 +69,7 @@ public class RaxenPlayer {
String inv = data.get(namespacedKey.inventory, PersistentDataType.STRING);
org.bukkit.inventory.Inventory invv = Bukkit.createInventory(player, InventoryType.PLAYER);
invv = setItemsToInventory(Items.listItemStackDeserilize(inv), invv);
gq.unurled.raxen.components.player.Storages.Inventory invvv = new gq.unurled.raxen.components.player.Storages.Inventory(invv);
gq.unurled.raxen.components.player.storages.Inventory invvv = new gq.unurled.raxen.components.player.storages.Inventory(invv);
Inventories invvvv = new Inventories(invvv);
return invvvv;
}

View file

@ -1,6 +1,6 @@
package gq.unurled.raxen.components.player;
import gq.unurled.raxen.components.player.Storages.EnderChest;
import gq.unurled.raxen.components.player.storages.EnderChest;
public class Storage {

View file

@ -1,4 +1,4 @@
package gq.unurled.raxen.components.player.Attributes;
package gq.unurled.raxen.components.player.attributes;
import lombok.Getter;
import lombok.Setter;

View file

@ -1,4 +1,4 @@
package gq.unurled.raxen.components.player.Attributes;
package gq.unurled.raxen.components.player.attributes;
import gq.unurled.raxen.Raxen;
import gq.unurled.raxen.components.entity.EntityNamespacedKey;

View file

@ -0,0 +1,4 @@
package gq.unurled.raxen.components.player.classes;
public class Class {
}

View file

@ -1,4 +1,4 @@
package gq.unurled.raxen.components.player.Storages;
package gq.unurled.raxen.components.player.storages;
import lombok.Getter;
import lombok.Setter;

View file

@ -1,4 +1,4 @@
package gq.unurled.raxen.components.player.Storages;
package gq.unurled.raxen.components.player.storages;
import lombok.Getter;
import lombok.Setter;

View file

@ -3,10 +3,10 @@ package gq.unurled.raxen.config;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Filters;
import gq.unurled.raxen.Raxen;
import gq.unurled.raxen.components.player.Attributes.Attributes;
import gq.unurled.raxen.components.player.attributes.Attributes;
import gq.unurled.raxen.components.player.Inventories;
import gq.unurled.raxen.components.player.Storage;
import gq.unurled.raxen.components.player.Storages.EnderChest;
import gq.unurled.raxen.components.player.storages.EnderChest;
import gq.unurled.raxen.manager.PlayerManager;
import gq.unurled.raxen.manager.StorageManager;
import gq.unurled.raxen.utils.Items;
@ -165,7 +165,7 @@ public class PlayerConfig {
inv = setItemsToInventory(listItemStackDeserilize(playerDoc.getString("inv")), inv);
player.getInventory().setContents(inv.getContents());
player.updateInventory();
gq.unurled.raxen.components.player.Storages.Inventory invv = new gq.unurled.raxen.components.player.Storages.Inventory(inv);
gq.unurled.raxen.components.player.storages.Inventory invv = new gq.unurled.raxen.components.player.storages.Inventory(inv);
Inventories invvv = new Inventories(invv);
playerManager.getRaxenPlayer(player).setInventory(inv);
}
@ -189,7 +189,7 @@ public class PlayerConfig {
inv = setItemsToInventory(listItemStackDeserilize((String) config.getString("inv")), inv);
player.getInventory().setContents(inv.getContents());
player.updateInventory();
gq.unurled.raxen.components.player.Storages.Inventory invv = new gq.unurled.raxen.components.player.Storages.Inventory(inv);
gq.unurled.raxen.components.player.storages.Inventory invv = new gq.unurled.raxen.components.player.storages.Inventory(inv);
Inventories invvv = new Inventories(invv);
playerManager.getRaxenPlayer(player).setInventory(inv);
}

View file

@ -41,7 +41,7 @@ public class DamageEntity implements Listener {
debug(main, e.getEntity().getName(), e.getEntity().getType().toString(), e.getDamager().getType().toString(), e.getDamager().getName());
if (e.getDamager() instanceof Player) {
Player playerDamager = (Player) e.getDamager();
gq.unurled.raxen.components.player.Attributes.Attributes attributes = new gq.unurled.raxen.components.player.Attributes.Attributes(main);
gq.unurled.raxen.components.player.attributes.Attributes attributes = new gq.unurled.raxen.components.player.attributes.Attributes(main);
strength = attributes.getStrength(playerDamager);
itemDmg = attributes.getItemStrength(playerDamager);
} else {
@ -55,7 +55,7 @@ public class DamageEntity implements Listener {
}
if (e.getEntity() instanceof Player) {
Player playerVictim = (Player) e.getEntity();
gq.unurled.raxen.components.player.Attributes.Attributes attributes = new gq.unurled.raxen.components.player.Attributes.Attributes(main);
gq.unurled.raxen.components.player.attributes.Attributes attributes = new gq.unurled.raxen.components.player.attributes.Attributes(main);
defense = attributes.getDefense(playerVictim);
health = attributes.getHealth(playerVictim);
itemDefense = attributes.getItemDefense(playerVictim);
@ -97,7 +97,7 @@ public class DamageEntity implements Listener {
}
if (e.getEntity() instanceof Player) {
Player playerVictim = (Player) e.getEntity();
gq.unurled.raxen.components.player.Attributes.Attributes attributes = new gq.unurled.raxen.components.player.Attributes.Attributes(main);
gq.unurled.raxen.components.player.attributes.Attributes attributes = new gq.unurled.raxen.components.player.attributes.Attributes(main);
attributes.setHealth(playerVictim, health);
} else {
Entity entityVictim = e.getEntity();
@ -113,7 +113,7 @@ public class DamageEntity implements Listener {
e.setDamage(0);
if (e.getEntity() instanceof Player) {
Player player = (Player) e.getEntity();
gq.unurled.raxen.components.player.Attributes.Attributes attributes = new gq.unurled.raxen.components.player.Attributes.Attributes(main);
gq.unurled.raxen.components.player.attributes.Attributes attributes = new gq.unurled.raxen.components.player.attributes.Attributes(main);
Integer health = attributes.getHealth(player);
}

View file

@ -3,7 +3,7 @@ package gq.unurled.raxen.listener.player;
import com.destroystokyo.paper.event.player.PlayerArmorChangeEvent;
import de.tr7zw.nbtapi.NBTItem;
import gq.unurled.raxen.Raxen;
import gq.unurled.raxen.components.player.Attributes.Attributes;
import gq.unurled.raxen.components.player.attributes.Attributes;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;

View file

@ -27,10 +27,12 @@ public class ClickBlockEvent implements Listener {
Block block = e.getClickedBlock();
Action action = e.getAction();
if (action.isRightClick()) {
if (block.getType() == Material.CHEST) {
player.closeInventory();
Inventory inv = gui.addItems(player);
player.openInventory(inv);
if (block != null) {
if (block.getType() == Material.CHEST) {
player.closeInventory();
Inventory inv = gui.addItems(player);
player.openInventory(inv);
}
}
}
}

View file

@ -2,7 +2,7 @@ package gq.unurled.raxen.listener.player;
import de.tr7zw.nbtapi.NBTItem;
import gq.unurled.raxen.Raxen;
import gq.unurled.raxen.components.player.Attributes.Attributes;
import gq.unurled.raxen.components.player.attributes.Attributes;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;

View file

@ -0,0 +1,37 @@
package gq.unurled.raxen.listener.player;
import gq.unurled.raxen.Raxen;
import lombok.Getter;
import lombok.Setter;
import net.kyori.adventure.text.Component;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.server.ServerListPingEvent;
import static gq.unurled.raxen.utils.Utils.color;
public class ServerPingEvent implements Listener {
private Raxen main;
@Getter
@Setter
private boolean loading = false;
public ServerPingEvent(Raxen main) {
this.main = main;
this.loading = true;
}
@EventHandler
public void ServerListPingEvent(ServerListPingEvent e) {
FileConfiguration config = this.main.getConfig();
String motd = config.getString("motd");
String motdReload = config.getString("motd-reload");
if (loading) {
e.motd(Component.text(color(motdReload)));
} else {
e.motd(Component.text(color(motd)));
}
}
}

View file

@ -3,6 +3,7 @@ package gq.unurled.raxen.manager;
import gq.unurled.raxen.Raxen;
import gq.unurled.raxen.commands.RaxenCommand;
import gq.unurled.raxen.commands.admin.*;
import gq.unurled.raxen.commands.player.ClassCommand;
import gq.unurled.raxen.commands.player.SkillsCommand;
import gq.unurled.raxen.commands.player.StorageCommand;
import lombok.Getter;
@ -20,12 +21,14 @@ public class CommandManager {
private SkillsCommand skillsCommand = new SkillsCommand(main);
private RaxenCommand raxenCommand = new RaxenCommand(main);
private SpawnEntity entityspawn;
private ClassCommand classCommand;
public CommandManager(Raxen main) {
this.main = main;
this.itemListCommand = new ItemListCommand(this.main);
this.nbtCommand = new NbtCommand(this.main);
this.entityspawn = new SpawnEntity(this.main);
this.classCommand = new ClassCommand(this.main);
}
public void register() {
@ -53,6 +56,9 @@ public class CommandManager {
main.getCommand("entityspawn").setTabCompleter(entityspawn);
main.getCommand("entityspawn").setExecutor(entityspawn);
main.getCommand("class").setTabCompleter(classCommand);
main.getCommand("class").setExecutor(classCommand);
}
}

View file

@ -5,16 +5,20 @@ import gq.unurled.raxen.listener.entity.DamageEntity;
import gq.unurled.raxen.listener.entity.SpawnEvent;
import gq.unurled.raxen.listener.player.*;
import gq.unurled.raxen.utils.Reload;
import lombok.Getter;
import org.bukkit.plugin.PluginManager;
public class ListenerManager {
private final Raxen main;
private final PluginManager pm;
@Getter
private ServerPingEvent serverPingEvent;
public ListenerManager(Raxen main) {
this.main = main;
this.pm = main.getPm();
this.serverPingEvent = new ServerPingEvent(main);
}
public void register() {
@ -28,5 +32,7 @@ public class ListenerManager {
this.pm.registerEvents(new DamageEntity(main), main);
this.pm.registerEvents(new SpawnEvent(main), main);
this.pm.registerEvents(new ClickBlockEvent(main), main);
this.pm.registerEvents(serverPingEvent, main);
}
}

View file

@ -1,8 +1,7 @@
package gq.unurled.raxen.manager;
import gq.unurled.raxen.Raxen;
import gq.unurled.raxen.components.player.Attributes.Attribute;
import gq.unurled.raxen.components.player.Attributes.Attributes;
import gq.unurled.raxen.components.player.attributes.Attribute;
import gq.unurled.raxen.components.player.RaxenPlayer;
import net.kyori.adventure.text.Component;
import org.bukkit.entity.Player;
@ -48,6 +47,6 @@ public class PlayerManager {
}
}
public void unEegisterRaxenPlayer(Player player) {
public void unRegisterRaxenPlayer(Player player) {
}
}

View file

@ -12,11 +12,16 @@ import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.LivingEntity;
import org.bukkit.inventory.*;
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;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.*;
import static gq.unurled.raxen.utils.Utils.color;
import static gq.unurled.raxen.utils.Utils.error;
import static gq.unurled.raxen.utils.Utils.*;
public class Items {
@ -67,12 +72,8 @@ public class Items {
}
public static String itemStackSerilize(ItemStack it, Integer slot) {
Gson gson = new Gson();
String str = gson.toJson(it.serialize());
net.minecraft.world.item.ItemStack itm = CraftItemStack.asNMSCopy(it);
//String str = gson.toJson(it.serialize(), new TypeToken<Map>(){}.getType());
NBTItem nbti = new NBTItem(it);
str = str + "@#NBT#" + nbti.toString();
String str = "";
str = itemTo64(it);
str = str + "@#SLOT#" + slot.toString();
return str;
}
@ -95,41 +96,21 @@ public class Items {
}
}
public static itemStackDeserilizeResult itemStackDeserilize(String str) {
List<String> mapp = Arrays.asList(str.split("@#NBT#"));
public static itemStackDeserilizeResult itemStackDeserilize(String str) {
List<String> mapp = Arrays.asList(str.split("@#SLOT#"));
Gson gson = new Gson();
ItemStack it = new ItemStack(Material.AIR);
Integer slot = 0;
log("full item " + str);
for (String s : mapp) {
if(s != null && s.length() > 2) {
if(s != null) {
try {
Map<String, Object> map = gson.fromJson(s, new TypeToken<Map<String, Object>>(){}.getType());
it = ItemStack.deserialize(map);
it = itemFrom64(s);
} catch (Exception e) {
List<String> mappp = Arrays.asList(s.split("@#SLOT#"));
for (String ss : mappp) {
try {
if (ss != null && ss.length() > 2) {
NBTItem nbti = new NBTItem(it);
String strrr = ss;
if (strrr.length() > 2) {
for (String strr : strrr.split(",")) {
if (!strr.equals("{") && !strr.equals("}")) {
strr = strr.replace("{", "");
strr = strr.replace("}", "");
String[] nb = strr.split(":");
nbti.setInteger(nb[0], Integer.valueOf(nb[1]));
it = nbti.getItem();
}
}
}
}
} catch (Exception ee) {
slot = Integer.valueOf(ss);
}
if (!s.equals("")) {
log("slot " + s);
slot = Integer.valueOf(s);
}
}
}
@ -178,11 +159,53 @@ public class Items {
public static PlayerInventory setItemsToInventory(HashMap<Integer, ItemStack> list, PlayerInventory inv) {
for (Map.Entry<Integer, ItemStack> entry : list.entrySet()) {
inv.setItem(entry.getKey(), entry.getValue());
if (entry.getKey() == 40) {
inv.setHelmet(entry.getValue());
} else if (entry.getKey() == 39) {
inv.setChestplate(entry.getValue());
} else if (entry.getKey() == 38) {
inv.setLeggings(entry.getValue());
} else if (entry.getKey() == 37) {
inv.setBoots(entry.getValue());
} else if (entry.getKey() == 41) {
inv.setItemInOffHand(entry.getValue());
}
else {
inv.setItem(entry.getKey(), entry.getValue());
}
}
return inv;
}
private static String itemTo64(ItemStack stack) throws IllegalStateException {
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);
dataOutput.writeObject(stack);
// Serialize that array
dataOutput.close();
return Base64Coder.encodeLines(outputStream.toByteArray());
}
catch (Exception e) {
throw new IllegalStateException("Unable to save item stack.", e);
}
}
private static ItemStack itemFrom64(String data) throws IOException {
try {
ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data));
BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
try {
return (ItemStack) dataInput.readObject();
} finally {
dataInput.close();
}
}
catch (ClassNotFoundException e) {
throw new IOException("Unable to decode class type.", e);
}
}
public static String attributes(String str) {
String stt = "";

View file

@ -1,7 +1,7 @@
package gq.unurled.raxen.utils;
import gq.unurled.raxen.Raxen;
import gq.unurled.raxen.components.player.Attributes.Attributes;
import gq.unurled.raxen.components.player.attributes.Attributes;
import org.bukkit.attribute.Attribute;
import org.bukkit.entity.Player;

View file

@ -29,6 +29,8 @@ commands:
description: Open ItemList menu
entityspawn:
description: Spawn an custom entity
class:
description: player command that make them choose their class and then their stats
permissions:
raxen.reload.cmd:
@ -42,4 +44,6 @@ permissions:
raxen.itemlist.cmd:
description: itemlist command permission
raxen.entityspawn.cmd:
description: entityspawn command permission
description: entityspawn command permission
raxen.class.cmd:
description: class command premission