I need help disabling Creative Inventory Opening.

Discussion in 'Plugin Development' started by VinexAx789, Mar 15, 2015.

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

    VinexAx789

    Hello, Ive been trying to code a spectating thing for my server gamemode but I'm having trouble will this code disable creative moving items to the inventory? And I also need someway to just flat out cancel the inventory to pop up for spectators in creative mode if thats possible well heres my code.

    Code for disabled moving items to inventory in creative
    Code:
    @EventHandler
    public void onClickInventorySpec(InventoryClickEvent event){
        Player player = (Player) event.getClickedInventory();
        if(spectator.contains(player))
        event.setResult(org.bukkit.event.Event.Result.DENY);
        }
    Code to disable the inventory open
    Code:
    @EventHandler
    public void onOpenInventorySpec(InventoryOpenEvent event){
        Player player = (Player) event.getInventory();
        if(spectator.contains(player))
        event.setCancelled(true);
        }
    Is this the correct way I would do this?
     
  2. Offline

    SuchSwegMuchWow

    @VinexAx789 Check if they have creative mode and cancel opening their own inventory
     
  3. Offline

    VinexAx789

    @SuchSwegMuchWow Okay give me a minute so which one do I not need? And what should I keep a check too.
     
  4. Offline

    SuchSwegMuchWow

    @VinexAx789 You're probably not going to need the inventory click, because they won't even get access to that. Btw, why don't you just use like adventure gamemode or spectator gamemode if you're using 1.8
     
  5. Offline

    VinexAx789

    Already did that in 1.8 PvP sucks didnt go so well I guess I could use Adventure
     
  6. Offline

    SuchSwegMuchWow

  7. Offline

    VinexAx789

    @SuchSwegMuchWow I need to use Creative because spectators block arrows and it fixes that.
     
  8. Offline

    SuchSwegMuchWow

    @VinexAx789 Ahh. Well just simply cancel inventory open if they are a spectator or have creative gamemode.
     
  9. Offline

    VinexAx789

    So like I had it?
    Code:
    @EventHandler
    public void onOpenInventorySpec(InventoryOpenEvent event){
        Player player = (Player) event.getInventory();
        if(spectator.contains(player))
        event.setCancelled(true);
        }
    @SuchSwegMuchWow

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  10. Offline

    SuchSwegMuchWow

    @VinexAx789 Ya, and btw store player names instead
     
  11. Offline

    VinexAx789

    @SuchSwegMuchWow Sorry I do not follow? What do you mean store player names instead? Like p.getPlayer().getName(); ??

    I'm sorry
     
  12. Offline

    SuchSwegMuchWow

    @VinexAx789 Ya so your array list will be like
    Code:
    ArrayList<String> spectator = new ArrayList<>(); 
    and when you put someone in the array list you use
    Code:
    spectator.add(player.getName());
     
  13. Offline

    VinexAx789

    Last edited by a moderator: Mar 15, 2015
  14. Offline

    SuchSwegMuchWow

    @VinexAx789 You can do that, but it would be better if you used
    Code:
    ArrayList<String> spectator = new ArrayList<String>();
    to store the people who are spectators
     
  15. Offline

    VinexAx789

    @SuchSwegMuchWow


    Inventory
    Code:
    @EventHandler
    public void onOpenInventorySpec(InventoryOpenEvent event){
        Player player = (Player) event.getInventory();
        if(spectator.contains(player))
        event.setCancelled(true);
        }
    

    Okay here's my spectator part
    Code:
        getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
              public void run() {
                 p.spigot().respawn();
                 p.teleport(lobby);
                 //p.setGameMode(GameMode.SPECTATOR);
                 //p.kickPlayer(ChatColor.RED + "You died");
                
                  p.getInventory().clear();
                  p.updateInventory();
                  p.setGameMode(GameMode.CREATIVE);
                  p.setAllowFlight(true);
                  p.setFlying(true);
                
                  for(Player player : Bukkit.getOnlinePlayers()){
                      if(!player.equals(p)){
                          player.hidePlayer(p);
                      }
                  }
                  spectator.add(p.getName());
              }
        }, 2L);
    }
    
    Now my InventoryEvent
    Code:
    @EventHandler
    public void onOpenInventorySpec(InventoryOpenEvent event){
        Player player = (Player) event.getInventory();
        if(spectator.contains(player))
        event.setCancelled(true);
        }
    I have a question now will this cancel the event for only Spectators? Or will it to players too?
     
  16. Offline

    SuchSwegMuchWow

  17. Offline

    VinexAx789

    @SuchSwegMuchWow Okay if I make it like this

    Code:
    @EventHandler
    public void onOpenInventorySpec(InventoryOpenEvent event){
        Player player = (Player) event.getInventory();
        Player player = (Player) event.getPlayer();
        if(spectator.contains(player))
        event.setCancelled(true);
    if (player.isOp()); {
    event.Cancelled(false);
        }
    Will that let ops open the inv?

    @SuchSwegMuchWow by any chance do you know how to make a tracking device for the spectators? Or a gui menu?

    Sadly testing this nothing worked I have no clue what to do now...

    @SuchSwegMuchWow

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  18. Offline

    xTigerRebornx

    @VinexAx789 The server is unable to detect when the client opens its inventory last time I checked
     
    1Rogue likes this.
  19. Offline

    VinexAx789

  20. Offline

    MajorSkillage

    Erm, why don't you just use if(p.getGamemode().equals(your gamemode)){ //instead of making an arraylist}
     
  21. Offline

    VinexAx789

    @MajorSkillage Because I need only spectators to have the inventory blocked. Because they will be in creative

    @SuchSwegMuchWow Any help?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  22. Offline

    teej107


    @VinexAx789 Just cancel the block place event for spectators.
     
  23. Offline

    VinexAx789

    @teej107 Already have that cancel it's the items I'm worried about they can shoot people with bows and spawn in a item to arm a bomb I need to disable the inventorymoveitem something.. For only spectators in creative

    @teej107 @xTigerRebornx @MajorSkillage @SuchSwegMuchWow I really need to solve this anyone else know how to do this?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  24. Offline

    teej107

    @VinexAx789 Cancel the respective events except the inventory open event since that can't be detected.
     
  25. Offline

    VinexAx789

    @teej107 I'm sorry I really don't understand what you mean can you tell me what exactly events I need to cancel thanks
     
  26. Offline

    teej107

    @VinexAx789 Well you don't want spectators to
    • Place blocks
    • Fire bows
    There are events for those
     
  27. Offline

    VinexAx789

    @teej107 I already have blocks disabled but I do not have the bows but I just want it like mineplex has it they have it so when a spectator moves a item out of creative inventory it doesn't work which I want.
     
  28. Offline

    teej107

    @VinexAx789 Then listen for an InventoryMoveEvent (or something like that)
     
  29. Offline

    1Rogue

    InventoryClickEvent (though there are methods in spigot-api for managing this).

    I'll probably release a plugin for easier spectator mangement
     
  30. Offline

    VinexAx789

    Will this work?
    @EventHandler
    public void onOpenInventorySpec(InventoryItemMoveEvent event){
    Player player = (Player) event.getInventory();
    if(spectator.contains(player))
    event.setCancelled(true);
    @1Rogue @teej107
     
Thread Status:
Not open for further replies.

Share This Page