i need knowledge

Discussion in 'Plugin Development' started by TerroDoor, Nov 21, 2019.

  1. Offline

    TerroDoor

    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?
     
  2. Online

    timtower Moderator Moderator

    @TerroDoor I am gonna go with multiple instances without seeing any code.
     
  3. Offline

    KarimAKL

    What isn't firing? The constructor? The constructor is called everytime you create a new instance of that class.
     
  4. Offline

    TerroDoor

    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
     
  5. Offline

    CraftCreeper6

    @TerroDoor
    Are you using the correct initializer for the constructor? Does your constructor take any parameters that you are failing to use?
     
  6. Offline

    KarimAKL

    If that's your constructor then it doesn't do anything.
     
  7. Offline

    TerroDoor

    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.
     
    Last edited by a moderator: Nov 21, 2019
  8. Offline

    CraftCreeper6

  9. Offline

    TerroDoor

    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
     
  10. Offline

    CraftCreeper6

    @TerroDoor
    That's not a constructor.

    EDIT: Blind as a bat.
     
    Last edited: Nov 21, 2019
  11. Offline

    KarimAKL

    @CraftCreeper6 The constructor is in the code.

    @TerroDoor Where do you create a new instance of this class? Did you try debugging the code?
     
  12. Offline

    CraftCreeper6

    @KarimAKL
    Thanks, didn't see, it's very messy.
     
  13. Offline

    TerroDoor

    I tried in my onEnable by using ‘new region();’

    But no errors and no luck


    Sent from my iPhone using Tapatalk
     
  14. Offline

    KarimAKL

    @CraftCreeper6 Yeah, i was just looking for "public region()" and then i copied it into notepad++ to format it. :p

    @TerroDoor Okay so, you're creating a new instance of "region" but, did you try debugging the code in the constructor?
     
  15. Offline

    TerroDoor

    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
     
  16. Offline

    CraftCreeper6

    @TerroDoor
    So in your original post you mentioned that you "removed the command". Does that mean you get rid of the instance too?
     
  17. Offline

    TerroDoor

    I kept the instance but removed the command


    Sent from my iPhone using Tapatalk
     
  18. Offline

    CraftCreeper6

    @TerroDoor
    Can you show me where you first create the instance.
     
  19. Offline

    TerroDoor

    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
     
  20. Offline

    CraftCreeper6

    @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.
     
  21. Offline

    TerroDoor

    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
     
  22. Offline

    CraftCreeper6

    @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.
     
  23. Offline

    TerroDoor

    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
     
  24. Offline

    CraftCreeper6

    @TerroDoor
    Yes. Just make sure it's the same instance.
     
  25. Offline

    TerroDoor

    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
     
  26. Offline

    CraftCreeper6

    @TerroDoor
    Do you have the @EventHandler annotations.
     
  27. Offline

    TerroDoor

    @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
     
    Last edited: Nov 21, 2019
  28. Online

    timtower Moderator Moderator

    @TerroDoor How do you have a constructor for region in a class called help?
     
  29. Offline

    TerroDoor

    I fixed that after posting sorry


    Sent from my iPhone using Tapatalk
     

Share This Page