Check buttons and boost

Please post any questions about developing your plugin here. Please use the search function before posting!
User avatar
JoJe3003
Junior Member
Posts: 4
Joined: Sun Jul 31, 2016 7:20 pm

Check buttons and boost

Postby JoJe3003 » Sat Feb 18, 2017 10:22 pm

Hey guys! As I'm new to sourcepython, and very eager to learn, you will probably hear alot from me on this forum :cool:.

My question probably has a very simple answer. I'm trying to make a double jump plugin, and I'm stuck at two things.

First beeing, how can i check if the player is holding down the space key?

Second beeing, how do I go about boosting a player upwards?

This is what i have so far, if that helps getting the bigger picture.

Syntax: Select all

from events import Event
from listeners import OnPlayerRunCommand
from players.helpers import index_from_userid
from players.constants import PlayerStates, PlayerButtons
from messages import SayText2

settings = {}
dj_commands = ["!doublejump", "!dj"]

@OnPlayerRunCommand
def PlayerRunCommand(player, cmd):
userid = player.userid

if not userid in settings:
settings[userid] = {'djump':0, 'jumpdone':0, 'jumpdonetick':0}

if player.dead:
return

if player.flags & PlayerStates.ONGROUND:
settings[userid]['jumpdonetick'] = 0
settings[userid]['jumpdone'] = 0
return

#if settings[userid]['jumpdone'] == 1:
# return

#JUMPING FUNCTION
SayText2('doublejump!' + str(settings[userid]['jumpdonetick']) + " " + str(settings[userid]['jumpdone'])).send()
settings[userid]['jumpdonetick'] += 1


settings[userid]['jumpdone'] = 1


@Event('player_say')
def on_player_say(event):
text = event['text']
userid = event['userid']
index = index_from_userid(userid)

# Makes sure userid is a string
str_userid = str(userid)

if text in dj_commands:
if not settings[str_userid]['jump']:
SayText2('\x05Doublejump: \x01ON').send(index)
settings[str_userid]['djump'] = 1
else:
SayText2('\x05Doublejump: \x01OFF').send(index)
settings[str_userid]['djump'] = 0


Thanks!
Last edited by Ayuto on Sat Feb 18, 2017 10:35 pm, edited 1 time in total.
Reason: code -> python
User avatar
Ayuto
Project Leader
Posts: 1543
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: Check buttons and boost

Postby Ayuto » Sat Feb 18, 2017 10:46 pm

To check whether the player is holding down the jump button, you can use this snippet:

Syntax: Select all

if cmd.buttons & PlayerButtons.JUMP:
# Player holds down +jump


To push a player, you can use Player.push:

Syntax: Select all

# Push the player upwards
player.push(0, 300, True)


Edit a small Python hint:

Instead of doing this:

Syntax: Select all

SayText2('doublejump!' + str(settings[userid]['jumpdonetick']) + " " + str(settings[userid]['jumpdone'])).send()
I would use format(). It's much easier to understand what's going on. :tongue:

Syntax: Select all

SayText2('doublejump! {} {}'.format(
settings[userid]['jumpdonetick'],
settings[userid]['jumpdone'])
).send()
User avatar
JoJe3003
Junior Member
Posts: 4
Joined: Sun Jul 31, 2016 7:20 pm

Re: Check buttons and boost

Postby JoJe3003 » Sun Feb 19, 2017 12:53 am

Thanks! Just finished the double jump plugin. There is just one problem.

Previously my friend Velocity and I made a bhop plugin. Whenever i turn that bhop plugin on, the double jump doesnt work.

Bhop plugin:

Syntax: Select all

from events import Event
from listeners import OnPlayerRunCommand
from players.helpers import index_from_userid
from players.constants import PlayerStates, PlayerButtons
from entities.constants import MoveType
from colors import Color
from messages import SayText2


# Settings for the player
bsettings = {}
bhop_commands = ["!autobhop",
"!bhop",
"!autohop",
"!autojump"]


@OnPlayerRunCommand
def PlayerRunCommand(player, cmd):

# Finds the userid (converts it to string)
userid = str(player.userid)

if not userid in bsettings:
bsettings[userid] = {'jump':0}

if player.dead:
return

if player.move_type & MoveType.LADDER:
return

if player.get_property_uchar('m_nWaterLevel') > 1:
return

if player.flags & PlayerStates.ONGROUND:
return

if bsettings[userid]['jump']:
cmd.buttons &= ~PlayerButtons.JUMP


@Event('player_say')
def on_player_say(event):
text = event['text']
userid = event['userid']
index = index_from_userid(userid)

# Makes sure userid is a string
str_userid = str(userid)

if text in bhop_commands:
if not bsettings[str_userid]['jump']:
SayText2('\x05Autojump: \x01ON').send(index)
bsettings[str_userid]['jump'] = 1
else:
SayText2('\x05Autojump: \x01OFF').send(index)
bsettings[str_userid]['jump'] = 0


Double jump plugin:

Syntax: Select all

from events import Event
from listeners import OnPlayerRunCommand
from players.helpers import index_from_userid
from players.constants import PlayerStates, PlayerButtons
from messages import SayText2

@OnPlayerRunCommand
def PlayerRunCommand(player, cmd):
userid = player.userid

if not userid in settings:
settings[userid] = {'djump':0, 'jumpdone':0, 'jumpdonetick':0}

if player.dead:
return

if player.flags & PlayerStates.ONGROUND:
settings[userid]['jumpdonetick'] = 0
settings[userid]['jumpdone'] = 0
return

if not player.flags & PlayerStates.ONGROUND:
settings[userid]['jumpdonetick'] += 1
SayText2(str(settings[userid]['jumpdonetick']))

if settings[userid]['jumpdonetick'] < 20:
return

if settings[userid]['jumpdone'] == 1:
return

#JUMPING FUNCTION
if cmd.buttons & PlayerButtons.JUMP:
player.push(0, 300, True)
settings[userid]['jumpdone'] = 1


Both works perfectly fine on their own, but for some reason they dont work together.

Thanks in advance!

(Also, how would i go about if i want to update a plugin post? I have updated my autohop plugin a little, and im unsure if I'm supposed to edit the post and reupload it, or just comment the new one in a comment below the plugin)
Last edited by Ayuto on Sun Feb 19, 2017 1:33 pm, edited 1 time in total.
Reason: code -> python
User avatar
Ayuto
Project Leader
Posts: 1543
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: Check buttons and boost

Postby Ayuto » Sun Feb 19, 2017 1:42 pm

The problem is that the bhop plugin removes the PlayerButtons.JUMP flag, so cmd.buttons & PlayerButtons.JUMP will never evaluate to True.

If you want to update your plugin, just update the main post and (if you want) add a new reply to the thread to notify that your plugin has been updated.

Also, please use syntax highlighting when posting code. That makes it much easier to read. :smile:

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 2 guests