turt2live

Untitled

Mar 17th, 2012
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.03 KB | None | 0 0
  1. package com.turt2live.antishare;
  2.  
  3. import java.util.List;
  4.  
  5. import org.bukkit.Bukkit;
  6. import org.bukkit.ChatColor;
  7. import org.bukkit.Location;
  8. import org.bukkit.Material;
  9. import org.bukkit.block.Block;
  10. import org.bukkit.block.Chest;
  11. import org.bukkit.entity.Player;
  12. import org.bukkit.metadata.FixedMetadataValue;
  13. import org.bukkit.metadata.MetadataValue;
  14.  
  15. public class InventoryMirror {
  16.  
  17.     public static void mirror(Player from, Player to, AntiShare plugin){
  18.         //Find where the chest can be placed
  19.         Block block = to.getTargetBlock(null, 5);
  20.         if(block == null){
  21.             Location location = to.getLocation();
  22.             location.setY(location.getY() - 1);
  23.             block = to.getWorld().getBlockAt(location);
  24.         }
  25.         //Convert it to a chest
  26.         Location location = block.getLocation();
  27.         location.setX(location.getX() + 1);
  28.         Block otherBlock = block.getLocation().getWorld().getBlockAt(location);
  29.         int id1 = block.getTypeId();
  30.         int id2 = otherBlock.getTypeId();
  31.         System.out.println(id1 + " " + id2);
  32.         block.setType(Material.CHEST);
  33.         otherBlock.setType(Material.CHEST);
  34.         Chest chest = (Chest) block.getState();
  35.         chest.setMetadata("original1", new FixedMetadataValue(plugin, id1));
  36.         chest.setMetadata("original2", new FixedMetadataValue(plugin, id2));
  37.         // Set inventory and key for listener
  38.         chest.getInventory().setContents(from.getInventory().getContents());
  39.         chest.setMetadata("invmirror", new FixedMetadataValue(plugin, to.getName()));
  40.         ASUtils.sendToPlayer(to, ChatColor.YELLOW + "A chest has spawned with " + from.getName() + "'s inventory.");
  41.         ASUtils.sendToPlayer(to, ChatColor.YELLOW + "The chest will despawn in 1 minute.");
  42.         scheduleRemoval(chest, to, plugin);
  43.     }
  44.  
  45.     private static void scheduleRemoval(final Chest chest, final Player player, final AntiShare plugin){
  46.         Bukkit.getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable(){
  47.             @Override
  48.             public void run(){
  49.                 List<MetadataValue> values = chest.getMetadata("original1");
  50.                 for(MetadataValue value : values){
  51.                     if(value.getOwningPlugin().getName().equalsIgnoreCase("AntiShare")){
  52.                         chest.getInventory().clear();
  53.                         Block block = chest.getBlock();
  54.                         block.setType(Material.getMaterial(value.asInt()));
  55.                         System.out.println("1: " + value.asInt());
  56.                     }
  57.                 }
  58.                 values = chest.getMetadata("original2");
  59.                 for(MetadataValue value : values){
  60.                     if(value.getOwningPlugin().getName().equalsIgnoreCase("AntiShare")){
  61.                         chest.getInventory().clear();
  62.                         Location location = chest.getBlock().getLocation();
  63.                         location.setX(location.getX() + 1);
  64.                         Block block = chest.getBlock().getWorld().getBlockAt(location);
  65.                         block.setType(Material.getMaterial(value.asInt()));
  66.                         System.out.println("2: " + value.asInt());
  67.                     }
  68.                 }
  69.                 chest.removeMetadata("invmirror", plugin);
  70.                 Block block = chest.getBlock();
  71.                 block.removeMetadata("invmirror", plugin);
  72.                 ASUtils.sendToPlayer(player, ChatColor.YELLOW + "1 minute has passed, the chest has despawned.");
  73.             }
  74.             // TODO: Restore
  75.         }, /*60 * */20); // 1 minute
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment