1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Event Function

Discussion in 'Development' started by ZackyVN, Aug 21, 2018.

  1. ZackyVN

    ZackyVN Baby Zombie

    Messages:
    150
    Help me i try $this->IronFarm($sender); but it didnt work!
    <?php
    namespace ZackyVNTM;
    use pocketmine\plugin\PluginBase;
    use pocketmine\utils\Config;
    use pocketmine\command\Command;
    use pocketmine\command\CommandSender;
    use pocketmine\command\ConsoleCommandSender;
    use pocketmine\math\Vector3;
    use pocketmine\Player;
    use pocketmine\event\block\BlockUpdateEvent;
    use pocketmine\item\Item;
    use pocketmine\event\Listener;
    use pocketmine\level\Level;
    use pocketmine\block\Block;
    use pocketmine\block\DiamondOre;
    use pocketmine\block\EmeraldOre;
    use pocketmine\block\Stone;
    use pocketmine\block\Water;
    use pocketmine\block\Fence;
    class Main extends PluginBase implements Listener{
    public function onEnable () : void{
    $this->getServer()->getLogger()->info("§l[§aUpgrade§r§l]§a Đã bật!");
    $this->getServer()->getPluginManager()->registerEvents($this, $this);
    }
    public function onCommand(CommandSender $sender, Command $cmd, string $label, array $args) : bool{
    if(!$sender instanceof Player){
    $sender->sendMessage("§l[§aUpgrade§r§l]§r§c Vào máy chủ để chơi :D");
    return false;
    }
    if($cmd == "upgrade"){
    if(empty($args[0])){
    $sender->sendMessage ("§c->§e Farms§a Upgrade§c <-");
    $sender->sendMessage ("§lIron - 10 Coins");
    return true;
    }
    if(!empty($args[0])){
    switch($args[0]){
    case "iron":
    $sender->sendMessage("§l[§aUpgrade§r§l]§a Bạn đã mua thành công Iron farms!");
    $this->IronFarm($sender);
    break;
    default:
    $sender->sendMessage ("§c->§e Farms§a Upgrade§c <-");
    $sender->sendMessage ("§lIron - 10 Coins");
    break;
    }
    }
    }
    return true;
    }

    public function IronFarm(BlockUpdateEvent $event){
    $block = $event->getBlock();
    $water = false;
    $fence = false;
    for($i = 2; $i <= 5; $i++) {
    $nearBlock = $block->getSide($i);
    if ($nearBlock instanceof Water) {
    $water = true;
    }elseif($nearBlock instanceof Fence) {
    $fence = true;
    }
    if ($water && $fence) {
    $id = mt_rand(1, 3);
    switch($id){
    case 50;
    $newBlock = new DiamondOre();
    break;
    case 49;
    $newBlock = new EmeraldOre();
    break;
    default:
    $newBlock = new Stone();
    }
    $ironfarm = $event->getPlayer()->getBlock()->getLevel()->setBlock($block, $newBlock, true, false);
    return;
    }
    }
    }
    }
     
  2. Flupko

    Flupko Silverfish

    Messages:
    24
    GitHub:
    Warex
    PHP:
    <?php
    namespace ZackyVNTM;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\ConsoleCommandSender;
    use 
    pocketmine\math\Vector3;
    use 
    pocketmine\Player;
    use 
    pocketmine\event\block\BlockUpdateEvent;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\level\Level;
    use 
    pocketmine\block\Block;
    use 
    pocketmine\block\DiamondOre;
    use 
    pocketmine\block\EmeraldOre;
    use 
    pocketmine\block\Stone;
    use 
    pocketmine\block\Water;
    use 
    pocketmine\block\Fence;
    class 
    Main extends PluginBase implements Listener{
    public function 
    onEnable () : void{
    $this->getServer()->getLogger()->info("§l[§aUpgrade§r§l]§a Đã bật!");
    $this->getServer()->getPluginManager()->registerEvents($this$this);
    }
    public function 
    onCommand(CommandSender $senderCommand $cmdstring $label, array $args) : bool{
    if(!
    $sender instanceof Player){
    $sender->sendMessage("§l[§aUpgrade§r§l]§r§c Vào máy chủ để chơi :D");
    return 
    false;
    }
    if(
    $cmd == "upgrade"){
    if(empty(
    $args[0])){
    $sender->sendMessage ("§c->§e Farms§a Upgrade§c <-");
    $sender->sendMessage ("§lIron - 10 Coins");
    return 
    true;
    }
    if(!empty(
    $args[0])){
    switch(
    $args[0]){
    case 
    "iron":
    $sender->sendMessage("§l[§aUpgrade§r§l]§a Bạn đã mua thành công Iron farms!");
    $this->IronFarm($sender);
    break;
    default:
    $sender->sendMessage ("§c->§e Farms§a Upgrade§c <-");
    $sender->sendMessage ("§lIron - 10 Coins");
    break;
    }
    }
    }
    return 
    true;
    }

    public function 
    IronFarm(BlockUpdateEvent $event){
    $block $event->getBlock();
    $water false;
    $fence false;
    for(
    $i 2$i <= 5$i++) {
    $nearBlock $block->getSide($i);
    if (
    $nearBlock instanceof Water) {
    $water true;
    }elseif(
    $nearBlock instanceof Fence) {
    $fence true;
    }
    if (
    $water && $fence) {
    $id mt_rand(13);
    switch(
    $id){
    case 
    50;
    $newBlock = new DiamondOre();
    break;
    case 
    49;
    $newBlock = new EmeraldOre();
    break;
    default:
    $newBlock = new Stone();
    }
    $ironfarm $event->getPlayer()->getBlock()->getLevel()->setBlock($block$newBlocktruefalse);
    return;
    }
    }
    }
    }
     
  3. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    of course this won't work, you are passing a CommandSender to a function that wants a BlockUpdateEvent.
     
    Muqsit likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.