Trying to Build Organized API

Discussion in 'Spigot Plugin Development' started by Gannam3, Jul 26, 2018.

Thread Status:
Not open for further replies.
  1. My last attempt at building a server plugin was a disorganized mess of "Well, I could do this..." and I'm trying to make as much of my code reusable as possible.

    But one issue I'm running into is that I'm not sure how to use information from Arraylists elsewhere without making them static. Here are some examples of what I mean:

    Code (Java):

    package pickquest.common;

    import java.util.ArrayList;

    import org.bukkit.Location;

    public class ScanRadiusBlk {
        public ArrayList<Location> buildRadius(Location loc, int radius) {
     
            ArrayList<Location> coordinatesCandidates = new ArrayList<Location>();

            for(double x = loc.getX() - radius; x<= (loc.getX() +radius); x++) {
                for (double y = loc.getY() - 1; y<= (loc.getY() + 1); y++) {
                    for (double z = loc.getZ() - radius; z<= (loc.getZ() + radius); z++) {
                    }
                }
            }
            return coordinatesCandidates;
        }
    }
     
    Code (Java):

    package pickquest.common;

    import java.util.ArrayList;

    import org.bukkit.Location;
    import org.bukkit.block.Block;

    public class HumanoidSafety {
        public enum unsafeUnder {
            AIR, LAVA, MAGMA_BLOCK, CAVE_AIR, LEGACY_AIR
        }
        public enum safeAbove {
            AIR, CAVE_AIR, LEGACY_AIR, WATER, DOUBLE_PLANT
        }
        public enum safeAt {
            AIR, CAVE_AIR, LEGACY_AIR, WATER, LONG_GRASS, OAK_SAPLING,
            SPRUCE_SAPLING, BIRCH_SAPLING, JUNGLE_SAPLING, DARK_OAK_SAPLING,
            ACACIA_SAPLING, RED_MUSHROOM, BROWN_MUSHROOM, WHEAT, REEDS,
            PUMPKIN_STEM, VINE, NETHER_WART, CARROTS, POTATOES, DANDELION,
            POPPY, BLUE_ORCHID, ALLIUM, AZURE_BLUET, RED_TULIP, ORANGE_TULIP,
            WHITE_TULIP, PINK_TULIP, OXEYE_DAISY, SUNFLOWER, LILAC, RED_ROSE,
            YELLOW_FLOWER, CARPET
        }

        public ArrayList<Location> safeLocation = new ArrayList<>();

        public ArrayList<Location> getsafe() {
            return safeLocation;
        }

        private boolean isSafe(Location loc) {
            Location loc1 = loc;
            Location locunder = loc1.subtract(0,1,0);
            Location locat = loc1.add(0,1,0);
            Location locabove = loc1.add(0,2,0);
            Block under = locunder.getBlock();
            Block at = locat.getBlock();
            Block above = locabove.getBlock();
            String material1 = under.getBlockData().getMaterial().toString();
            String material3 = above.getBlockData().getMaterial().toString();
            String material2 = at.getBlockData().getMaterial().toString();
            for(unsafeUnder ms: unsafeUnder.values()) {
                if(material1.equals(ms.toString())) {
                    break;
                }
                else {
                    for(safeAt sa: safeAt.values()) {
                        if(material2.equals(sa.toString())) {
                            for(safeAbove st: safeAbove.values()) {
                                if(material3.equals(st.toString())) {
                                    return true;
                        }
                            }
                        }
                        else if(material2.contains("DOOR")) {
                            safeLocation.add(loc);
               
                            return true;
                            //Add to arraylist of safe location
                        }
                    }
                }
            }
            return false;
        }
        public boolean getSafe(Location loc) {
            return this.isSafe(loc);
        }
    }
     
    I'd like to use these two snippets for AStar pathfinding in a bit, but I see the value of them in many other places as well. But for AStar, I'd want to store the blocks in an arraylist, then decide which ones were safe, then remove the starting position, consider waiting locations against movement cost, modify NPC behaviour ... you know. Pathfinding stuff. For potentially multiple NPCs at a time.

    I guess, if I can't use arraylists for this, could you tell me how I could reliably carry this information over to be handled in other processes across multiple classes? I know the use of static is strongly discouraged, and I'd probably have several instances of each of these processes going at once for each NPC.
    Thank you for your help.
     
    #1 Gannam3, Jul 26, 2018
    Last edited: Jul 26, 2018
Thread Status:
Not open for further replies.