mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2025-06-27 19:24:44 +02:00
Refactor ALL THE THINGS!
This commit is contained in:
119
src/main/java/com/gmail/nossr50/util/BlockChecks.java
Normal file
119
src/main/java/com/gmail/nossr50/util/BlockChecks.java
Normal file
@ -0,0 +1,119 @@
|
||||
package com.gmail.nossr50.util;
|
||||
|
||||
import org.bukkit.Material;
|
||||
|
||||
import com.gmail.nossr50.config.Config;
|
||||
|
||||
public class BlockChecks {
|
||||
|
||||
/**
|
||||
* Checks to see if a block type awards XP.
|
||||
*
|
||||
* @param material The type of Block to check
|
||||
* @return true if the block type awards XP, false otherwise
|
||||
*/
|
||||
public static boolean shouldBeWatched(Material material) {
|
||||
switch (material) {
|
||||
case BROWN_MUSHROOM:
|
||||
case CACTUS:
|
||||
case CLAY:
|
||||
case COAL_ORE:
|
||||
case DIAMOND_ORE:
|
||||
case DIRT:
|
||||
case ENDER_STONE:
|
||||
case GLOWING_REDSTONE_ORE:
|
||||
case GLOWSTONE:
|
||||
case GOLD_ORE:
|
||||
case GRASS:
|
||||
case GRAVEL:
|
||||
case IRON_ORE:
|
||||
case JACK_O_LANTERN:
|
||||
case LAPIS_ORE:
|
||||
case LOG:
|
||||
case MELON_BLOCK:
|
||||
case MOSSY_COBBLESTONE:
|
||||
case MYCEL:
|
||||
case NETHERRACK:
|
||||
case OBSIDIAN:
|
||||
case PUMPKIN:
|
||||
case RED_MUSHROOM:
|
||||
case RED_ROSE:
|
||||
case REDSTONE_ORE:
|
||||
case SAND:
|
||||
case SANDSTONE:
|
||||
case SOUL_SAND:
|
||||
case STONE:
|
||||
case SUGAR_CANE_BLOCK:
|
||||
case VINE:
|
||||
case WATER_LILY:
|
||||
case YELLOW_FLOWER:
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a block should allow for the activation of abilities.
|
||||
*
|
||||
* @param material The type of Block to check
|
||||
* @return true if the block should allow ability activation, false otherwise
|
||||
*/
|
||||
public static boolean abilityBlockCheck(Material material) {
|
||||
switch (material) {
|
||||
case BED_BLOCK:
|
||||
case BREWING_STAND:
|
||||
case BOOKSHELF:
|
||||
case BURNING_FURNACE:
|
||||
case CAKE_BLOCK:
|
||||
case CHEST:
|
||||
case DISPENSER:
|
||||
case ENCHANTMENT_TABLE:
|
||||
case FENCE_GATE:
|
||||
case FURNACE:
|
||||
case IRON_DOOR_BLOCK:
|
||||
case JUKEBOX:
|
||||
case LEVER:
|
||||
case NOTE_BLOCK:
|
||||
case STONE_BUTTON:
|
||||
case TRAP_DOOR:
|
||||
case WALL_SIGN:
|
||||
case WOODEN_DOOR:
|
||||
case WORKBENCH:
|
||||
return false;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (Material.getMaterial(Config.getInstance().getRepairAnvilId()).equals(material)) {
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a block type is an ore.
|
||||
*
|
||||
* @param material The type of Block to check
|
||||
* @return true if the Block is an ore, false otherwise
|
||||
*/
|
||||
public static boolean isOre(Material material) {
|
||||
switch (material) {
|
||||
case COAL_ORE:
|
||||
case DIAMOND_ORE:
|
||||
case GLOWING_REDSTONE_ORE:
|
||||
case GOLD_ORE:
|
||||
case IRON_ORE:
|
||||
case LAPIS_ORE:
|
||||
case REDSTONE_ORE:
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
487
src/main/java/com/gmail/nossr50/util/Combat.java
Normal file
487
src/main/java/com/gmail/nossr50/util/Combat.java
Normal file
@ -0,0 +1,487 @@
|
||||
package com.gmail.nossr50.util;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.AnimalTamer;
|
||||
import org.bukkit.entity.Animals;
|
||||
import org.bukkit.entity.Arrow;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.IronGolem;
|
||||
import org.bukkit.entity.LivingEntity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.entity.Wolf;
|
||||
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
||||
import org.bukkit.event.entity.EntityDamageEvent;
|
||||
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import com.gmail.nossr50.mcMMO;
|
||||
import com.gmail.nossr50.config.Config;
|
||||
import com.gmail.nossr50.datatypes.AbilityType;
|
||||
import com.gmail.nossr50.datatypes.PlayerProfile;
|
||||
import com.gmail.nossr50.datatypes.SkillType;
|
||||
import com.gmail.nossr50.datatypes.ToolType;
|
||||
import com.gmail.nossr50.events.fake.FakeEntityDamageByEntityEvent;
|
||||
import com.gmail.nossr50.events.fake.FakeEntityDamageEvent;
|
||||
import com.gmail.nossr50.locale.LocaleLoader;
|
||||
import com.gmail.nossr50.party.Party;
|
||||
import com.gmail.nossr50.runnables.GainXp;
|
||||
import com.gmail.nossr50.runnables.BleedTimer;
|
||||
import com.gmail.nossr50.skills.Acrobatics;
|
||||
import com.gmail.nossr50.skills.Archery;
|
||||
import com.gmail.nossr50.skills.Axes;
|
||||
import com.gmail.nossr50.skills.Skills;
|
||||
import com.gmail.nossr50.skills.Swords;
|
||||
import com.gmail.nossr50.skills.Taming;
|
||||
import com.gmail.nossr50.skills.Unarmed;
|
||||
|
||||
public class Combat {
|
||||
|
||||
/**
|
||||
* Apply combat modifiers and process and XP gain.
|
||||
*
|
||||
* @param event The event to run the combat checks on.
|
||||
* @param plugin mcMMO plugin instance
|
||||
*/
|
||||
public static void combatChecks(EntityDamageByEntityEvent event, mcMMO plugin) {
|
||||
if (event.getDamage() == 0 || event.getEntity().isDead()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Entity damager = event.getDamager();
|
||||
LivingEntity target = (LivingEntity) event.getEntity();
|
||||
EntityType damagerType = damager.getType();
|
||||
EntityType targetType = target.getType();
|
||||
|
||||
switch (damagerType) {
|
||||
case PLAYER:
|
||||
Player attacker = (Player) event.getDamager();
|
||||
ItemStack itemInHand = attacker.getItemInHand();
|
||||
PlayerProfile PPa = Users.getProfile(attacker);
|
||||
|
||||
combatAbilityChecks(attacker);
|
||||
|
||||
if (ItemChecks.isSword(itemInHand) && Permissions.getInstance().swords(attacker)) {
|
||||
if (!BleedTimer.contains(target) && Permissions.getInstance().swordsBleed(attacker)) {
|
||||
Swords.bleedCheck(attacker, target, plugin);
|
||||
}
|
||||
|
||||
if (PPa.getAbilityMode(AbilityType.SERRATED_STRIKES) && Permissions.getInstance().serratedStrikes(attacker)) {
|
||||
applyAbilityAoE(attacker, target, event.getDamage(), plugin, SkillType.SWORDS);
|
||||
}
|
||||
|
||||
startGainXp(attacker, PPa, target, SkillType.SWORDS, plugin);
|
||||
}
|
||||
else if (ItemChecks.isAxe(itemInHand) && Permissions.getInstance().axes(attacker)) {
|
||||
if (Permissions.getInstance().axeBonus(attacker)) {
|
||||
Axes.axesBonus(attacker, event);
|
||||
}
|
||||
|
||||
if (Permissions.getInstance().criticalHit(attacker)) {
|
||||
Axes.axeCriticalCheck(attacker, event);
|
||||
}
|
||||
|
||||
if (Permissions.getInstance().impact(attacker)) {
|
||||
Axes.impact(attacker, target, event);
|
||||
}
|
||||
|
||||
if (PPa.getAbilityMode(AbilityType.SKULL_SPLIITER) && Permissions.getInstance().skullSplitter(attacker)) {
|
||||
applyAbilityAoE(attacker, target, event.getDamage(), plugin, SkillType.AXES);
|
||||
}
|
||||
|
||||
startGainXp(attacker, PPa, target, SkillType.AXES, plugin);
|
||||
}
|
||||
else if (itemInHand.getType().equals(Material.AIR) && Permissions.getInstance().unarmed(attacker)) {
|
||||
if (Permissions.getInstance().unarmedBonus(attacker)) {
|
||||
Unarmed.unarmedBonus(PPa, event);
|
||||
}
|
||||
|
||||
if (PPa.getAbilityMode(AbilityType.BERSERK) && Permissions.getInstance().berserk(attacker)) {
|
||||
event.setDamage((int) (event.getDamage() * 1.5));
|
||||
}
|
||||
|
||||
if (targetType.equals(EntityType.PLAYER) && Permissions.getInstance().disarm(attacker)) {
|
||||
Unarmed.disarmProcCheck(attacker, (Player) target);
|
||||
}
|
||||
|
||||
startGainXp(attacker, PPa, target, SkillType.UNARMED, plugin);
|
||||
}
|
||||
else if (itemInHand.getType().equals(Material.BONE) && Permissions.getInstance().beastLore(attacker)) {
|
||||
Taming.beastLore(event, target, attacker);
|
||||
}
|
||||
break;
|
||||
|
||||
case WOLF:
|
||||
Wolf wolf = (Wolf) damager;
|
||||
|
||||
if (wolf.isTamed() && wolf.getOwner() instanceof Player) {
|
||||
Player master = (Player) wolf.getOwner();
|
||||
PlayerProfile PPo = Users.getProfile(master);
|
||||
|
||||
if (Permissions.getInstance().taming(master)) {
|
||||
if (Permissions.getInstance().fastFoodService(master)) {
|
||||
Taming.fastFoodService(PPo, wolf, event);
|
||||
}
|
||||
|
||||
if (Permissions.getInstance().sharpenedclaws(master)) {
|
||||
Taming.sharpenedClaws(PPo, event);
|
||||
}
|
||||
|
||||
if (Permissions.getInstance().gore(master)) {
|
||||
Taming.gore(PPo, event, master, plugin);
|
||||
}
|
||||
|
||||
startGainXp(master, PPo, target, SkillType.TAMING, plugin);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case ARROW:
|
||||
archeryCheck((EntityDamageByEntityEvent) event, plugin);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (targetType.equals(EntityType.PLAYER)) {
|
||||
Swords.counterAttackChecks(event);
|
||||
Acrobatics.dodgeChecks(event);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Process combat abilities based on weapon preparation modes.
|
||||
*
|
||||
* @param attacker The player attacking
|
||||
*/
|
||||
public static void combatAbilityChecks(Player attacker) {
|
||||
PlayerProfile PPa = Users.getProfile(attacker);
|
||||
|
||||
if (PPa.getToolPreparationMode(ToolType.AXE)) {
|
||||
Skills.abilityCheck(attacker, SkillType.AXES);
|
||||
}
|
||||
else if (PPa.getToolPreparationMode(ToolType.SWORD)) {
|
||||
Skills.abilityCheck(attacker, SkillType.SWORDS);
|
||||
}
|
||||
else if (PPa.getToolPreparationMode(ToolType.FISTS)) {
|
||||
Skills.abilityCheck(attacker, SkillType.UNARMED);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Process archery abilities.
|
||||
*
|
||||
* @param event The event to run the archery checks on.
|
||||
* @param pluginx mcMMO plugin instance
|
||||
*/
|
||||
public static void archeryCheck(EntityDamageByEntityEvent event, mcMMO pluginx) {
|
||||
Arrow arrow = (Arrow) event.getDamager();
|
||||
LivingEntity shooter = arrow.getShooter();
|
||||
LivingEntity target = (LivingEntity) event.getEntity();
|
||||
|
||||
if (target instanceof Player) {
|
||||
Player defender = (Player) target;
|
||||
|
||||
if (Permissions.getInstance().unarmed(defender) && defender.getItemInHand().getType().equals(Material.AIR)) {
|
||||
Unarmed.deflectCheck(defender, event);
|
||||
}
|
||||
}
|
||||
|
||||
if (shooter instanceof Player) {
|
||||
Player attacker = (Player) shooter;
|
||||
PlayerProfile PPa = Users.getProfile(attacker);
|
||||
int damage = event.getDamage();
|
||||
|
||||
if (Permissions.getInstance().archery(attacker) && damage > 0) {
|
||||
|
||||
/*Archery needs a damage bonus to be viable in PVP*/
|
||||
int skillLvl = Users.getProfile(attacker).getSkillLevel(SkillType.ARCHERY);
|
||||
double dmgBonusPercent = ((skillLvl / 50) * 0.1D);
|
||||
|
||||
/* Cap maximum bonus at 200% */
|
||||
if(dmgBonusPercent > 2)
|
||||
dmgBonusPercent = 2;
|
||||
|
||||
/* Every 100 skill levels Archery gains 20% damage bonus, set that here */
|
||||
//TODO: Work in progress for balancing out Archery, will work on it more later...
|
||||
//TODO: Right now this is calculating a 10% bonus every 50 levels, not 20% every 100. Is this intended?
|
||||
int archeryBonus = (int)(event.getDamage() * dmgBonusPercent);
|
||||
event.setDamage(event.getDamage() + archeryBonus);
|
||||
|
||||
if (Permissions.getInstance().trackArrows(attacker)) {
|
||||
Archery.trackArrows(pluginx, target, PPa);
|
||||
}
|
||||
|
||||
startGainXp(attacker, PPa, target, SkillType.ARCHERY, pluginx);
|
||||
|
||||
if (target instanceof Player) {
|
||||
Player defender = (Player) target;
|
||||
PlayerProfile PPd = Users.getProfile(defender);
|
||||
|
||||
if (PPa.inParty() && PPd.inParty() && Party.getInstance().inSameParty(defender, attacker)) {
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
|
||||
Archery.dazeCheck(defender, attacker);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to damage target for value dmg with reason CUSTOM
|
||||
*
|
||||
* @param target LivingEntity which to attempt to damage
|
||||
* @param dmg Amount of damage to attempt to do
|
||||
*/
|
||||
public static void dealDamage(LivingEntity target, int dmg) {
|
||||
dealDamage(target, dmg, EntityDamageEvent.DamageCause.CUSTOM);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to damage target for value dmg with reason cause
|
||||
*
|
||||
* @param target LivingEntity which to attempt to damage
|
||||
* @param dmg Amount of damage to attempt to do
|
||||
* @param cause DamageCause to pass to damage event
|
||||
*/
|
||||
private static void dealDamage(LivingEntity target, int dmg, DamageCause cause) {
|
||||
if (Config.getInstance().getEventCallbackEnabled()) {
|
||||
EntityDamageEvent ede = (EntityDamageEvent) new FakeEntityDamageEvent(target, cause, dmg);
|
||||
mcMMO.p.getServer().getPluginManager().callEvent(ede);
|
||||
|
||||
if (ede.isCancelled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
target.damage(ede.getDamage());
|
||||
}
|
||||
else {
|
||||
target.damage(dmg);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to damage target for value dmg with reason ENTITY_ATTACK with damager attacker
|
||||
*
|
||||
* @param target LivingEntity which to attempt to damage
|
||||
* @param dmg Amount of damage to attempt to do
|
||||
* @param attacker Player to pass to event as damager
|
||||
*/
|
||||
private static void dealDamage(LivingEntity target, int dmg, Player attacker) {
|
||||
if (Config.getInstance().getEventCallbackEnabled()) {
|
||||
EntityDamageEvent ede = (EntityDamageByEntityEvent) new FakeEntityDamageByEntityEvent(attacker, target, EntityDamageEvent.DamageCause.ENTITY_ATTACK, dmg);
|
||||
mcMMO.p.getServer().getPluginManager().callEvent(ede);
|
||||
|
||||
if (ede.isCancelled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
target.damage(ede.getDamage());
|
||||
}
|
||||
else {
|
||||
target.damage(dmg);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply Area-of-Effect ability actions.
|
||||
*
|
||||
* @param attacker The attacking player
|
||||
* @param target The defending entity
|
||||
* @param damage The initial damage amount
|
||||
* @param plugin mcMMO plugin instance
|
||||
* @param type The type of skill being used
|
||||
*/
|
||||
private static void applyAbilityAoE(Player attacker, LivingEntity target, int damage, mcMMO plugin, SkillType type) {
|
||||
int numberOfTargets = Misc.getTier(attacker.getItemInHand()); //The higher the weapon tier, the more targets you hit
|
||||
int damageAmount = 0;
|
||||
|
||||
if (type.equals(SkillType.AXES)) {
|
||||
damageAmount = damage / 2;
|
||||
}
|
||||
else if (type.equals(SkillType.SWORDS)) {
|
||||
damageAmount = damage / 4;
|
||||
}
|
||||
|
||||
if (damageAmount < 1) {
|
||||
damageAmount = 1;
|
||||
}
|
||||
|
||||
for (Entity entity : target.getNearbyEntities(2.5, 2.5, 2.5)) {
|
||||
EntityType entityType = entity.getType();
|
||||
|
||||
if (entityType.equals(EntityType.WOLF)) {
|
||||
Wolf wolf = (Wolf) entity;
|
||||
AnimalTamer tamer = wolf.getOwner();
|
||||
|
||||
if (tamer instanceof Player) {
|
||||
Player owner = (Player) tamer;
|
||||
|
||||
if (owner.equals(attacker) || Party.getInstance().inSameParty(attacker, owner)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (entity instanceof LivingEntity && numberOfTargets >= 1) {
|
||||
if (entityType.equals(EntityType.PLAYER)) {
|
||||
Player defender = (Player) entity;
|
||||
PlayerProfile PP = Users.getProfile(defender);
|
||||
|
||||
//Reasons why the target shouldn't be hit
|
||||
if (PP.getGodMode()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (defender.getName().equals(attacker.getName())) { //Is this even possible?
|
||||
continue;
|
||||
}
|
||||
|
||||
if (Party.getInstance().inSameParty(attacker, defender)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (defender.isDead()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
//Apply effect to players only if PVP is enabled
|
||||
if (target.getWorld().getPVP()) {
|
||||
String message = "";
|
||||
|
||||
if (type.equals(SkillType.AXES)) {
|
||||
message = LocaleLoader.getString("Axes.Combat.Cleave.Struck");
|
||||
}
|
||||
else if (type.equals(SkillType.SWORDS)) {
|
||||
message = LocaleLoader.getString("Swords.Combat.SS.Struck");
|
||||
}
|
||||
|
||||
dealDamage(defender, damageAmount, attacker);
|
||||
defender.sendMessage(message);
|
||||
|
||||
if (type.equals(SkillType.SWORDS)) {
|
||||
PP.addBleedTicks(5);
|
||||
}
|
||||
|
||||
numberOfTargets--;
|
||||
}
|
||||
}
|
||||
else {
|
||||
LivingEntity livingEntity = (LivingEntity) entity;
|
||||
|
||||
if (type.equals(SkillType.SWORDS)) {
|
||||
BleedTimer.add(livingEntity);
|
||||
}
|
||||
|
||||
dealDamage(livingEntity, damageAmount, attacker);
|
||||
numberOfTargets--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the task that gives combat XP.
|
||||
*
|
||||
* @param attacker The attacking player
|
||||
* @param PP The player's PlayerProfile
|
||||
* @param target The defending entity
|
||||
* @param skillType The skill being used
|
||||
* @param plugin mcMMO plugin instance
|
||||
*/
|
||||
public static void startGainXp(Player attacker, PlayerProfile PP, LivingEntity target, SkillType skillType, mcMMO pluginx) {
|
||||
double baseXP = 0;
|
||||
|
||||
if (target instanceof Player) {
|
||||
if (!Config.getInstance().getExperienceGainsPlayerVersusPlayerEnabled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Player defender = (Player) target;
|
||||
PlayerProfile PPd = Users.getProfile(defender);
|
||||
|
||||
if (System.currentTimeMillis() >= (PPd.getRespawnATS() * 1000) + 5000 && ((PPd.getLastLogin() + 5) * 1000) < System.currentTimeMillis() && defender.getHealth() >= 1) {
|
||||
baseXP = 20 * Config.getInstance().getPlayerVersusPlayerXP();
|
||||
}
|
||||
}
|
||||
else if (!target.hasMetadata("mcmmoFromMobSpawner")) {
|
||||
if (target instanceof Animals && !target.hasMetadata("mcmmoSummoned")) {
|
||||
baseXP = Config.getInstance().getAnimalsXP();
|
||||
}
|
||||
else {
|
||||
EntityType type = target.getType();
|
||||
|
||||
switch (type) {
|
||||
case BLAZE:
|
||||
baseXP = Config.getInstance().getBlazeXP();
|
||||
break;
|
||||
|
||||
case CAVE_SPIDER:
|
||||
baseXP = Config.getInstance().getCaveSpiderXP();
|
||||
break;
|
||||
|
||||
case CREEPER:
|
||||
baseXP = Config.getInstance().getCreeperXP();
|
||||
break;
|
||||
|
||||
case ENDER_DRAGON:
|
||||
baseXP = Config.getInstance().getEnderDragonXP();
|
||||
break;
|
||||
|
||||
case ENDERMAN:
|
||||
baseXP = Config.getInstance().getEndermanXP();
|
||||
break;
|
||||
|
||||
case GHAST:
|
||||
baseXP = Config.getInstance().getGhastXP();
|
||||
break;
|
||||
|
||||
case MAGMA_CUBE:
|
||||
baseXP = Config.getInstance().getMagmaCubeXP();
|
||||
break;
|
||||
|
||||
case IRON_GOLEM:
|
||||
if (!((IronGolem) target).isPlayerCreated())
|
||||
baseXP = Config.getInstance().getIronGolemXP();
|
||||
break;
|
||||
|
||||
case PIG_ZOMBIE:
|
||||
baseXP = Config.getInstance().getPigZombieXP();
|
||||
break;
|
||||
|
||||
case SILVERFISH:
|
||||
baseXP = Config.getInstance().getSilverfishXP();
|
||||
break;
|
||||
|
||||
case SKELETON:
|
||||
baseXP = Config.getInstance().getSkeletonXP();
|
||||
break;
|
||||
|
||||
case SLIME:
|
||||
baseXP = Config.getInstance().getSlimeXP();
|
||||
break;
|
||||
|
||||
case SPIDER:
|
||||
baseXP = Config.getInstance().getSpiderXP();
|
||||
break;
|
||||
|
||||
case ZOMBIE:
|
||||
baseXP = Config.getInstance().getZombieXP();
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
baseXP *= 10;
|
||||
}
|
||||
|
||||
if (baseXP != 0) {
|
||||
mcMMO.p.getServer().getScheduler().scheduleSyncDelayedTask(pluginx, new GainXp(attacker, PP, skillType, baseXP, target), 0);
|
||||
}
|
||||
}
|
||||
}
|
295
src/main/java/com/gmail/nossr50/util/Database.java
Normal file
295
src/main/java/com/gmail/nossr50/util/Database.java
Normal file
@ -0,0 +1,295 @@
|
||||
package com.gmail.nossr50.util;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.util.HashMap;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Properties;
|
||||
|
||||
import com.gmail.nossr50.mcMMO;
|
||||
import com.gmail.nossr50.config.Config;
|
||||
import com.gmail.nossr50.datatypes.DatabaseUpdate;
|
||||
import com.gmail.nossr50.runnables.SQLReconnect;
|
||||
|
||||
public class Database {
|
||||
|
||||
private static String connectionString = "jdbc:mysql://" + Config.getInstance().getMySQLServerName() + ":" + Config.getInstance().getMySQLServerPort() + "/" + Config.getInstance().getMySQLDatabaseName() + "?user=" + Config.getInstance().getMySQLUserName() + "&password=" + Config.getInstance().getMySQLUserPassword();
|
||||
private static String tablePrefix = Config.getInstance().getMySQLTablePrefix();
|
||||
private static Connection conn = null;
|
||||
private static mcMMO plugin = null;
|
||||
private static long reconnectTimestamp = 0;
|
||||
|
||||
public Database(mcMMO instance) {
|
||||
plugin = instance;
|
||||
connect(); //Connect to MySQL
|
||||
|
||||
// Load the driver instance
|
||||
try {
|
||||
Class.forName("com.mysql.jdbc.Driver");
|
||||
DriverManager.getConnection(connectionString);
|
||||
}
|
||||
catch (ClassNotFoundException e) {
|
||||
plugin.getLogger().warning(e.getLocalizedMessage());
|
||||
}
|
||||
catch (SQLException ex) {
|
||||
plugin.getLogger().warning(ex.getLocalizedMessage());
|
||||
printErrors(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to connect to the mySQL database.
|
||||
*/
|
||||
public static void connect() {
|
||||
try {
|
||||
System.out.println("[mcMMO] Attempting connection to MySQL...");
|
||||
Properties conProperties = new Properties();
|
||||
conProperties.put("autoReconnect", "false");
|
||||
conProperties.put("maxReconnects", "0");
|
||||
conn = DriverManager.getConnection(connectionString, conProperties);
|
||||
System.out.println("[mcMMO] Connection to MySQL was a success!");
|
||||
}
|
||||
catch (SQLException ex) {
|
||||
System.out.println("[mcMMO] Connection to MySQL failed!");
|
||||
ex.printStackTrace();
|
||||
printErrors(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to create the database structure.
|
||||
*/
|
||||
public void createStructure() {
|
||||
write("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "huds` (`user_id` int(10) unsigned NOT NULL,"
|
||||
+ "`hudtype` varchar(50) NOT NULL DEFAULT '',"
|
||||
+ "PRIMARY KEY (`user_id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;");
|
||||
write("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "users` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT,"
|
||||
+ "`user` varchar(40) NOT NULL,"
|
||||
+ "`lastlogin` int(32) unsigned NOT NULL,"
|
||||
+ "`party` varchar(100) NOT NULL DEFAULT '',"
|
||||
+ "PRIMARY KEY (`id`),"
|
||||
+ "UNIQUE KEY `user` (`user`)) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;");
|
||||
write("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "cooldowns` (`user_id` int(10) unsigned NOT NULL,"
|
||||
+ "`taming` int(32) unsigned NOT NULL DEFAULT '0',"
|
||||
+ "`mining` int(32) unsigned NOT NULL DEFAULT '0',"
|
||||
+ "`woodcutting` int(32) unsigned NOT NULL DEFAULT '0',"
|
||||
+ "`repair` int(32) unsigned NOT NULL DEFAULT '0',"
|
||||
+ "`unarmed` int(32) unsigned NOT NULL DEFAULT '0',"
|
||||
+ "`herbalism` int(32) unsigned NOT NULL DEFAULT '0',"
|
||||
+ "`excavation` int(32) unsigned NOT NULL DEFAULT '0',"
|
||||
+ "`archery` int(32) unsigned NOT NULL DEFAULT '0',"
|
||||
+ "`swords` int(32) unsigned NOT NULL DEFAULT '0',"
|
||||
+ "`axes` int(32) unsigned NOT NULL DEFAULT '0',"
|
||||
+ "`acrobatics` int(32) unsigned NOT NULL DEFAULT '0',"
|
||||
+ "`blast_mining` int(32) unsigned NOT NULL DEFAULT '0',"
|
||||
+ "PRIMARY KEY (`user_id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;");
|
||||
write("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "skills` (`user_id` int(10) unsigned NOT NULL,"
|
||||
+ "`taming` int(10) unsigned NOT NULL DEFAULT '0',"
|
||||
+ "`mining` int(10) unsigned NOT NULL DEFAULT '0',"
|
||||
+ "`woodcutting` int(10) unsigned NOT NULL DEFAULT '0',"
|
||||
+ "`repair` int(10) unsigned NOT NULL DEFAULT '0',"
|
||||
+ "`unarmed` int(10) unsigned NOT NULL DEFAULT '0',"
|
||||
+ "`herbalism` int(10) unsigned NOT NULL DEFAULT '0',"
|
||||
+ "`excavation` int(10) unsigned NOT NULL DEFAULT '0',"
|
||||
+ "`archery` int(10) unsigned NOT NULL DEFAULT '0',"
|
||||
+ "`swords` int(10) unsigned NOT NULL DEFAULT '0',"
|
||||
+ "`axes` int(10) unsigned NOT NULL DEFAULT '0',"
|
||||
+ "`acrobatics` int(10) unsigned NOT NULL DEFAULT '0',"
|
||||
+ "PRIMARY KEY (`user_id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;");
|
||||
write("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "experience` (`user_id` int(10) unsigned NOT NULL,"
|
||||
+ "`taming` int(10) unsigned NOT NULL DEFAULT '0',"
|
||||
+ "`mining` int(10) unsigned NOT NULL DEFAULT '0',"
|
||||
+ "`woodcutting` int(10) unsigned NOT NULL DEFAULT '0',"
|
||||
+ "`repair` int(10) unsigned NOT NULL DEFAULT '0',"
|
||||
+ "`unarmed` int(10) unsigned NOT NULL DEFAULT '0',"
|
||||
+ "`herbalism` int(10) unsigned NOT NULL DEFAULT '0',"
|
||||
+ "`excavation` int(10) unsigned NOT NULL DEFAULT '0',"
|
||||
+ "`archery` int(10) unsigned NOT NULL DEFAULT '0',"
|
||||
+ "`swords` int(10) unsigned NOT NULL DEFAULT '0',"
|
||||
+ "`axes` int(10) unsigned NOT NULL DEFAULT '0',"
|
||||
+ "`acrobatics` int(10) unsigned NOT NULL DEFAULT '0',"
|
||||
+ "PRIMARY KEY (`user_id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;");
|
||||
|
||||
checkDatabaseStructure(DatabaseUpdate.FISHING);
|
||||
checkDatabaseStructure(DatabaseUpdate.BLAST_MINING);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check database structure for missing values.
|
||||
*
|
||||
* @param update Type of data to check updates for
|
||||
*/
|
||||
public void checkDatabaseStructure(DatabaseUpdate update) {
|
||||
String sql = null;
|
||||
ResultSet rs = null;
|
||||
HashMap<Integer, ArrayList<String>> Rows = new HashMap<Integer, ArrayList<String>>();
|
||||
|
||||
switch (update) {
|
||||
case BLAST_MINING:
|
||||
sql = "SELECT * FROM `"+tablePrefix+"cooldowns` ORDER BY `"+tablePrefix+"cooldowns`.`blast_mining` ASC LIMIT 0 , 30";
|
||||
break;
|
||||
case FISHING:
|
||||
sql = "SELECT * FROM `"+tablePrefix+"experience` ORDER BY `"+tablePrefix+"experience`.`fishing` ASC LIMIT 0 , 30";
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
try {
|
||||
PreparedStatement stmt = conn.prepareStatement(sql);
|
||||
if (stmt.executeQuery() != null) {
|
||||
stmt.executeQuery();
|
||||
rs = stmt.getResultSet();
|
||||
while (rs.next()) {
|
||||
ArrayList<String> Col = new ArrayList<String>();
|
||||
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
|
||||
Col.add(rs.getString(i));
|
||||
}
|
||||
Rows.put(rs.getRow(), Col);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (SQLException ex) {
|
||||
if (update.equals(DatabaseUpdate.BLAST_MINING)) {
|
||||
System.out.println("Updating mcMMO MySQL tables for Blast Mining...");
|
||||
write("ALTER TABLE `"+tablePrefix + "cooldowns` ADD `blast_mining` int(32) NOT NULL DEFAULT '0' ;");
|
||||
}
|
||||
else if (update.equals(DatabaseUpdate.FISHING)) {
|
||||
System.out.println("Updating mcMMO MySQL tables for Fishing...");
|
||||
write("ALTER TABLE `"+tablePrefix + "skills` ADD `fishing` int(10) NOT NULL DEFAULT '0' ;");
|
||||
write("ALTER TABLE `"+tablePrefix + "experience` ADD `fishing` int(10) NOT NULL DEFAULT '0' ;");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to write the SQL query.
|
||||
*
|
||||
* @param sql Query to write.
|
||||
* @return true if the query was successfully written, false otherwise.
|
||||
*/
|
||||
public boolean write(String sql) {
|
||||
if (isConnected()) {
|
||||
try {
|
||||
PreparedStatement stmt = conn.prepareStatement(sql);
|
||||
stmt.executeUpdate();
|
||||
return true;
|
||||
}
|
||||
catch (SQLException ex) {
|
||||
printErrors(ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
attemptReconnect();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Integer. Only return first row / first field.
|
||||
*
|
||||
* @param sql SQL query to execute
|
||||
* @return the value in the first row / first field
|
||||
*/
|
||||
public Integer getInt(String sql) {
|
||||
ResultSet rs = null;
|
||||
Integer result = 0;
|
||||
|
||||
if (isConnected()) {
|
||||
try {
|
||||
PreparedStatement stmt = conn.prepareStatement(sql);
|
||||
stmt = conn.prepareStatement(sql);
|
||||
if (stmt.executeQuery() != null) {
|
||||
stmt.executeQuery();
|
||||
rs = stmt.getResultSet();
|
||||
if (rs.next()) {
|
||||
result = rs.getInt(1);
|
||||
}
|
||||
else {
|
||||
result = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (SQLException ex) {
|
||||
printErrors(ex);
|
||||
}
|
||||
}
|
||||
else {
|
||||
attemptReconnect();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get connection status
|
||||
*
|
||||
* @return the boolean value for whether or not we are connected
|
||||
*/
|
||||
public static boolean isConnected() {
|
||||
if(conn == null)
|
||||
return false;
|
||||
|
||||
try {
|
||||
return conn.isValid(3);
|
||||
} catch (SQLException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedules a Sync Delayed Task with the Bukkit Scheduler to attempt reconnection after a minute has elapsed
|
||||
* This will check for a connection being present or not to prevent unneeded reconnection attempts
|
||||
*/
|
||||
public static void attemptReconnect() {
|
||||
if(reconnectTimestamp + 60000 < System.currentTimeMillis()) {
|
||||
System.out.println("[mcMMO] Connection to MySQL was lost! Attempting to reconnect in 60 seconds..."); //Only reconnect if another attempt hasn't been made recently
|
||||
reconnectTimestamp = System.currentTimeMillis();
|
||||
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new SQLReconnect(plugin), 1200);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read SQL query.
|
||||
*
|
||||
* @param sql SQL query to read
|
||||
* @return the rows in this SQL query
|
||||
*/
|
||||
public HashMap<Integer, ArrayList<String>> read(String sql) {
|
||||
ResultSet rs = null;
|
||||
HashMap<Integer, ArrayList<String>> Rows = new HashMap<Integer, ArrayList<String>>();
|
||||
|
||||
if (isConnected()) {
|
||||
try {
|
||||
PreparedStatement stmt = conn.prepareStatement(sql);
|
||||
if (stmt.executeQuery() != null) {
|
||||
stmt.executeQuery();
|
||||
rs = stmt.getResultSet();
|
||||
while (rs.next()) {
|
||||
ArrayList<String> Col = new ArrayList<String>();
|
||||
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
|
||||
Col.add(rs.getString(i));
|
||||
}
|
||||
Rows.put(rs.getRow(), Col);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (SQLException ex) {
|
||||
printErrors(ex);
|
||||
}
|
||||
}
|
||||
else {
|
||||
attemptReconnect();
|
||||
}
|
||||
return Rows;
|
||||
}
|
||||
|
||||
private static void printErrors(SQLException ex) {
|
||||
System.out.println("SQLException: " + ex.getMessage());
|
||||
System.out.println("SQLState: " + ex.getSQLState());
|
||||
System.out.println("VendorError: " + ex.getErrorCode());
|
||||
}
|
||||
}
|
64
src/main/java/com/gmail/nossr50/util/Item.java
Normal file
64
src/main/java/com/gmail/nossr50/util/Item.java
Normal file
@ -0,0 +1,64 @@
|
||||
package com.gmail.nossr50.util;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import com.gmail.nossr50.locale.LocaleLoader;
|
||||
import com.gmail.nossr50.skills.Skills;
|
||||
import com.gmail.nossr50.config.Config;
|
||||
import com.gmail.nossr50.datatypes.PlayerProfile;
|
||||
|
||||
public class Item {
|
||||
|
||||
/**
|
||||
* Check for item usage.
|
||||
*
|
||||
* @param player Player whose item usage to check
|
||||
*/
|
||||
public static void itemchecks(Player player) {
|
||||
ItemStack inhand = player.getItemInHand();
|
||||
|
||||
if (Config.getInstance().getChimaeraEnabled() && inhand.getTypeId() == Config.getInstance().getChimaeraItemId()) {
|
||||
chimaerawing(player);
|
||||
}
|
||||
}
|
||||
|
||||
private static void chimaerawing(Player player) {
|
||||
PlayerProfile PP = Users.getProfile(player);
|
||||
ItemStack is = player.getItemInHand();
|
||||
Block block = player.getLocation().getBlock();
|
||||
int amount = is.getAmount();
|
||||
|
||||
if (Permissions.getInstance().chimaeraWing(player) && is.getTypeId() == Config.getInstance().getChimaeraItemId()) {
|
||||
if (Skills.cooldownOver(PP.getRecentlyHurt(), 60) && amount >= Config.getInstance().getChimaeraCost()) {
|
||||
player.setItemInHand(new ItemStack(Config.getInstance().getChimaeraItemId(), amount - Config.getInstance().getChimaeraCost()));
|
||||
|
||||
for (int y = 0; block.getY() + y < player.getWorld().getMaxHeight(); y++) {
|
||||
if (!block.getRelative(0, y, 0).getType().equals(Material.AIR)) {
|
||||
player.sendMessage(LocaleLoader.getString("Item.ChimaeraWing.Fail"));
|
||||
player.teleport(block.getRelative(0, y - 1, 0).getLocation());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (player.getBedSpawnLocation() != null && player.getBedSpawnLocation().getBlock().getType().equals(Material.BED_BLOCK)) {
|
||||
player.teleport(player.getBedSpawnLocation());
|
||||
}
|
||||
else {
|
||||
player.teleport(player.getWorld().getSpawnLocation());
|
||||
}
|
||||
|
||||
player.sendMessage(LocaleLoader.getString("Item.ChimaeraWing.Pass"));
|
||||
}
|
||||
else if (!Skills.cooldownOver(PP.getRecentlyHurt(), 60) && is.getAmount() >= Config.getInstance().getChimaeraCost()) {
|
||||
player.sendMessage(LocaleLoader.getString("Item.Injured.Wait", new Object[] {Skills.calculateTimeLeft(PP.getRecentlyHurt(), 60)}));
|
||||
}
|
||||
else if (is.getAmount() <= Config.getInstance().getChimaeraCost()) {
|
||||
player.sendMessage(LocaleLoader.getString("Skills.NeedMore")+ " " + ChatColor.GRAY + Misc.prettyItemString(Config.getInstance().getChimaeraItemId()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
398
src/main/java/com/gmail/nossr50/util/ItemChecks.java
Normal file
398
src/main/java/com/gmail/nossr50/util/ItemChecks.java
Normal file
@ -0,0 +1,398 @@
|
||||
package com.gmail.nossr50.util;
|
||||
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
public class ItemChecks {
|
||||
|
||||
/**
|
||||
* Checks if the item is a sword.
|
||||
*
|
||||
* @param is Item to check
|
||||
* @return true if the item is a sword, false otherwise
|
||||
*/
|
||||
public static boolean isSword(ItemStack is) {
|
||||
switch (is.getType()) {
|
||||
case DIAMOND_SWORD:
|
||||
case GOLD_SWORD:
|
||||
case IRON_SWORD:
|
||||
case STONE_SWORD:
|
||||
case WOOD_SWORD:
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the item is a hoe.
|
||||
*
|
||||
* @param is Item to check
|
||||
* @return true if the item is a hoe, false otherwise
|
||||
*/
|
||||
public static boolean isHoe(ItemStack is) {
|
||||
switch (is.getType()) {
|
||||
case DIAMOND_HOE:
|
||||
case GOLD_HOE:
|
||||
case IRON_HOE:
|
||||
case STONE_HOE:
|
||||
case WOOD_HOE:
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the item is a shovel.
|
||||
*
|
||||
* @param is Item to check
|
||||
* @return true if the item is a shovel, false otherwise
|
||||
*/
|
||||
public static boolean isShovel(ItemStack is) {
|
||||
switch (is.getType()) {
|
||||
case DIAMOND_SPADE:
|
||||
case GOLD_SPADE:
|
||||
case IRON_SPADE:
|
||||
case STONE_SPADE:
|
||||
case WOOD_SPADE:
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the item is an axe.
|
||||
*
|
||||
* @param is Item to check
|
||||
* @return true if the item is an axe, false otherwise
|
||||
*/
|
||||
public static boolean isAxe(ItemStack is) {
|
||||
switch (is.getType()) {
|
||||
case DIAMOND_AXE:
|
||||
case GOLD_AXE:
|
||||
case IRON_AXE:
|
||||
case STONE_AXE:
|
||||
case WOOD_AXE:
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the item is a pickaxe.
|
||||
*
|
||||
* @param is Item to check
|
||||
* @return true if the item is a pickaxe, false otherwise
|
||||
*/
|
||||
public static boolean isMiningPick(ItemStack is) {
|
||||
switch (is.getType()) {
|
||||
case DIAMOND_PICKAXE:
|
||||
case GOLD_PICKAXE:
|
||||
case IRON_PICKAXE:
|
||||
case STONE_PICKAXE:
|
||||
case WOOD_PICKAXE:
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the item is a helmet.
|
||||
*
|
||||
* @param is Item to check
|
||||
* @return true if the item is a helmet, false otherwise
|
||||
*/
|
||||
public static boolean isHelmet(ItemStack is) {
|
||||
switch (is.getType()) {
|
||||
case DIAMOND_HELMET:
|
||||
case GOLD_HELMET:
|
||||
case IRON_HELMET:
|
||||
case LEATHER_HELMET:
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the item is a chestplate.
|
||||
*
|
||||
* @param is Item to check
|
||||
* @return true if the item is a chestplate, false otherwise
|
||||
*/
|
||||
public static boolean isChestplate(ItemStack is) {
|
||||
switch (is.getType()) {
|
||||
case DIAMOND_CHESTPLATE:
|
||||
case GOLD_CHESTPLATE:
|
||||
case IRON_CHESTPLATE:
|
||||
case LEATHER_CHESTPLATE:
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the item is a pair of pants.
|
||||
*
|
||||
* @param is Item to check
|
||||
* @return true if the item is a pair of pants, false otherwise
|
||||
*/
|
||||
public static boolean isPants(ItemStack is) {
|
||||
switch (is.getType()) {
|
||||
case DIAMOND_LEGGINGS:
|
||||
case GOLD_LEGGINGS:
|
||||
case IRON_LEGGINGS:
|
||||
case LEATHER_LEGGINGS:
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the item is a pair of boots.
|
||||
*
|
||||
* @param is Item to check
|
||||
* @return true if the item is a pair of boots, false otherwise
|
||||
*/
|
||||
public static boolean isBoots(ItemStack is) {
|
||||
switch (is.getType()) {
|
||||
case DIAMOND_BOOTS:
|
||||
case GOLD_BOOTS:
|
||||
case IRON_BOOTS:
|
||||
case LEATHER_BOOTS:
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if an item is a wearable armor piece.
|
||||
*
|
||||
* @param is Item to check
|
||||
* @return true if the item is armor, false otherwise
|
||||
*/
|
||||
public static boolean isArmor(ItemStack is) {
|
||||
return isLeatherArmor(is) || isGoldArmor(is) || isIronArmor(is) || isDiamondArmor(is);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if an item is a leather armor piece.
|
||||
*
|
||||
* @param is Item to check
|
||||
* @return true if the item is leather armor, false otherwise
|
||||
*/
|
||||
public static boolean isLeatherArmor(ItemStack is) {
|
||||
switch (is.getType()) {
|
||||
case LEATHER_BOOTS:
|
||||
case LEATHER_CHESTPLATE:
|
||||
case LEATHER_HELMET:
|
||||
case LEATHER_LEGGINGS:
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if an item is a gold armor piece.
|
||||
*
|
||||
* @param is Item to check
|
||||
* @return true if the item is gold armor, false otherwise
|
||||
*/
|
||||
public static boolean isGoldArmor(ItemStack is) {
|
||||
switch (is.getType()) {
|
||||
case GOLD_BOOTS:
|
||||
case GOLD_CHESTPLATE:
|
||||
case GOLD_HELMET:
|
||||
case GOLD_LEGGINGS:
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if an item is an iron armor piece.
|
||||
*
|
||||
* @param is Item to check
|
||||
* @return true if the item is iron armor, false otherwise
|
||||
*/
|
||||
public static boolean isIronArmor(ItemStack is) {
|
||||
switch (is.getType()) {
|
||||
case IRON_BOOTS:
|
||||
case IRON_CHESTPLATE:
|
||||
case IRON_HELMET:
|
||||
case IRON_LEGGINGS:
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if an item is a diamond armor piece.
|
||||
*
|
||||
* @param is Item to check
|
||||
* @return true if the item is diamond armor, false otherwise
|
||||
*/
|
||||
public static boolean isDiamondArmor(ItemStack is) {
|
||||
switch (is.getType()) {
|
||||
case DIAMOND_BOOTS:
|
||||
case DIAMOND_CHESTPLATE:
|
||||
case DIAMOND_HELMET:
|
||||
case DIAMOND_LEGGINGS:
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if an item is a tool.
|
||||
*
|
||||
* @param is Item to check
|
||||
* @return true if the item is a tool, false otherwise
|
||||
*/
|
||||
public static boolean isTool(ItemStack is) {
|
||||
return isStoneTool(is) || isWoodTool(is) || isGoldTool(is) || isIronTool(is) || isDiamondTool(is) || isStringTool(is);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if an item is a stone tool.
|
||||
*
|
||||
* @param is Item to check
|
||||
* @return true if the item is a stone tool, false otherwise
|
||||
*/
|
||||
public static boolean isStoneTool(ItemStack is) {
|
||||
switch (is.getType()) {
|
||||
case STONE_AXE:
|
||||
case STONE_HOE:
|
||||
case STONE_PICKAXE:
|
||||
case STONE_SPADE:
|
||||
case STONE_SWORD:
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if an item is a wooden tool.
|
||||
*
|
||||
* @param is Item to check
|
||||
* @return true if the item is a wooden tool, false otherwise
|
||||
*/
|
||||
public static boolean isWoodTool(ItemStack is) {
|
||||
switch (is.getType()) {
|
||||
case WOOD_AXE:
|
||||
case WOOD_HOE:
|
||||
case WOOD_PICKAXE:
|
||||
case WOOD_SPADE:
|
||||
case WOOD_SWORD:
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if an item is a string tool.
|
||||
*
|
||||
* @param is Item to check
|
||||
* @return true if the item is a string tool, false otherwise
|
||||
*/
|
||||
public static boolean isStringTool(ItemStack is) {
|
||||
switch (is.getType()) {
|
||||
case BOW:
|
||||
case FISHING_ROD:
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks to see if an item is a gold tool.
|
||||
*
|
||||
* @param is Item to check
|
||||
* @return true if the item is a stone tool, false otherwise
|
||||
*/
|
||||
public static boolean isGoldTool(ItemStack is) {
|
||||
switch (is.getType()) {
|
||||
case GOLD_AXE:
|
||||
case GOLD_HOE:
|
||||
case GOLD_PICKAXE:
|
||||
case GOLD_SPADE:
|
||||
case GOLD_SWORD:
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if an item is an iron tool.
|
||||
*
|
||||
* @param is Item to check
|
||||
* @return true if the item is an iron tool, false otherwise
|
||||
*/
|
||||
public static boolean isIronTool(ItemStack is) {
|
||||
switch (is.getType()) {
|
||||
case IRON_AXE:
|
||||
case IRON_HOE:
|
||||
case IRON_PICKAXE:
|
||||
case IRON_SPADE:
|
||||
case IRON_SWORD:
|
||||
case SHEARS:
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if an item is a diamond tool.
|
||||
*
|
||||
* @param is Item to check
|
||||
* @return true if the item is a diamond tool, false otherwise
|
||||
*/
|
||||
public static boolean isDiamondTool(ItemStack is) {
|
||||
switch (is.getType()) {
|
||||
case DIAMOND_AXE:
|
||||
case DIAMOND_HOE:
|
||||
case DIAMOND_PICKAXE:
|
||||
case DIAMOND_SPADE:
|
||||
case DIAMOND_SWORD:
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
301
src/main/java/com/gmail/nossr50/util/Leaderboard.java
Normal file
301
src/main/java/com/gmail/nossr50/util/Leaderboard.java
Normal file
@ -0,0 +1,301 @@
|
||||
package com.gmail.nossr50.util;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import com.gmail.nossr50.mcMMO;
|
||||
import com.gmail.nossr50.config.Config;
|
||||
import com.gmail.nossr50.datatypes.PlayerStat;
|
||||
import com.gmail.nossr50.datatypes.SkillType;
|
||||
import com.gmail.nossr50.datatypes.Tree;
|
||||
|
||||
public class Leaderboard {
|
||||
private static String leaderboardsDirectory = mcMMO.leaderboardDirectory;
|
||||
private static String location = mcMMO.usersFile;
|
||||
private static mcMMO plugin = mcMMO.p;
|
||||
|
||||
/**
|
||||
* Create the leaderboards.
|
||||
*/
|
||||
public static void makeLeaderboards() {
|
||||
//Make Trees
|
||||
Tree Mining = new Tree();
|
||||
Tree WoodCutting = new Tree();
|
||||
Tree Herbalism = new Tree();
|
||||
Tree Excavation = new Tree();
|
||||
Tree Acrobatics = new Tree();
|
||||
Tree Repair = new Tree();
|
||||
Tree Swords = new Tree();
|
||||
Tree Axes = new Tree();
|
||||
Tree Archery = new Tree();
|
||||
Tree Unarmed = new Tree();
|
||||
Tree Taming = new Tree();
|
||||
Tree Fishing = new Tree();
|
||||
Tree PowerLevel = new Tree();
|
||||
|
||||
//Add Data To Trees
|
||||
try {
|
||||
FileReader file = new FileReader(location);
|
||||
BufferedReader in = new BufferedReader(file);
|
||||
String line = "";
|
||||
ArrayList<String> players = new ArrayList<String>();
|
||||
|
||||
while ((line = in.readLine()) != null) {
|
||||
String[] character = line.split(":");
|
||||
String p = character[0];
|
||||
int powerLevel = 0;
|
||||
|
||||
//Prevent the same player from being added multiple times
|
||||
if (players.contains(p)) {
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
players.add(p);
|
||||
}
|
||||
|
||||
if (character.length > 1 && Misc.isInt(character[1])) {
|
||||
Mining.add(p, Integer.valueOf(character[1]));
|
||||
powerLevel += Integer.valueOf(character[1]);
|
||||
}
|
||||
|
||||
if (character.length > 5 && Misc.isInt(character[5])) {
|
||||
WoodCutting.add(p, Integer.valueOf(character[5]));
|
||||
powerLevel += Integer.valueOf(character[5]);
|
||||
}
|
||||
|
||||
if (character.length > 7 && Misc.isInt(character[7])) {
|
||||
Repair.add(p, Integer.valueOf(character[7]));
|
||||
powerLevel += Integer.valueOf(character[7]);
|
||||
}
|
||||
|
||||
if (character.length > 8 && Misc.isInt(character[8])) {
|
||||
Unarmed.add(p, Integer.valueOf(character[8]));
|
||||
powerLevel += Integer.valueOf(character[8]);
|
||||
}
|
||||
|
||||
if (character.length > 9 && Misc.isInt(character[9])) {
|
||||
Herbalism.add(p, Integer.valueOf(character[9]));
|
||||
powerLevel += Integer.valueOf(character[9]);
|
||||
}
|
||||
|
||||
if (character.length > 10 && Misc.isInt(character[10])) {
|
||||
Excavation.add(p, Integer.valueOf(character[10]));
|
||||
powerLevel += Integer.valueOf(character[10]);
|
||||
}
|
||||
|
||||
if (character.length > 11 && Misc.isInt(character[11])) {
|
||||
Archery.add(p, Integer.valueOf(character[11]));
|
||||
powerLevel += Integer.valueOf(character[11]);
|
||||
}
|
||||
|
||||
if (character.length > 12 && Misc.isInt(character[12])) {
|
||||
Swords.add(p, Integer.valueOf(character[12]));
|
||||
powerLevel += Integer.valueOf(character[12]);
|
||||
}
|
||||
|
||||
if (character.length > 13 && Misc.isInt(character[13])) {
|
||||
Axes.add(p, Integer.valueOf(character[13]));
|
||||
powerLevel += Integer.valueOf(character[13]);
|
||||
}
|
||||
|
||||
if (character.length > 14 && Misc.isInt(character[14])) {
|
||||
Acrobatics.add(p, Integer.valueOf(character[14]));
|
||||
powerLevel += Integer.valueOf(character[14]);
|
||||
}
|
||||
|
||||
if (character.length > 24 && Misc.isInt(character[24])) {
|
||||
Taming.add(p, Integer.valueOf(character[24]));
|
||||
powerLevel += Integer.valueOf(character[24]);
|
||||
}
|
||||
|
||||
if (character.length > 34 && Misc.isInt(character[34])) {
|
||||
Fishing.add(p, Integer.valueOf(character[34]));
|
||||
powerLevel += Integer.valueOf(character[34]);
|
||||
}
|
||||
|
||||
PowerLevel.add(p, powerLevel);
|
||||
}
|
||||
in.close();
|
||||
}
|
||||
catch (Exception e) {
|
||||
plugin.getLogger().severe(("Exception while reading " + location + " (Are you sure you formatted it correctly?)" + e.toString()));
|
||||
}
|
||||
|
||||
//Write the leader board files
|
||||
leaderWrite(Mining.inOrder(), SkillType.MINING);
|
||||
leaderWrite(WoodCutting.inOrder(), SkillType.WOODCUTTING);
|
||||
leaderWrite(Repair.inOrder(), SkillType.REPAIR);
|
||||
leaderWrite(Unarmed.inOrder(), SkillType.UNARMED);
|
||||
leaderWrite(Herbalism.inOrder(), SkillType.HERBALISM);
|
||||
leaderWrite(Excavation.inOrder(), SkillType.EXCAVATION);
|
||||
leaderWrite(Archery.inOrder(), SkillType.ARCHERY);
|
||||
leaderWrite(Swords.inOrder(), SkillType.SWORDS);
|
||||
leaderWrite(Axes.inOrder(), SkillType.AXES);
|
||||
leaderWrite(Acrobatics.inOrder(), SkillType.ACROBATICS);
|
||||
leaderWrite(Taming.inOrder(), SkillType.TAMING);
|
||||
leaderWrite(Fishing.inOrder(), SkillType.FISHING);
|
||||
leaderWrite(PowerLevel.inOrder(), SkillType.ALL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write to the leaderboards.
|
||||
*
|
||||
* @param ps Stats to write to the leaderboard
|
||||
* @param skillType Skill type to write the leaderboard of
|
||||
*/
|
||||
private static void leaderWrite(PlayerStat[] ps, SkillType skillType) {
|
||||
String theLocation = leaderboardsDirectory + skillType.toString().toLowerCase() + ".mcmmo";
|
||||
File theDir = new File(theLocation);
|
||||
|
||||
//CHECK IF THE FILE EXISTS
|
||||
if (!theDir.exists()) {
|
||||
FileWriter writer = null;
|
||||
|
||||
try {
|
||||
writer = new FileWriter(theLocation);
|
||||
}
|
||||
catch (Exception e) {
|
||||
plugin.getLogger().severe(("Exception while creating " + theLocation + e.toString()));
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
if (writer != null) {
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
plugin.getLogger().severe("Exception while closing writer for " + theLocation + e.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
try {
|
||||
FileReader file = new FileReader(theLocation);
|
||||
BufferedReader in = new BufferedReader(file);
|
||||
StringBuilder writer = new StringBuilder();
|
||||
|
||||
for (PlayerStat p : ps) {
|
||||
if (p.name.equals("$mcMMO_DummyInfo")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (p.statVal == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
writer.append(p.name + ":" + p.statVal);
|
||||
writer.append("\r\n");
|
||||
}
|
||||
|
||||
in.close();
|
||||
FileWriter out = new FileWriter(theLocation);
|
||||
out.write(writer.toString());
|
||||
out.close();
|
||||
}
|
||||
catch (Exception e) {
|
||||
plugin.getLogger().severe("Exception while writing to " + theLocation + " (Are you sure you formatted it correctly?)" + e.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve leaderboard info.
|
||||
*
|
||||
* @param skillName Skill to retrieve info on.
|
||||
* @param pagenumber Which page in the leaderboards to retrieve
|
||||
* @return the requested leaderboard information
|
||||
*/
|
||||
public static String[] retrieveInfo(String skillName, int pagenumber) {
|
||||
String theLocation = leaderboardsDirectory + skillName.toLowerCase() + ".mcmmo";
|
||||
|
||||
try {
|
||||
FileReader file = new FileReader(theLocation);
|
||||
BufferedReader in = new BufferedReader(file);
|
||||
int destination;
|
||||
|
||||
//How many lines to skip through
|
||||
if (pagenumber == 1) {
|
||||
destination = 0;
|
||||
}
|
||||
else {
|
||||
destination = (pagenumber * 10) - 9;
|
||||
}
|
||||
|
||||
int x = 0; //how many lines we've gone through
|
||||
int y = 0; //going through the lines
|
||||
String line = "";
|
||||
String[] info = new String[10]; //what to return
|
||||
|
||||
while ((line = in.readLine()) != null && y < 10) {
|
||||
x++;
|
||||
|
||||
if (x >= destination && y < 10) {
|
||||
info[y] = line.toString();
|
||||
y++;
|
||||
}
|
||||
}
|
||||
|
||||
in.close();
|
||||
return info;
|
||||
}
|
||||
catch (Exception e) {
|
||||
plugin.getLogger().severe("Exception while reading " + theLocation + " (Are you sure you formatted it correctly?)" + e.toString());
|
||||
}
|
||||
|
||||
return null; //Shouldn't get here
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the leaderboards.
|
||||
*
|
||||
* @param ps Stats to update the leaderboard with.
|
||||
* @param skillType Skill whose leaderboard is being updated.
|
||||
*/
|
||||
public static void updateLeaderboard(PlayerStat ps, SkillType skillType) {
|
||||
if (Config.getInstance().getUseMySQL()) {
|
||||
return;
|
||||
}
|
||||
|
||||
String theLocation = leaderboardsDirectory + skillType.toString().toLowerCase() + ".mcmmo";
|
||||
|
||||
try {
|
||||
FileReader file = new FileReader(theLocation);
|
||||
BufferedReader in = new BufferedReader(file);
|
||||
StringBuilder writer = new StringBuilder();
|
||||
String line = "";
|
||||
Boolean inserted = false;
|
||||
|
||||
while ((line = in.readLine()) != null) {
|
||||
|
||||
//Insert the player into the line before it finds a smaller one
|
||||
if (Integer.valueOf(line.split(":")[1]) < ps.statVal && !inserted) {
|
||||
writer.append(ps.name + ":" + ps.statVal).append("\r\n");
|
||||
inserted = true;
|
||||
}
|
||||
|
||||
//Write anything that isn't the player already in the file so we remove the duplicate
|
||||
if (!line.split(":")[0].equalsIgnoreCase(ps.name)) {
|
||||
writer.append(line).append("\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
if(!inserted) {
|
||||
writer.append(ps.name + ":" + ps.statVal).append("\r\n");
|
||||
}
|
||||
|
||||
in.close();
|
||||
|
||||
//Write the new file
|
||||
FileWriter out = new FileWriter(theLocation);
|
||||
out.write(writer.toString());
|
||||
out.close();
|
||||
}
|
||||
catch (Exception e) {
|
||||
plugin.getLogger().severe("Exception while writing to " + theLocation + " (Are you sure you formatted it correctly?)" + e.toString());
|
||||
}
|
||||
}
|
||||
}
|
599
src/main/java/com/gmail/nossr50/util/Metrics.java
Normal file
599
src/main/java/com/gmail/nossr50/util/Metrics.java
Normal file
@ -0,0 +1,599 @@
|
||||
package com.gmail.nossr50.util;
|
||||
/*
|
||||
* Copyright 2011 Tyler Blair. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are
|
||||
* permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* The views and conclusions contained in the software and documentation are those of the
|
||||
* authors and contributors and should not be interpreted as representing official policies,
|
||||
* either expressed or implied, of anybody else.
|
||||
*/
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.configuration.InvalidConfigurationException;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.PluginDescriptionFile;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.Proxy;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Level;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* The metrics class obtains data about a plugin and submits statistics about it to the metrics backend.
|
||||
* </p>
|
||||
* <p>
|
||||
* Public methods provided by this class:
|
||||
* </p>
|
||||
* <code>
|
||||
* Graph createGraph(String name); <br/>
|
||||
* void addCustomData(Metrics.Plotter plotter); <br/>
|
||||
* void start(); <br/>
|
||||
* </code>
|
||||
*/
|
||||
public class Metrics {
|
||||
|
||||
/**
|
||||
* The current revision number
|
||||
*/
|
||||
private final static int REVISION = 5;
|
||||
|
||||
/**
|
||||
* The base url of the metrics domain
|
||||
*/
|
||||
private static final String BASE_URL = "http://mcstats.org";
|
||||
|
||||
/**
|
||||
* The url used to report a server's status
|
||||
*/
|
||||
private static final String REPORT_URL = "/report/%s";
|
||||
|
||||
/**
|
||||
* The file where guid and opt out is stored in
|
||||
*/
|
||||
private static final String CONFIG_FILE = "plugins/PluginMetrics/config.yml";
|
||||
|
||||
/**
|
||||
* The separator to use for custom data. This MUST NOT change unless you are hosting your own
|
||||
* version of metrics and want to change it.
|
||||
*/
|
||||
private static final String CUSTOM_DATA_SEPARATOR = "~~";
|
||||
|
||||
/**
|
||||
* Interval of time to ping (in minutes)
|
||||
*/
|
||||
private static final int PING_INTERVAL = 10;
|
||||
|
||||
/**
|
||||
* The plugin this metrics submits for
|
||||
*/
|
||||
private final Plugin plugin;
|
||||
|
||||
/**
|
||||
* All of the custom graphs to submit to metrics
|
||||
*/
|
||||
private final Set<Graph> graphs = Collections.synchronizedSet(new HashSet<Graph>());
|
||||
|
||||
/**
|
||||
* The default graph, used for addCustomData when you don't want a specific graph
|
||||
*/
|
||||
private final Graph defaultGraph = new Graph("Default");
|
||||
|
||||
/**
|
||||
* The plugin configuration file
|
||||
*/
|
||||
private final YamlConfiguration configuration;
|
||||
|
||||
/**
|
||||
* The plugin configuration file
|
||||
*/
|
||||
private final File configurationFile;
|
||||
|
||||
/**
|
||||
* Unique server id
|
||||
*/
|
||||
private final String guid;
|
||||
|
||||
/**
|
||||
* Lock for synchronization
|
||||
*/
|
||||
private final Object optOutLock = new Object();
|
||||
|
||||
/**
|
||||
* Id of the scheduled task
|
||||
*/
|
||||
private volatile int taskId = -1;
|
||||
|
||||
public Metrics(final Plugin plugin) throws IOException {
|
||||
if (plugin == null) {
|
||||
throw new IllegalArgumentException("Plugin cannot be null");
|
||||
}
|
||||
|
||||
this.plugin = plugin;
|
||||
|
||||
// load the config
|
||||
configurationFile = new File(CONFIG_FILE);
|
||||
configuration = YamlConfiguration.loadConfiguration(configurationFile);
|
||||
|
||||
// add some defaults
|
||||
configuration.addDefault("opt-out", false);
|
||||
configuration.addDefault("guid", UUID.randomUUID().toString());
|
||||
|
||||
// Do we need to create the file?
|
||||
if (configuration.get("guid", null) == null) {
|
||||
configuration.options().header("http://mcstats.org").copyDefaults(true);
|
||||
configuration.save(configurationFile);
|
||||
}
|
||||
|
||||
// Load the guid then
|
||||
guid = configuration.getString("guid");
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct and create a Graph that can be used to separate specific plotters to their own graphs
|
||||
* on the metrics website. Plotters can be added to the graph object returned.
|
||||
*
|
||||
* @param name
|
||||
* @return Graph object created. Will never return NULL under normal circumstances unless bad parameters are given
|
||||
*/
|
||||
public Graph createGraph(final String name) {
|
||||
if (name == null) {
|
||||
throw new IllegalArgumentException("Graph name cannot be null");
|
||||
}
|
||||
|
||||
// Construct the graph object
|
||||
final Graph graph = new Graph(name);
|
||||
|
||||
// Now we can add our graph
|
||||
graphs.add(graph);
|
||||
|
||||
// and return back
|
||||
return graph;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a custom data plotter to the default graph
|
||||
*
|
||||
* @param plotter
|
||||
*/
|
||||
public void addCustomData(final Plotter plotter) {
|
||||
if (plotter == null) {
|
||||
throw new IllegalArgumentException("Plotter cannot be null");
|
||||
}
|
||||
|
||||
// Add the plotter to the graph o/
|
||||
defaultGraph.addPlotter(plotter);
|
||||
|
||||
// Ensure the default graph is included in the submitted graphs
|
||||
graphs.add(defaultGraph);
|
||||
}
|
||||
|
||||
/**
|
||||
* Start measuring statistics. This will immediately create an async repeating task as the plugin and send
|
||||
* the initial data to the metrics backend, and then after that it will post in increments of
|
||||
* PING_INTERVAL * 1200 ticks.
|
||||
*
|
||||
* @return True if statistics measuring is running, otherwise false.
|
||||
*/
|
||||
public boolean start() {
|
||||
synchronized (optOutLock) {
|
||||
// Did we opt out?
|
||||
if (isOptOut()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Is metrics already running?
|
||||
if (taskId >= 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Begin hitting the server with glorious data
|
||||
taskId = plugin.getServer().getScheduler().scheduleAsyncRepeatingTask(plugin, new Runnable() {
|
||||
|
||||
private boolean firstPost = true;
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
// This has to be synchronized or it can collide with the disable method.
|
||||
synchronized (optOutLock) {
|
||||
// Disable Task, if it is running and the server owner decided to opt-out
|
||||
if (isOptOut() && taskId > 0) {
|
||||
plugin.getServer().getScheduler().cancelTask(taskId);
|
||||
taskId = -1;
|
||||
}
|
||||
}
|
||||
|
||||
// We use the inverse of firstPost because if it is the first time we are posting,
|
||||
// it is not a interval ping, so it evaluates to FALSE
|
||||
// Each time thereafter it will evaluate to TRUE, i.e PING!
|
||||
postPlugin(!firstPost);
|
||||
|
||||
// After the first post we set firstPost to false
|
||||
// Each post thereafter will be a ping
|
||||
firstPost = false;
|
||||
} catch (IOException e) {
|
||||
Bukkit.getLogger().log(Level.INFO, "[Metrics] " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}, 0, PING_INTERVAL * 1200);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Has the server owner denied plugin metrics?
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean isOptOut() {
|
||||
synchronized(optOutLock) {
|
||||
try {
|
||||
// Reload the metrics file
|
||||
configuration.load(CONFIG_FILE);
|
||||
} catch (IOException ex) {
|
||||
Bukkit.getLogger().log(Level.INFO, "[Metrics] " + ex.getMessage());
|
||||
return true;
|
||||
} catch (InvalidConfigurationException ex) {
|
||||
Bukkit.getLogger().log(Level.INFO, "[Metrics] " + ex.getMessage());
|
||||
return true;
|
||||
}
|
||||
return configuration.getBoolean("opt-out", false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables metrics for the server by setting "opt-out" to false in the config file and starting the metrics task.
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
public void enable() throws IOException {
|
||||
// This has to be synchronized or it can collide with the check in the task.
|
||||
synchronized (optOutLock) {
|
||||
// Check if the server owner has already set opt-out, if not, set it.
|
||||
if (isOptOut()) {
|
||||
configuration.set("opt-out", false);
|
||||
configuration.save(configurationFile);
|
||||
}
|
||||
|
||||
// Enable Task, if it is not running
|
||||
if (taskId < 0) {
|
||||
start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Disables metrics for the server by setting "opt-out" to true in the config file and canceling the metrics task.
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
public void disable() throws IOException {
|
||||
// This has to be synchronized or it can collide with the check in the task.
|
||||
synchronized (optOutLock) {
|
||||
// Check if the server owner has already set opt-out, if not, set it.
|
||||
if (!isOptOut()) {
|
||||
configuration.set("opt-out", true);
|
||||
configuration.save(configurationFile);
|
||||
}
|
||||
|
||||
// Disable Task, if it is running
|
||||
if (taskId > 0) {
|
||||
this.plugin.getServer().getScheduler().cancelTask(taskId);
|
||||
taskId = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic method that posts a plugin to the metrics website
|
||||
*/
|
||||
private void postPlugin(final boolean isPing) throws IOException {
|
||||
// The plugin's description file containg all of the plugin data such as name, version, author, etc
|
||||
final PluginDescriptionFile description = plugin.getDescription();
|
||||
|
||||
// Construct the post data
|
||||
final StringBuilder data = new StringBuilder();
|
||||
data.append(encode("guid")).append('=').append(encode(guid));
|
||||
encodeDataPair(data, "version", description.getVersion());
|
||||
encodeDataPair(data, "server", Bukkit.getVersion());
|
||||
encodeDataPair(data, "players", Integer.toString(Bukkit.getServer().getOnlinePlayers().length));
|
||||
encodeDataPair(data, "revision", String.valueOf(REVISION));
|
||||
|
||||
// If we're pinging, append it
|
||||
if (isPing) {
|
||||
encodeDataPair(data, "ping", "true");
|
||||
}
|
||||
|
||||
// Acquire a lock on the graphs, which lets us make the assumption we also lock everything
|
||||
// inside of the graph (e.g plotters)
|
||||
synchronized (graphs) {
|
||||
final Iterator<Graph> iter = graphs.iterator();
|
||||
|
||||
while (iter.hasNext()) {
|
||||
final Graph graph = iter.next();
|
||||
|
||||
// Because we have a lock on the graphs set already, it is reasonable to assume
|
||||
// that our lock transcends down to the individual plotters in the graphs also.
|
||||
// Because our methods are private, no one but us can reasonably access this list
|
||||
// without reflection so this is a safe assumption without adding more code.
|
||||
for (Plotter plotter : graph.getPlotters()) {
|
||||
// The key name to send to the metrics server
|
||||
// The format is C-GRAPHNAME-PLOTTERNAME where separator - is defined at the top
|
||||
// Legacy (R4) submitters use the format Custom%s, or CustomPLOTTERNAME
|
||||
final String key = String.format("C%s%s%s%s", CUSTOM_DATA_SEPARATOR, graph.getName(), CUSTOM_DATA_SEPARATOR, plotter.getColumnName());
|
||||
|
||||
// The value to send, which for the foreseeable future is just the string
|
||||
// value of plotter.getValue()
|
||||
final String value = Integer.toString(plotter.getValue());
|
||||
|
||||
// Add it to the http post data :)
|
||||
encodeDataPair(data, key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create the url
|
||||
URL url = new URL(BASE_URL + String.format(REPORT_URL, encode(plugin.getDescription().getName())));
|
||||
|
||||
// Connect to the website
|
||||
URLConnection connection;
|
||||
|
||||
// Mineshafter creates a socks proxy, so we can safely bypass it
|
||||
// It does not reroute POST requests so we need to go around it
|
||||
if (isMineshafterPresent()) {
|
||||
connection = url.openConnection(Proxy.NO_PROXY);
|
||||
} else {
|
||||
connection = url.openConnection();
|
||||
}
|
||||
|
||||
connection.setDoOutput(true);
|
||||
|
||||
// Write the data
|
||||
final OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
|
||||
writer.write(data.toString());
|
||||
writer.flush();
|
||||
|
||||
// Now read the response
|
||||
final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
final String response = reader.readLine();
|
||||
|
||||
// close resources
|
||||
writer.close();
|
||||
reader.close();
|
||||
|
||||
if (response == null || response.startsWith("ERR")) {
|
||||
throw new IOException(response); //Throw the exception
|
||||
} else {
|
||||
// Is this the first update this hour?
|
||||
if (response.contains("OK This is your first update this hour")) {
|
||||
synchronized (graphs) {
|
||||
final Iterator<Graph> iter = graphs.iterator();
|
||||
|
||||
while (iter.hasNext()) {
|
||||
final Graph graph = iter.next();
|
||||
|
||||
for (Plotter plotter : graph.getPlotters()) {
|
||||
plotter.reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//if (response.startsWith("OK")) - We should get "OK" followed by an optional description if everything goes right
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if mineshafter is present. If it is, we need to bypass it to send POST requests
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private boolean isMineshafterPresent() {
|
||||
try {
|
||||
Class.forName("mineshafter.MineServer");
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Encode a key/value data pair to be used in a HTTP post request. This INCLUDES a & so the first
|
||||
* key/value pair MUST be included manually, e.g:</p>
|
||||
* <code>
|
||||
* StringBuffer data = new StringBuffer();
|
||||
* data.append(encode("guid")).append('=').append(encode(guid));
|
||||
* encodeDataPair(data, "version", description.getVersion());
|
||||
* </code>
|
||||
*
|
||||
* @param buffer
|
||||
* @param key
|
||||
* @param value
|
||||
* @return
|
||||
*/
|
||||
private static void encodeDataPair(final StringBuilder buffer, final String key, final String value) throws UnsupportedEncodingException {
|
||||
buffer.append('&').append(encode(key)).append('=').append(encode(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode text as UTF-8
|
||||
*
|
||||
* @param text
|
||||
* @return
|
||||
*/
|
||||
private static String encode(final String text) throws UnsupportedEncodingException {
|
||||
return URLEncoder.encode(text, "UTF-8");
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a custom graph on the website
|
||||
*/
|
||||
public static class Graph {
|
||||
|
||||
/**
|
||||
* The graph's name, alphanumeric and spaces only :)
|
||||
* If it does not comply to the above when submitted, it is rejected
|
||||
*/
|
||||
private final String name;
|
||||
|
||||
/**
|
||||
* The set of plotters that are contained within this graph
|
||||
*/
|
||||
private final Set<Plotter> plotters = new LinkedHashSet<Plotter>();
|
||||
|
||||
private Graph(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the graph's name
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a plotter to the graph, which will be used to plot entries
|
||||
*
|
||||
* @param plotter
|
||||
*/
|
||||
public void addPlotter(final Plotter plotter) {
|
||||
plotters.add(plotter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a plotter from the graph
|
||||
*
|
||||
* @param plotter
|
||||
*/
|
||||
public void removePlotter(final Plotter plotter) {
|
||||
plotters.remove(plotter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an <b>unmodifiable</b> set of the plotter objects in the graph
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Set<Plotter> getPlotters() {
|
||||
return Collections.unmodifiableSet(plotters);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return name.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object object) {
|
||||
if (!(object instanceof Graph)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final Graph graph = (Graph) object;
|
||||
return graph.name.equals(name);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface used to collect custom data for a plugin
|
||||
*/
|
||||
public static abstract class Plotter {
|
||||
|
||||
/**
|
||||
* The plot's name
|
||||
*/
|
||||
private final String name;
|
||||
|
||||
/**
|
||||
* Construct a plotter with the default plot name
|
||||
*/
|
||||
public Plotter() {
|
||||
this("Default");
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a plotter with a specific plot name
|
||||
*
|
||||
* @param name
|
||||
*/
|
||||
public Plotter(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current value for the plotted point
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public abstract int getValue();
|
||||
|
||||
/**
|
||||
* Get the column name for the plotted point
|
||||
*
|
||||
* @return the plotted point's column name
|
||||
*/
|
||||
public String getColumnName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called after the website graphs have been updated
|
||||
*/
|
||||
public void reset() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return getColumnName().hashCode() + getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object object) {
|
||||
if (!(object instanceof Plotter)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final Plotter plotter = (Plotter) object;
|
||||
return plotter.name.equals(name) && plotter.getValue() == getValue();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
272
src/main/java/com/gmail/nossr50/util/Misc.java
Normal file
272
src/main/java/com/gmail/nossr50/util/Misc.java
Normal file
@ -0,0 +1,272 @@
|
||||
package com.gmail.nossr50.util;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.entity.LivingEntity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.entity.EntityDamageEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import com.gmail.nossr50.mcMMO;
|
||||
import com.gmail.nossr50.config.Config;
|
||||
import com.gmail.nossr50.events.fake.FakeBlockBreakEvent;
|
||||
import com.gmail.nossr50.events.fake.FakePlayerAnimationEvent;
|
||||
import com.gmail.nossr50.events.items.McMMOItemSpawnEvent;
|
||||
|
||||
public class Misc {
|
||||
|
||||
private static Random random = new Random();
|
||||
|
||||
/**
|
||||
* Gets a capitalized version of the target string.
|
||||
*
|
||||
* @param target String to capitalize
|
||||
* @return the capitalized string
|
||||
*/
|
||||
public static String getCapitalized(String target) {
|
||||
String firstLetter = target.substring(0,1);
|
||||
String remainder = target.substring(1);
|
||||
String capitalized = firstLetter.toUpperCase() + remainder.toLowerCase();
|
||||
|
||||
return capitalized;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a nicely formatted string version of an item name from a given item ID.
|
||||
*
|
||||
* @param itemID The ID of the item to convert to string.
|
||||
* @return the nicely formatting string
|
||||
*/
|
||||
public static String prettyItemString(int itemID) {
|
||||
String baseString = Material.getMaterial(itemID).toString();
|
||||
String[] substrings = baseString.split("_");
|
||||
String prettyString = "";
|
||||
int size = 1;
|
||||
|
||||
for (String s : substrings) {
|
||||
prettyString = prettyString.concat(Misc.getCapitalized(s));
|
||||
|
||||
if (size < substrings.length) {
|
||||
prettyString = prettyString.concat(" ");
|
||||
}
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
return prettyString;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the int represented by this string.
|
||||
*
|
||||
* @param string The string to parse
|
||||
* @return the int represented by this string
|
||||
*/
|
||||
public static int getInt(String string) {
|
||||
if (isInt(string)) {
|
||||
return Integer.parseInt(string);
|
||||
}
|
||||
else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if an entity is currently invincible.
|
||||
*
|
||||
* @param le The LivingEntity to check
|
||||
* @param event The event the entity is involved in
|
||||
* @return true if the entity is invincible, false otherwise
|
||||
*/
|
||||
public static boolean isInvincible(LivingEntity le, EntityDamageEvent event) {
|
||||
|
||||
/*
|
||||
* So apparently if you do more damage to a LivingEntity than its last damage int you bypass the invincibility.
|
||||
* So yeah, this is for that.
|
||||
*/
|
||||
if (le.getNoDamageTicks() > le.getMaximumNoDamageTicks() / 2.0F && event.getDamage() <= le.getLastDamage()) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Simulate a block break event.
|
||||
*
|
||||
* @param block The block to break
|
||||
* @param player The player breaking the block
|
||||
* @param shouldArmSwing true if an armswing event should be fired, false otherwise
|
||||
* @return true if the event wasn't cancelled, false otherwise
|
||||
*/
|
||||
public static boolean blockBreakSimulate(Block block, Player player, Boolean shouldArmSwing) {
|
||||
|
||||
//Support for NoCheat
|
||||
if (shouldArmSwing) {
|
||||
FakePlayerAnimationEvent armswing = new FakePlayerAnimationEvent(player);
|
||||
mcMMO.p.getServer().getPluginManager().callEvent(armswing);
|
||||
}
|
||||
|
||||
FakeBlockBreakEvent event = new FakeBlockBreakEvent(block, player);
|
||||
mcMMO.p.getServer().getPluginManager().callEvent(event);
|
||||
|
||||
if (!event.isCancelled()) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the upgrade tier of the item in hand.
|
||||
*
|
||||
* @param inHand The item to check the tier of
|
||||
* @return the tier of the item
|
||||
*/
|
||||
public static int getTier(ItemStack inHand) {
|
||||
int tier = 0;
|
||||
|
||||
if (ItemChecks.isWoodTool(inHand)) {
|
||||
tier = 1;
|
||||
}
|
||||
else if (ItemChecks.isStoneTool(inHand)) {
|
||||
tier = 2;
|
||||
}
|
||||
else if (ItemChecks.isIronTool(inHand)) {
|
||||
tier = 3;
|
||||
}
|
||||
else if(ItemChecks.isGoldTool(inHand)) {
|
||||
tier = 1;
|
||||
}
|
||||
else if(ItemChecks.isDiamondTool(inHand))
|
||||
tier = 4;
|
||||
|
||||
return tier;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if two locations are near each other.
|
||||
*
|
||||
* @param first The first location
|
||||
* @param second The second location
|
||||
* @param maxDistance The max distance apart
|
||||
* @return true if the distance between <code>first</code> and <code>second</code> is less than <code>maxDistance</code>, false otherwise
|
||||
*/
|
||||
public static boolean isNear(Location first, Location second, double maxDistance) {
|
||||
if (!first.getWorld().equals(second.getWorld())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (first.distanceSquared(second) < (maxDistance * maxDistance)) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a string represents an Integer
|
||||
*
|
||||
* @param string String to check
|
||||
* @return true if the string is an Integer, false otherwise
|
||||
*/
|
||||
public static boolean isInt(String string) {
|
||||
try {
|
||||
Integer.parseInt(string);
|
||||
return true;
|
||||
}
|
||||
catch (NumberFormatException nFE) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Drop items at a given location.
|
||||
*
|
||||
* @param location The location to drop the items at
|
||||
* @param is The items to drop
|
||||
* @param quantity The amount of items to drop
|
||||
*/
|
||||
public static void mcDropItems(Location location, ItemStack is, int quantity) {
|
||||
for (int i = 0; i < quantity; i++) {
|
||||
mcDropItem(location, is);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Randomly drop an item at a given location.
|
||||
*
|
||||
* @param location The location to drop the items at
|
||||
* @param is The item to drop
|
||||
* @param chance The percentage chance for the item to drop
|
||||
*/
|
||||
public static void mcRandomDropItem(Location location, ItemStack is, double chance) {
|
||||
if (random.nextInt(100) < chance) {
|
||||
mcDropItem(location, is);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Randomly drop items at a given location.
|
||||
*
|
||||
* @param location The location to drop the items at
|
||||
* @param is The item to drop
|
||||
* @param chance The percentage chance for the item to drop
|
||||
* @param quantity The amount of items to drop
|
||||
*/
|
||||
public static void mcRandomDropItems(Location location, ItemStack is, int chance, int quantity) {
|
||||
for(int i = 0; i < quantity; i++) {
|
||||
mcRandomDropItem(location, is, chance);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Drop an item at a given location.
|
||||
*
|
||||
* @param location The location to drop the item at
|
||||
* @param itemStack The item to drop
|
||||
*/
|
||||
public static void mcDropItem(Location location, ItemStack itemStack) {
|
||||
|
||||
// We can't get the item until we spawn it and we want to make it cancellable, so we have a custom event.
|
||||
McMMOItemSpawnEvent event = new McMMOItemSpawnEvent(location, itemStack);
|
||||
mcMMO.p.getServer().getPluginManager().callEvent(event);
|
||||
|
||||
if (event.isCancelled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
location.getWorld().dropItemNaturally(location, itemStack);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a skill level is higher than the max bonus level of the ability.
|
||||
*
|
||||
* @param skillLevel Skill level to check
|
||||
* @param maxLevel Max level of the ability
|
||||
* @return whichever value is lower
|
||||
*/
|
||||
public static int skillCheck(int skillLevel, int maxLevel) {
|
||||
if (skillLevel > maxLevel) {
|
||||
return maxLevel;
|
||||
}
|
||||
else {
|
||||
return skillLevel;
|
||||
}
|
||||
}
|
||||
|
||||
public static int getPowerLevelCap() {
|
||||
if (Config.getInstance().getPowerLevelCap() > 0) {
|
||||
return Config.getInstance().getPowerLevelCap();
|
||||
}
|
||||
else {
|
||||
return Integer.MAX_VALUE;
|
||||
}
|
||||
}
|
||||
}
|
@ -5,15 +5,14 @@ import java.util.ArrayList;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.gmail.nossr50.m;
|
||||
import com.gmail.nossr50.datatypes.SkillType;
|
||||
import com.gmail.nossr50.locale.mcLocale;
|
||||
import com.gmail.nossr50.locale.LocaleLoader;
|
||||
|
||||
public class Page {
|
||||
|
||||
public static int getTotalPageNumber(String address)
|
||||
{
|
||||
String[] addressSplit = mcLocale.getString(address).split("\n");
|
||||
String[] addressSplit = LocaleLoader.getString(address).split("\n");
|
||||
if(addressSplit.length <= 8)
|
||||
return 1;
|
||||
else
|
||||
@ -31,7 +30,7 @@ public class Page {
|
||||
}
|
||||
|
||||
ArrayList<String> allStrings = new ArrayList<String>();
|
||||
String split[] = mcLocale.getString(address).split("\n");
|
||||
String split[] = LocaleLoader.getString(address).split("\n");
|
||||
|
||||
allStrings.add(ChatColor.GOLD+"-="+ChatColor.GREEN+header+ChatColor.GOLD+"=-"); //So stylish
|
||||
|
||||
@ -59,7 +58,7 @@ public class Page {
|
||||
|
||||
public static void grabGuidePageForSkill(SkillType skilltype, Player player, String[] args)
|
||||
{
|
||||
String capitalized = m.getCapitalized(skilltype.toString());
|
||||
String capitalized = Misc.getCapitalized(skilltype.toString());
|
||||
player.sendMessage(ChatColor.DARK_AQUA+"Guide for "+capitalized+" available type /"+skilltype.toString().toLowerCase()+" ? [Page#]"); //TODO: Needs more locale.
|
||||
if (args.length >= 1)
|
||||
{
|
||||
@ -68,10 +67,10 @@ public class Page {
|
||||
|
||||
String address = "Guides."+capitalized;
|
||||
|
||||
if(args.length > 1 && m.isInt(args[1]) && m.getInt(args[1]) <= Page.getTotalPageNumber(address))
|
||||
if(args.length > 1 && Misc.isInt(args[1]) && Misc.getInt(args[1]) <= Page.getTotalPageNumber(address))
|
||||
{
|
||||
Page.clearChat(player);
|
||||
for(String target : Page.grabPageContents(capitalized+" Guide", address, m.getInt(args[1])))
|
||||
for(String target : Page.grabPageContents(capitalized+" Guide", address, Misc.getInt(args[1])))
|
||||
{
|
||||
player.sendMessage(target);
|
||||
}
|
||||
@ -83,11 +82,11 @@ public class Page {
|
||||
player.sendMessage(target);
|
||||
}
|
||||
}
|
||||
else if(args.length > 1 && m.getInt(args[1]) > Page.getTotalPageNumber(address))
|
||||
else if(args.length > 1 && Misc.getInt(args[1]) > Page.getTotalPageNumber(address))
|
||||
{
|
||||
player.sendMessage("That page doesn't exist, there are only "+Page.getTotalPageNumber(address)+" total pages"); //TODO: Needs more locale.
|
||||
}
|
||||
else if(args.length > 1 && !m.isInt(args[1]))
|
||||
else if(args.length > 1 && !Misc.isInt(args[1]))
|
||||
{
|
||||
player.sendMessage("Not a valid page number!"); //TODO: Needs more locale.
|
||||
}
|
||||
|
418
src/main/java/com/gmail/nossr50/util/Permissions.java
Normal file
418
src/main/java/com/gmail/nossr50/util/Permissions.java
Normal file
@ -0,0 +1,418 @@
|
||||
package com.gmail.nossr50.util;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Permissions {
|
||||
private static volatile Permissions instance;
|
||||
|
||||
public boolean permission(Player player, String perm) {
|
||||
return player.hasPermission(perm);
|
||||
}
|
||||
|
||||
public static Permissions getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new Permissions();
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
/*
|
||||
* GENERIC PERMISSIONS
|
||||
*/
|
||||
|
||||
public boolean motd(Player player) {
|
||||
return player.hasPermission("mcmmo.motd");
|
||||
}
|
||||
|
||||
public boolean admin(Player player) {
|
||||
return player.hasPermission("mcmmo.admin");
|
||||
}
|
||||
|
||||
/*
|
||||
* MCMMO.BYPASS.*
|
||||
*/
|
||||
public boolean arcaneBypass(Player player) {
|
||||
return player.hasPermission("mcmmo.bypass.arcanebypass");
|
||||
}
|
||||
|
||||
public boolean inspectDistanceBypass(Player player) {
|
||||
return player.hasPermission("mcmmo.bypass.inspect.distance");
|
||||
}
|
||||
|
||||
public boolean inspectOfflineBypass(Player player) {
|
||||
return player.hasPermission("mcmmo.bypass.inspect.offline");
|
||||
}
|
||||
|
||||
/*
|
||||
* MCMMO.TOOLS.*
|
||||
*/
|
||||
|
||||
public boolean mcrefresh(Player player) {
|
||||
return player.hasPermission("mcmmo.tools.mcrefresh");
|
||||
}
|
||||
|
||||
public boolean mcremove(Player player) {
|
||||
return player.hasPermission("mcmmo.tools.mcremove");
|
||||
}
|
||||
|
||||
public boolean mmoedit(Player player) {
|
||||
return player.hasPermission("mcmmo.tools.mmoedit");
|
||||
}
|
||||
|
||||
public boolean mcgod(Player player) {
|
||||
return player.hasPermission("mcmmo.tools.mcgod");
|
||||
}
|
||||
|
||||
/*
|
||||
* MCMMO.ABILITY.TAMING.*
|
||||
*/
|
||||
|
||||
public boolean fastFoodService(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.taming.fastfoodservice");
|
||||
}
|
||||
|
||||
public boolean sharpenedclaws(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.taming.sharpenedclaws");
|
||||
}
|
||||
|
||||
public boolean gore(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.taming.gore");
|
||||
}
|
||||
|
||||
public boolean callOfTheWild(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.taming.callofthewild");
|
||||
}
|
||||
|
||||
public boolean environmentallyAware(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.taming.environmentallyaware");
|
||||
}
|
||||
|
||||
public boolean thickFur(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.taming.thickfur");
|
||||
}
|
||||
|
||||
public boolean shockProof(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.taming.shockproof");
|
||||
}
|
||||
|
||||
public boolean beastLore(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.taming.beastlore");
|
||||
}
|
||||
|
||||
/*
|
||||
* MCMMO.ABILITY.FISHING.*
|
||||
*/
|
||||
|
||||
public boolean shakeMob(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.fishing.shakemob");
|
||||
}
|
||||
|
||||
/*
|
||||
* MCMMO.ABILITY.MINING.*
|
||||
*/
|
||||
|
||||
public boolean superBreaker(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.mining.superbreaker");
|
||||
}
|
||||
|
||||
public boolean miningDoubleDrops(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.mining.doubledrops");
|
||||
}
|
||||
|
||||
/*
|
||||
* MCMMO.ABILITY.WOODCUTTING.*
|
||||
*/
|
||||
|
||||
public boolean treeFeller(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.woodcutting.treefeller");
|
||||
}
|
||||
|
||||
public boolean leafBlower(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.woodcutting.leafblower");
|
||||
}
|
||||
|
||||
public boolean woodcuttingDoubleDrops(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.woodcutting.doubledrops");
|
||||
}
|
||||
|
||||
/*
|
||||
* MCMMO.ABILITY.REPAIR.*
|
||||
*/
|
||||
|
||||
public boolean repairBonus(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.repair.repairbonus");
|
||||
}
|
||||
|
||||
public boolean arcaneForging(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.repair.arcaneforging");
|
||||
}
|
||||
|
||||
public boolean woodRepair(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.repair.woodrepair");
|
||||
}
|
||||
|
||||
public boolean stoneRepair(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.repair.stonerepair");
|
||||
}
|
||||
|
||||
public boolean leatherRepair(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.repair.leatherrepair");
|
||||
}
|
||||
|
||||
public boolean ironRepair(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.repair.ironrepair");
|
||||
}
|
||||
|
||||
public boolean goldRepair(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.repair.goldrepair");
|
||||
}
|
||||
|
||||
public boolean diamondRepair(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.repair.diamondrepair");
|
||||
}
|
||||
|
||||
public boolean armorRepair(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.repair.armorrepair");
|
||||
}
|
||||
|
||||
public boolean toolRepair(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.repair.toolrepair");
|
||||
}
|
||||
|
||||
public boolean stringRepair(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.repair.stringrepair");
|
||||
}
|
||||
|
||||
/*
|
||||
* MCMMO.ABILITY.UNARMED.*
|
||||
*/
|
||||
|
||||
public boolean unarmedBonus(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.unarmed.bonusdamage");
|
||||
}
|
||||
|
||||
public boolean disarm(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.unarmed.disarm");
|
||||
}
|
||||
|
||||
public boolean berserk(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.unarmed.berserk");
|
||||
}
|
||||
|
||||
public boolean deflect(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.unarmed.deflect");
|
||||
}
|
||||
|
||||
/*
|
||||
* MCMMO.ABILITY.ARCHERY.*
|
||||
*/
|
||||
|
||||
public boolean trackArrows(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.archery.trackarrows");
|
||||
}
|
||||
|
||||
public boolean ignition(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.archery.ignition");
|
||||
}
|
||||
|
||||
public boolean daze(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.archery.daze");
|
||||
}
|
||||
|
||||
/*
|
||||
* MCMMO.ABILITY.HERBALISM.*
|
||||
*/
|
||||
|
||||
public boolean herbalismDoubleDrops(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.herbalism.doubledrops");
|
||||
}
|
||||
|
||||
public boolean greenTerra(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.herbalism.greenterra");
|
||||
}
|
||||
|
||||
public boolean greenThumbBlocks(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.herbalism.greenthumbblocks");
|
||||
}
|
||||
|
||||
public boolean greenThumbWheat(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.herbalism.greenthumbwheat");
|
||||
}
|
||||
|
||||
/*
|
||||
* MCMMO.ABILITY.EXCAVATION.*
|
||||
*/
|
||||
|
||||
public boolean gigaDrillBreaker(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.excavation.gigadrillbreaker");
|
||||
}
|
||||
|
||||
public boolean excavationTreasures(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.excavation.treasures");
|
||||
}
|
||||
|
||||
/*
|
||||
* MCMMO.ABILITY.SWORDS.*
|
||||
*/
|
||||
|
||||
public boolean swordsBleed(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.swords.bleed");
|
||||
}
|
||||
|
||||
public boolean serratedStrikes(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.swords.serratedstrikes");
|
||||
}
|
||||
|
||||
public boolean counterAttack(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.swords.counterattack");
|
||||
}
|
||||
|
||||
/*
|
||||
* MCMMO.ABILITY.AXES.*
|
||||
*/
|
||||
|
||||
public boolean skullSplitter(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.axes.skullsplitter");
|
||||
}
|
||||
|
||||
public boolean axeBonus(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.axes.bonusdamage");
|
||||
}
|
||||
|
||||
public boolean criticalHit(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.axes.criticalhit");
|
||||
}
|
||||
|
||||
public boolean impact(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.axes.impact");
|
||||
}
|
||||
|
||||
/*
|
||||
* MCMMO.ABILITY.ACROBATICS.*
|
||||
*/
|
||||
|
||||
public boolean roll(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.acrobatics.roll");
|
||||
}
|
||||
|
||||
public boolean gracefulRoll(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.acrobatics.gracefulroll");
|
||||
}
|
||||
|
||||
public boolean dodge(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.acrobatics.dodge");
|
||||
}
|
||||
|
||||
/*
|
||||
* MCMMO.ABILITY.BLASTMINING.*
|
||||
*/
|
||||
|
||||
public boolean biggerBombs(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.blastmining.biggerbombs");
|
||||
}
|
||||
|
||||
public boolean demolitionsExpertise(Player player) {
|
||||
return player.hasPermission("mcmmo.ability.blastmining.demolitionsexpertise");
|
||||
}
|
||||
|
||||
/*
|
||||
* MCMMO.ITEM.*
|
||||
*/
|
||||
|
||||
public boolean chimaeraWing(Player player) {
|
||||
return player.hasPermission("mcmmo.item.chimaerawing");
|
||||
}
|
||||
|
||||
/*
|
||||
* MCMMO.COMMANDS.*
|
||||
*/
|
||||
|
||||
public boolean mcAbility(Player player) {
|
||||
return player.hasPermission("mcmmo.commands.ability");
|
||||
}
|
||||
|
||||
public boolean partyTeleport(Player player) {
|
||||
return player.hasPermission("mcmmo.commands.ptp");
|
||||
}
|
||||
|
||||
public boolean inspect(Player player) {
|
||||
return player.hasPermission("mcmmo.commands.inspect");
|
||||
}
|
||||
|
||||
public boolean party(Player player) {
|
||||
return player.hasPermission("mcmmo.commands.party");
|
||||
}
|
||||
|
||||
/*
|
||||
* MCMMO.CHAT.*
|
||||
*/
|
||||
|
||||
public boolean partyChat(Player player) {
|
||||
return player.hasPermission("mcmmo.chat.partychat");
|
||||
}
|
||||
|
||||
public boolean partyLock(Player player) {
|
||||
return player.hasPermission("mcmmo.chat.partylock");
|
||||
}
|
||||
|
||||
public boolean adminChat(Player player) {
|
||||
return player.hasPermission("mcmmo.chat.adminchat");
|
||||
}
|
||||
|
||||
/*
|
||||
* MCMMO.SKILLS.*
|
||||
*/
|
||||
|
||||
public boolean taming(Player player) {
|
||||
return player.hasPermission("mcmmo.skills.taming");
|
||||
}
|
||||
|
||||
public boolean mining(Player player) {
|
||||
return player.hasPermission("mcmmo.skills.mining");
|
||||
}
|
||||
|
||||
public boolean blastMining(Player player) {
|
||||
return player.hasPermission("mcmmo.skills.blastmining");
|
||||
}
|
||||
|
||||
public boolean fishing(Player player) {
|
||||
return player.hasPermission("mcmmo.skills.fishing");
|
||||
}
|
||||
|
||||
public boolean woodcutting(Player player) {
|
||||
return player.hasPermission("mcmmo.skills.woodcutting");
|
||||
}
|
||||
|
||||
public boolean repair(Player player) {
|
||||
return player.hasPermission("mcmmo.skills.repair");
|
||||
}
|
||||
|
||||
public boolean unarmed(Player player) {
|
||||
return player.hasPermission("mcmmo.skills.unarmed");
|
||||
}
|
||||
|
||||
public boolean archery(Player player) {
|
||||
return player.hasPermission("mcmmo.skills.archery");
|
||||
}
|
||||
|
||||
public boolean herbalism(Player player) {
|
||||
return player.hasPermission("mcmmo.skills.herbalism");
|
||||
}
|
||||
|
||||
public boolean excavation(Player player) {
|
||||
return player.hasPermission("mcmmo.skills.excavation");
|
||||
}
|
||||
|
||||
public boolean swords(Player player) {
|
||||
return player.hasPermission("mcmmo.skills.swords");
|
||||
}
|
||||
|
||||
public boolean axes(Player player) {
|
||||
return player.hasPermission("mcmmo.skills.axes");
|
||||
}
|
||||
|
||||
public boolean acrobatics(Player player) {
|
||||
return player.hasPermission("mcmmo.skills.acrobatics");
|
||||
}
|
||||
}
|
121
src/main/java/com/gmail/nossr50/util/Users.java
Normal file
121
src/main/java/com/gmail/nossr50/util/Users.java
Normal file
@ -0,0 +1,121 @@
|
||||
package com.gmail.nossr50.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.gmail.nossr50.mcMMO;
|
||||
import com.gmail.nossr50.datatypes.PlayerProfile;
|
||||
|
||||
public class Users {
|
||||
|
||||
public static String location = mcMMO.usersFile;
|
||||
public static String directory = mcMMO.flatFileDirectory;
|
||||
public static String directoryb = mcMMO.leaderboardDirectory;
|
||||
|
||||
public static HashMap<String, PlayerProfile> players = new HashMap<String, PlayerProfile>();
|
||||
|
||||
/**
|
||||
* Load users.
|
||||
*/
|
||||
public static void loadUsers() {
|
||||
new File(directory).mkdir();
|
||||
new File(directoryb).mkdir();
|
||||
File theDir = new File(location);
|
||||
|
||||
if (!theDir.exists()) {
|
||||
try {
|
||||
FileWriter writer = new FileWriter(theDir);
|
||||
writer.close();
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new user.
|
||||
*
|
||||
* @param player The player to create a user record for
|
||||
*/
|
||||
public static void addUser(Player player) {
|
||||
if (!players.containsKey(player.getName().toLowerCase())) {
|
||||
players.put(player.getName().toLowerCase(), new PlayerProfile(player.getName(), true));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all users.
|
||||
*/
|
||||
public static void clearUsers() {
|
||||
players.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all PlayerProfiles.
|
||||
*
|
||||
* @return a HashMap containing the PlayerProfile of everyone in the database
|
||||
*/
|
||||
public static HashMap<String, PlayerProfile> getProfiles() {
|
||||
return players;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a user from the database.
|
||||
*
|
||||
* @param player The player to remove
|
||||
*/
|
||||
public static void removeUser(Player player) {
|
||||
|
||||
//Only remove PlayerProfile if user is offline and we have it in memory
|
||||
if (!player.isOnline() && players.containsKey(player.getName().toLowerCase())) {
|
||||
players.get(player.getName().toLowerCase()).save();
|
||||
players.remove(player.getName().toLowerCase());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a user from the DB by name.
|
||||
*
|
||||
* @param playerName The name of the player to remove
|
||||
*/
|
||||
public static void removeUserByName(String playerName) {
|
||||
players.remove(playerName.toLowerCase());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the profile of a player.
|
||||
*
|
||||
* @param player The player whose profile to retrieve
|
||||
* @return the player's profile
|
||||
*/
|
||||
public static PlayerProfile getProfile(OfflinePlayer player) {
|
||||
return getProfileByName(player.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the profile of a player by name.
|
||||
*
|
||||
* @param player The name of the player whose profile to retrieve
|
||||
* @return the player's profile
|
||||
*/
|
||||
public static PlayerProfile getProfileByName(String playerName) {
|
||||
if (mcMMO.p.getServer().getOfflinePlayer(playerName).isOnline() || players.containsKey(playerName.toLowerCase())) {
|
||||
if (players.get(playerName.toLowerCase()) != null) {
|
||||
return players.get(playerName.toLowerCase());
|
||||
}
|
||||
else {
|
||||
players.put(playerName.toLowerCase(), new PlayerProfile(playerName, true));
|
||||
return players.get(playerName.toLowerCase());
|
||||
}
|
||||
}
|
||||
else {
|
||||
return new PlayerProfile(playerName, false);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user