why isn't my config file modifying my servers motd? - java

So as the title says I need my config file to modify my servers motd, I feel like I've done everything the right way but obviously something is wrong, so let me show you what I'm working with
me.zavage.motd.Main
package me.zavage.motd;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin {
#Override
public void onEnable() {
saveDefaultConfig();
}
}
me.zavage.motd.listeners.PingListener
package me.zavage.motd.listeners;
import java.io.File;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.server.ServerListPingEvent;
import me.zavage.motd.Main;
import me.zavage.motd.utils.Utils;
public class PingListener implements Listener {
private Main plugin;
#EventHandler
public void onPing(ServerListPingEvent e) {
e.setMaxPlayers(plugin.getConfig().getInt("maxplayers"));
e.setMotd(Utils.chat(plugin.getConfig().getString("motd" + "\n" + (Utils.chat(plugin.getConfig().getString("motd_line_2"))))));
try {
e.setServerIcon(Bukkit.loadServerIcon(new File(plugin.getConfig().getString("server_icon_path"))));
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
Then we have the config.yml
#can only be a NUMBER with NO DECIMALS!
maxplayers: ''
motd: ''
#next line on motd
motd_line_2:
#create a folder inside the first page of your server files, call it "icon" and drop you server icon in there
#server icon MUST be 64 x 64 pixels
# #NameOfIcon#
#path example "C:\Users\user\Desktop\minecraft test server\icon\icon.png
server_icon_path: ''

If the code that you show if everything that you are using, there is few issues.
You don't register the listener
package me.zavage.motd;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin {
#Override
public void onEnable() {
saveDefaultConfig();
getServer().getPluginManager().registerEvents(new PingListener(this), this);
}
}
You don't set the plugin variable in the listener, and you don't have constructor
public class PingListener implements Listener {
private Main plugin;
public PingListener(Main plugin) {
this.plugin = plugin;
}
}
Finally, you are searching for the motd\n motd_line_2 message. You have to use this in your ServerListPingEvent listener :
e.setMaxPlayers(plugin.getConfig().getInt("maxplayers"));
e.setMotd(Utils.chat(plugin.getConfig().getString("motd") + "\n" + plugin.getConfig().getString("motd_line_2")));
try {
e.setServerIcon(Bukkit.loadServerIcon(new File(plugin.getConfig().getString("server_icon_path"))));
} catch (Exception exc) {
exc.printStackTrace();
}

Related

TypeTransformer applyAdviceToMethod(isConstructor() doesn't works in opentelemetry extensions

I'm trying to add spans when constructor of some class called. I'm using opentelemetry javaagent and extensions to add tracing to my application.
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.matcher.ElementMatchers;
import static net.bytebuddy.matcher.ElementMatchers.isConstructor;
import static net.bytebuddy.matcher.ElementMatchers.named;
public class ClassConstructorInstrumentation implements TypeInstrumentation {
#Override
public ElementMatcher<TypeDescription> typeMatcher() {
return ElementMatchers
.namedOneOf("org.example.ServiceManagerDummy");
}
#Override
public void transform(TypeTransformer transformer) {
transformer.applyAdviceToMethod(
isConstructor(),
this.getClass().getName() + "$ConstructorSpanCreateAdvice");
// transformer.applyAdviceToMethod(
// named("dummyMethod"),
// this.getClass().getName() + "$ConstructorSpanCreateAdvice");
}
#SuppressWarnings("unused")
public static class ConstructorSpanCreateAdvice {
#Advice.OnMethodEnter
public static void onEnter() {
System.out.println("START SPAN ");
}
#Advice.OnMethodExit(onThrowable = Throwable.class)
public static void onExit(
#Advice.Thrown Throwable throwable
) {
System.out.println("END SPAN ");
}
}
}
public class ServiceManagerDummy {
public ServiceManagerDummy() {
System.out.println("SERVICE MANAGER CONSTR");
dummyMethod();
}
private void dummyMethod() {
System.out.println("DUMMY METHOD CALLED");
}
}
I'm using a simple configuration as above just to verify that when the constructor was called my advice method log it. But when it configured to add some logs when the constructor was called, I received nothing in the log. But when I add config for method calling (commented code) it works. What's wrong in my configuration?
What Byte Buddy would normally do would be to wrap the constructor in a try-finally-block. For a constructor, that is not possible as the super method call cannot be wrapped in such a block. "onThrowable" is therefore not possible for constructors.

I want to use my other class includes listener on my main class / Java minecraft Paper plugin

I use gradle for build, Java, minecraft, Paper plugin
ctrl+f , figure out " the problem "
I made each of these plugins separately. Now, I am putting them together into one plugin but separating each class. Then, this error occurred. I want to solve this problem.
I thought third_function.third_listener could be used for that place, but It seems to be worng...
package com;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.GameRule;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
public class mainclass extends JavaPlugin implements Listener {
#Override
public void onEnable() {
getLogger().warning("Server started.");
first_function first = new first_function(this);
first.onEnable();
second_function second = new second_function(this);
second.onEnable();
third_function third = new third_function(this);
third.onEnable();
PlayerJoinEvent third_listener;
third.onPlayerJoin(third_function.third_listener); // the problem. please help me, how can I get rid of this error?
}
}
class first_function implements Listener {
private final mainclass plugin;
first_function(mainclass plugin) {
this.plugin = plugin;
}
void onEnable() {
this.plugin.getLogger().warning("Hello, world!");
}
}
class second_function implements Listener {
private final mainclass plugin;
second_function(mainclass plugin) {
this.plugin = plugin;
}
void onEnable() {
World world = this.plugin.getServer().getWorld("world");
world.setGameRule(GameRule.DO_DAYLIGHT_CYCLE, false);
}
}
class third_function implements Listener {
private final mainclass plugin;
third_function(mainclass plugin) {
this.plugin = plugin;
}
Listener third_listener = (this);
void onEnable() {
this.plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
#EventHandler
void onPlayerJoin(PlayerJoinEvent event) {
event.getPlayer().sendActionBar(Component.text("Welcome",(NamedTextColor.BLUE)));
}
}
You should register the listener and not try to call the method yourself. For example:
#Override
public void onEnable() {
third_function third = new third_function(this);
getServer().getPluginManager().registerEvents(third, this);
}
Also, I mostly suggest you to use global java names convention, such as name ThirdFunction instead of third_function for example

My Minecraft Spigot Plugin Not Reporting Back

I am pretty new to coding java and I have done a few tutorials, which have been great but i don't know why it isn't working in-game. I have tried everything such as changing it and looking at so many different forums. There are two classes for the events(Join and Leave Event) and the main class. I have made sure to check for importing them and errors none for me to see from where I have looked. If anyone can help it would be a blessing.
Code: - Main class:
package me.JimmyClown.FirstPlugin;
import org.bukkit.plugin.java.JavaPlugin;
public class MainClass extends JavaPlugin {
#Override
public void onEnable() {
System.out.println("It works!");
getServer().getPluginManager().registerEvents(new PlayerJoin(), this);
getServer().getPluginManager().registerEvents(new PlayerLeave(), this);
}
#Override
public void onDisable() {
}
}
**Code for the Join-Class:**
package me.JimmyClown.FirstPlugin;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
public class PlayerJoin implements Listener{
#EventHandler
void onPlayerJoin(PlayerJoinEvent e) {
Player player = e.getPlayer();
e.setJoinMessage(ChatColor.BLUE + "Welcome back to the server!" + player.getDisplayName());
}
}
**Code for Leave Class:**
package me.JimmyClown.FirstPlugin;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
public class PlayerLeave implements Listener {
#EventHandler
void onPlayerLeave(PlayerQuitEvent e) {
Player player = e.getPlayer();
e.setQuitMessage(ChatColor.DARK_BLUE + "Awwww, We hope we see you soon" + player.getDisplayName());
}
}
The plugin.yml:
name: FirstProject
version: 1.0
main: me.JimmyClown.FirstPlugin.MainClass
authors: JimmyTheClown
description: This is my first plugin.
Try to set the access modifier of the event methods to public.
public void onPlayerLeave(PlayerQuitEvent e) { }
public void onPlayerJoin(PlayerJoinEvent e) { }

[java]Add the jar fail,I can't ues the class

I have added the jar to Build path and the Reference Libraries also have the jar.IDE is the eclipse2019.9. Can you see my image?I can't sure whether it was posted
package com.itranswarp.learnjava;
import java.io.UnsupportedEncodingException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class Main {
static final Log log = LogFactory.getLog(Main.class);
public static void main(String[] args) {
log.info("Start process...");
try {
"".getBytes("invalidCharsetName");
} catch (UnsupportedEncodingException e) {
}
log.info("Process end.");
}
}
screenshot:This is my project structure
Please add build path.. refer image

Bukkit PlayerDeathEvent

why isn't this working? I don't get any response if I've been killed! So as you can see I have tested multiple ways. But no one is working.
package net.gameforce.testing;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin implements Listener {
#Override
public void onEnable() {
getServer().getPluginManager().registerEvents(this,this);
getLogger().info("Testing Plugin Started");
}
public void onDisable() {}
#EventHandler
public void onPlayerInteract(PlayerInteractEvent event){
Bukkit.broadcastMessage("test");
event.getPlayer().setExp(100);
}
public void onPlayerDeath (PlayerDeathEvent event){
Bukkit.broadcastMessage("send");
event.getEntity().getPlayer().setExp(1000);
}
public boolean onDeath (PlayerDeathEvent event) {
Player Player = event.getEntity();
Bukkit.broadcastMessage(Player.getKiller().getDisplayName() + ", has killed you!");
if (Player.getKiller() != null) {
Bukkit.broadcastMessage("No Player");
}
else {
Bukkit.broadcastMessage("IDK");
}
return true;
}
}
Have I done something wrong?
Quotation from http://bukkit.gamepedia.com/Event_API_Reference#.40EventHandler
"Before this method can be invoked by Bukkit when the "Event" is fired, we need to annotate it. We do this with EventHandlers."
You've added this annotation to your onPlayerInteract method, but none of your others, such as your onDeath method. If your listener is setup correctly, adding the #EventHandler annotation to these methods will allow bukkit to correctly invoke them, like so:
#EventHandler
public void onPlayerDeath (PlayerDeathEvent event){
Bukkit.broadcastMessage("send");
event.getEntity().getPlayer().setExp(1000);
}

Categories