[Solved]How to separate Minecart/Storage Cart/Powered Cart?

Discussion in 'Plugin Development' started by recon88, Feb 23, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    recon88

    Ok, i finally manged it to protect all kinds of Minecarts.
    It works fine but there's one little Problem.

    PHP:
    if (!(event.getRightClicked() instanceof StorageMinecart)) {
    Cancel Event 
    +
    Message 1
    PHP:
    if (!(event.getRightClicked() instanceof PoweredMinecart)) {
    Cancel Event 
    +
    Message 2
    PHP:
    if (!(event.getRightClicked() instanceof Minecart)) {
    Cancel Event 
    +
    Message 3
    All 3 got a message which appears when some1 rightclicks the entity if it is protected.

    The Problem:

    Minecart protection = on
    PoweredMinecart protection = on
    =
    Rightclick on PoweredMinecart = Message 2 AND Message 3 appears because PoweredMinecart is a child of Minecart.

    How to fix that?
    .
     
  2. Use else if() ?
     
  3. Offline

    recon88

    I don't get it.. It's all wrong what I'm trying.

    Well, that are the 2 Sections (Minecart + PoweredMinecart)


    PHP:
        @EventHandler
        
    public void onPlayerInteractEntityEvent2(PlayerInteractEntityEvent event){
            if (!(
    event.getRightClicked() instanceof PoweredMinecart)) {
                return;
                }
                        
    Player player event.getPlayer();
                        
    OwnedChunk newchunk = new     OwnedChunk(event.getRightClicked().getLocation().getBlock().getChunk());
                        
    String claimcheck plugin.RegionHandler.ClaimCheck(newchunk);
                     if (
    claimcheck != ""){
     
                            if (new 
    ConfigChunk(newchunk).store==1){  // store==1  = Protection ON //
                                
    if (plugin.RegionHandler.CanBuildHere(playernewchunk) == false){
                                    
    event.setCancelled(true);
                                    
    player.sendMessage(ChatColor.RED "Powered Minecart owned by " claimcheck ". You can't use this.");
                                }
                            }
                        }
       }
        
        @
    EventHandler
        
    public void onPlayerInteractEntityEvent3(PlayerInteractEntityEvent event){
            if (!(
    event.getRightClicked() instanceof Minecart)) {
                return;
                }
                        
    Player player event.getPlayer();
                        
    OwnedChunk newchunk = new     OwnedChunk(event.getRightClicked().getLocation().getBlock().getChunk());
                        
    String claimcheck plugin.RegionHandler.ClaimCheck(newchunk);
                     if (
    claimcheck != ""){
     
                            if (new 
    ConfigChunk(newchunk).cartdrive==1){ // cartdrive==1 = Protection ON //
                                
    if (plugin.RegionHandler.CanBuildHere(playernewchunk) == false){
                                    
    event.setCancelled(true);
                                    
    player.sendMessage(ChatColor.RED "Minecart owned by " claimcheck ". You can't drive with it.");
                                }
                            }
                        }
       }
     
  4. Offline

    Sir Savary

    recon88
    Hrm, interesting problem. Unless someone else helps you first, you'll have to wait until tomorrow for me to help you with this as I worked the grave yard shift yard tonight. If you need this done RIGHT now, just comment out the message under powered minecart for the time being(As the normal minecart message is sent to both powered minecart and normal minecart).
     
  5. Offline

    Njol

    Put everything into one function, it's more efficient, cleaner and easier to maintain:
    Code:java
    1.  
    2. @EventHandler
    3. public void onPlayerInteractEntityEvent(PlayerInteractEntityEvent event){
    4. if (!(event.getRightClicked() instanceof Minecart))
    5. return; // not a minecart at all
    6.  
    7. Player player = event.getPlayer();
    8. OwnedChunk newchunk = new OwnedChunk(event.getRightClicked().getLocation().getBlock().getChunk());
    9. String claimcheck = plugin.RegionHandler.ClaimCheck(newchunk);
    10. if (!claimcheck.isEmpty()){// you cannot compare strings with '==' or '!='
    11. if (new ConfigChunk(newchunk).store==1){ // store==1 = Protection ON //
    12. if (plugin.RegionHandler.CanBuildHere(player, newchunk) == false){
    13. event.setCancelled(true);
    14. if (event.getRightClicked() instanceof PoweredMinecart) {
    15. player.sendMessage(ChatColor.RED + "Powered Minecart owned by " + claimcheck + ". You can't use this.");
    16. } else if (event.getRightClicked() instanceof StorageMinecart) {
    17. player.sendMessage(ChatColor.RED + "Storage Minecart owned by " + claimcheck + ". You can't use this.");
    18. } else if (event.getRightClicked() instanceof Minecart) {
    19. player.sendMessage(ChatColor.RED + "Minecart owned by " + claimcheck + ". You can't drive with it.");
    20. }
    21. }
    22. }
    23. }
    24. }
     
  6. Offline

    recon88

    Uhm but i wanna separate the normal Minecart from Storage and Powered.
    They got 2 commands like "/prot cartdrive" and "/prot store"

    PHP:
    if (new ConfigChunk(newchunk).store==1){ // store==1 = Protection ON //
     
    if (new ConfigChunk(newchunk).cartdrive=1){// cartdrive==1 = Protection ON //
     
  7. Offline

    Shamebot

    Code:
    if(entity instanceof Minecart && !(entity instanceof PoweredMinecart || entity instanceof StorageMinecart))
     
  8. Offline

    recon88

    Never thought about that solution.. Thanks that helped =)
    Finally got it fixed
     
Thread Status:
Not open for further replies.

Share This Page