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

Creating classes, but how?

Discussion in 'Development' started by Daniel Pereira, Mar 7, 2018.

  1. Daniel Pereira

    Daniel Pereira Silverfish

    Messages:
    22
    GitHub:
    Undentified
    Now before you think I'm talking in classes on the programming side I'm not or atleast for now I'm not!

    You see, all these little things that I created in my small new plugins thanks to all of your support I can at least always understand on where to start and thanks to documentation I can build a code for you all to review it if I need.

    BUT. I can't figure out how to create things in pocketmine plugin development.

    I want to create classes for the game I'm creating.
    Classes like these:
    - Serf / Description: Can get resources or make food but can't use armor and almost no weapons.
    - Footman / Description: Can use good armors. Can't use bow&arrow nor Diamond Armor.
    - Sergeant / Description: Can use all armors. Can't use bow&arrow.
    - Archer / Description: Can't use Iron nor Diamond armor. Can't use Diamond Sword. Can use bow&arrow.
    - Doctor / Description: Can't use Iron nor Diamond armor. Can't use Diamond Sword. Can't use bow&arrow. Can heal others with a special item!

    These classes will be trained in settlements where there will be signs in a specific room / house (training room).

    How would create this and give it the properties ? Anywhere a novice like can me see something to help? Thank you all !!
     
    Muqsit likes this.
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    When you say a class can't use item X, do you also mean they can't drop the item, can't use it as a weapon and can't use it for mining a block?
     
    Daniel Pereira and LewBr like this.
  3. Daniel Pereira

    Daniel Pereira Silverfish

    Messages:
    22
    GitHub:
    Undentified
    They can drop the item. But when they put it in their hand an X item they can't use, It drops on the floor for example and that way makes them unable to do anything to the item except carry it on their inventory when they pick it up.
     
  4. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    Why you don't just block the player to pick it up the item on the floor?
    That's better and uncomplicated, better than blocking the player from using such an item, I do not think this would be possible, just with an interaction in PlayerInteractEvent, but like you said, do you intend to block the player from hitting the player with a sword? or what?
     
  5. Daniel Pereira

    Daniel Pereira Silverfish

    Messages:
    22
    GitHub:
    Undentified
    No. I want them to be able to carry the items but not to use them. Say that a footman grabs a pickaxe and well, his companion serf needs it to go mining. Footman can't use it but he can pick it up and give it to his Serf lad! Anyways thanks for the response :)
     
    Thunder33345 likes this.
  6. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    How did you stored those ranks? by mysql database, sql or YML file? You can cancel an event to a player that has the rank for example:
    PHP:
      
        
    public function onInteract(\pocketmine\event\player\PlayerInteractEvent $event){
            
    //cool example, *I did everything in the rush.* sry if has something wrong :D
          
            
    $player $event->getPlayer();
            
    $item $player->getInventory()->getItemInHand();
            
    //ID of your pickaxe or sword
            
    $id "276";
            
    //damage of item
            
    $damage "0";
                
    //you should create a function to get the player rank stored in your YML file or database.. whatever
            
    if($this->getrank($player) == "Footman" and $item->getId() == $id and $item->getDamage() == $damage){
              
                
    $event->setCancelled();
              
            }
        }
     
    Last edited: Mar 7, 2018
  7. Daniel Pereira

    Daniel Pereira Silverfish

    Messages:
    22
    GitHub:
    Undentified
    I didn't stored them! I'm just trying to learn how will I make this in the most simple way :) And thanks for the code since it's really simple to understand!

    But as you say, I need to store the ranks. I think I will go for YML. So I create a resource file and a yml file with the ranks? How would I ? @LewBr , thanks for the help aswell!
     
  8. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    first thing first is the "class" permanent or temp as say for this match
    i will assume it's temp per match
    assume this simplified example
    class:
    attacker: can attack, cant mine, can place block
    miner: can mine, cant attack, can place block

    so you can create a class storage to remember who is in what class assume:
    ClassManager::setClass(Player,Class Const)
    ClassManager::getClass(Player):Class Const

    you will need the actual thing that impose limits and block events,
    lets call it "BaseClass" as our miner and attacker will extend it...
    BaseClass::onAdd(Player)
    //triggered by classmanager when player is added, useful to inflict effects(say miner get resistance) or show message
    assume same for onRemove
    BaseClass::__construct(Mixed)
    //assume mixed to be the things base class will need
    //this also trigger register events our subclass will listen for...
    BaseClass::filterPlayer(Player):bool
    //assume this will be a shorthand to check if player is in THIS team

    now for the miner and attacker class, you can listen events here and block them if they matched said filter
    MinerClass::AttackEvent(PlayerAttackEvent)
    //assume event is valid...
    //you would check if(!$this->filter($event->player))return; else $event->cancel();
    //which just means if player is not miner return, else cancel

    now for attacker class which cant mine
    AttackerClass::MineEvent(PlayerMineEvent)
    //assume event is valid
    //you would check the same thing as in miner class
    //but this time it means if player is not attacker return, else cancel

    all of this code here is justoncept to get you started, please do not actually try to use anything here by copy pasting without understanding what it even do
    anything here may not be factually correct in any way
     
    Teamblocket and LewBr like this.
  9. di2134876

    di2134876 Spider Jockey

    Messages:
    29
    GitHub:
    dk1234987
    He just wants the item to be dropped when a player with the rank not equal to the one the item wants holds
     
  10. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    from what i can tell OP just want to stop usages of X item IF their rank dosent allow it
    in nowhere op said it to be dropped and op even specify op wanted ability to CARRY items cant be used

    i think taking an OOP approach is better then clustering tons of "if class = x else if class = y" on event listeners
    while its true it might be easy to do that but it will be crumble some to maintain over long term or to add new things as you make change your game

    and to return to the main point, my post is an SIMPLIFIED example which OP is free to shape it to any form op wished without cluttering my example and begin confusing to OP
     
  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.