I have a cube that can generate anywhere on the server which is working well. however, i want to disable pvp in these cubes but the co0rds can change, how can i achieve this? creation method Code: public void generateCube(Location loc) { World w = loc.getWorld(); int radius = 3; for (int x = loc.getBlockX() - radius; x <= loc.getBlockX() + radius; x++) { for (int y = loc.getBlockY() - 1; y <= loc.getBlockY() + 11; y++) { for (int z = loc.getBlockZ() - radius; z <= loc.getBlockZ() + radius; z++) { Location area = new Location(w, x, y, z); if (y == loc.getBlockY() - 1) { area.getBlock().setType(Material.BEDROCK); if (x <= loc.getBlockX() - radius || x <= loc.getBlockX() + radius || z <= loc.getBlockZ() - radius || z <= loc.getBlockZ() + radius) { area.getBlock().setType(Material.GLOWSTONE); } } else { area.getBlock().setType(Material.AIR); }
You can store all the locations each cube contains in an arraylist or set and check in the EntityDamageByEntityEvent if the location is in the list and cancel it. Alternatively you can create a class cube which holds all the relevant information (would be more organized)
How would I go about storing the whole cube, I defined it as a Location called “area”. Would I just add that to a list and save the list to a file? Thanks for the suggestion! Sent from my iPhone using Tapatalk
You can create a custom class Cube. Depending on what your goal is this class contains different things, such as: A Set of locations (every location you use to build the cube) A list of players in the cube Many more things you need You can move your createCube method also in there (either constructor or method). On the event you then loop through all cubes and check if the event location is in the location list from the cube.
Thanks for the suggestions, it’s really helpful! I’m stuck with Getting the location from the createCube method and using the locations in other methods (checking if a player enters or leaves the cube) I’m self taught so all help is sincerely appreciated thankyou EDIT its crashing my server and saving every xyz into the config, can I fix this and is this the right way of doing? Code: public ArrayList<Location> blocks = new ArrayList<Location>(); public void generateCube(Location loc) { World w = loc.getWorld(); int radius = 3; for (int x = loc.getBlockX() - radius; x <= loc.getBlockX() + radius; x++) { for (int y = loc.getBlockY() - 1; y <= loc.getBlockY() + 11; y++) { for (int z = loc.getBlockZ() - radius; z <= loc.getBlockZ() + radius; z++) { Location area = new Location(w, x, y, z); blocks.add(area); pl.getConfig().set("loc", blocks); pl.saveConfig(); if (y == loc.getBlockY() - 1) { area.getBlock().setType(Material.BEDROCK); if (x <= loc.getBlockX() - radius || x <= loc.getBlockX() + radius || z <= loc.getBlockZ() - radius || z <= loc.getBlockZ() + radius) { area.getBlock().setType(Material.GLOWSTONE); } } else { area.getBlock().setType(Material.AIR); } } } } return; Sent from my iPhone using Tapatalk
Generally speaking it looks fine to me. Only thing that is weird, is that you're storing the list to the config every time. You store and save the config 432 times (no need to store it in a config at that point anyway). Maybe it's just too much for the server to handle. You can debug your code by printing out different messages to the console at different stages of your code to see what exactly causes the crash.
@wand555 thanks for the tip, here's what I got now: Code: public void setCube(Location loc) { ConfigurationSection cs = pl.getConfig().getConfigurationSection("loc"); World w = loc.getWorld(); int radius = 3; for (int x = loc.getBlockX() - radius; x <= loc.getBlockX() + radius; x++) { for (int y = loc.getBlockY() - 1; y <= loc.getBlockY() + 11; y++) { for (int z = loc.getBlockZ() - radius; z <= loc.getBlockZ() + radius; z++) { Location area = new Location(w, x, y, z); if (y == loc.getBlockY() - 1) { area.getBlock().setType(Material.BEDROCK); } else { area.getBlock().setType(Material.AIR); } if (cs == null) { cs = pl.getConfig().createSection("loc"); } cs.set("world", w.getName()); pl.getConfig().set("x", x); pl.getConfig().set("y", y); pl.getConfig().set("z", z); pl.saveConfig(); blocks.add(area); } How can I check if the player enters my newly created cube?
Well, on PlayerMoveEvent you loop over all cubes and look if the location of the player is in the location list of the cube. You can store every cube you've created in a static List btw. However this method has the downside that it will be very resource intensive. Depending on what your overall goal is, there is surely a better way to go. (e.g. if the exact moment the player enters the cube doesn't matter, you can have a runnable looping through all online players and checking their current position). Why are you storing the list in the config though? It'd be much faster if you just keep it in the list.
@TerroDoor Why store all the locations of the cube in a config? By knowing the origin of the cube and its width, depth, and height you can figure out of a location is inside: Code: public boolean insideCube(Location playerLoc, Location cubeOrigin, int width, int height, int depth) { int playerX = playerLoc.getBlockX(); int playerY = playerLoc.getBlockY(); int playerZ = playerLoc.getBlockZ(); int originX = cubeOrigin.getBlockX(); int originY = cubeOrigin.getBlockY(); int originZ = cubeOrigin.getBlockZ(); return (playerX > originX && playerX < originX + width) && (playerY > originY && playerY < originY + height) && (playerZ > originZ && playerZ < originZ + depth); } For each axis the cube exists in, we can test whether the player is inside the bounds that is defined by the cube. The separating axis theorem explains this in more detail. Edit: Also, if you're in eclipse, you can use ctrl+shift+f to automatically format your code so it's easier to read.