1.8.8 I want this code to apply to a single block

Discussion in 'Spigot Plugin Development' started by ADAAM_TM, Jul 8, 2021.

  1. Hello!
    I made a launchpad code but it applies to all "LIGHT_WEIGHTED_PRESSURE_PLATE" and I want it to apply to one.

    Is this possible, please?
    Thank you for reading!
     

    Attached Files:

  2. Hello / Salut l'ami,

    If you want a specific block, you'll have to code something more specific: maybe the block below the pressure plate will have to be a specific one, like bedrock? Maybe coordinates? Maybe WorldGuard Regions? Maybe WorldGuard flags?
     
    • Agree Agree x 2
  3. Use coordinates, it is the most ideal :unsure:

    1. Save the coords in any file
    2. Check if the stored coordinates are the same as the player's
    3. Do something...
     
  4. Well that will cost a lot of performance.

    A more efficient way would be to check if the player is standing on a light weight pressure plate and check if there is a specific block unterneeth it. And only if there is one of those blocks set the velocity.
     
    • Funny Funny x 4
  5. That is valid if there is no problem in having a block below it, imagine if it is suppsote to work as some sort of portable jumppad.
    In order to save memory what could be done is to create a ArrayList<Location> that collects the locations from the config when the plugin initializes.
    The rest is basically the same as above, every time a player presses the plate it iterates the list and checks if the Location is contained.
     
    • Like Like x 1
  6. I am not sure how you got to this conclusion.

    What you are suggesting is: retrieving block object below the pressure plate and comparing it to some value (to retrieve it, you either use coordinates of the clicked (activated) pressure plate and subtract 1 from it's Y coords, or you use player's location (coords) and block that's relative to their position - either way, you will use coords/location).

    What use of coordinates does is: retrieves location property of pressureplate (activated block), compares it to the location of known pressure plates that should activate launching pad. The location can be read from config when plugin is being enabled and stored in memory (it's 3 double variables, world and some metadata, really not a big deal, memory-wise).

    In grand scheme of things, these two are really similar when it comes to how performance heavy they are. The difference however lies in how flexible these solutions are, and how universaly it solves the OP's problem. If you use the checking-block-below strategy, you are locking yourself into specific design decision that users of your plugin cannot influence, or in some cases don't want to implement (seeing 1.8.8 tag on this thread, I assume some minigame on small map, where changing the appearance of the map is not desirable). If you use precise location of the pressure plate, you are not locked into anything else, but the location of the plate.

    Sometimes, when I put these together, I may sound ignorant or combative, but it is never my intention. Hope this post helps someone, have a nice day!
     
    • Agree Agree x 2
  7. In addition to that, If you are short on RAM, you could create an object to save 3 integers and one string for the world, this is as memory cheap as I believe.
    I say integer because as the plate occupies the whole block you can save it as the 2nd lightiest data type.
     
    • Agree Agree x 1
  8. hello / salut
    thank you very much for your answer!
    but what should I mark in my code because I have no idea... I can't do it
     
  9. Just compare the pressure plate's location to a configured location. If you're not sure how to use a config, then assuming you know the location of your pressure plate, you can just hardcode it (at least until you learn how to use a config).
    And don't worry about the comments above saying this and that about performance. You shouldn't really need to worry about performance optimization until you actually encounter an issue with it.
     
  10. Code (Java):
    Location loc2 = new Location(Bukkit.getWorld("world"), 43.589 ,155 ,-8.700);
        if(player.getLocation() == loc2 && player.getLocation().getBlock().equals(Material.LIGHT_WEIGHTED_PRESSURE_PLATE)) {

    like that ??

    it not work :cry::cry::cry::cry::cry:
     
  11. Code (Java):
    player.getLocation().getBlock().equals(Material.LIGHT_WEIGHTED_PRESSURE_PLATE)
    This will always return false. A block will never be equal to a material because they are not the same type or class. Instead you'll need to use Block#getType, then check if that is equal to the light weighted pressure plate.

    Code (Java):
    player.getLocation() == loc2
    This part is also very likely to return false - firstly, objects should generally be compared using the #equals method instead of ==, but also your player would need to be at those very specific coordinates, including the decimals.
    You should round your coordinates to the lowest whole number, i.e. 43, 155, -9 instead of 43.589, 155, -8.700. Then, instead of comparing your pressure plate's location to the player's location, you'd compare it to the location of the block where the player is.
    Something like this:
    Code (Java):
    Block block = player.getLocation().getBlock();
    if (block.getLocation().equals(loc2) && block.getType() == Material.LIGHT_WEIGHTED_PRESSURE_PLATE) {
        // ...
    }
     
    • Like Like x 1
  12. Comparing three integers is heavy? Lol
     
    • Like Like x 1
    • Agree Agree x 1
  13. man post farming
     
  14. I got enough posts and ratings. I just wanted to say: comparing three integers is no problem at all unless it’s done a million times per tick
     
    #14 mfnalex, Jul 11, 2021
    Last edited: Jul 11, 2021
    • Like Like x 2
    • Funny Funny x 2
  15. This is thread is such a mess.

    The effective solution of this to use a 4-objectwrapper class, LocationWrapper(String, int, int, int), which implements an override for the hash and equals method. You may reference the bukkit Location hash method (no need to respect pitch/yaw, remember that you use integer accuracy.) like that you can simply use a Map<LocationWrapper, ?> which maps to the specifications of your launch pad.

    You can then just pass the player location into your location wrapper, and retrieve it like that.

    This is the only correct solution to a problem like this, doing it otherwise is just wrong.

    If your launch pads are all expected to operate the same, you can just use a Set<LocationWrapper> instead of a map for it as you will not require any configuration on it.
     
    • Creative Creative x 1
  16. Mr Bloodcrown provided the optimal solution to this problem. I‘m still waiting for the LocationWrapper class to be released as maven artifact though
     
  17. you might wanna specify the block in-game first, and getting it's position exactly in the world and save it somewhere, although, you can save the location from the start but this might be not efficient enough because you might replace the block by another one and it'll still work because it has the same location, so you might wanna get as far as you can with it, for example: save the location, the type, and the data of the block so that it might get as far as possible of specification, but i think it's impossible to get a "UUID" of a block unless there is a way of adding an attachment to it, something like a permission or so.
     
  18. Uhh just use the location of that block? What do you mean by "apply it to one"?
    also dude indent your code
     
  19. This dude only wants this to apply to ONE block, he only needs ONE location, not a single list or array object is needed what are you all on???
     
    • Like Like x 1
    • Funny Funny x 1
  20. single SPECIFIED block not a location, because that location can literally hold any single block you might imagine.