Search in sources :

Example 1 with CommandSyntaxException

use of com.mojang.brigadier.exceptions.CommandSyntaxException in project MinecraftForge by MinecraftForge.

the class CraftingHelper method getItemStack.

public static ItemStack getItemStack(JsonObject json, boolean readNBT, boolean disallowsAirInRecipe) {
    String itemName = GsonHelper.getAsString(json, "item");
    ResourceLocation itemKey = new ResourceLocation(itemName);
    if (!ForgeRegistries.ITEMS.containsKey(itemKey))
        throw new JsonSyntaxException("Unknown item '" + itemName + "'");
    Item item = ForgeRegistries.ITEMS.getValue(itemKey);
    if (disallowsAirInRecipe && item == Items.AIR)
        throw new JsonSyntaxException("Invalid item: " + itemName);
    if (readNBT && json.has("nbt")) {
        // Lets hope this works? Needs test
        try {
            JsonElement element = json.get("nbt");
            CompoundTag nbt;
            if (element.isJsonObject())
                nbt = TagParser.parseTag(GSON.toJson(element));
            else
                nbt = TagParser.parseTag(GsonHelper.convertToString(element, "nbt"));
            CompoundTag tmp = new CompoundTag();
            if (nbt.contains("ForgeCaps")) {
                tmp.put("ForgeCaps", nbt.get("ForgeCaps"));
                nbt.remove("ForgeCaps");
            }
            tmp.put("tag", nbt);
            tmp.putString("id", itemName);
            tmp.putInt("Count", GsonHelper.getAsInt(json, "count", 1));
            return ItemStack.of(tmp);
        } catch (CommandSyntaxException e) {
            throw new JsonSyntaxException("Invalid NBT Entry: " + e.toString());
        }
    }
    return new ItemStack(item, GsonHelper.getAsInt(json, "count", 1));
}
Also used : Item(net.minecraft.world.item.Item) JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonElement(com.google.gson.JsonElement) ResourceLocation(net.minecraft.resources.ResourceLocation) ItemStack(net.minecraft.world.item.ItemStack) CompoundTag(net.minecraft.nbt.CompoundTag) CommandSyntaxException(com.mojang.brigadier.exceptions.CommandSyntaxException)

Example 2 with CommandSyntaxException

use of com.mojang.brigadier.exceptions.CommandSyntaxException in project AgriCraft by AgriCraft.

the class TagUtil method addNbtData.

@Nonnull
private static Optional<ItemStack> addNbtData(@Nonnull ItemStack stack, @Nullable String tags) {
    // Step 0. Validate.
    Preconditions.checkNotNull(stack, "The itemstack to add NBT data to may not be null");
    // Step 1. Abort if tags are null.
    if (Strings.isNullOrEmpty(tags)) {
        return Optional.of(stack);
    }
    // Step 2. Get the tag instance.
    final CompoundNBT tag = stack.hasTag() ? stack.getTag() : new CompoundNBT();
    // Step 3. Parse the tags.
    try {
        final CompoundNBT added = JsonToNBT.getTagFromJson(tags);
        tag.merge(added);
        stack.setTag(tag);
        return Optional.of(stack);
    } catch (CommandSyntaxException e) {
        AgriCore.getLogger("agricraft").error("Unable to parse NBT Data: \"{0}\".\nCause: {1}", tags, e);
        return Optional.empty();
    }
}
Also used : CompoundNBT(net.minecraft.nbt.CompoundNBT) CommandSyntaxException(com.mojang.brigadier.exceptions.CommandSyntaxException) Nonnull(javax.annotation.Nonnull)

Example 3 with CommandSyntaxException

use of com.mojang.brigadier.exceptions.CommandSyntaxException in project MyPet by xXKeyleXx.

the class PlatformHelper method playParticleEffect.

/**
 * @param location   the {@link Location} around which players must be to see the effect
 * @param effectName list of effects: https://gist.github.com/riking/5759002
 * @param offsetX    the amount to be randomly offset by in the X axis
 * @param offsetY    the amount to be randomly offset by in the Y axis
 * @param offsetZ    the amount to be randomly offset by in the Z axis
 * @param speed      the speed of the particles
 * @param count      the number of particles
 * @param radius     the radius around the location
 */
@Override
public void playParticleEffect(Location location, String effectName, float offsetX, float offsetY, float offsetZ, float speed, int count, int radius, de.Keyle.MyPet.api.compat.Compat<Object> data) {
    Particle effect = IRegistry.PARTICLE_TYPE.get(new MinecraftKey(effectName));
    Validate.notNull(location, "Location cannot be null");
    Validate.notNull(effect, "Effect cannot be null");
    Validate.notNull(location.getWorld(), "World cannot be null");
    ParticleParam particle = null;
    if (effect.d() != null && data != null) {
        try {
            particle = effect.d().b(effect, new StringReader(" " + data.get().toString()));
        } catch (CommandSyntaxException e) {
            e.printStackTrace();
        }
    } else if (effect instanceof ParticleType) {
        particle = (ParticleType) effect;
    }
    if (particle == null) {
        return;
    }
    PacketPlayOutWorldParticles packet = new PacketPlayOutWorldParticles(particle, false, (float) location.getX(), (float) location.getY(), (float) location.getZ(), offsetX, offsetY, offsetZ, speed, count);
    radius = radius * radius;
    for (Player player : location.getWorld().getPlayers()) {
        if ((int) MyPetApi.getPlatformHelper().distanceSquared(player.getLocation(), location) <= radius) {
            ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
        }
    }
}
Also used : MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) CraftPlayer(org.bukkit.craftbukkit.v1_16_R3.entity.CraftPlayer) StringReader(com.mojang.brigadier.StringReader) CraftPlayer(org.bukkit.craftbukkit.v1_16_R3.entity.CraftPlayer) CommandSyntaxException(com.mojang.brigadier.exceptions.CommandSyntaxException)

Example 4 with CommandSyntaxException

use of com.mojang.brigadier.exceptions.CommandSyntaxException in project MyPet by xXKeyleXx.

the class PlatformHelper method playParticleEffect.

/**
 * @param location   the {@link Location} around which players must be to see the effect
 * @param effectName list of effects: https://gist.github.com/riking/5759002
 * @param offsetX    the amount to be randomly offset by in the X axis
 * @param offsetY    the amount to be randomly offset by in the Y axis
 * @param offsetZ    the amount to be randomly offset by in the Z axis
 * @param speed      the speed of the particles
 * @param count      the number of particles
 * @param radius     the radius around the location
 */
@Override
public void playParticleEffect(Location location, String effectName, float offsetX, float offsetY, float offsetZ, float speed, int count, int radius, de.Keyle.MyPet.api.compat.Compat<Object> data) {
    ParticleType effect = Registry.PARTICLE_TYPE.get(new ResourceLocation(effectName));
    Validate.notNull(location, "Location cannot be null");
    Validate.notNull(effect, "Effect cannot be null");
    Validate.notNull(location.getWorld(), "World cannot be null");
    ParticleOptions particle = null;
    if (effect.getDeserializer() != null && data != null) {
        try {
            particle = effect.getDeserializer().fromCommand(effect, new StringReader(" " + data.get().toString()));
        } catch (CommandSyntaxException e) {
            e.printStackTrace();
        }
    } else if (effect instanceof SimpleParticleType) {
        particle = (SimpleParticleType) effect;
    }
    if (particle == null) {
        return;
    }
    ClientboundLevelParticlesPacket packet = new ClientboundLevelParticlesPacket(particle, false, (float) location.getX(), (float) location.getY(), (float) location.getZ(), offsetX, offsetY, offsetZ, speed, count);
    radius = radius * radius;
    for (Player player : location.getWorld().getPlayers()) {
        if ((int) MyPetApi.getPlatformHelper().distanceSquared(player.getLocation(), location) <= radius) {
            ((CraftPlayer) player).getHandle().connection.send(packet);
        }
    }
}
Also used : CraftPlayer(org.bukkit.craftbukkit.v1_18_R1.entity.CraftPlayer) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) ParticleOptions(net.minecraft.core.particles.ParticleOptions) ResourceLocation(net.minecraft.resources.ResourceLocation) StringReader(com.mojang.brigadier.StringReader) ParticleType(net.minecraft.core.particles.ParticleType) SimpleParticleType(net.minecraft.core.particles.SimpleParticleType) ClientboundLevelParticlesPacket(net.minecraft.network.protocol.game.ClientboundLevelParticlesPacket) CraftPlayer(org.bukkit.craftbukkit.v1_18_R1.entity.CraftPlayer) SimpleParticleType(net.minecraft.core.particles.SimpleParticleType) CommandSyntaxException(com.mojang.brigadier.exceptions.CommandSyntaxException)

Example 5 with CommandSyntaxException

use of com.mojang.brigadier.exceptions.CommandSyntaxException in project MyPet by xXKeyleXx.

the class PlatformHelper method playParticleEffect.

/**
 * @param location   the {@link Location} around which players must be to see the effect
 * @param effectName list of effects: https://gist.github.com/riking/5759002
 * @param offsetX    the amount to be randomly offset by in the X axis
 * @param offsetY    the amount to be randomly offset by in the Y axis
 * @param offsetZ    the amount to be randomly offset by in the Z axis
 * @param speed      the speed of the particles
 * @param count      the number of particles
 * @param radius     the radius around the location
 */
@Override
public void playParticleEffect(Player player, Location location, String effectName, float offsetX, float offsetY, float offsetZ, float speed, int count, int radius, de.Keyle.MyPet.api.compat.Compat<Object> data) {
    ParticleType effect = Registry.PARTICLE_TYPE.get(new ResourceLocation(effectName));
    Validate.notNull(location, "Location cannot be null");
    Validate.notNull(effect, "Effect cannot be null");
    Validate.notNull(location.getWorld(), "World cannot be null");
    ParticleOptions particle = null;
    if (effect.getDeserializer() != null && data != null) {
        try {
            particle = effect.getDeserializer().fromCommand(effect, new StringReader(" " + data.get().toString()));
        } catch (CommandSyntaxException e) {
            e.printStackTrace();
        }
    } else if (effect instanceof SimpleParticleType) {
        particle = (SimpleParticleType) effect;
    }
    if (particle == null) {
        return;
    }
    ClientboundLevelParticlesPacket packet = new ClientboundLevelParticlesPacket(particle, false, (float) location.getX(), (float) location.getY(), (float) location.getZ(), offsetX, offsetY, offsetZ, speed, count);
    if (MyPetApi.getPlatformHelper().distanceSquared(player.getLocation(), location) <= radius) {
        ((CraftPlayer) player).getHandle().connection.send(packet);
    }
}
Also used : ParticleOptions(net.minecraft.core.particles.ParticleOptions) ResourceLocation(net.minecraft.resources.ResourceLocation) StringReader(com.mojang.brigadier.StringReader) ParticleType(net.minecraft.core.particles.ParticleType) SimpleParticleType(net.minecraft.core.particles.SimpleParticleType) ClientboundLevelParticlesPacket(net.minecraft.network.protocol.game.ClientboundLevelParticlesPacket) CraftPlayer(org.bukkit.craftbukkit.v1_18_R1.entity.CraftPlayer) SimpleParticleType(net.minecraft.core.particles.SimpleParticleType) CommandSyntaxException(com.mojang.brigadier.exceptions.CommandSyntaxException)

Aggregations

CommandSyntaxException (com.mojang.brigadier.exceptions.CommandSyntaxException)9 StringReader (com.mojang.brigadier.StringReader)6 ResourceLocation (net.minecraft.resources.ResourceLocation)5 MyPetPlayer (de.Keyle.MyPet.api.player.MyPetPlayer)4 ParticleOptions (net.minecraft.core.particles.ParticleOptions)4 ParticleType (net.minecraft.core.particles.ParticleType)4 SimpleParticleType (net.minecraft.core.particles.SimpleParticleType)4 ClientboundLevelParticlesPacket (net.minecraft.network.protocol.game.ClientboundLevelParticlesPacket)4 CraftPlayer (org.bukkit.craftbukkit.v1_17_R1.entity.CraftPlayer)2 CraftPlayer (org.bukkit.craftbukkit.v1_18_R1.entity.CraftPlayer)2 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 LiteralArgumentBuilder (com.mojang.brigadier.builder.LiteralArgumentBuilder)1 CommandContext (com.mojang.brigadier.context.CommandContext)1 StringJoiner (java.util.StringJoiner)1 Function (java.util.function.Function)1 Nonnull (javax.annotation.Nonnull)1 Serializers (net.darkhax.bookshelf.serialization.Serializers)1 CommandSource (net.minecraft.command.CommandSource)1