Created
January 30, 2013 17:54
-
-
Save Xyene/4675153 to your computer and use it in GitHub Desktop.
A static factory for converting between Canary and Bukkit types.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.surgedev.legacy; | |
import org.bukkit.*; | |
import org.bukkit.block.*; | |
import org.bukkit.craftbukkit.CraftServer; | |
import org.bukkit.craftbukkit.CraftWorld; | |
import org.bukkit.craftbukkit.entity.CraftPlayer; | |
import org.bukkit.enchantments.Enchantment; | |
import org.bukkit.inventory.ItemStack; | |
import org.bukkit.inventory.meta.ItemMeta; | |
import org.surgedev.legacy.canary.*; | |
import org.surgedev.legacy.canary.Block; | |
import org.surgedev.legacy.canary.Location; | |
import org.surgedev.legacy.canary.World; | |
import java.util.ArrayList; | |
import java.util.Map; | |
public class Canary { | |
public static Item asCanaryCopy(ItemStack orig) { | |
Item i = new Item(); | |
i.setItemId(orig.getTypeId()); | |
i.setAmount(orig.getAmount()); | |
i.setDamage(orig.getDurability()); | |
if (orig.getItemMeta().hasLore()) | |
i.setLore(orig.getItemMeta().getLore().toArray(new String[0])); | |
Map<Enchantment, Integer> enchilada = orig.getEnchantments(); | |
for (Map.Entry<Enchantment, Integer> en : enchilada.entrySet()) { | |
i.addEnchantment(en.getKey().getId(), en.getValue()); | |
} | |
return i; | |
} | |
public static ItemStack asBukkitCopy(Item orig) { | |
ItemStack i = new ItemStack(orig.getItemId(), orig.getAmount()); | |
i.setDurability((short) orig.getDamage()); | |
ItemMeta meta = i.getItemMeta(); | |
ArrayList<String> lore = new ArrayList<String>(); | |
for (String s : orig.getLore()) | |
lore.add(s); | |
meta.setLore(lore); | |
for (org.surgedev.legacy.canary.Enchantment ench : orig.getEnchantments()) { | |
Enchantment buk = Enchantment.getByName(ench.getType().name()); | |
i.addEnchantment(buk, buk.getStartLevel()); | |
} | |
return i; | |
} | |
public static org.bukkit.Location asBukkitCopy(Location loc) { | |
return new org.bukkit.Location(loc.getWorld().getWorld().getWorld(), loc.x, loc.y, loc.z, loc.rotX, loc.rotY); | |
} | |
public static Location asCanaryCopy(org.bukkit.Location loc) { | |
return new Location(new World(((CraftWorld)loc.getWorld()).getHandle()), loc.getX(), loc.getY(), loc.getZ()); | |
} | |
public static org.bukkit.entity.Player asBukkitCopy(Player p) { | |
return new CraftPlayer((CraftServer)Bukkit.getServer(), p.getEntity()); | |
} | |
public static Player asCanaryCopy(org.bukkit.entity.Player p) { | |
return new Player(((CraftPlayer)p).getHandle()); | |
} | |
public static Block asCanaryCopy(org.bukkit.block.Block b) { | |
return new Block(asCanaryCopy(b.getWorld()), b.getTypeId(), b.getX(), b.getY(), b.getZ(), b.getData()); | |
} | |
public static org.bukkit.block.Block asBukkitCopy(Block b) { | |
return b.getWorld().getWorld().getWorld().getBlockAt(b.getX(), b.getY(), b.getZ()); | |
} | |
public static World asCanaryCopy(org.bukkit.World world) { | |
return new World(((CraftWorld)world).getHandle()); | |
} | |
public static org.bukkit.World asBukkitCopy(World world) { | |
return Bukkit.getWorld(world.getName()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment