Detect player in cobweb

Discussion in 'Plugin Development' started by The Fancy Whale, Jul 20, 2014.

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

    The Fancy Whale

    Anyone know of a good way to detect if a player is in cobweb? Please don't spoonfeed. I'm guessing you use player move event and try to use relative locations.
     
  2. Offline

    jeussa

    I believe this is what you're looking for:

    (Place this inside your listener)

    Code:java
    1. @EventHandler
    2. public void onPlayerMove(PlayerMoveEvent evt){
    3. //When a player moves from one block to another
    4. if(!evt.getFrom().getBlock().equals(evt.getTo().getBlock()){
    5. //The block located at the player's location
    6. Block b1 = evt.getTo().getBlock();
    7. //The block located above the player's location (which is at the eyehight of the player)
    8. Block b2 = evt.getTo().getBlock().getRelative(BlockFace.UP);
    9. if(b1.getType() == Material.WEB ||b2.getType() == Material.WEB){
    10. //TODO add your code here
    11. }
    12. }
    13. }
     
  3. Offline

    stormneo7

    It's better to make a boolean check. For example,
    Code:java
    1. public static boolean inWebs(Player p){
    2. if((p.getLocation().getBlock() != null && p.getLocation().getBlock().getType() == Material.WEB)
    3. || (p.getLocation().getRelative(BlockFace.UP).getBlock() != null && p.getLocation().getBlock().getRelative(BlockFace.UP).getType() == Material.WEB))
    4. return true;
    5. return false;
    6. }

    Now if you want to check if a player's in a web, using PlayerMoveEvent...
    Code:java
    1. @EventHandler
    2. public void onPlayerMove(PlayerMoveEvent evt){
    3. if(inWebs(evt.getPlayer())){
    4. //Code
    5. }
    6. }

    However, if you are, say, trying to do something to the player when they are in a web instead of when they move into a web (PlayerMoveEvent is only triggered when they move their screen), it is better to create a repeated scheduler.
     
  4. Offline

    Geekhellmc

    I think this is the answer
    Code:java
    1. public void onPlayerTouchLava(PlayerMoveEvent event) {
    2. if (event.getFrom().getBlock().getType() == Material.COBWEB && event.getTo().getBlock().getType() != Material.COBWEB) {
    3. //code
     
  5. Offline

    TwistPvP

    Any of these above should work, and the boolean check doesnt really make the code any better.. it just creates more methods being called upon.
    You can't argue it's more reliable either because it is virtually does the same thing.
    Hope this helps!
    Cameron
     
  6. Offline

    TheWolfBadger

    stormneo7 Why would you put a null check in the same if statement as a .getType();? It will throw a NullPointer if it is not null either way because the if statement contains .getType() on the null block...
     
  7. Offline

    fireblast709

    stormneo7 you don't need the null checks there
    TheWolfBadger
    Code:Java
    1. if(block == null || block.getType() == Material.AIR)
    is valid, because if the block is null, the OR can already return true thus Java won't need to validate the rest
    Code:Java
    1. if(block.getType() == Material.AIR || block == null)
    however this would throw a NPE because it tries to get the type of null (since that comes first in a left-to-right evaluation)
     
    TheWolfBadger likes this.
  8. Offline

    TwistPvP

    Additionally, in order for a player to get in a web in the first place they must move into it. Creating a scheduler for this is outright stupid, it just creates more threads causing the server to lag more.

    And the null checks are wrong too.. xD ^ as people pointed out

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

    stormneo7


    You need to check if it's null. For example, if you are say trying to get a block that doesn't exist or hasn't been rendered, it'll NPE. This obviously could happen if you are running a loop for getOnlinePlayers() while some of them are glitched entities.



    Private checks make the code clean. For example, if you were to rename multiple items to "rkct", you don't make multiple ItemMetas, that just makes your code long and boring. Instead you make a ItemStack returner that does it for you.

    4~ lines of code down to 1, simple as that. Likewise, it is alternatively better for bigger projects, the reason I referred it on here was for its practice for the future.

    Let's take an ability for example, infinite drown. This ability gives you full bubbles when you are underwater. Referring this to the cobweb situation, replace the webs with water. If you were to idle in the water and you were to use a player move event to check... well the guy would drown as they are not moving. That's why I create schedulers for idling checks / abilities. Also, creating a scheduler that triggers every 1 second is obviously better than creating a move event that triggers 20 times a second.

    Please don't call my ideas 'outright stupid' if you are not fulfilled by the topic. That is extremely rude.
     
  10. Offline

    fireblast709

    stormneo7 seems legit, players existing in unloaded chunks. If you experience glitches entities, you probably have other issues to look at first xP. I can pretty much guarantee you (by reading the cb source) that neither the block nor the type will be null under any circumstance (in fact I even have my doubts about null for unloaded chunks - it seems to give a non null block but I can be wrong)
    TwistPvP if you actually knew how the scheduler worked, you wouldn't have posted that. Async tasks create 'new' threads from a Thread pool, and sync task don't even use any thread aside the main thread.
     
  11. Offline

    TwistPvP

    fireblast709 Sorry but I program mostly java not bukkit so I am no expert. Appreciate it if you don't act like a know-it-all, because as everyone can quite clearly see you have made mistakes too.
     
  12. Offline

    fireblast709

    TwistPvP I don't act like a know-it-all, I simply share whatever I know. If you feel like I made mistakes somewhere, feel free to tahg me and correct me
     
  13. Offline

    stormneo7

    So why the hell would you call my idea stupid if you've no god dam experience?
    You don't say pizza's awful if you haven't ate it before.

    Everything you said in this thread were incorrect.
    You said my null checks were incorrect when they weren't.
    You said my boolean check isn't reliable when it makes coding repeated things simpler.
    You said that creating a scheduler makes multiple threads when it only makes one.

    Learn Bukkit before correcting people. That's something I'd appreciate.
     
  14. Offline

    LugnutsK

    If you are checking only once in all of your code, there is no reason to create a new method just for it. However, if you do need to know in more than one place, it makes a lot of sense to have a method.

    Also, change
    Code:java
    1. public static boolean inWebs(Player p) {
    2. if((p.getLocation().getBlock() != null && p.getLocation().getBlock().getType() == Material.WEB)
    3. || (p.getLocation().getRelative(BlockFace.UP).getBlock() != null && p.getLocation().getBlock().getRelative(BlockFace.UP).getType() == Material.WEB))
    4. return true;
    5. return false;
    6. }

    Code:java
    1. public static boolean inWebs(Player p) {
    2. return (p.getLocation().getBlock() != null && p.getLocation().getBlock().getType() == Material.WEB)
    3. || (p.getLocation().getRelative(BlockFace.UP).getBlock() != null && p.getLocation().getBlock().getRelative(BlockFace.UP).getType() == Material.WEB);
    4. }
     
  15. Offline

    ZodiacTheories

     
    The Fancy Whale likes this.
Thread Status:
Not open for further replies.

Share This Page