Odd error with door-replacing.

Discussion in 'Spigot Plugin Development' started by boolean, Jul 1, 2016.

  1. Hello! I currently have a Crowbar plugin (that cuts iron bars when right-clicking) Earlier I decided to add a feature that allows you to temporarily "disable" iron doors and trap doors. I got into the game, and replacing trapdoors worked fine (same for cutting bars) and mining spawners (gives you the spawnertype that you mined)

    However, when trying to execute the iron-door replacement, I came across an error. Only the bottom half of the door actually re-appears and ends up facing the complete wrong way (always pointing North)

    Screenshot of the problem: http://prntscr.com/bnjdg2

    My current code (Listener class):

    Code (Java):

    package com.crowbar;

    import java.util.Arrays;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.block.CreatureSpawner;
    import org.bukkit.entity.Player;
    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.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    public class CrowbarEvent implements Listener {
     
        @EventHandler
        public void onInteract(PlayerInteractEvent e) {
            if(e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                Block block = e.getClickedBlock();
                Player player = e.getPlayer();
                ItemStack item = e.getItem();
                if(item.getType() == Material.GOLD_PICKAXE && (item.getItemMeta().getDisplayName().equals(ChatColor.RED + "Crowbar"))) {
                    if(block.getType() == Material.IRON_DOOR_BLOCK) {
                        block.setType(Material.AIR);
                        Bukkit.getScheduler().scheduleSyncDelayedTask(Crowbar.getInstance(), new Runnable() {
                            @Override
                            public void run() {
                                block.setType(Material.IRON_DOOR_BLOCK);
                             
                            }
                        }, 600L);
                        player.sendMessage(ChatColor.RED + "You have 30 seconds before the door reappears!");
                        player.getInventory().remove(item);
                    }
                    if(block.getType() == Material.IRON_TRAPDOOR) {
                        block.setType(Material.AIR);
                        Bukkit.getScheduler().scheduleSyncDelayedTask(Crowbar.getInstance(), new Runnable() {
                            @Override
                            public void run() {
                                block.setType(Material.IRON_TRAPDOOR);
                            }
                        }, 600L);
                        player.sendMessage(ChatColor.RED + "You have 30 seconds before the trapdoor reappears!");
                        player.getInventory().remove(item);
                    }
                    if(block.getType() == Material.IRON_FENCE) {
                        block.setType(Material.AIR);
                        player.sendMessage(ChatColor.GREEN + "Bars cut successfully!");
                        player.getInventory().remove(item);
                    }
                }
            }
        }
     
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onBreak(BlockBreakEvent e) {
            Player player = e.getPlayer();
            Block block = e.getBlock();
            ItemStack item = player.getItemInHand();
            if(item.getType() == Material.GOLD_PICKAXE && item.hasItemMeta() && item.getItemMeta().hasDisplayName() && item.getItemMeta().getDisplayName().equals(ChatColor.RED + "Crowbar")) {
                if(player.getWorld().getEnvironment() == World.Environment.NETHER) {
                    player.sendMessage(ChatColor.RED + "You cannot mine blocks in this World with the Crowbar!");
                    e.setCancelled(true);
                    return;
                }
                if(player.getWorld().getEnvironment() == World.Environment.THE_END) {
                    player.sendMessage(ChatColor.RED + "You cannot mine blocks in this World with the Crowbar!");
                    e.setCancelled(true);
                    return;
                }
                if(block.getType() != Material.MOB_SPAWNER) {
                    player.sendMessage(ChatColor.RED + "You can only mine Mob Spawners with the Crowbar!");
                    e.setCancelled(true);
                    return;
                }
                ItemStack spawner = new ItemStack(block.getType());
                ItemMeta spawnerMeta = spawner.getItemMeta();
                spawnerMeta.setDisplayName(ChatColor.WHITE + ((CreatureSpawner)block.getState()).getSpawnedType().getName().toLowerCase() + "spawner");
                spawnerMeta.setLore(Arrays.asList(new String[] { new String(ChatColor.AQUA + "Broke with a " + ChatColor.RED + "Crowbar " + ChatColor.AQUA + "by " + ChatColor.GOLD + player.getName())}));
                spawner.setItemMeta(spawnerMeta);
                spawner.setDurability(((CreatureSpawner)block.getState()).getSpawnedType().getTypeId());
                block.getWorld().dropItemNaturally(block.getLocation(), spawner);
                item.setDurability((short) (item.getDurability() / 4));
                player.sendMessage(ChatColor.GREEN + "Spawner mined successfully!");
                player.giveExpLevels(player.getTotalExperience() / 10);
            }
        }

    }
    Any help whatsoever will be greatly appreciated,

    Thanks,
    ~ boolean
     
  2. Not sure how to fix it, but nice song choice ;)
     
    • Funny Funny x 1
  3. Thanks, you helped me replace the top part of the door. The only problem now is setting it back to where it originally was.
     
  4. its becuz ur gay and u cant cod3 fam!!!!!!!!!!!!!11111111111
    u shud kwit minecraft coz its for my xx_clan__xx u fgt

    (This is jokes, gl fixing the problem Ryan.)
     
  5. I think you need to check if it's right click then if it's clicking iron bars then remove it
     
  6. That's not the issue.. And uh..

     
  7. @EventHandler
    public void BarRemove(PlayerInteractEvent e)
    {
    Block b = e.getClickedBlock();

    if(e.getAction() == Action.RIGHT_CLICK_BLOCK)
    {
    if(e.getClickedBlock().getType() == Material.IRON_BARDING)
    {
    b.setType(Material.AIR);
    }
    }
    }
     
  8. Please refrain from double posting.
     
  9. Iron Barding is completely wrong, that's Iron Horse Armor. Iron_Fence is Bars. And if you had acknowledged the first 3 lines of my methods, you'd notice that's not the problem, nor the solution I was actually trying to find
     
    • Agree Agree x 1