from pyspades.server import *                                         # i dont care 
from pyspades.collision import vector_collision, distance_3d_vector   # if i realy 
from commands import name, add, admin, alias                          # need them
import commands                                                       # all

@admin
@name('spaderange')
def set_spaderange(self, value):
    self.protocol.spaderange = int(value)
    message = 'The spade has got a range of %s blocks now!' % (value)
    self.protocol.send_chat(message) 
    return message
commands.add(set_spaderange)

@admin                                                              # i would realy like to explain here
@name('tolerance')                                                  # what the hit tolerance is but due
def set_tolerance(self, value):                                     # to my crappy english (thank you
    self.protocol.tolerance = int(value)                            # school you realy helped me out),
    return 'Done, the hit tolerance has been set to %s.' % (value)  # i am not able to find the right
commands.add(set_tolerance)                                         # words neccessary for doing so


def apply_script(protocol, connection, config):

    class spaderangeprotocol(protocol):

        spaderange = 3 # default range of your spade
        tolerance = 3 # uh i dont know if this is the default value for the spades hit tolerance
    
    class spaderangeconnection(connection):

        def on_shoot_set(self, fire):

            if self.tool == SPADE_TOOL: # huehuehue i think i will script a block_kill soon

                for kitten in self.protocol.players.values(): # here we go again with my cats <3

                    if distance_3d_vector(self.world_object.position, kitten.world_object.position) <= self.protocol.spaderange: # man you can do pretty much with this vertex3 thingy

                        if self.world_object.validate_hit(kitten.world_object, MELEE, self.protocol.tolerance): # pretty fancy function 
                        # if vector_collision(self.world_object.position, kitten.world_object.position, 100) and self != kitten: kitten.hit(100, self, MELEE_KILL) # lol that would be kinda OP

                            kitten.hit(config.get('melee_damage'), self, MELEE_KILL) # poor kitten :<

    return spaderangeprotocol, spaderangeconnection