javaoopbukkitextending-classes

(Java, Bukkit) Using extended class methods don't work (Noob oop)


I have one class named 'Regions', which make region and check if coordinates are in it. Then I have other class 'ArrowTower' which extends 'Region'. Class 'ArrowTower' creates towers. All day I was experimenting with no results. What I want to do is assign first and second location to class 'Regions' then make region and check if coordinates belongs to it. I also have events - 'BlockTouch' class which creates 'ArrowTower' object.. When I try to tower.insideRegion(e.getClickedBlock()); it gives me my location and then zeros, because it didin't set values in public void buildArrowTower, but values are right here. So I just don't understand why this isn't working :/

My Events class:

import eu.anavicius.TomTom1997.TomTowerDefence.Towers.ArrowTower;

public class BlockTouch implements Listener {   

    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e) {
        ArrowTower tower = new ArrowTower();
        if (e.getAction() == Action.RIGHT_CLICK_BLOCK ) {
            if (e.getItem().getType() == Material.ARROW) {
                tower.buildArrowTower(e.getClickedBlock(),e.getPlayer());
            } else if (e.getItem().getType() == Material.BONE) {
                Bukkit.broadcastMessage("Bone");
                tower.insideRegion(e.getClickedBlock());
            }
        }
    }
}

My Region.class:

public class Regions {
    private int xl,yl,zl,xh,yh,zh;

    public void setLRegion (int x, int y, int z) {
        xl = x;
        yl = y;
        zl = z;
        //Bukkit.broadcastMessage("SetLMethod" + " \t " +    "|" + xl + "|"+ xh + "||" + "|" + yl + "|" + yh + "||"  + "|" +zl + "|" + zh + "||");
    }

    public void setHRegion (int x, int y, int z) {
        xh = x;
        yh = y;
        zh = z;
        //Bukkit.broadcastMessage("SetHMethod" + " \t " +    "|" + xl + "|"+ xh + "||" + "|" + yl + "|" + yh + "||"  + "|" +zl + "|" + zh + "||");
    }


    public void insideRegion (Block l) {
        int x,y,z;
        x = l.getX();
        y = l.getY();
        z =l.getZ();
        Bukkit.broadcastMessage("InsideMethod" + " \\t " + x  + "|" + xl + "|"+ xh + "||" +y + "|" + yl + "|" + yh + "||" + z + "|" +zl + "|" + zh + "||");

        if (x >= xl && x <= xh ) {
            Bukkit.broadcastMessage("Region check 1");
            if (z >= zl && z <= zh) {
                Bukkit.broadcastMessage("Region check 2");
                if (y >= yl && y >= yh) {
                    Bukkit.broadcastMessage("Regione");
                }
            }
        }

    }
}

My ArrowTower.class:

public class ArrowTower extends Regions {

    public ArrowTower () {

    }

    public void buildArrowTower (Block b,Player p) {
        if (b.getType().equals(Material.EMERALD_BLOCK)) {
            Location loc = b.getLocation();
            for (int y = 0; y < 4;y++) {
                int layloc = 0;
                int by = loc.getBlock().getY()+1 + y;
                for (int x = 0;x <3;x++) {
                    int bx = loc.getBlock().getX()-1 + x;
                    for (int z = 0;z < 3;z++) { // Pagalvot dar del delay, nes su kintamaisiais pirma blogai buvau
                        int bz = loc.getBlock().getZ()-1 + z; // sugalvojas, atsispausdina tuscios vietos
                        Location block = new Location(b.getWorld(),bx,by,bz); // pass loop values to method
                        if (y == 0 && layloc == 0) {
                            Bukkit.broadcastMessage("SetR L");
                            setLRegion(bx,by,bz);
                        } else if (y == 3 && layloc == 8) {
                            Bukkit.broadcastMessage("SetR H");
                            setHRegion(bx,by,bz);
                        }
                        block.getBlock().setType(Material.matchMaterial(towerl1(y,layloc)));
                        layloc++;
                    }
                }
            }
        }
    }

    public String towerl1(int h, int layloc) {
        String[] layer1 = { // - l
                "LOG","AIR","LOG",
                "AIR","AIR","AIR",
                "LOG","AIR","LOG"}; 
        String[] layer2 = { // - i
                "COBBLE_WALL","COBBLESTONE","COBBLE_WALL",
                "COBBLESTONE","AIR","COBBLESTONE",
                "COBBLE_WALL","COBBLESTONE","COBBLE_WALL"};
        String[] layer3 = { // - t
                "COBBLE_WALL","AIR","COBBLE_WALL",
                "COBBLE_WALL","MOSSY_COBBLESTONE","COBBLE_WALL",
                "COBBLE_WALL","AIR","COBBLE_WALL"};
        String[] layer4 = {
                "AIR","AIR","AIR",
                "AIR","JACK_O_LANTERN","AIR",
                "AIR","AIR","AIR"};
        if (h == 0) {
            return layer1[layloc];
        } else if (h == 1) {
            return layer2[layloc];
        } else if (h == 2) {
            return layer3[layloc];
        } else if (h == 3) {
            return layer4[layloc];
        } else {
            return null;
        }
    }
}

Solution

  • The problem is that you create a new ArrowTower object on each interact event. I assume you first right-click on a block while holding an arrow in your hand - that's when tower.buildArrowTower(e.getClickedBlock(),e.getPlayer()); is called.

    Then you take a bone in your hand, and click again - but this time your event handler will create a completely new ArrowTower with it's first line (actually you don't even have a reference to the first one, since it was only declared in the function's scope):

    ArrowTower tower = new ArrowTower();
    

    Then you call:

    tower.insideRegion(e.getClickedBlock());
    

    but this tower was just created - actually it's xh, yh, etc. values weren't ever initialized.