eren.
eren.
Explore posts from servers
JCHJava Community | Help. Code. Learn.
Created by eren. on 10/3/2024 in #java-help
Help
I am working on a plugin updater / version checker. I will start by sending the code first so it is easier to explain the issue
public final class SkriptPlus extends JavaPlugin {

private static SkriptPlus instance;
private static final Map<String, AddonService> services = Map.of(
"github", new GithubService(),
"skunity", new SkUnityService()
);

@Override
public void onEnable() {
instance = this;
...
public final class SkriptPlus extends JavaPlugin {

private static SkriptPlus instance;
private static final Map<String, AddonService> services = Map.of(
"github", new GithubService(),
"skunity", new SkUnityService()
);

@Override
public void onEnable() {
instance = this;
...
public class SkUnityService implements AddonService {

private static final String API_KEY;

static {
API_KEY = SkriptPlus.getInstance().getConfig().getString("update-checker.skunity-api-key", "disabled");
...
}
public class SkUnityService implements AddonService {

private static final String API_KEY;

static {
API_KEY = SkriptPlus.getInstance().getConfig().getString("update-checker.skunity-api-key", "disabled");
...
}
Since SkUnityService is loaded before onEnable() the static block errors. java.lang.NullPointerException: Cannot invoke "me.eren.skriptplus.SkriptPlus.getConfig()" because the return value of "me.eren.skriptplus.SkriptPlus.getInstance()" is null How would I go about doing this?
19 replies
JCHJava Community | Help. Code. Learn.
Created by eren. on 7/16/2024 in #java-help
Help with Lombok
I am making utility methods for sending discord webhooks. my goal is this:
DiscordWebhook webhook = DiscordWebhook.builder()
.webhookURI(new URI("https://discord.com/api/webhooks/123"))
.content("hello this is content")
.username("person")
.avatar(new URI("https://cdn.discordapp.com/attachments/123"))
.addEmbed(
DiscordEmbed.builder()
.hex("#FF0000")
.title("embed title")
.description("embed description")
.build()
)
.build();
DiscordWebhook webhook = DiscordWebhook.builder()
.webhookURI(new URI("https://discord.com/api/webhooks/123"))
.content("hello this is content")
.username("person")
.avatar(new URI("https://cdn.discordapp.com/attachments/123"))
.addEmbed(
DiscordEmbed.builder()
.hex("#FF0000")
.title("embed title")
.description("embed description")
.build()
)
.build();
everything works, except for the addEmbed part my class is defined like this:
@Builder
@Getter
@ToString
public class DiscordWebhook {

private static final HttpClient httpClient = HttpClient.newBuilder().build();

private final URI webhookURI;
private final String username;
private final String content;
private final URI avatar;
private final List<DiscordEmbed> embeds = new ArrayList<>(10);

public void addEmbed(DiscordEmbed embed) { // this was a test which didnt work since the method shouldve been inside the builder class (but lombok generates that so i cant edit it)
this.embeds.add(embed);
}

...
@Builder
@Getter
@ToString
public class DiscordWebhook {

private static final HttpClient httpClient = HttpClient.newBuilder().build();

private final URI webhookURI;
private final String username;
private final String content;
private final URI avatar;
private final List<DiscordEmbed> embeds = new ArrayList<>(10);

public void addEmbed(DiscordEmbed embed) { // this was a test which didnt work since the method shouldve been inside the builder class (but lombok generates that so i cant edit it)
this.embeds.add(embed);
}

...
so how do i make an embed adder?
14 replies
JCHJava Community | Help. Code. Learn.
Created by eren. on 4/5/2024 in #java-help
Help with Gradle
No description
9 replies
JCHJava Community | Help. Code. Learn.
Created by eren. on 3/29/2024 in #java-help
Making 2 flavors of a jar
I got 2 folders, src/core/java/... and src/bukkit/java/... I want to setup maven in a way that it will build core first, then use the output as a dependency to build bukkit. The output will be 2 jars
16 replies
JCHJava Community | Help. Code. Learn.
Created by eren. on 3/21/2024 in #java-help
Quick Help with FileWatcher
I got a filewatcher to detect file changes, but i just cant get file.isFile() condition to pass.
public static void start() {
try {
WatchService watchService = FileSystems.getDefault().newWatchService();
Path scriptsFolder = Paths.get(SCRIPTS_FOLDER);

scriptsFolder.register(watchService,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE,
StandardWatchEventKinds.ENTRY_MODIFY
);

SkReloader.getLog().info("Started FileWatcher! Scripts folder: '" + SCRIPTS_FOLDER + "'");

while (true) {
WatchKey key = watchService.take();

for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
Path path = (Path) event.context();

File file = new File(path.toFile().getAbsolutePath()); // if (!file.toFile().isFile()) return;
SkReloader.getLog().info(file + " " + file.getClass());
Script script = ScriptLoader.getScript(file);
...
public static void start() {
try {
WatchService watchService = FileSystems.getDefault().newWatchService();
Path scriptsFolder = Paths.get(SCRIPTS_FOLDER);

scriptsFolder.register(watchService,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE,
StandardWatchEventKinds.ENTRY_MODIFY
);

SkReloader.getLog().info("Started FileWatcher! Scripts folder: '" + SCRIPTS_FOLDER + "'");

while (true) {
WatchKey key = watchService.take();

for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
Path path = (Path) event.context();

File file = new File(path.toFile().getAbsolutePath()); // if (!file.toFile().isFile()) return;
SkReloader.getLog().info(file + " " + file.getClass());
Script script = ScriptLoader.getScript(file);
...
the last line throws an exception (I have no permission to edit that class or method)
public static Script getScript(File file) {
if (!file.isFile())
throw new IllegalArgumentException("Something other than a file was provided.");
...
public static Script getScript(File file) {
if (!file.isFile())
throw new IllegalArgumentException("Something other than a file was provided.");
...
6 replies
JCHJava Community | Help. Code. Learn.
Created by eren. on 3/11/2024 in #java-help
Help with sending a packet
I am making a socket server and trying to send a Server -> Client packet but I am getting a method ChiropteraServer#broadcast called with (Packet[id=4, data={0=abc, 1=asd}] (Packet)) threw a IllegalBlockingModeException: null I tried a few stuff but I couldn't get it to work, one of the attempts is the one on github The sendPacket method: https://github.com/erenkarakal/Chiroptera/blob/master/src/main/java/me/eren/chiroptera/ChiropteraServer.java#L128
public static void sendPacket(String clientIdentifier, Packet packet) {
if (!isListening) return;

SocketChannel client = authenticatedClients.get(clientIdentifier);
if (client == null) return;

try (OutputStream cos = client.socket().getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(cos)) {

client.configureBlocking(true);
oos.writeObject(packet);
client.configureBlocking(false);

} catch (IOException e) {
Chiroptera.getLog().warning("Error while sending a packet to " + getClientIdentifier(client) + ". " + e.getMessage());
}
}
public static void sendPacket(String clientIdentifier, Packet packet) {
if (!isListening) return;

SocketChannel client = authenticatedClients.get(clientIdentifier);
if (client == null) return;

try (OutputStream cos = client.socket().getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(cos)) {

client.configureBlocking(true);
oos.writeObject(packet);
client.configureBlocking(false);

} catch (IOException e) {
Chiroptera.getLog().warning("Error while sending a packet to " + getClientIdentifier(client) + ". " + e.getMessage());
}
}
!!! ping me if you respond please !!!
11 replies
JCHJava Community | Help. Code. Learn.
Created by eren. on 3/3/2024 in #java-help
Need help with handling disconnects on my socket server.
I've been looking for a way to properly handle disconnects / timeouts on my socket server for hours. All I wanted to do was log a disconnect message and un-authenticate them if they are authenticated. Here is my code (see line 79, it currently does nothing): https://github.com/erenkarakal/Chiroptera/blob/master/src/main/java/me/eren/chiroptera/ChiropteraServer.java
5 replies
JCHJava Community | Help. Code. Learn.
Created by eren. on 3/1/2024 in #java-help
Need help with sockets!
No description
5 replies
JCHJava Community | Help. Code. Learn.
Created by eren. on 12/22/2023 in #java-help
Help with registration system
I am making an addon for a scripting language. The way to register a syntax is to make class for the syntax that extends the type of the syntax and call the register method (each register method has different parameters):
public class SecRunFunction extends Section {

static {
Skript.registerSection(SecRunFunction.class, "(execute|run) function <.+> [and store it in %-~objects%]");
}
public class SecRunFunction extends Section {

static {
Skript.registerSection(SecRunFunction.class, "(execute|run) function <.+> [and store it in %-~objects%]");
}
it doesn't have to be in a static block and the method doesn't have to be called in that class but I would like to register the syntax in its own class since its easier to manage. I am looping all classes on enable and registering them using
private void registerClass(String className) {
try {
Class<?> c = Class.forName("me.eren.skcheese.elements." + className);
c.getConstructor().newInstance();
} catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException |
InvocationTargetException exception) {
getLogger().log(Level.SEVERE, "Failed to load the addon class " + className, exception);
}
}
private void registerClass(String className) {
try {
Class<?> c = Class.forName("me.eren.skcheese.elements." + className);
c.getConstructor().newInstance();
} catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException |
InvocationTargetException exception) {
getLogger().log(Level.SEVERE, "Failed to load the addon class " + className, exception);
}
}
is there a way to do this better? there has to be ping me if you respond please!!!
4 replies
JCHJava Community | Help. Code. Learn.
Created by eren. on 12/13/2023 in #java-help
Feedback on my Registration System
I am making an addon for a scripting language that adds new syntaxes (Skript) In Skript, each syntax gets its own class and you must call the Skript.registerWhateverSyntax method on server load to use it. You can bulk register syntaxes using Skript.loadClasses(package, subpackage) (assuming you register the syntax on the static block) but this comes with a problem, you can't create a config file where you can edit what syntaxes are going to be registered, so you need to make your own registration system. I came up with this and I want feedback on it. Perhaps a way without reflection or something cleaner than this? On server start: https://github.com/erenkarakal/SkCheese/blob/master/src/main/java/me/eren/skcheese/SkCheese.java#L14 Register syntax: https://github.com/erenkarakal/SkCheese/blob/master/src/main/java/me/eren/skcheese/elements/ExprParsedAs.java#L22
6 replies