[Protection] Region/Cuboid creation

Discussion in 'Resources' started by KingFaris11, Jul 31, 2013.

Thread Status:
Not open for further replies.
  1. Credits for this topic goes to desht
    Desht made the whole cuboid class, I just edited it a bit, cleaned it up and added a few more things.

    This thread has the code to make Cuboids. This can be used for protection, block modification such as making huge glass cubes, etc.
    Cuboid class code: http://pastebin.com/pv8JFXXL
    Cuboid class code alternative: http://pastie.org/8273339

    UPDATED (Less methods but only necessary ones): https://gist.github.com/KingFaris10/4527fbaf8caa9fd7b800

    @EventHandler(ignoreCancelled = true, priority = EventPriority.LOW)
    public void onPlayerInteract(PlayerInteractEvent event) {
        if (event.getAction() == Action.RIGHT_CLICK_AIR) {
            if (event.getItem() != null) {
                if (event.getItem().getType() == Material.BLAZE_ROD) {
                    Location loc1 = new Location(event.getPlayer().getLocation().getWorld(), event.getPlayer().getLocation().getX() + 2, event.getPlayer().getLocation().getY() + 1, event.getPlayer().getLocation().getZ() + 2);
                    Location loc2 = new Location(event.getPlayer().getLocation().getWorld(), event.getPlayer().getLocation().getX() - 2, event.getPlayer().getLocation().getY(), event.getPlayer().getLocation().getZ() - 2);
                    Cuboid cuboid = new Cuboid(loc1, loc2);
                    for (Block block : cuboid)
    I hope this helped you.
  2. Offline


  3. historio
    No problem but I just edited and cleaned it.

  4. Offline


    KingFaris11 desht Thanks for this. I needed something like this for a while now
    KingFaris11 likes this.
  5. Offline


    Glad it's helpful to some people. However, I made Cuboid implement Iterable<Block>, so there's no need to add a getBlocks() method. You can just do:
    for (Block b cuboid) {
    KingFaris11 likes this.

  6. True, I realised that when I made it.
  7. Offline


    you can re-upload the code?
  8. Offline


  9. Yeah sorry, it took me a while to make that code and hastebin removed it. I'm too busy to retype everything from scratch. If anyone already used this code, could you please upload it somewhere and private message me the link so I can update it. Thanks.

    Edit: I've fixed the links.
  10. Offline


    If i wanted to implement a rollback system with this code, how would i go about doing that?
  11. You would need to loop through every block in the cuboid and save the id, data and location into a Map/config. Then when you want to rollback, you would need to loop through the saved blocks and set the blocks at the saved location to the saved id and data. I would use a List instead of a Map to save 3 values in a configuration.
    To save the blocks I would do this:
    List<String> cuboidBlocks = new ArrayList<String>();
    for (Block block : cuboid) {
        if (block != null) {
            cuboidBlocks.add(methodToConvertLocationToString(block.getLocation()) + " : " + block.getType().getId() + " : " + block.getData());
    int regionNumber = (this.getConfig().getValues(false).size() + 1);
    this.getConfig().set("Region " + regionNumber, cuboidBlocks);
    Then to rollback the blocks I would do this:
    List<String> cuboidBlocks = this.getConfig().getStringList("Region " + regionNumber);
    for (String strBlock : cuboidBlocks) {
        String[] split = strBlock.split(" : ");
        Block block = world.getBlockAt(methodToConvertStringToLocation(split[0]));
        block.setTypeIdAndData(Integer.parseInt(split[1]), Short.parseShort(split[2]), true);
    You need to create 2 methods, one to that converts a String into a Location and one that converts a Location into a String.
    How to convert a Location into a String (this is from my code so you need a few more parameters):
    CeramicTitan likes this.
  12. Offline


    Wow, just wow. This tutorial was amazing, it was so clear and easy to follow. Thanks a lot. :)
  13. No problem.
  14. Offline


    KingFaris11 Why is it Right Click Air if you want to right click a block?
  15. I didn't want to make it so you can change the block, I did it so it makes yourself trapped in a glass block if you right click with a blaze rod.
  16. Offline


    This needs a bump. Its soooo usefull!

    KingFaris11 Non-random question. How would I check if a player is in a cuboid? I coulnd't find a method for it in your cuboid class.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 3, 2016
    KingFaris11 likes this.
  17. Offline



    if (cuboid.contains(player.getLocation())) {
    // ...
  18. Offline


  19. Didn't know that existed xP Thanks. In my Prisons plugin I had this:

        public List<Player> getPlayers() {
            List<Player> playerList = new ArrayList<Player>();
            World world = this.getWorld(true);
            if (world != null) {
                List<Player> worldPlayers = world.getPlayers();
                for (int x = this.xPos1; x <= this.xPos2; x++) {
                    for (int y = this.yPos1; y <= this.yPos2; y++) {
                        for (int z = this.zPos1; z <= this.zPos2; z++) {
                            for (Player player : worldPlayers) {
                                Location pLoc = player.getLocation();
                                if ((int) pLoc.getX() == x && (int) pLoc.getY() == y && (int) pLoc.getZ() == z) playerList.add(player);
                return playerList;
            } else {
                return new ArrayList<Player>();
    This is my own version of the Cuboid class if you want to use it. It is just a simple Cuboid class with not many features but only the main, useful ones. I prefer desht's one (the one on the top) but I use this one for Prisons only as it doesn't need many features.

    Link 1: http://pastebin.com/QX6X5XV1
    Link 2: http://pastie.org/8427201

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 3, 2016
  20. @KingFaris
    I can't seem to find the code for his Cuboid class anywhere. Could you link me to location where I can find it or give me a copy of the version that you are using?
  21. Offline


  22. By desht's one I meant the one I used at the top. It's basically desht's one just formatted and a few different stuff, although it's still his. :p
  23. Offline


    desht KingFaris11
    How would I go about setting the Cuboid to a file, then being able to call it back up?
    What I am trying to do is create a cuboid, then when a players walks into it, he turns into a ghost.
  24. Offline


    It implements ConfigurationSerializable, so you can just save to a config and cast back:
    yml.set("path.to.cuboid", cuboid);
    Cuboid getting = (Cuboid) yml.get("path.to.cuboid");
    Cuboid getting = new Cuboid(yml.get("path.to.cuboid"));

    I'm not sure which one is correct, I think the second one is called implicitly when you use the first one?
  25. Offline


    Ah ok, makes sense.
    Also, checking if a player walks in and out of a region? I feel too costly using PlayerMoveEvent, is there a better way?
    Or should I just use getPlayers() and check if any are in the region?
  26. Offline


    The first one is correct.

    PlayerMoveEvent is really your only option. Just take care with it - in particular, return immediately from the event handler if none of the X, Y and Z block coords have changed (comparing event.getFrom().getBlockX() and event.getTo().getBlockX(), and similarly for Y & Z).

    Once you're sure the player has moved to a different block, you can then see if they're in a region, e.g.:
    // where myStoredRegions is some list or other
    // iterable of the regions your plugin cares about..
    for (Cuboid region myStoredRegions) {
      if (
    region.contains(event.getTo()) && !region.contains(event.getFrom()) {
    // player is moving into a region!  do something
    } else if (!region.contains(event.getTo()) && region.contains(event.getFrom()) {
    // player is moving out of a region!  do something
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 3, 2016
  27. Offline


    Thanks man, figured it out.
    If anyone wants to see how to modify it to store 2 locations in a flat file on left and right click, then on command, get it and do something within it, let me know.
    KingFaris11 likes this.
  28. Thanks for helping around. I rarely check this forums so...
  29. Offline


    So i'm a complete noob at java. Know nothing about it. But i really want to make plugins and reading all the development stuff i'm getting some things complete. I want to make a command that set up a region with a size argument from the players point in the world. I'm trying to learn more and i want to get a region/ cuboid code working but when i use this code and class i'm getting issues in the class file.

    So in the class i get this warning

    1. public boolean containsOnly(int blockId) {
    2. for (Block b : this) {
    3. if (b.getTypeId() != blockId) return false;
    4. }
    5. return true;
    6. }

    and because i'm a noob i don't know how to fix it. Only reason why i'm asking is because the code is up for resource and it doesn't seem to be up to date?
Thread Status:
Not open for further replies.

Share This Page