Hello sponge community, I’m new to sponge and I am wanting to move my server from spigot to sponge but I need to first move some of the plugins I created to be supported on sponge.
The first plugin I’m making is a simple plugin that will send links to players that are like voting links and a few others. In this plugin I have a command that when players do /rp it sends them the resource pack we use that the download link is saved in the config. I know that you can put the resource pack link in the server config but I have it in the plugin config so we can change it without needing to restart the server for it to take place.
The issue I’m having is I have no idea how to send the resource pack to the player when they do the /rp command.
I have done some google searching but there really isn’t anything out there telling how it’s done. Also I’m fairly new at coding so some examples would be great.
Here’s what I have so far from just doing google searches
ResourcePacks.fromUri(new URI(ConfigurationManager.getInstance().getConfig().getNode(“RP”).toString()));
player.sendResourcePack(“not sure what to put here”);
I would appreciate any help that can be given, thanks.
Use the ResourcePackFactory
:
https://github.com/SpongePowered/SpongeAPI/blob/bleeding/src/main/java/org/spongepowered/api/resourcepack/ResourcePackFactory.java#L44
Edit: If you are using Configurate, then ConfigurationManager.getInstance().getConfig().getNode("RP").toString()
won’t work. Node#getString()
works.
Okay how Exactly do I use the resource pack factory correctly?
in spigot I was able to send the resource pack with
player.setResourcePack(config.getString(“RP”));
How different is it to send the resource pack to a player from spigot?
You can see the obvious differences yourself:
- Sponge: Use
ResourcePackFactory
to make your ResourcePack
object which you then send with Player#sendResourcePack(ResourcePack)
- Spigot: I don’t know Spigot or Bukkit
As you can see on GitHub there are only two methods in ResourcePackFactory
you can use. The JavaDocs explain the methods in detail. The first method verifies the URI, checks if the resource pack is valid. The second method doesn’t do that.
I think the second method can be used in your case because you make sure the resource pack is valid before putting the link in the config.
Okay here’s what I have so far but I’m confused on how I get the URI and send it to the player.
ResourcePacks.fromUriUnchecked(new URI(ConfigurationManager.getInstance().getConfig().getNode(“RP”).getString()));
player.sendResourcePack(pack);
Like I said earlier I don’t know how to send the URI to the player so they can get the resource pack. I know it has something to do with the player.sendResourcePack(“Have Know clue what to put here”)
Hopefully I’m doing this right I’m still pretty new to Java
The URI is loaded from the config by the ConfigurationManager. It is a String. That String is given to the URI class constructor. The constructed URI object is then given to the ResourcePackFactory. It creates a ResourcePack object. The ResourcePack object can then be sent to the player.
That’s all. You don’t manually send the URI to the player. Sponge wraps the URI and does other things, which aren’t important for us(I actually don’t know what other things happen when the ResourcePack is sent, but that’s not important). Try your code.
so my code doesn’t work because in the section of code that I have
player.sendResourcePack(); it ask for a pack between the parentheses however I don’t know what to put in the parentheses because I’ve tried just ResourcePack but I still get an error.
The methods in ResourcePackFactory
both return a ResourcePack
which you have to use for Player#sendResourcePack(...)
Okay I get that the Resourcepackfactory returns a resource pack however that still doesn’t answer my question of what to put in the () of the player.sendResroucePack()
Here’s a picture of my code
You’re using the method ResourcePacks.fromUriUnchecked wrong.
ResourcePack pack = ResourcePacks.fromUriUnchecked(Uri uri);
player.sendResourcePack(pack);
I don’t mean to insult you but I really suggest you learn more on Java before trying to make a plugin. Something like this is incredibly basic and you’re only going to encounter more difficulties if you keep going as you are now.
1 Like
but I’m wanting to get the Resource pack link from the config for the plugin. I’ve made this plugin for spigot but I’m wanting to switch over to sponge so I need to convert my plugins over to sponge, However I’m not familiar with the sponge API at all.
I think what they mean is your not assigning #fromUriUnchecked to a variable in the pictured code provided
Also read the docs to familiarize yourself with Sponge
https://docs.spongepowered.org
1 Like
Okay since many people keep telling me that I’m doing something wrong can someone please provide an example of how it’s supposed to be done. Basicly all I want is to download a resource pack that it gets the link from the config. Also please try and break it down into layman’s term I’m still fairly new to java and I find the best way for me to code is by doing projects. Also I have been reading the sponge docs on creating a plugin.
You’ve been given a number of examples. Working with a project like this is a terrible way to learn Java.
The examples that I’ve seen haven’t answered my questions at all.
Here’s what I’ve tried for setting a variable but java wants me to add a try and catch which I’ve tried but doesn’t work
ResourcePack pack = ResourcePacks.fromUriUnchecked(new URI(ConfigurationManager.getInstance().getConfig().getNode(“RP”).getString()));
player.sendResourcePack(pack);
Edit: Sorry If I’m not making any sense but none of the documentation makes sense to me
What do you mean by it doesn’t work? What doesn’t work?
Eclipse has the ResourcePack pack = ResourcePacks.fromUriUnchecked(new URI(ConfigurationManager.getInstance().getConfig().getNode(“RP”).getString())); underlined in red and when I hover over it it suggests a try catch to be added. I’ve tried adding the try catch but when I test the command in the game nothing happens.
There should be some sort of error. Is your try statement printing a stack trace or outputting any information in the event of an error?
So I just added the try catch again and tested it and it worked this time. I think what was going on was that I was using the first method from the resourcePackFactory which had multiple try catches which I think was causing the problem. I must have been thinking that the try catch statement would have been the same results with the second method. Anyways thanks for the help sorry if I seemed stuborn
The official oracle Java tutorials have a great section on exception handling, I get that you are probably a new programmer, and I’m sorry that no one has offered information that managed to allow you to understand what’s happening rather then copying and pasting examples, hopefully reading the official tutorial will give you some guidance.
https://docs.oracle.com/javase/tutorial/essential/exceptions/handling.html
I had started to provide a step by step explanation to try to convey the reasoning behind each method call, but then I got hungry.