first of all; Thanks for reading and helping me out, I'm new to Java I ran into a problem.
I've made instance variables inside a particular class. After this I create a new instance/object of this class and specify the new input of the variable. The problem is: when I run the code and try if its working, it isnt working. It appears that the new variable is not passed to the class being instantiated.
(When I left click the emerald block an empty string appears)
What I've already tried:
- creating a setter method to set the variable.
- using a constructor
Class that I want to make an instance of:
package rico.polkadot;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
public class InstanceClass implements Listener {
public String name;
@EventHandler
public void onInteract(PlayerInteractEvent e) {
Action action = e.getAction();
Player p = e.getPlayer();
Block b = e.getClickedBlock();
if(action.equals(Action.LEFT_CLICK_BLOCK)) {
if(b.getType().equals(Material.EMERALD_BLOCK)) {
p.sendMessage(name);
}
}
}
}
Main class:
package rico.polkadot;
import java.util.List;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin {
@Override
public void onEnable() {
getServer().getPluginManager().registerEvents(new InstanceClass(this),this);
InstanceClass test = new InstanceClass();{
test.name = "lol";
}
}
}
question from:
https://stackoverflow.com/questions/66064936/instance-isnt-passsing-given-variables-to-class 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…