Faction plugin - Claiming land

Discussion in 'Plugin Development' started by closeplanet2, Oct 5, 2018.

Thread Status:
Not open for further replies.
  1. Offline

    closeplanet2

    Hello. I am writing a custom faction plugin for the server. I am getting through nicely however i am interested in knowing what is the best way to set the claiming function up for the plugin so that when you do the commands it interacts with the chunk.

    /f claim --- claim the chunk
    /f unclaim --- unclaim the chunk

    Also if its possible, can someone tell me the easiest method for being able to add rules to chunks (No pvp in safezone, no blocks break in safezone etc.)
     
  2. @closeplanet2
    For claiming land, you can divide everything in chunks, but you can use any other size or form as well. I don't think dividing the available land in chunks is very clever because it's a lot of data and most factions will need land that covers many chunks. (This means that all those chunks will have the same data.)

    Anyway, you asked for chunks, so I will give some tips for chunks.

    You will need to create some kind of map that takes an X and Z coordinate as input and gives ChunkData as output. (In this case, ChunkData should be your class that contains per-chunk data.)
    The most simple approach would be to create a HashMap (or TreeMap) that takes a Point (chunkX, chunkY) as key and ChunkData as value.
    The con of this approach is that you will have to keep everything in memory all the time. This is fine unless you are writing for a huge server.
    You could also create your own map implementation. Reply if you would like help with that.

    After you created a proper map, this is how to use the map:
    Upon claiming a chunk, get the ChunkData using the map. If it is null, create a new instance of ChunkData and set the value for the chunk coords to that instance.
    Then set the faction of the ChunkData to the faction that is claiming. (You should check if it is already claimed and that kind of stuff.)

    You probably want to keep your data after a server restart, so you will have to save your data.
    There are multiple ways to save your data, but I will just show this one:
    Create a FileOutputStream for the file where you want to save everything (use yourPlugin.getDataFolder() ). Then create a DataOutputStream and use the FileOutputStream as argument to the constructor of DataOutputStream.
    Use the first byte of the file to indicate the version (so that you can load older saves after you expand ChunkData). Then use an int to store the amount of entries in your map.
    Then, for every entry in the map, save its coordinates and data.
    Close the stream at the end.

    Ok, saving your data is useless if you don't load it, so load it.
    For loading, use a FileInputStream and DataInputStream. If the data file can't be found, you should assume its the first time the plug-in is used and create a new empty map.
    If the file is found, load the data and put that in the map.
    The first byte you read it will indicate the version. If the byte indicates the version you used, continue. Otherwise, something weird happened and you should log some kind of error.
    Then use the DataInputStream to read the amount of entries that was saved.
    Then create the map where you store the data.
    When you read that your data contains N entries, do the following step N times:
    Read the next data from the input to create a new entry with the same data as the entry that was saved and put the entry in the new map.

    Save your data in the onDisable method of your plug-in and load your data in the onEnable method of your plug-in.
    You can reply if you have any questions.
     
    closeplanet2 likes this.
  3. @closeplanet2

    I see you liked my answer, so did you manage to solve it?
    If so, please mark this answer as 'Solved' in the title.
     
  4. that was helpful even to me :)
     
Thread Status:
Not open for further replies.

Share This Page