Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

collapse

Author Topic: How to tell if a player stepped on a pressure plate?  (Read 82 times)

Fredashay

  • Plug-in Maker
  • Sr. Member

  • Offline
  • ****

  • 262
  • Reputation:
    +26/-1
  • Personal Text
    "Tira mah! This graw is fredashay!"
    • View Profile
    • ClockPie
How to tell if a player stepped on a pressure plate?
« on: August 05, 2017, 03:21:22 PM »
Can I just do something like:

Code: [Select]
public void onAttemptPlayerMove(MC_Player plr, MC_Location locFrom, MC_Location locTo, MC_EventInfo ei) {
int plrD = plr.getLocation().dimension;
int plrX = (int) plr.getLocation().x;
int plrY = (int) plr.getLocation().y;
int plrZ = (int) plr.getLocation().z;
if (myServer.getWorld(plrD).getBlockAt(plrX, plrY - 1, plrZ).getId() == MC_BlockType.HEAVY_WEIGHTED_PRESSURE_PLATE) {
    plr.sendMessage("<Subway> You stepped on a pressure plate. ");
    }

Or is a pressure plate an entity for which I need to use
Code: [Select]
onEntiryInteract()?

JD9999

  • Plug-in Maker
  • Hero Member

  • Offline
  • ****

  • 746
  • Reputation:
    +45/-0
    • View Profile
Re: How to tell if a player stepped on a pressure plate?
« Reply #1 on: August 05, 2017, 05:12:02 PM »
The first one should mostly work. I don't think you need the -1 for the y as the pressure plate counts as being in the same block the player's foot is on, but I could be wrong.

I would also be careful with converting it to an int. If a player has an x value of 59.993 and is partly standing on the pressure plate, the plugin will think it is not (as converting a double to an int will always truncate its decimal).

I'm not sure what exactly the value is when this happens, and whether you will have to do direction-related stuff, but I'm thinking something like below:

NOTE: This has not been tested as is suspect to screwing things up
Code: [Select]
public void onAttemptPlayerMove(MC_Player plr, MC_Location locFrom, MC_Location locTo, MC_EventInfo ei) {
int plrD = locTo.dimension;
double x = locTo.x;
int plrX = mostLikelyValue(x, plr);
double y = locTo.y;
int plrY = mostLikelyValue(y, plr);
double z = locTo.z;
int plrZ = mostLikelyValue(z, plr);
if (myServer.getWorld(plrD).getBlockAt(plrX, plrY, plrZ).getId() == MC_BlockType.HEAVY_WEIGHTED_PRESSURE_PLATE) {
    plr.sendMessage("<Subway> You stepped on a pressure plate. ");
    }
}

private int mostLikelyValue(double value, MC_Player plr){
    if(plr.getDirection() == MC_DirectionNESWUD.NORTH || plr.getDirection() == MC_DirectionNESWUD.SOUTH){
        int mostLikely = (value - (double)(int)value) > 0.8 ? (int)Math.round(value) : (int)value;
        return mostLikely;
    }else return (int)value;
}

I doubt the second one will work as there is no entity for a weighted pressure plate (or a pressure plate at all!)

It might be useful for you to look at my Transporter plugin and see what I've done there.
« Last Edit: August 05, 2017, 10:31:47 PM by JD9999 »

Fredashay

  • Plug-in Maker
  • Sr. Member

  • Offline
  • ****

  • 262
  • Reputation:
    +26/-1
  • Personal Text
    "Tira mah! This graw is fredashay!"
    • View Profile
    • ClockPie
Re: How to tell if a player stepped on a pressure plate?
« Reply #2 on: August 05, 2017, 09:35:29 PM »
Thanks!  It turns out that my problem is what you suspected: the fractions contained in plr.location weren't being resolved to the correct integer values for the actual block the player is standing on.  Grrr!  I experimented with floor, ceil, round, etc., and discovered that
Code: [Select]
int plrX = (int) Math.round(plr.getLocation().x;
etc.
etc.
gives the correct block coords that the player is standing on.  It seems to work correctly for positive and negative X and Z values.

P.S.  I had no idea there was a mostLikelyValue() function...

My next problem is doing all this math every time a player moves really lags my server :-(  So I think the whole plugin is doa (I want to give my players a subway map every time they pass through a turnstyle to enter my minecart subway, lol)....

JD9999

  • Plug-in Maker
  • Hero Member

  • Offline
  • ****

  • 746
  • Reputation:
    +45/-0
    • View Profile
Re: How to tell if a player stepped on a pressure plate?
« Reply #3 on: August 05, 2017, 10:31:25 PM »
Sorry, there isn't a mostLikelyValue function. I meant to call my private method mostLikelyValue but instead called it mostLikelyInt. I've changed it now.

To fix your issue, you could load the locations of all weighted plates into memory on startup. For example:

NOTE: This also hasn't been tested, and may not work as expected
Code: [Select]
private List<MC_Location> locations = new ArrayList<>(); //Can be protected or public if required.

public void onServerFullyLoaded(){
    MC_Server server = RainbowUtils.getServer();
    server.broadcastMessage("Enabling Subway plugin. Please wait...");
    MC_World overworld = server.getWorld(0); //If this is for other worlds, you can use MC_Server.getWorlds()
    for(MC_Chunk chk : overworld.getAllLoadedChunks()){
        int lowX = chk.getCX << 4; //I don't know what this does, but it works
        int lowZ = chk.getCZ << 4;
        for(int incrementX = 0; incrementX < 16; incrementX++){
            for(int incrementZ = 0; incrementZ < 16; incrementZ++){
                for(int y = 0; y <= server.getMaxBuildHeight(); y++){
                    int x = lowX + incrementX;
                    int z = lowZ + incrementZ;
                    MC_Location loc = new MC_Location(x, y, z, 0);
                    MC_Block possBlock = overworld.getBlockAt(loc);
                    if(possBlock.getID() == MC_BlockType.HEAVY_WEIGHTED_PRESSURE_PLATE) location.add(loc);
                }
            }
        }
    }
    server.broadcastMessage("Subway plugin enabled!");
}

public void onAttemptPlayerMove(MC_Player plr, MC_Location locFrom, MC_Location locTo, MC_EventInfo ei){
    for(MC_Location loc : locations){
        if(loc.isSameBlockLocationAs(locTo) plr.sendMessage("<Subway> You stepped on a pressure plate.");
    }
}

CodeCrafter47

  • Administrator
  • Hero Member

  • Offline
  • *****

  • 511
  • Reputation:
    +128/-2
    • View Profile
Re: How to tell if a player stepped on a pressure plate?
« Reply #4 on: August 06, 2017, 03:18:26 AM »
Instead of rounding or casting it might be better to use
Code: [Select]
int x = plr.getLocation().getBlockX();
int y = plr.getLocation().getBlockY();
int z = plr.getLocation().getBlockZ();

plr.getLocation() will give you the location of the player (of his feet, horizontally centered). In the case of a pressure plate the above will give you the coordinates of the pressure plate (as with pressure plates the player literally stands inside them).

Instead of using onAttemptPlayerMove I'd recommend using onTick to check for every player whether he's standing on a pressure plate. Checking the block every player stand on once every tick shouldn't make your server lag.

Fredashay

  • Plug-in Maker
  • Sr. Member

  • Offline
  • ****

  • 262
  • Reputation:
    +26/-1
  • Personal Text
    "Tira mah! This graw is fredashay!"
    • View Profile
    • ClockPie
Re: How to tell if a player stepped on a pressure plate?
« Reply #5 on: August 06, 2017, 09:01:25 AM »
Thanks!  I'll try that.  But there's more to it than just a single pressure plate -- lots of players use pressure plates for various reasons that aren't all subway turnstyles, lol.  There's a certain configuration of specific blocks surrounding the player that the plugin will recognize as a "turnstyle" and gives the player a subway map. 

Maybe I'll use your idea of just hard-coding all the x-y-z locations of all the "turnstyles" in the plugin, but that'll be a pain to check them all.  Over the years, my server has an grown an extensive "subway" system with a great many stations...

Fredashay

  • Plug-in Maker
  • Sr. Member

  • Offline
  • ****

  • 262
  • Reputation:
    +26/-1
  • Personal Text
    "Tira mah! This graw is fredashay!"
    • View Profile
    • ClockPie
Re: How to tell if a player stepped on a pressure plate?
« Reply #6 on: August 14, 2017, 05:43:01 PM »
FYI, follow up.... the plugin works great now! :-)  You get a subway map whenever you pass through a turnstyle if you don't already have one in your inventory!

 

* Notifications

* Member Info

 
 
Welcome, Guest. Please login or register.
Did you miss your activation email?

* Recent Topics

* Forum Stats

  • stats Total Members: 3571
  • stats Total Posts: 12286
  • stats Total Topics: 1659
  • stats Total Categories: 5
  • stats Total Boards: 16
  • stats Most Online: 568