Skip to content

Instantly share code, notes, and snippets.

@IDragonfire
Created July 27, 2012 11:43
Show Gist options
  • Save IDragonfire/3187540 to your computer and use it in GitHub Desktop.
Save IDragonfire/3187540 to your computer and use it in GitHub Desktop.
[HeroesSkill] SkillMiningDoubleDrop
package com.herocraftonline.heroes.characters.skill.skills;
import java.util.HashSet;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.inventory.ItemStack;
import com.herocraftonline.heroes.Heroes;
import com.herocraftonline.heroes.characters.Hero;
import com.herocraftonline.heroes.characters.effects.EffectType;
import com.herocraftonline.heroes.characters.skill.PassiveSkill;
import com.herocraftonline.heroes.characters.skill.SkillConfigManager;
import com.herocraftonline.heroes.characters.skill.SkillType;
import com.herocraftonline.heroes.listeners.HBlockListener;
import com.herocraftonline.heroes.util.Util;
/**
* SkillMiningDoubleDrop<br>
* http://dev.bukkit.org/server-mods/heroes/forum/unofficialdev/40202-build-successful-in-net-beans-but-finer-throw-error/#p4
*/
public class SkillMiningDoubleDrop extends PassiveSkill {
private final Material[] MINING_DOUBLE_DROP = new Material[] {
Material.COAL_ORE, Material.GOLD_ORE, Material.REDSTONE_ORE,
Material.COBBLESTONE, Material.ENDER_STONE,
Material.MOSSY_COBBLESTONE, Material.NETHER_BRICK,
Material.NETHER_FENCE, Material.NETHERRACK, Material.OBSIDIAN,
Material.SANDSTONE, Material.STONE };
private HashSet<Material> miningDoubleDrop;
public SkillMiningDoubleDrop(Heroes plugin) {
super(plugin, "MiningDoubleDrop");
setDescription("You have a chance at getting Double Drops while Mining");
setEffectTypes(EffectType.BENEFICIAL);
setTypes(SkillType.KNOWLEDGE, SkillType.EARTH, SkillType.BUFF);
this.miningDoubleDrop = new HashSet<Material>();
for (int i = 0; i < this.MINING_DOUBLE_DROP.length; i++) {
this.miningDoubleDrop.add(this.MINING_DOUBLE_DROP[i]);
}
Bukkit.getServer().getPluginManager().registerEvents(
new SkillMiningDoubleDrop.SkillBlockListener(), plugin);
}
@Override
public ConfigurationSection getDefaultConfig() {
ConfigurationSection node = super.getDefaultConfig();
node.set("coal-chance-base", 0.1);
node.set("coal-chance-per-level", 0.002);
node.set("iron-chance-base", 0.1);
node.set("iron-chance-per-level", 0.002);
node.set("gold-chance-base", 0.1);
node.set("gold-chance-per-level", 0.002);
node.set("redstone-chance-base", 0.1);
node.set("redstone-chance-per-level", 0.002);
node.set("cobble-chance-base", 0.1);
node.set("cobble-chance-per-level", 0.002);
node.set("endstone-chance-base", 0.1);
node.set("endstone-chance-per-level", 0.002);
node.set("mossstone-chance-base", 0.1);
node.set("mossstone-chance-per-level", 0.002);
node.set("netherbrick-chance-base", 0.1);
node.set("netherbrick-chance-per-level", 0.002);
node.set("netherfence-chance-base", 0.1);
node.set("netherfence-chance-per-level", 0.002);
node.set("netherrack-chance-base", 0.1);
node.set("netherrack-chance-per-level", 0.002);
node.set("obsidian-chance-base", 0.1);
node.set("obsidian-chance-per-level", 0.002);
node.set("sandstone-chance-base", 0.1);
node.set("sandstone-chance-per-level", 0.002);
node.set("stone-chance-base", 0.1);
node.set("stone-chance-per-level", 0.002);
return node;
}
@Override
public String getDescription(Hero hero) {
double chanceCoal = getChance(hero, Material.COAL_ORE + "");
double chanceIron = getChance(hero, Material.IRON_ORE + "");
double chanceGold = getChance(hero, Material.GOLD_ORE + "");
double chanceRestone = getChance(hero, Material.REDSTONE_ORE + "");
double chanceCobble = getChance(hero, Material.COBBLESTONE + "");
double chanceEndstone = getChance(hero, Material.ENDER_STONE + "");
double chanceMossstone = getChance(hero, Material.MOSSY_COBBLESTONE
+ "");
double chanceNetherbrick = getChance(hero, Material.NETHER_BRICK + "");
double chanceNetherbrickfence = getChance(hero, Material.NETHER_FENCE
+ "");
double chanceNetherrack = getChance(hero, Material.NETHERRACK + "");
double chanceObsidian = getChance(hero, Material.OBSIDIAN + "");
double chanceSandstone = getChance(hero, Material.SANDSTONE + "");
double chanceStone = getChance(hero, Material.STONE + "");
return getDescription();
}
private double getChance(Hero hero, String key) {
return SkillConfigManager.getUseSetting(hero, this, key
+ "-chance-base", 0.1, false)
+ SkillConfigManager.getUseSetting(hero, this, key
+ "-chance-per-level", 0.002, false)
* hero.getSkillLevel(this);
}
private Material roll(Hero hero, Material material) {
if (Util.nextRand() < getChance(hero, material + "")) {
if (material == Material.REDSTONE_ORE) {
return Material.REDSTONE_WIRE;
}
return material;
}
return null;
}
public class SkillBlockListener implements Listener {
@SuppressWarnings("synthetic-access")
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockBreak(BlockBreakEvent event) {
if (!SkillMiningDoubleDrop.this.miningDoubleDrop.contains(event
.getBlock().getType())) {
return; // if no double drop material
}
Hero hero = SkillMiningDoubleDrop.this.plugin.getCharacterManager()
.getHero(event.getPlayer());
if (!hero.hasEffect(getName())) {
return; // if hero has not these skill
}
Block block = event.getBlock();
if (HBlockListener.placedBlocks.containsKey(block.getLocation())) {
return; // recently placed block
}
Material extraMaterial = roll(hero, event.getBlock().getType());
if (extraMaterial != null) {
block.getWorld().dropItemNaturally(block.getLocation(),
new ItemStack(extraMaterial, 2));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment