turt2live

Untitled

Mar 17th, 2012
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.11 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); //Spits out correct values
  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.                 chest.removeMetadata("invmirror", plugin);
  50.                 chest.getInventory().clear();
  51.                 int id1 = -1;
  52.                 int id2 = -1;
  53.                 List<MetadataValue> values = chest.getMetadata("original1");
  54.                 for(MetadataValue value : values){
  55.                     if(value.getOwningPlugin().getDescription().getName().equalsIgnoreCase("AntiShare")){
  56.                         id1 = value.asInt();
  57.                         System.out.println("1: " + value.asInt()); //Never called
  58.                     }
  59.                 }
  60.                 values = chest.getMetadata("original2");
  61.                 for(MetadataValue value : values){
  62.                     if(value.getOwningPlugin().getDescription().getName().equalsIgnoreCase("AntiShare")){
  63.                         id2 = value.asInt();
  64.                         System.out.println("2: " + value.asInt()); //Never called
  65.                     }
  66.                 }
  67.                 Block block = chest.getBlock();
  68.                 block.setType(Material.getMaterial(id1)); //ArrayIndexOutOfRange -1
  69.                 Location location = chest.getBlock().getLocation();
  70.                 location.setX(location.getX() + 1);
  71.                 block = chest.getBlock().getWorld().getBlockAt(location);
  72.                 block.setType(Material.getMaterial(id2));
  73.                 ASUtils.sendToPlayer(player, ChatColor.YELLOW + "1 minute has passed, the chest has despawned.");
  74.             }
  75.             // TODO: Restore
  76.         }, /*60 * */20); // 1 minute [temporarily 20 ticks for testing]
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment