Added ArgumentItemStack

This commit is contained in:
themode 2020-10-16 14:31:15 +02:00
parent 8561e0cddc
commit 512b30e9df
8 changed files with 149 additions and 59 deletions

View File

@ -1,10 +1,7 @@
package net.minestom.server.chat; package net.minestom.server.chat;
import com.google.gson.JsonArray; import com.google.gson.*;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
/** /**
* Class used to convert JSON string to proper chat message representation. * Class used to convert JSON string to proper chat message representation.
@ -20,22 +17,42 @@ public final class ChatParser {
* @return a {@link ColoredText} representing the text * @return a {@link ColoredText} representing the text
*/ */
public static ColoredText toColoredText(String json) { public static ColoredText toColoredText(String json) {
json = json.replace("\\\"", "\"");
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
final JsonObject object = JsonParser.parseString(json).getAsJsonObject(); try {
final JsonElement element = JsonParser.parseString(json);
if (element instanceof JsonObject) {
final JsonObject object = element.getAsJsonObject();
appendBuilder(builder, object);
} else if (element instanceof JsonArray) {
final JsonArray array = element.getAsJsonArray();
for (JsonElement e : array) {
JsonObject object = e.getAsJsonObject();
appendBuilder(builder, object);
}
}
return ColoredText.of(builder.toString());
} catch (JsonSyntaxException e) {
// Not a json text
return ColoredText.of(json);
}
}
private static void appendBuilder(StringBuilder builder, JsonObject object) {
builder.append(parseText(object)); builder.append(parseText(object));
final boolean hasExtra = object.has("extra"); final boolean hasExtra = object.has("extra");
if (hasExtra) { if (hasExtra) {
JsonArray extraArray = object.get("extra").getAsJsonArray(); JsonArray extraArray = object.get("extra").getAsJsonArray();
for (JsonElement element : extraArray) { for (JsonElement extraElement : extraArray) {
JsonObject extraObject = element.getAsJsonObject(); JsonObject extraObject = extraElement.getAsJsonObject();
builder.append(parseText(extraObject)); builder.append(parseText(extraObject));
} }
} }
return ColoredText.of(builder.toString());
} }
/** /**
@ -49,13 +66,25 @@ public final class ChatParser {
if (!hasText) if (!hasText)
return ""; return "";
final boolean hasColor = textObject.has("color");
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
// Add color appendColor(textObject, builder);
if (hasColor) { appendExtra(textObject, builder, "bold");
String colorString = textObject.get("color").getAsString(); appendExtra(textObject, builder, "italic");
appendExtra(textObject, builder, "underlined");
appendExtra(textObject, builder, "strikethrough");
appendExtra(textObject, builder, "obfuscated");
// Add text
final String text = textObject.get("text").getAsString();
builder.append(text);
return builder.toString();
}
private static void appendColor(JsonObject textObject, StringBuilder builder) {
if (textObject.has("color")) {
final String colorString = textObject.get("color").getAsString();
if (colorString.startsWith("#")) { if (colorString.startsWith("#")) {
// RGB format // RGB format
builder.append("{").append(colorString).append("}"); builder.append("{").append(colorString).append("}");
@ -65,11 +94,14 @@ public final class ChatParser {
builder.append(color); builder.append(color);
} }
} }
}
// Add text private static void appendExtra(JsonObject textObject, StringBuilder builder, String name) {
String text = textObject.get("text").getAsString(); if (textObject.has(name)) {
builder.append(text); final boolean value = textObject.get(name).getAsBoolean();
if (value) {
return builder.toString(); builder.append("{#").append(name).append("}");
}
}
} }
} }

View File

@ -512,6 +512,9 @@ public final class CommandManager {
} }
packetWriter.writeByte(mask); packetWriter.writeByte(mask);
}; };
} else if (argument instanceof ArgumentItemStack) {
DeclareCommandsPacket.Node argumentNode = simpleArgumentNode(nodes, argument, executable, false);
argumentNode.parser = "minecraft:item_stack";
} }
return nodes; return nodes;

View File

@ -4,6 +4,7 @@ import net.minestom.server.chat.ChatColor;
import net.minestom.server.entity.Entity; import net.minestom.server.entity.Entity;
import net.minestom.server.entity.EntityType; import net.minestom.server.entity.EntityType;
import net.minestom.server.item.Enchantment; import net.minestom.server.item.Enchantment;
import net.minestom.server.item.ItemStack;
import net.minestom.server.particle.Particle; import net.minestom.server.particle.Particle;
import net.minestom.server.potion.PotionEffect; import net.minestom.server.potion.PotionEffect;
import net.minestom.server.utils.math.FloatRange; import net.minestom.server.utils.math.FloatRange;
@ -89,6 +90,10 @@ public class Arguments {
return (List<Entity>) getObject(id); return (List<Entity>) getObject(id);
} }
public ItemStack getItemStack(String id) {
return (ItemStack) getObject(id);
}
public Object getObject(String id) { public Object getObject(String id) {
return args.getOrDefault(id, null); return args.getOrDefault(id, null);
} }

View File

@ -94,4 +94,8 @@ public class ArgumentType {
return new ArgumentEntities(id); return new ArgumentEntities(id);
} }
public static ArgumentItemStack ItemStack(String id) {
return new ArgumentItemStack(id);
}
} }

View File

@ -0,0 +1,73 @@
package net.minestom.server.command.builder.arguments.minecraft;
import net.minestom.server.command.builder.arguments.Argument;
import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material;
import net.minestom.server.registry.Registries;
import net.minestom.server.utils.NBTUtils;
import org.jglrxavpok.hephaistos.nbt.NBT;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import org.jglrxavpok.hephaistos.nbt.SNBTParser;
import java.io.StringReader;
public class ArgumentItemStack extends Argument<ItemStack> {
public static final int NO_MATERIAL = 1;
public static final int INVALID_NBT = 2;
public ArgumentItemStack(String id) {
super(id, true);
}
@Override
public int getCorrectionResult(String value) {
if (value.startsWith("{")) {
return NO_MATERIAL;
}
final int nbtIndex = value.indexOf("{");
if (nbtIndex == -1 && !value.contains(" ")) {
// Only item name
return SUCCESS;
} else {
// has nbt
final String sNBT = value.substring(nbtIndex);
try {
NBT nbt = new SNBTParser(new StringReader(sNBT)).parse();
return nbt instanceof NBTCompound ? SUCCESS : INVALID_NBT;
} catch (Exception e) {
return INVALID_NBT;
}
}
}
@Override
public ItemStack parse(String value) {
final int nbtIndex = value.indexOf("{");
if (nbtIndex == -1) {
// Only item name
final Material material = Registries.getMaterial(value);
return new ItemStack(material, (byte) 1);
} else {
final String materialName = value.substring(0, nbtIndex);
final Material material = Registries.getMaterial(materialName);
ItemStack itemStack = new ItemStack(material, (byte) 1);
final String sNBT = value.substring(nbtIndex);
final NBTCompound compound = (NBTCompound) new SNBTParser(new StringReader(sNBT)).parse();
NBTUtils.loadDataIntoItem(itemStack, compound);
return itemStack;
}
}
@Override
public int getConditionResult(ItemStack value) {
return SUCCESS;
}
}

View File

@ -121,8 +121,12 @@ public final class NBTUtils {
if (nbt.containsKey("Unbreakable")) item.setUnbreakable(nbt.getInt("Unbreakable") == 1); if (nbt.containsKey("Unbreakable")) item.setUnbreakable(nbt.getInt("Unbreakable") == 1);
if (nbt.containsKey("HideFlags")) item.setHideFlag(nbt.getInt("HideFlags")); if (nbt.containsKey("HideFlags")) item.setHideFlag(nbt.getInt("HideFlags"));
if (nbt.containsKey("display")) { if (nbt.containsKey("display")) {
NBTCompound display = nbt.getCompound("display"); final NBTCompound display = nbt.getCompound("display");
if (display.containsKey("Name")) item.setDisplayName(ChatParser.toColoredText(display.getString("Name"))); if (display.containsKey("Name")) {
final String rawName = display.getString("Name");
final ColoredText displayName = ChatParser.toColoredText(rawName);
item.setDisplayName(displayName);
}
if (display.containsKey("Lore")) { if (display.containsKey("Lore")) {
NBTList<NBTString> loreList = display.getList("Lore"); NBTList<NBTString> loreList = display.getList("Lore");
ArrayList<ColoredText> lore = new ArrayList<>(); ArrayList<ColoredText> lore = new ArrayList<>();
@ -182,7 +186,7 @@ public final class NBTUtils {
public static void loadEnchantments(NBTList<NBTCompound> enchantments, EnchantmentSetter setter) { public static void loadEnchantments(NBTList<NBTCompound> enchantments, EnchantmentSetter setter) {
for (NBTCompound enchantment : enchantments) { for (NBTCompound enchantment : enchantments) {
final short level = enchantment.getShort("lvl"); final short level = enchantment.getAsShort("lvl");
final String id = enchantment.getString("id"); final String id = enchantment.getString("id");
final Enchantment enchant = Registries.getEnchantment(id); final Enchantment enchant = Registries.getEnchantment(id);
if (enchant != null) { if (enchant != null) {

View File

@ -7,10 +7,7 @@ import net.minestom.server.MinecraftServer;
import net.minestom.server.attribute.Attribute; import net.minestom.server.attribute.Attribute;
import net.minestom.server.attribute.AttributeOperation; import net.minestom.server.attribute.AttributeOperation;
import net.minestom.server.benchmark.BenchmarkManager; import net.minestom.server.benchmark.BenchmarkManager;
import net.minestom.server.chat.ChatColor;
import net.minestom.server.chat.ChatHoverEvent;
import net.minestom.server.chat.ColoredText; import net.minestom.server.chat.ColoredText;
import net.minestom.server.chat.RichMessage;
import net.minestom.server.data.Data; import net.minestom.server.data.Data;
import net.minestom.server.entity.*; import net.minestom.server.entity.*;
import net.minestom.server.entity.damage.DamageType; import net.minestom.server.entity.damage.DamageType;
@ -254,18 +251,6 @@ public class PlayerInit {
player.setGameMode(GameMode.CREATIVE); player.setGameMode(GameMode.CREATIVE);
player.teleport(new Position(0, 73f, 0)); player.teleport(new Position(0, 73f, 0));
//player.setHeldItemSlot((byte) 5);
ColoredText coloredText = ColoredText.of(ChatColor.RED + "" + ChatColor.BOLD + " test" + ChatColor.BRIGHT_GREEN + " lol");
coloredText.append("test");
player.sendMessage(coloredText);
System.out.println(coloredText.toString());
{
RichMessage richMessage = RichMessage.of(ColoredText.of(ChatColor.RED + " HEY MESSAGE"));
richMessage.setHoverEvent(ChatHoverEvent.showItem(new ItemStack(Material.STONE, (byte) 5)));
//player.sendMessage(richMessage);
}
player.getInventory().addItemStack(new ItemStack(Material.STONE, (byte) 1)); player.getInventory().addItemStack(new ItemStack(Material.STONE, (byte) 1));
player.setGlowing(true); player.setGlowing(true);

View File

@ -5,7 +5,7 @@ import net.minestom.server.command.builder.Arguments;
import net.minestom.server.command.builder.Command; import net.minestom.server.command.builder.Command;
import net.minestom.server.command.builder.arguments.Argument; import net.minestom.server.command.builder.arguments.Argument;
import net.minestom.server.command.builder.arguments.ArgumentType; import net.minestom.server.command.builder.arguments.ArgumentType;
import net.minestom.server.entity.Player; import net.minestom.server.item.ItemStack;
public class TestCommand extends Command { public class TestCommand extends Command {
@ -18,32 +18,16 @@ public class TestCommand extends Command {
//addSyntax(this::execute, dynamicWord); //addSyntax(this::execute, dynamicWord);
} }
Argument test = ArgumentType.Word("test").from("hey"); Argument test = ArgumentType.ItemStack("test");
Argument num = ArgumentType.Integer("num");
Argument test2 = ArgumentType.Word("test2").from("oh");
Argument num2 = ArgumentType.Float("num2");
addSyntax((source, args) -> { addSyntax((source, args) -> {
}, test, num); System.out.println("SUCCESS");
addSyntax((source, args) -> { ItemStack itemStack = args.getItemStack("test");
}, test2, num2); source.asPlayer().getInventory().addItemStack(itemStack);
}, test);
} }
private void usage(CommandSender sender, Arguments arguments) { private void usage(CommandSender sender, Arguments arguments) {
sender.sendMessage("Incorrect usage"); sender.sendMessage("Incorrect usage");
} }
private void execute(CommandSender sender, Arguments arguments) {
final String word = arguments.getWord("test");
sender.sendMessage("word: " + word);
}
private boolean isAllowed(Player player) {
return true; // TODO: permissions
}
@Override
public String[] onDynamicWrite(String text) {
return new String[]{"test1", "test2"};
}
} }