Code not working properly

Discussion in 'Plugin Development' started by PatoTheBest, May 30, 2014.

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

    PatoTheBest

    I want to rollback some blocks, the method rollbacks the blocks but when it tries to rollback the chest contents, it rollbacks each item in the correct position and everything, but when the item is a stack of 2 or more items, it rollbacks it as if it would only had one item.

    HashMap
    Code:java
    1. List<BlockState> states;
    2. private HashMap<Location, ItemStack[]> chests = new HashMap<Location, ItemStack[]>();


    RollBack Method
    Code:java
    1. int i = 0;
    2. if(states != null){
    3. for (BlockState state : states) {
    4. state.update(true);
    5. i++;
    6. }
    7. for(Entity e : Bukkit.getWorld(s.getWorld().getName()).getEntities()){
    8. if(s.contains(e.getLocation()) && e.getType() == EntityType.DROPPED_ITEM){
    9. e.remove();
    10. }
    11. }
    12. for (Entry<Location, ItemStack[]> chest : chests.entrySet()){
    13. Location l = chest.getKey();
    14. Block block = l.getBlock();
    15. Chest c = (Chest) block.getState();
    16. c.getBlockInventory().setContents(chest.getValue());
    17. }
    18. chests.clear();
    19. states.clear();
    20. }
    21. sender.sendMessage(ChatColor.DARK_PURPLE + "Rollbacked " + i
    22. + " Blocks.");

    Save Method:
    Code:java
    1. states.add(event.getBlock().getState());
    2. if (event.getBlock().getType() == Material.CHEST){
    3. Chest chest = (Chest) event.getBlock().getState();
    4. chests.put(chest.getLocation(), chest.getBlockInventory().getContents());
    5. }
     
  2. Offline

    fireblast709

    PatoTheBest clone the contents (so call .clone() on the ItemStack[])
     
  3. Offline

    PatoTheBest

    fireblast709 Tried that but no luck, btw, What is the different between getBlockInventory and getInventory?
     
  4. Offline

    Rocoty

    Read the docs. And when you say you tried, you did do it in the save method right?
     
  5. Offline

    PatoTheBest

    Rocoty Tried it everywhere I could, nothing
     
  6. Offline

    Rocoty

  7. Offline

    PatoTheBest

    Rocoty Can you try and fix my code?
     
  8. Offline

    Rocoty

    PatoTheBest I can try and help you fix your own code. Can you post your full updated code?
     
  9. Offline

    PatoTheBest

    Code:java
    1. package org.pato.simplerollback;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.HashMap;
    5. import java.util.List;
    6. import java.util.Map.Entry;
    7.  
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.Location;
    10. import org.bukkit.Material;
    11. import org.bukkit.block.Block;
    12. import org.bukkit.block.BlockState;
    13. import org.bukkit.block.Chest;
    14. import org.bukkit.command.Command;
    15. import org.bukkit.command.CommandSender;
    16. import org.bukkit.entity.Player;
    17. import org.bukkit.event.EventHandler;
    18. import org.bukkit.event.Listener;
    19. import org.bukkit.event.block.BlockBreakEvent;
    20. import org.bukkit.inventory.ItemStack;
    21. import org.bukkit.plugin.java.JavaPlugin;
    22.  
    23. public class SimpleRollback extends JavaPlugin implements Listener {
    24.  
    25. List<BlockState> states;
    26. private HashMap<Location, ItemStack[]> chests = new HashMap<Location, ItemStack[]>();
    27.  
    28. public void onEnable() {
    29. getServer().getPluginManager().registerEvents(this, this);
    30.  
    31. if (states == null) {
    32. states = new ArrayList<BlockState>();
    33. }
    34. }
    35.  
    36. public void onDisable() {
    37.  
    38. }
    39.  
    40. public boolean onCommand(CommandSender sender, Command command,
    41. String label, String[] args) {
    42. if (sender instanceof Player) {
    43. if (label.equalsIgnoreCase("rollback")) {
    44. int i = 0;
    45. if(states != null){
    46. for (BlockState state : states) {
    47. state.update(true);
    48. i++;
    49. }
    50. for (Entry<Location, ItemStack[]> chest : chests.entrySet()){
    51. Location l = chest.getKey();
    52. Block block = l.getBlock();
    53. Chest c = (Chest) block.getState();
    54. c.getInventory().setContents(chest.getValue());
    55. }
    56. chests.clear();
    57. states.clear();
    58. }
    59. sender.sendMessage(ChatColor.DARK_PURPLE + "Rollbacked " + i
    60. + " Blocks.");
    61.  
    62. }
    63. } else {
    64. sender.sendMessage("Only in-game players can perform this command.");
    65. }
    66. return true;
    67. }
    68.  
    69. @EventHandler
    70. public void blockBreak(BlockBreakEvent event) {
    71. states.add(event.getBlock().getState());
    72. if (event.getBlock().getType() == Material.CHEST){
    73. Chest chest = (Chest) event.getBlock().getState();
    74. chests.put(chest.getLocation(), chest.getInventory().getContents().clone());
    75. }
    76. }
    77. }


    Replace getInventory with getBlockInventory

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

    PatoTheBest

    3 page bump
     
Thread Status:
Not open for further replies.

Share This Page