collapse

: How to tell if a player stepped on a pressure plate?  ( 210 )

Fredashay

  • Plug-in Maker
  • Sr. Member
  • ****
  • : 269
  • Reputation: +26/-1
  • "Tira mah! This graw is fredashay!"
    • ClockPie
How to tell if a player stepped on a pressure plate?
« : August 05, 2017, 03:21:22 PM »
Can I just do something like:

:
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
:
onEntiryInteract()?



JD9999

  • Plug-in Maker
  • Hero Member
  • ****
  • : 754
  • Reputation: +45/-0
Re: How to tell if a player stepped on a pressure plate?
« #1 : 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
:
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.
« : August 05, 2017, 10:31:47 PM JD9999 »

Fredashay

  • Plug-in Maker
  • Sr. Member
  • ****
  • : 269
  • Reputation: +26/-1
  • "Tira mah! This graw is fredashay!"
    • ClockPie
Re: How to tell if a player stepped on a pressure plate?
« #2 : 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
:
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
  • ****
  • : 754
  • Reputation: +45/-0
Re: How to tell if a player stepped on a pressure plate?
« #3 : 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
:
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
  • *****
  • : 518
  • Reputation: +131/-2
Re: How to tell if a player stepped on a pressure plate?
« #4 : August 06, 2017, 03:18:26 AM »
Instead of rounding or casting it might be better to use
:
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
  • ****
  • : 269
  • Reputation: +26/-1
  • "Tira mah! This graw is fredashay!"
    • ClockPie
Re: How to tell if a player stepped on a pressure plate?
« #5 : 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
  • ****
  • : 269
  • Reputation: +26/-1
  • "Tira mah! This graw is fredashay!"
    • ClockPie
Re: How to tell if a player stepped on a pressure plate?
« #6 : 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

 
 

* Recent Topics

Re: Thoughts on Minecraft 1.13 Fredashay
[February 07, 2018, 06:24:46 PM]


Re: Thoughts on Minecraft 1.13 CodeCrafter47
[February 07, 2018, 09:29:50 AM]


Re: Thoughts on Minecraft 1.13 JD9999
[January 23, 2018, 05:38:41 AM]


Re: Thoughts on Minecraft 1.13 UnknownUser500
[January 21, 2018, 12:36:40 PM]


Re: Thoughts on Minecraft 1.13 BlackScorpion
[January 20, 2018, 04:00:58 PM]

* Forum Stats

  • stats : 3684
  • stats : 12313
  • stats : 1663
  • stats : 5
  • stats : 16
  • stats Most Online: 568
Powered by SMFPacks Alerts Pro Mod
SimplePortal 2.3.5 © 2008-2012, SimplePortal