Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Killing in Creative Alert

Comments in 'Plugin Development' started by Jag, May 21, 2014.

  1. Jag
    Offline

    Jag Active Member

    Joined:
    Mar 26, 2014
    Posts:
    53
    Minecraft User:
    Jag
    Hi,
    I was wondering if anyone could point out a fix for this potential plugin that alerts players when someone is killing in creative (and changes them to survival).
    PHP:
    class StopCreativeKilling implements Plugin{
        private 
    $api;
        public function 
    __construct(ServerAPI $api$server false){
            
    $this->api $api;
            
    $this->server ServerAPI::request();}
        public function 
    init(){
            
    $this->api->addHandler("player.death", array($this"handler"), 5);    }
        public function 
    __destruct(){}
        public function 
    eventHandler($data$event){
            switch(
    $event){
               case 
    "player.death":
                    
    $player $this->api->entity->get($data["cause"]);
                    if(
    $player->Gamemode === CREATIVE){
                        
    $broadcast "$player is in creative mode.\n";
                        
    $this->api->chat->broadcast($broadcast);
                       
    $this -> api -> console -> run("gamemode 0 $player");     }
            break;}
        }      
        }    

    For the record, every single line below is from another plugin (even if its just an online plugin generator) or the pocketmine files on the server, and I am happy to list sources (or multiple sources) for each line item. I am not a coder, but also not looking to claim that I am.

    So for example, the if($player->Gamemode === CREATIVE){ is from KsyMC's Essentials plugin.
    However, that is the only line borrowed from that plugin. It may not be the right code for what I am trying to do, but it looks like it logically should be.
    RuinPVP and InfoEssentials have also been helpful. I want to be upfront about that because I don't know what is considered ok for borrowing or imitating when coding vs. being accused of stealing.
    Jag
    Last edited by a moderator: May 21, 2014
  2. Lambo
    Offline

    Lambo Notable Member Plugin Developer

    Joined:
    Sep 14, 2013
    Posts:
    431
    Plugins:
    4
    Minecraft User:
    Lambo
    Does it not work?
  3. Jag
    Offline

    Jag Active Member

    Joined:
    Mar 26, 2014
    Posts:
    53
    Minecraft User:
    Jag
    No it doesn't. It seems to be something with the If function, as it broadcasts $player is in creative mode, whether they are in it or not.
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Function doesn't match callback
  5. Lambo
    Offline

    Lambo Notable Member Plugin Developer

    Joined:
    Sep 14, 2013
    Posts:
    431
    Plugins:
    4
    Minecraft User:
    Lambo
    Based on https://github.com/PocketMine/Pocke...cf82cdfb292f02dc1ee7fe3f26b7a3/src/Player.php at line 1030, something like this should work:

    PHP:
    <?php

    /*
    __PocketMine Plugin__
    name=NoKillCreative
    version=1.0
    description=No killing in creative.
    author=Lambo
    class=nokillc
    apiversion=12
    */

    class nokillc implements Plugin{
            private 
    $api;

            public function 
    __construct(ServerAPI $api$server false){
                
    $this->api $api;
            }

            public function 
    __destruct(){}

            public function 
    init(){
                
    $this->api->addHandler("player.interact", array($this,"onHit"));
            }

            public function 
    onHit($d){
               if(
    $d["entity"]->player->getGamemode()=="creative" and $d["targetentity"]->player->getGamemode()=="survival"){
                  
    $d["entity"]->player->setGamemode(0);
                  
    $d["entity"]->player->sendChat("You cannot kill in creative!");
                  return 
    false;
            }
    }
    I'm not quite sure if this would work, and if it doesn't, PM me and I will try to fix it.
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
  7. Lambo
    Offline

    Lambo Notable Member Plugin Developer

    Joined:
    Sep 14, 2013
    Posts:
    431
    Plugins:
    4
    Minecraft User:
    Lambo
    Yeah I was talking about the getGamemode function :p
  8. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    oh :p
  9. Jag
    Offline

    Jag Active Member

    Joined:
    Mar 26, 2014
    Posts:
    53
    Minecraft User:
    Jag
    I tested is a couple of times and it worked great after I added another } at the end. The only thing that would be better is if instead of send chat to send the message, it was broadcast to all. Because the game mode switch logs the offending player out, you don't have time to see the message. At least if it was broadcast they could find out why their game mode was switched when they returned.

    Thanks a lot for putting this together, as it will help me with not only with this, but also I would like to use similar code to create a map that prevents creative players from building on it. The idea being a server that has a creative and a survival section, with protections in place to prevent players using creative in the survival sections (even if they are teleported there).
    Of course with the API update coming soon, it won't work for very long.
    Jag
  10. Lambo
    Offline

    Lambo Notable Member Plugin Developer

    Joined:
    Sep 14, 2013
    Posts:
    431
    Plugins:
    4
    Minecraft User:
    Lambo
    I don't really understand the first paragraph: you want it to broadcast in the chat that you cannot kill in creative?

    And about the other plugins, PM me and I will see what I can do.

Share This Page

Advertisement