Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.turt2live.antishare;
- import java.util.List;
- import org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.block.Block;
- import org.bukkit.block.Chest;
- import org.bukkit.entity.Player;
- import org.bukkit.metadata.FixedMetadataValue;
- import org.bukkit.metadata.MetadataValue;
- public class InventoryMirror {
- public static void mirror(Player from, Player to, AntiShare plugin){
- //Find where the chest can be placed
- Block block = to.getTargetBlock(null, 5);
- if(block == null){
- Location location = to.getLocation();
- location.setY(location.getY() - 1);
- block = to.getWorld().getBlockAt(location);
- }
- //Convert it to a chest
- Location location = block.getLocation();
- location.setX(location.getX() + 1);
- Block otherBlock = block.getLocation().getWorld().getBlockAt(location);
- int id1 = block.getTypeId();
- int id2 = otherBlock.getTypeId();
- System.out.println(id1 + " " + id2); //Spits out correct values
- block.setType(Material.CHEST);
- otherBlock.setType(Material.CHEST);
- Chest chest = (Chest) block.getState();
- chest.setMetadata("original1", new FixedMetadataValue(plugin, id1));
- chest.setMetadata("original2", new FixedMetadataValue(plugin, id2));
- // Set inventory and key for listener
- chest.getInventory().setContents(from.getInventory().getContents());
- chest.setMetadata("invmirror", new FixedMetadataValue(plugin, to.getName()));
- ASUtils.sendToPlayer(to, ChatColor.YELLOW + "A chest has spawned with " + from.getName() + "'s inventory.");
- ASUtils.sendToPlayer(to, ChatColor.YELLOW + "The chest will despawn in 1 minute.");
- scheduleRemoval(chest, to, plugin);
- }
- private static void scheduleRemoval(final Chest chest, final Player player, final AntiShare plugin){
- Bukkit.getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable(){
- @Override
- public void run(){
- chest.removeMetadata("invmirror", plugin);
- chest.getInventory().clear();
- int id1 = -1;
- int id2 = -1;
- List<MetadataValue> values = chest.getMetadata("original1");
- for(MetadataValue value : values){
- if(value.getOwningPlugin().getDescription().getName().equalsIgnoreCase("AntiShare")){
- id1 = value.asInt();
- System.out.println("1: " + value.asInt()); //Never called
- }
- }
- values = chest.getMetadata("original2");
- for(MetadataValue value : values){
- if(value.getOwningPlugin().getDescription().getName().equalsIgnoreCase("AntiShare")){
- id2 = value.asInt();
- System.out.println("2: " + value.asInt()); //Never called
- }
- }
- Block block = chest.getBlock();
- block.setType(Material.getMaterial(id1)); //ArrayIndexOutOfRange -1
- Location location = chest.getBlock().getLocation();
- location.setX(location.getX() + 1);
- block = chest.getBlock().getWorld().getBlockAt(location);
- block.setType(Material.getMaterial(id2));
- ASUtils.sendToPlayer(player, ChatColor.YELLOW + "1 minute has passed, the chest has despawned.");
- }
- // TODO: Restore
- }, /*60 * */20); // 1 minute [temporarily 20 ticks for testing]
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment