hi guys I have a class that holds a constructor which works well, I used to have a command "set" in my class and registered the command in my onenable. I later removed that command and just kept the constructor but now it isn't firing. My question is how do I initiate my constructor class?
What isn't firing? The constructor? The constructor is called everytime you create a new instance of that class.
I created a class called ‘region’ and created a constructor to generate a hollow cuboid: Public region() { } I don’t know why it isn’t firing but I haven’t set anything in my onEnable. I tried using ‘new region();’ in my onEnable but still nothing Sent from my iPhone using Tapatalk
@TerroDoor Are you using the correct initializer for the constructor? Does your constructor take any parameters that you are failing to use?
How can I get it to fire? Sorry I’ve just learnt constructors Sent from my iPhone using Tapatalk What would I need to initialise between the Params, in my class I’m only using two set vectors to create the cube Sent from my iPhone using Tapatalk EDIT by Moderator: merged posts, please use the edit button instead of double posting.
@CraftCreeper6 @KarimAKL Code: public class region implements Listener { World w = Bukkit.getWorld("world"); Vector pos1 = new Vector(100, 100, 100); Vector pos2 = new Vector(120, 90, 120); public ArrayList<UUID> inspawn = new ArrayList<UUID>(); public region() { double minx = Math.min(pos1.getBlockX(), pos2.getBlockX()); double maxx = Math.max(pos1.getBlockX(), pos2.getBlockX()); double minz = Math.min(pos1.getBlockZ(), pos2.getBlockZ()); double maxz = Math.max(pos1.getBlockZ(), pos2.getBlockZ()); double miny = Math.min(pos1.getBlockY(), pos2.getBlockY()); double maxy = Math.max(pos1.getBlockY(), pos2.getBlockY()); for (double x = minx; x <= maxx; x++) { for (double y = miny; y <= maxy; y++) { for (double z = minz; z <= maxz; z++) { Location loc = new Location(w, x, y, z); if (x == minx && x == maxx && z == minz && z == maxz) { loc.getBlock().setType(Material.PINK_WOOL); } else { loc.getBlock().setType(Material.AIR); btw im using vectors for my PlayerMoveEvent to use the 'isInAABB' method
@CraftCreeper6 The constructor is in the code. @TerroDoor Where do you create a new instance of this class? Did you try debugging the code?
I tried in my onEnable by using ‘new region();’ But no errors and no luck Sent from my iPhone using Tapatalk
@CraftCreeper6 Yeah, i was just looking for "public region()" and then i copied it into notepad++ to format it. @TerroDoor Okay so, you're creating a new instance of "region" but, did you try debugging the code in the constructor?
Yeah I tried but have still failed to get it working, any suggestions to help guide me to this fiz Sent from my iPhone using Tapatalk
@TerroDoor So in your original post you mentioned that you "removed the command". Does that mean you get rid of the instance too?
I have a listener in my class also so I am registering the events in my onEnable and also have an instance of my constructor in the onEnable I’d post code but not with laptop atm Sent from my iPhone using Tapatalk
@TerroDoor Okay, could you give an example of how you initialize it? All I need to know is if you're creating more than one instance by accident.
in my onEnable: PluginManager pm = getPM new region(); Pm.registerevents(new region); Also, I have a Boolean method to check if the player is inside the location, do I need to use that method somewhere for it to work also? Sent from my iPhone using Tapatalk
@TerroDoor Just, new region(); ? You aren't storing that anywhere, additionally, you're registering the events in a completely different instance of region. Create a variable that holds the Region instance, and use that instead.
So I can still register the events while creating an instance of my region class? I will do that now Sent from my iPhone using Tapatalk
Okay so it’s creating the cube which means the constructor is working in the onEnable using the instance but it’s not listening for the player entering or leaving however I’m registering the events Sent from my iPhone using Tapatalk
@CraftCreeper6 Code: public class help implements Listener { World w = Bukkit.getWorld("world"); Vector pos1 = new Vector(100, 100, 100); Vector pos2 = new Vector(120, 90, 120); public ArrayList<UUID> inspawn = new ArrayList<UUID>(); public region() { double minx = Math.min(pos1.getBlockX(), pos2.getBlockX()); double maxx = Math.max(pos1.getBlockX(), pos2.getBlockX()); double minz = Math.min(pos1.getBlockZ(), pos2.getBlockZ()); double maxz = Math.max(pos1.getBlockZ(), pos2.getBlockZ()); double miny = Math.min(pos1.getBlockY(), pos2.getBlockY()); double maxy = Math.max(pos1.getBlockY(), pos2.getBlockY()); for (double x = minx; x <= maxx; x++) { for (double y = miny; y <= maxy; y++) { for (double z = minz; z <= maxz; z++) { Location loc = new Location(w, x, y, z); if (x == minx || x == maxx || z == minz || z == maxz) { loc.getBlock().setType(Material.GLASS); } else { loc.getBlock().setType(Material.AIR); } } } } } public boolean inSpawn(Location ploc) { if (ploc == null) { return false; } else { return ploc.getBlockX() >= pos1.getBlockX() && ploc.getBlockX() <= pos2.getBlockX() && ploc.getBlockY() >= pos1.getBlockY() && ploc.getBlockY() <= pos2.getBlockY() && ploc.getBlockZ() >= pos1.getBlockZ() && ploc.getBlockZ() <= pos2.getBlockZ(); } } @EventHandler public void onCheckLoc(PlayerMoveEvent e) { Player p = (Player)e.getPlayer(); Vector pvec = p.getLocation().toVector(); if (pvec.isInAABB(pos1, pos2)) { if (inspawn.contains(p.getUniqueId())) { return; } else { p.sendMessage("in spawn"); inspawn.add(p.getUniqueId()); } } else { if (inspawn.contains(p.getUniqueId())) { p.sendMessage("pvp area"); inspawn.remove(p.getUniqueId()); Sent from my iPhone using Tapatalk