Chest refill every X minutes within area

Discussion in 'Plugin Development' started by KarimAKL, May 23, 2018.

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

    KarimAKL

    @timtower Refill as in put items in it (like survival/hunger games) and i would rather have a list of chests(cords for the chests maybe?) and then use them for it if possible.
     
  2. Offline

    timtower Administrator Administrator Moderator

    Then you need to start by getting that list.
    A one time search and then listening for BlockPlaceEvent will probably do the trick.
     
  3. Offline

    KarimAKL

    @timtower Would it be good to make a yml file with all the cords for the chests? Also what do you mean by listening for BlockPlaceEvent? What should i do there?
     
  4. Offline

    timtower Administrator Administrator Moderator

    Any type of storage is possible.
    And then you can listen for placement of new chests.
     
  5. Offline

    KarimAKL

    @timtower I see, then everytime a new chest is set inside the area i should put it in the yml file?
     
  6. Offline

    timtower Administrator Administrator Moderator

    Or you check the entire area again.
    There are multiple options to do this. Your call on which one to use.
     
  7. Offline

    KarimAKL

    @timtower When you said a list did you mean something like this?:
    Code:Java
    1.  
    2. List<Block> whatever = new ArrayList<Block>();
    3.  

    Or maybe something else? Also wouldn't checking the entire area again maybe cause some lag?
    EDIT: Here is the cords for my area if you need it to explain something better or anything like that: (X 225) (Y 91) (Z 107) | (X 333) (Y 205) (Z 259) In between this.
     
    Last edited by a moderator: May 23, 2018
  8. Offline

    timtower Administrator Administrator Moderator

    @KarimAKL List of Locations is probably better for this.
    And it will cause some lag yes.
     
  9. Offline

    KarimAKL

    @timtower Okay, like this then?:
    Code:Java
    1.  
    2. List<Location> whatever = new ArrayList<Location>();
    3.  

    And then would it be better to do it in waves instead of all at once?
     
  10. Offline

    timtower Administrator Administrator Moderator

    @KarimAKL It is your call how you want to do it. Not mine.
     
  11. Offline

    KarimAKL

    @timtower I just wanted to ask if it would reduce the lag if i made it in waves instead of all at once.
    EDIT: Btw was the List with Locations correct?
     
  12. Offline

    timtower Administrator Administrator Moderator

    @KarimAKL There are many ways to reduce the lag. Focus on getting it working first.
    Amd did it give errors? If not: correct.
     
  13. Offline

    KarimAKL

    @timtower Okay, i'll focus on getting it working first as you said. And no it doesn't come with any errors, now i just need to add them to the list, right? As you said before one way of doing that would be listening to the BlockPlaceEvent, i'm thinking of making a command like "/randomchest" and then a HashMap boolean so that if you type "/randomchest" the next chest you place will be added to the list, that way you can choose if it should be added to the list or not, what do you think?
     
  14. Offline

    timtower Administrator Administrator Moderator

    @KarimAKL I think that it is your plugin so your decision to make.
     
  15. Offline

    KarimAKL

    @timtower I just wanted to ask if it sounds like a good idea or maybe there is some more effecient way or something. :7 Anyway, i'll try making this now.
    EDIT: What's the difference between HashMap and HashSet? EDIT: Just looked up the difference, would it be better to use HashSet to look if the player's UUID is there? (for a boolean)
     
  16. Offline

    timtower Administrator Administrator Moderator

    @KarimAKL Use a Set for the UUID, if the set contains it: do stuff.
    Using a map of any kind is overkill for a true false.
     
  17. Offline

    KarimAKL

    @timtower Okay, thank you for that information. :)
    EDIT: Okay, now i've made a command that enables or disables if you want to place a random chest and i've made a PlayerQuitEvent that disables it if you left the server while it was enabled and i've made a BlockPlaceEvent and BlockBreakEvent checking if the player has permission to place/break a block and adding/removing the location from the list, now i need to get the area it should search for random chests in and then refill them every X minutes, what should i do now to do that?
     
    Last edited by a moderator: May 24, 2018
  18. Offline

    KarimAKL

  19. Offline

    KarimAKL

    @timtower Where can i read about how to check for blocks(chests in this case) within 2 locations? I've been trying to look for it but couldn't find anything. :7
     
  20. Offline

    timtower Administrator Administrator Moderator

  21. Offline

    KarimAKL

    @timtower I decided to make a config.yml with the world and the coordinates, anyway i have this:
    Code:Java
    1.  
    2. double pos1x = plugin.getConfig().getDouble("Area.Position1.X");
    3. double pos1y = plugin.getConfig().getDouble("Area.Position1.Y");
    4. double pos1z = plugin.getConfig().getDouble("Area.Position1.Z");
    5. double pos2x = plugin.getConfig().getDouble("Area.Position2.X");
    6. double pos2y = plugin.getConfig().getDouble("Area.Position2.Y");
    7. double pos2z = plugin.getConfig().getDouble("Area.Position2.Z");
    8. Location loc1 = new Location(Bukkit.getWorld(plugin.getConfig().getString("Area.World")),pos1x,pos1y,pos1z);
    9. Location loc2 = new Location(Bukkit.getWorld(plugin.getConfig().getString("Area.World")),pos2x,pos2y,pos2z);
    10.  

    And i tried looking up triple for loops but couldn't find anything that explained what is going on. :7 So this is my guess(probably very wrong):
    Code:Java
    1.  
    2. for (double x = pos1x; x <= pos2x; x++) {
    3. for (double y = pos1y; y <= pos2y; y++) {
    4. for (double z = pos1z; z <= pos2z; z++) {
    5. //Do something
    6. }
    7. }
    8. }
    9.  

    How is this?
    EDIT: Should i do this instead?:
    Code:Java
    1.  
    2. for (double x >= pos1x; x <= pos2x; x++) {
    3. for (double y >= pos1y; y <= pos2y; y++) {
    4. for (double z >= pos1z; z <= pos2z; z++) {
    5. //Do something
    6. }
    7. }
    8. }
    9.  

    Or not?
    EDIT2: Nvm, the second one made errors at ">=". :p
     
  22. Offline

    Sploon

    This will only work if loc2's values are always greater than loc1's. For example, if loc1.getX() is 100, and loc2.getX() is 0 it will not work as you want.
    Iterating through the entire section of blocks to find the chests you're looking for is going to be real slow, though. A list of chest locations, as timtower mentioned, is definitely a better solution.
     
  23. Offline

    KarimAKL

    @Sploon I have this:
    Code:Java
    1.  
    2. public class LocationList {
    3.  
    4. public static List<Location> locations = new ArrayList<Location>();
    5. }
    6.  

    And i have a way of adding the chests locations to the list, how would i get the list in the BukkitRunnable and fill them up tho? :7
    EDIT: Also for the triple loop should i just change the "<=" to ">=" if i wanted to do it like that instead?
     
  24. Offline

    Sploon

    Both the class and the locations list are public, so you should have no problem accessing then.
    You can get the chest block state from a location with:
    Code:
    (Chest)location.getBlock().getState()
    The chest class has methods for accessing the inventory.
     
  25. Offline

    KarimAKL

    @Sploon I know how to access the list in a static way but i don't know what i should do from there to access the locations'(chests) inventories and fill them up. :7
     
  26. Offline

    Sploon

    See my previous post:

     
  27. Offline

    KarimAKL

    @Sploon I saw it but i don't know how i should use that. :7 How would i get all the locations from the list?
     
  28. Offline

    Sploon

    For loop:
    for(Location chestLocation : list) {
    }
     
  29. Offline

    KarimAKL

    @Sploon Okay, so like this?:
    Code:Java
    1.  
    2. for (Location chests : LocationList.locations) {
    3. //Do something
    4. }
    5.  
     
  30. Offline

    Sploon

    Yes. In the "// do something" area, you want to access the chest from the blockstate (as seen above).
     
Thread Status:
Not open for further replies.

Share This Page