Place Reset after game is done

Discussion in 'Plugin Development' started by Alex_Cboy, Jan 18, 2017.

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

    Alex_Cboy

    Hey guys, im stuck.I want to store the broken stained clay blocks and restore them after the gamestate = finished.I got the event, but i do not know how to trigger it and if there's a better way on storing broken blocks instead of a hashmap,if yes please gimme an example. My code rn:
    Code:
    @EventHandler
        public void onBlockbreak(BlockBreakEvent e){
                HashMap<Material,Location> map = new HashMap<Material,Location>();
                if(e.getBlock().equals(Material.STAINED_CLAY)){
                    map.put(e.getBlock().getType(), e.getBlock().getLocation());
                }
         
         
            }
    
    Thanks for taking the time to even read this guys!

    @Zombie_Striker Checking that rightnow, i'll inform you after i try this if it worked or not!
    @Zombie_Striker I cant get the material from the hashmap as a value. Any ideas on how to do it?I get an error.
    Code:
    do{
                    for(int i=0;i < map.size();i++){
                        e.getPlayer().sendMessage("Keyset: "+ map.keySet().toString());
                        Bukkit.getWorld(e.getPlayer().getWorld().toString()).getBlockAt((Location) map.keySet()).setType(map.values().toString());
                    }
                }while(Gamestate.getState() == 0);
    
    @kameronn that's what im doing right now, not exactly that but in some way.Thanks for the advice. @Zombie_Striker i solved it. Now i have to setup the states correctly.

    ---
    Still, haven't figured out how to place back the blocks, as it does nothing whatever i do.It does not place even one block. Anyone can help on how to do it?
    @Zombie_Striker
    yes, but there are no blocks being placed, i followed lots of tutorials and techniques none worked.Im pretty sure im missing something in the above code.
     
    Last edited: Jan 19, 2017
  2. Offline

    Zombie_Striker

    @Alex_Cboy
    1. Create a Repeating task.
    2. If gamestate = finished
    3. Loop through the list, reset each material.
    BTW: You got the map backwards. The location should be the key, and the type should be the value.
     
  3. Offline

    kameronn

    @Zombie_Striker @Alex_Cboy
    Why cant you just make your own event and execute it when the game is finished or just call a void when you set the gamestate to finished
     
    Zombie_Striker likes this.
  4. Offline

    Alex_Cboy

    Yet to be solved.Any help is appreciated!
     
  5. Offline

    Zombie_Striker

    @Alex_Cboy
    Have you read these posts yet?
     
  6. Offline

    kameronn

    @Alex_Cboy
    I really dont understand what the point of a for loop instead of a while loop instead of just a plain for loop?...
    Just remove the while loop all together

    Did you do debugging and check for the following:

    • The map size is greater than 0.
    • The block you are getting is not null.
    • The values you are putting in the hashmap are actually getting added.
    • Any errors you may have in the code since we are not able to see much.
    Also why are you using Bukkit.getWorld() instead of just player.getWorld()? Doesn't make much sense to me.

    More questions for you:
    • Is the player getting the message you're sending?
    • Is wherever you are running that code getting executed?
    • Honestly to me your code just seems extremely weird it just doesn't work with the for loop.
    The for loop will just run through the code multiple times but you arent getting any values with the loop so try this kind of a much simpler for loop.
    Code:
            for (Entry<Location, Material> set : map.entrySet()) {
                Location loc = map.getKey();
                Material mat = map.getValue();
            }
    Please take my suggestions and post your problem if a new one occurs.
     
  7. Online

    timtower Administrator Administrator Moderator

    @Alex_Cboy Please don't edit the main post all the time.
    Just respond to the thread.
     
  8. Offline

    Alex_Cboy

    @kameronn Sorry for being that late, it was too late for me when i saw the thread and i did not have much time atm.
    So, everything works find except this line of code, i do not get any errors, i have it in a different class so when i call this function from the main class EggListener.resetarena(); I just get an error, at that line. I'm going to try and resolv it and i'll post here as soon as possible!
     
  9. Offline

    Zombie_Striker

    @Alex_Cboy
    Can you post that line? Can you post the error?
     
  10. Offline

    Alex_Cboy

    @Zombie_Striker that's the error:
    Code:
    [22:17:29 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'game' in plugin SpleefWars vV0.1
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.11.jar:git-Spigot-7d78b81-775f1e1]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.11.jar:git-Spigot-7d78b81-775f1e1]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchCommand(CraftServer.java:650) ~[spigot-1.11.jar:git-Spigot-7d78b81-775f1e1]
            at net.minecraft.server.v1_11_R1.PlayerConnection.handleCommand(PlayerConnection.java:1344) [spigot-1.11.jar:git-Spigot-7d78b81-775f1e1]
            at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:1179) [spigot-1.11.jar:git-Spigot-7d78b81-775f1e1]
            at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.11.jar:git-Spigot-7d78b81-775f1e1]
            at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.11.jar:git-Spigot-7d78b81-775f1e1]
            at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.11.jar:git-Spigot-7d78b81-775f1e1]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_111]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_111]
            at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) [spigot-1.11.jar:git-Spigot-7d78b81-775f1e1]
            at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:739) [spigot-1.11.jar:git-Spigot-7d78b81-775f1e1]
            at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot-1.11.jar:git-Spigot-7d78b81-775f1e1]
            at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:675) [spigot-1.11.jar:git-Spigot-7d78b81-775f1e1]
            at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:574) [spigot-1.11.jar:git-Spigot-7d78b81-775f1e1]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_111]
    Caused by: java.lang.ClassCastException: java.util.HashMap cannot be cast to java.util.Map$Entry
            at me.shyblade.spleefwars.EggListener.resetarena(EggListener.java:44) ~[?:?]
            at me.shyblade.spleefwars.Main.onCommand(Main.java:51) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.11.jar:git-Spigot-7d78b81-775f1e1]
            ... 15 more
    
    If you need anything else or the code i use just hit me up :D Thamks for losing so much time for me tho
     
    Last edited by a moderator: Jan 20, 2017
  11. Offline

    Zombie_Striker

    @Alex_Cboy
    You're casting a Hashmap to an entry. Just don't do that.

    If you still have problems post the updated code.
     
  12. Offline

    kameronn

  13. Offline

    Alex_Cboy

    @Zombie_Striker @kameronn
    Here's my code:
    Code:
    package me.shyblade.spleefwars;
    
    import java.util.HashMap;
    import java.util.Map.Entry;
    
    import javax.tools.Tool;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Effect;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Snowball;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.EntityBlockFormEvent;
    import org.bukkit.event.entity.EntityDamageByBlockEvent;
    import org.bukkit.event.entity.ProjectileHitEvent;
    import org.bukkit.event.player.PlayerChangedWorldEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.metadata.FixedMetadataValue;
    import org.bukkit.util.BlockIterator;
    
    public class EggListener implements Listener{
    static HashMap<Location,Material> map = new HashMap<Location,Material>();
      
      
        @SuppressWarnings("unchecked")
        public static void resetarena() { // this is the restore code I use
            for (Entry<Location, Material> set : map.entrySet()) {
                   Location loc = ((Entry<Location, Material>) map).getKey();
                   Material mat = ((Entry<Location, Material>) map).getValue();
                   Bukkit.getWorld("world").getBlockAt(loc).setType(mat);
               }
            
         map.clear();
         Gamestate.setState(Gamestate.IN_LOBBY);
       }
      
        @EventHandler
        public void onBlockbreak(BlockBreakEvent e){
            if (Gamestate.isState(Gamestate.IN_GAME)){
            Bukkit.broadcastMessage("" + e.getBlock());
                if(e.getBlock().equals(Material.STAINED_CLAY)){
                    map.put(e.getBlock().getLocation(),e.getBlock().getType());
                    Bukkit.broadcastMessage("adff " + map.keySet());
                }
              
                }
              
              
            }
      
        @EventHandler
        public void onShovRight(PlayerInteractEvent e){
            if(e.getPlayer().getItemInHand().getTypeId() != 256 && !Gamestate.isState(Gamestate.IN_GAME) && (e.getAction() != Action.RIGHT_CLICK_AIR || e.getAction() != Action.RIGHT_CLICK_BLOCK )){
            //    e.getPlayer().sendMessage("Yolooooo");
            }else{
                if(Gamestate.isState(Gamestate.IN_GAME)){
                e.setCancelled(true);
                Snowball ball = e.getPlayer().launchProjectile(Snowball.class);
                ball.setVelocity(e.getPlayer().getEyeLocation().getDirection().multiply(2));
                //e.getPlayer().sendMessage("§b§lLaunched!");
                } else{
                    e.setCancelled(false);
                }
            }
          
        }
    
      
      
        @EventHandler
        public void onPJoin(PlayerJoinEvent e){
            Player p = e.getPlayer();
            ItemStack is = new ItemStack(Material.IRON_SPADE);
            ItemMeta im = is.getItemMeta();
            im.setDisplayName(ChatColor.AQUA +""+ ChatColor.BOLD + "BlockBruiser");
            is.setItemMeta(im);
          
            if(p.getInventory().getItemInHand() != is){
                p.getInventory().setItemInHand(is);
            }else {e.getPlayer().sendMessage("You already have this!");}
          
        }
    
        @EventHandler
        public void EntityHitEvent(ProjectileHitEvent e){
            Player p = (Player) e.getEntity().getShooter();
            if(e.getEntity() instanceof Snowball){
                BlockIterator iterator = new BlockIterator(e.getEntity().getWorld(), e.getEntity().getLocation().toVector(), e.getEntity().getVelocity().normalize(), 0.0D, 4);
                Block hitBlock = null;
                while (iterator.hasNext()) {
                    hitBlock = iterator.next();
                    
                    if (hitBlock.getTypeId() != 0) {
                    break;
                    }
                    }
                    
                    if (hitBlock.getType() == Material.STAINED_CLAY) {
                    hitBlock.getWorld().playEffect(hitBlock.getLocation(), Effect.STEP_SOUND, hitBlock.getTypeId());
                    hitBlock.breakNaturally();
                    map.put(hitBlock.getLocation(), hitBlock.getType());
                    p.sendMessage("" + map.size());
                    }else{
                    //    Player p = (Player) e.getEntity().getShooter();
                    //    if(e.getEntity().getShooter() instanceof Player){
                          
                            //p.sendMessage("§4§lError: §cThis is not Stained Clay");
                          
                        }
                      
                    }
                  
              
                /*float y = e.getEntity().getLocation().getBlockY();
                Location loc = e.getEntity().getLocation();
                 int locx = (int)loc.getX();
                  int locy =loc.getBlockY()-1;
                  int locz = (int)loc.getZ();
                  Location nloc = new Location(e.getEntity().getWorld(),locx, locy, locz);
                  Block b = nloc.getBlock();
    
                try{
                  if(b.getType() == Material.AIR){
                      locx = locx-1;
                      b.setType(Material.AIR);
                  }else if(b.getType() == Material.AIR){
                             
                  }
                    b.setType(Material.AIR);
                loc.getBlock().breakNaturally();
                }catch (Exception ex) {
                    Bukkit.broadcastMessage("Error: "+ex);
                }*/
            //Bukkit.broadcastMessage("Someone Hit a block");
            }      
        }
    
    
    Code:
    package me.shyblade.spleefwars;
    
    import java.util.Map;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin{
    
      
    
      
        public void onEnable(){
            Gamestate.setState(Gamestate.IN_LOBBY);
          
            try{
            this.getServer().getPluginManager().registerEvents(new EggListener(), this);
            }catch (Exception e) {
                // TODO: handle exception
                System.out.println("ERROR REGISTERING THE LISTENER: " + e.getMessage());
            }
            }  
      
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Player p = (Player) sender;
            if(label.equalsIgnoreCase("game") && sender instanceof Player){
                if(args.length != 1){
          
                }else{
                    //p.sendMessage("§cUsage: /game <start|stop>" + args[0] + args[1]);
                  
                    if(/*args[0].equalsIgnoreCase("game") &&*/ args[0].equalsIgnoreCase("start")){
                        //gamestate = started.
                        Gamestate.setState(Gamestate.IN_GAME);
                        p.sendMessage(Gamestate.getState() + " Gamestate");
                        //setState() == Gamestate.State.Started;
                    }else
                        if(/*args[0].equalsIgnoreCase("game") &&*/ args[0].equalsIgnoreCase("stop")){
                            p.sendMessage("Stoping...");
                            Gamestate.setState(Gamestate.RESETING);
                            EggListener.resetarena();
                          
                          
                            Bukkit.broadcastMessage("§l§5Arena Reset!");
                    }else{
                p.sendMessage("§c§lWhat are you doing... There is no such command. Use /game for more info.");
                    }
                //p.sendMessage("Args " + args[0]);
            }
          
          
            return false;
            }
            return false;
            }
      
    
    }
    
    
    EDIT: I fixed the error but it doesn't place the block back
     
    Last edited: Jan 20, 2017
  14. Offline

    Zombie_Striker

    Don't cast. Just replace "map" and the cast with "set"
     
  15. Offline

    Alex_Cboy

    @Zombie_Striker
    I did i just forgot to edit the post sorry!
    Now there's another problem that i really have no idea how to solve.I have different types of stained clay and it places the main type.How am i going to set the specific type of stained clay?
     
  16. Offline

    Zombie_Striker

    @Alex_Cboy
    Change the block data.
    Code:
    Block#setData(the color, use DyeColor to figure out the id for each color)
     
  17. Offline

    kameronn

    @Alex_Cboy
    Just do
    Code:
                   Location loc = set.getKey();
    
     
Thread Status:
Not open for further replies.

Share This Page