JefferiesTube's Enchantments

Enchantment Showcase - JefferiesTube

Mining Helmet

public class EnchantmentMiningHelmet extends CustomEnchantment implements Listener 
{
    static final String[] MINING_ITEMS = new String[] {"iron_helmet", "gold_helmet", "chain_helmet", "diamond_helmet", "book"};

    public static HashMap<Player, Boolean> pluginEnabled = new HashMap<Player, Boolean>();
	public static HashMap<Player, Location> previousLoc = new HashMap<Player, Location>();
	public static HashMap<Player, Integer> previousBlock = new HashMap<Player, Integer>();
	public static HashMap<Player, Byte> previousBlockData = new HashMap<Player, Byte>();
	public static HashMap<Player, Sign> signData = new HashMap<Player, Sign>();	    
    
    public EnchantmentMiningHelmet(JavaPlugin plugin) 
    {
    	super("Mining", MINING_ITEMS);
    }

    @EventHandler
	public void onPlayerJoin(PlayerJoinEvent evt){
    	EnchantmentMiningHelmet.pluginEnabled.put(evt.getPlayer(), true);
	}

    @EventHandler
	public void onPlayerQuit(PlayerQuitEvent evt){
		Player p = evt.getPlayer();
		Location l = p.getLocation();
		l.setY(l.getY() - 1);
		if(l.getWorld().getBlockAt(l).getType() == Material.GLOWSTONE){
			if(EnchantmentMiningHelmet.pluginEnabled.get(p)){
				l.getWorld().getBlockAt(l).setTypeId(previousBlock.get(p));
			}
		}
	}	    
    
    @Override
    public void applyEquipEffect(Player player, int enchantLevel)
    {
    	EnchantmentMiningHelmet.pluginEnabled.put(player, true);
    }

    @Override
    public void applyUnequipEffect(Player player, int enchantLevel)
    {
    	if(EnchantmentMiningHelmet.pluginEnabled.containsKey(player))
    	{
    		EnchantmentMiningHelmet.pluginEnabled.remove(player);	    		
    	}
    	
		EnchantmentMiningHelmet.pluginEnabled.put(player, false);
    }

    // Calculates a level of enchantment based on the exp level used
    // Return less than 1 for no enchantment
    @Override
    public int getEnchantmentLevel(int expLevel) {
    	if(expLevel >= 30)
    		return 1;
    	return 0;
    }      
    
	@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled=true)
	public  void onPlayerMove(PlayerMoveEvent evt) {
		Location loc = evt.getPlayer().getLocation();
		World w = loc.getWorld();
		
		Player p = evt.getPlayer();
		if (p.hasPermission("RWtl.allow"))
		{
			loc.setY(loc.getY() - 1);
			
			Material _bTypes[] = {Material.AIR, Material.FENCE, Material.NETHER_FENCE, Material.BED, Material.LEVER,
					Material.REDSTONE_WIRE, Material.TORCH, Material.REDSTONE_TORCH_ON, Material.REDSTONE_TORCH_OFF,
					Material.LADDER, Material.VINE};
			
			Integer _bTypeIDs[] = {37, 38, 39, 40, 32, 30, 31, 66, 27, 28, 107, 70, 72, 69, 96, 116, 117, 115};
			
			HashSet<Material> blockTypes = new HashSet<Material>(Arrays.asList(_bTypes));
			HashSet<Integer> blockTypeIDs = new HashSet<Integer>(Arrays.asList(_bTypeIDs));

					//Added this bit in to make the lantern still work while jumping or falling.
			while ((blockTypes.contains(w.getBlockAt(loc).getType())) || (blockTypeIDs.contains(w.getBlockAt(loc).getTypeId())))
			{
				loc.setY(loc.getY() - 1);
			}
			
			try 
			{ 
				if(previousLoc.containsKey(p))
				{
					if(!signData.containsKey(p))
					{
						p.sendBlockChange(previousLoc.get(p), previousBlock.get(p), (byte)previousBlockData.get(p));
					} 
					else
					{
						Sign oldSign = signData.get(p);
	
						Sign newSign = (Sign) w.getBlockAt(previousLoc.get(p)).getState();
						p.sendBlockChange(previousLoc.get(p), previousBlock.get(p), (byte)previousBlockData.get(p));
						for(int i = 0; i <= 3; i++)
						{
							newSign.setLine(i, oldSign.getLine(i));newSign.update();
						}
						signData.remove(p);
					}
				}		
			}
			catch (ClassCastException e)
			{
				
			}

			if(w.getBlockAt(loc).getType() != Material.AIR && w.getBlockAt(loc).getType() != Material.WATER)
			{
				if(w.getBlockAt(loc).getTypeId() == 323 || w.getBlockAt(loc).getTypeId() == 68 || w.getBlockAt(loc).getType() == Material.SIGN || w.getBlockAt(loc).getType() == Material.SIGN_POST){
					signData.put(p, (Sign)w.getBlockAt(loc).getState());
				}
				if(w.getBlockAt(loc).getTypeId() == 93 || w.getBlockAt(loc).getTypeId() == 94 || w.getBlockAt(loc).getTypeId() == 93 || w.getBlockAt(loc).getTypeId() == 94){
					signData.put(p, (Sign)w.getBlockAt(loc).getState());
				}
				if(w.getBlockAt(loc).getTypeId() == 54 || w.getBlockAt(loc).getTypeId() == 78 || w.getBlockAt(loc).getTypeId() == 54 || w.getBlockAt(loc).getTypeId() == 78){
					signData.put(p, (Sign)w.getBlockAt(loc).getState());
				}
				if(w.getBlockAt(loc).getTypeId() == 44 || w.getBlockAt(loc).getTypeId() == 111 || w.getBlockAt(loc).getTypeId() == 44 || w.getBlockAt(loc).getTypeId() == 111){
					signData.put(p, (Sign)w.getBlockAt(loc).getState());
				}
			
				try 
				{ 
					if (p.getInventory().getItemInHand().getTypeId() == Material.TORCH.getId())
					{
						previousBlock.put(p, w.getBlockAt(loc).getTypeId());
						previousBlockData.put(p, w.getBlockAt(loc).getData());
						previousLoc.put(p, loc);
				
						p.sendBlockChange(loc, Material.GLOWSTONE, (byte)0);
					}
				}
				catch (NullPointerException e) 
				{
					 
				}
			}
		}
	}
}

Night Vision

public class EnchantmentNightvision extends CustomEnchantment implements Listener
{
	EnchantmentTimer timer;
	
    static final String[] NIGHTVISION_ITEMS = new String[] {"iron_helmet", "gold_helmet", "chain_helmet", "diamond_helmet"};

    public EnchantmentNightvision(JavaPlugin plugin) 
    {
    	super("Nightvision", NIGHTVISION_ITEMS);
    	timer = new EnchantmentTimer(plugin, new Runnable() {
			
			@Override
			public void run() 
			{
				Enumeration<Player> enumKey = timer.entries.keys();
				while(enumKey.hasMoreElements()) {
					Player key = enumKey.nextElement();
					Integer val = timer.entries.get(key);
				    applyEffect(key, val);
				}					
			}
		}, 20*20);
    }
    
    @EventHandler
    public void onPlayerLogin(PlayerLoginEvent event) {
    	ItemStack helmet = event.getPlayer().getEquipment().getHelmet();
    	if(helmet != null)
    	{
    		if (EnchantmentAPI.itemHasEnchantment(helmet, "Nightvision")) 
    		{
    			applyEffect(event.getPlayer(), 3);
    		}
    	}
    }    
    
    @Override
    public void applyEquipEffect(Player player, int enchantLevel)
    {
    	timer.entries.put(player, enchantLevel);
    	applyEffect(player, enchantLevel);
    }

	private void applyEffect(Player player, int enchantLevel) {
		if(player.hasPotionEffect(PotionEffectType.NIGHT_VISION))
			removeEffect(player);
		
		player.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, 20 * 60, enchantLevel - 1));
	}
    
    @Override
    public void applyUnequipEffect(Player player, int enchantLevel)
    {
    	timer.entries.remove(player);
    	removeEffect(player);
    }

	private void removeEffect(Player player) {
		player.removePotionEffect(PotionEffectType.NIGHT_VISION);
	}
    
    // Calculates a level of enchantment based on the exp level used
    // Return less than 1 for no enchantment
    @Override
    public int getEnchantmentLevel(int expLevel) {
    	if(expLevel >= 30)
    		return 3;
    	if(expLevel >= 20)
    		return 2;
    	if(expLevel >= 10)
    		return 1;
    	return 0;
    }      
}

public class EnchantmentTimer extends BukkitRunnable 
{
	public Hashtable<Player, Integer> entries = new Hashtable<Player, Integer>();

	public Runnable action;
	
	public EnchantmentTimer(JavaPlugin plugin, Runnable _action, long freq)
	{
		super();
		action = _action;
		plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, _action, 0, freq);
	}
	
	@Override
	public void run()
	{
		purgePlayers();
		action.run();
	}

	private void purgePlayers()
	{
		Enumeration<Player> enumKey = entries.keys();
		while(enumKey.hasMoreElements()) {
			Player key = enumKey.nextElement();
		    if(key == null)
		    	entries.remove(key);
		}		
	}
}

Life Steal

public class EnchantmentLifesteal extends CustomEnchantment
{
	// Items able to be enchanted with lifesteal
    static final String[] LIFESTEAL_ITEMS = new String[] {
            "wood_sword", "stone_sword", "iron_sword", "gold_sword", "diamond_sword",
            "bow", "book"};

    // Constructor
    public EnchantmentLifesteal() 
    {
        super("Lifesteal", LIFESTEAL_ITEMS);
    }
    
    // An on-hit enchantment example
    @Override
    public void applyEffect(LivingEntity user, LivingEntity target, int enchantLevel, EntityDamageByEntityEvent event)
    {
        // Gain health depending on enchantment level
        int health = (int) (user.getHealth() + Math.floor((0.1f * enchantLevel) * event.getDamage()));
        if (health > user.getMaxHealth()) health = user.getMaxHealth();
        user.setHealth(health);
    }  
    
 // Calculates a level of enchantment based on the exp level used
    // Return less than 1 for no enchantment
    @Override
    public int getEnchantmentLevel(int expLevel) {
    	if(expLevel >= 30)
    		return 3;
    	if(expLevel >= 20)
    		return 2;
    	if(expLevel >= 10)
    		return 1;
    	return 0;
    }    
}

Comments

Posts Quoted:
Reply
Clear All Quotes