Throwable cleavers!

Discussion in 'CommandHelper Scripts' started by MCmacjuul, Apr 11, 2015.

  1. MCmacjuul

    MCmacjuul Member

    Here is a cool throw able cleaver script I made. If you Right-Click an Iron Axe with the name: 'Cleaver «×»'
    it will turn into a deadly weapon!

    If the cleaver hits a person, he will take lots of damage! If the cleaver hits the ground, it will just disappear.


    MAIN.MS:
    PHP:

    bind(player_interact, null, null, @e) {
        if(@e['item'] == '258:0') {
            @meta = get_itemmeta(null)
            if(!is_null(@meta) && !is_null(@meta['display']) && (strip_colors(@meta['display']) == 'Cleaver')) {
                cancel()
                @src = player()
           
                # Item take mechanism
               if(pinv(player(), null)['qty'] == 1) {
                    set_pinv(player(), array('null': null))
                } else {
                    @i = pinv(player(), null)
                    @i['qty'] = @i['qty'] - 1
                    set_pinv(player(), array('null': @i))
                }
           
                # Some nice effects
               play_sound(ploc(), array('sound': 'ZOMBIE_METAL', 'pitch': 2))
           
                # Cleaver mechanics
               @loc = array(ploc()[0], ploc()[1] + 1.75, ploc()[2], ploc()[3])
                @item = drop_item(@loc, array('type': 258, 'meta': array('display': player().' cleaver')), false)
                set_entity_spec(@item, array('pickupdelay': 0))
     
                @facing = pfacing()
                @facing[0] = @facing[0] + 90
                @facing[1] = 0 - @facing[1]
             
                @x = 1.45 * cos(to_radians(@facing[0])) * cos(to_radians(@facing[1]))
                if(pfacing()[1] > 0 && pfacing()[1] < 30) {
                    @y = 0.15 + 1 * (1 * sin(to_radians(@facing[1])))
                } else if(pfacing()[1] > 30) {
                    @y = 1 * (1 * sin(to_radians(@facing[1])))
                } else {
                    @y = 0.3 + 1 * (1 * sin(to_radians(@facing[1])))
                }
                @z = 1.45 * sin(to_radians(@facing[0])) * cos(to_radians(@facing[1]))
           
                set_entity_velocity(@item, array(@x,@y,@z))
           
                #Cleaver despawn
               @evt = bind(item_pickup, null, null, @e, @src) {
                    if(is_null(@e['item']['meta'])
                    || is_null(@e['item']['meta']['display'])) {
                        die()
                    }
                    try(
                        @name = split(' ',@e['item']['meta']['display'])[0]
                        @conf = split(' ',@e['item']['meta']['display'])[1]
                   
                        if(@conf == 'cleaver') {
                            if(@name == player()) {
                                cancel()
                            } else {
                           
                                modify_event('item', null)
                                damage_entity(pinfo(player(), 13), rand(30, 40), pinfo(@src, 13))
                            }
                        }
                    )
                }
           
                @int = set_interval(100, closure(
                    if(entity_exists(@item)) {
                        if(entity_grounded(@item)) {
                            entity_remove(@item)
                            clear_task()
                            unbind(@evt)
                        }
                    } else {
                        clear_task()
                        unbind(@evt)
                    }
                ))
            }
        }
    }
     
    To quickly give yourself some cleavers:

    ALIASES.MSA:
    PHP:

    *:/cleaver = pgive_item(player(), 258, 10, array('display': colorize('&eCleaver')))
     
    Last edited: May 8, 2015
    Merge likes this.
  2. PseudoKnight

    PseudoKnight Well-Known Member

    Well thought out. Too bad the item rotation won't be correct, because it's a pretty good implementation of a throwable weapon, which are tricky. I was trying to come up with a way of using armor stands, but entity collision is tough. Same reason I use item drops for flags in CTF. One idea is to see if the x or z velocity changes, then detect players in a small radius. I'm not sure if player entities are enough to change the velocity, though. I'd have to test. The collision would be a little wonky too, given the armor stand's height.
    Last edited: Apr 11, 2015
  3. MCmacjuul

    MCmacjuul Member

    Using armor stands was my first idea too but I could not figure out the poses :p
    I think the items will do a good job for now. The collision seems to be working good althrough the 'item pickup radius' might be a little big. I tried limiting it by checking for a max distance between the player and item tho when I used entity_loc() on the item in the item_pickup event it would give me a totaly wrong location (Bug???).
  4. Jason Krause

    Jason Krause Member

    MCmacjuul, I used your code above for Ninja Stars (using Nether Stars) and instead of detecting a pickup to damage target players, I'm checking the entities_in_radius(@starlocation,1,'PLAYER') to detect collision with players. The thing, is you have to check two locations as players are 2 blocks high, so I'm checking the star's location AND the star's location y - 1. At intervals of 100ms, it detects the collision quite well. Thanks for the bulk of the code though... :)
  5. PseudoKnight

    PseudoKnight Well-Known Member

    100ms interval would probably not be enough unless the velocity was slow or the hit detection was generous.
  6. MCmacjuul

    MCmacjuul Member

    No problem :) I like it if people customize their code to how they want it. I'm currently working on some fancy new things like my new CHHolographicDisplays extension I put on GitHub yesterday and some "utility classes/procs" that you can use to easily create sidebars and interactive GUI menus, that I'll be putting on the script forums ASAP