PlayerListener cleanup

This commit is contained in:
GJ 2013-01-24 14:19:26 -05:00
parent 9dac898c1c
commit 76a987e1e0
13 changed files with 209 additions and 132 deletions

View File

@ -14,10 +14,11 @@ import com.gmail.nossr50.util.Misc;
public class XprateCommand implements CommandExecutor {
private final mcMMO plugin;
private static double oldRate = Config.getInstance().xpGainMultiplier;
private static boolean xpEvent = false;
private boolean xpEvent;
public XprateCommand (mcMMO plugin) {
this.plugin = plugin;
this.xpEvent = plugin.isXPEventEnabled();
}
@Override
@ -39,6 +40,7 @@ public class XprateCommand implements CommandExecutor {
}
xpEvent = !xpEvent;
plugin.setXPEventEnabled(xpEvent);
Config.getInstance().xpGainMultiplier = oldRate;
}
else {
@ -60,6 +62,7 @@ public class XprateCommand implements CommandExecutor {
if (args[1].equalsIgnoreCase("true") || args[1].equalsIgnoreCase("false")) {
xpEvent = Boolean.valueOf(args[1]);
plugin.setXPEventEnabled(xpEvent);
}
else {
sender.sendMessage(usage3);
@ -90,8 +93,4 @@ public class XprateCommand implements CommandExecutor {
return true;
}
}
public static boolean isXpEventRunning() {
return xpEvent;
}
}

View File

@ -280,8 +280,8 @@ public class BlockListener implements Listener {
/*
* ABILITY PREPARATION CHECKS
*/
if (BlockChecks.abilityBlockCheck(block)) {
if (profile.getToolPreparationMode(ToolType.HOE) && (BlockChecks.canBeGreenTerra(block) || BlockChecks.makeMossy(block))) {
if (BlockChecks.canActivateAbilities(block)) {
if (profile.getToolPreparationMode(ToolType.HOE) && (BlockChecks.canBeGreenTerra(block) || BlockChecks.canMakeMossy(block))) {
Skills.abilityCheck(player, SkillType.HERBALISM);
}
else if (profile.getToolPreparationMode(ToolType.AXE) && BlockChecks.isLog(block) && Permissions.treeFeller(player)) { //TODO: Why are we checking the permissions here?
@ -306,7 +306,7 @@ public class BlockListener implements Listener {
/*
* ABILITY TRIGGER CHECKS
*/
if (profile.getAbilityMode(AbilityType.GREEN_TERRA) && Permissions.greenTerra(player) && BlockChecks.makeMossy(block)) {
if (profile.getAbilityMode(AbilityType.GREEN_TERRA) && Permissions.greenTerra(player) && BlockChecks.canMakeMossy(block)) {
Herbalism.greenTerra(player, block);
}
else if (profile.getAbilityMode(AbilityType.GIGA_DRILL_BREAKER) && Skills.triggerCheck(player, block, AbilityType.GIGA_DRILL_BREAKER)) {

View File

@ -1,13 +1,11 @@
package com.gmail.nossr50.listeners;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerChangedWorldEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
@ -22,22 +20,21 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.PluginDescriptionFile;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.commands.general.XprateCommand;
import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.datatypes.PlayerProfile;
import com.gmail.nossr50.events.chat.McMMOAdminChatEvent;
import com.gmail.nossr50.events.chat.McMMOPartyChatEvent;
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.party.Party;
import com.gmail.nossr50.runnables.BleedTimer;
import com.gmail.nossr50.skills.SkillType;
import com.gmail.nossr50.skills.Skills;
import com.gmail.nossr50.skills.fishing.Fishing;
import com.gmail.nossr50.skills.herbalism.Herbalism;
import com.gmail.nossr50.skills.mining.BlastMining;
import com.gmail.nossr50.skills.mining.MiningManager;
import com.gmail.nossr50.skills.repair.Repair;
import com.gmail.nossr50.skills.repair.Salvage;
import com.gmail.nossr50.skills.taming.TamingManager;
import com.gmail.nossr50.util.BlockChecks;
import com.gmail.nossr50.util.ChatManager;
import com.gmail.nossr50.util.Item;
import com.gmail.nossr50.util.MOTD;
import com.gmail.nossr50.util.Misc;
@ -168,8 +165,7 @@ public class PlayerListener implements Listener {
motd.displayWebsite(pluginDescription.getWebsite());
}
//TODO: MAKE THIS SUCK LESS. THIS IS VERY BAD WAY TO DO THINGS, NEED BETTER WAY
if (XprateCommand.isXpEventRunning()) {
if (plugin.isXPEventEnabled()) {
player.sendMessage(LocaleLoader.getString("XPRate.Event", new Object[] {Config.getInstance().xpGainMultiplier}));
}
}
@ -191,84 +187,110 @@ public class PlayerListener implements Listener {
}
/**
* Monitor PlayerInteract events.
* Handle PlayerInteract events that involve modifying the event.
*
* @param event The event to watch
*/
@EventHandler(priority = EventPriority.LOW)
public void onPlayerInteract(PlayerInteractEvent event) {
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerInteractLowest(PlayerInteractEvent event) {
Player player = event.getPlayer();
if (player.hasMetadata("NPC")) return; // Check if this player is a Citizens NPC
Action action = event.getAction();
if (Misc.isNPC(player)) {
return;
}
Block block = event.getClickedBlock();
ItemStack inHand = player.getItemInHand();
Material material;
ItemStack heldItem = event.getItem();
/* Fix for NPE on interacting with air */
if (block == null) {
material = Material.AIR;
}
else {
material = block.getType();
}
switch (action) {
switch (event.getAction()) {
case RIGHT_CLICK_BLOCK:
int blockID = block.getTypeId();
/* REPAIR CHECKS */
if (Permissions.repair(player) && block.getTypeId() == Config.getInstance().getRepairAnvilId()) {
if (mcMMO.repairManager.isRepairable(inHand)) {
mcMMO.repairManager.handleRepair(player, inHand);
if (blockID == Repair.anvilID && Permissions.repair(player) && mcMMO.repairManager.isRepairable(heldItem)) {
mcMMO.repairManager.handleRepair(player, heldItem);
event.setCancelled(true);
player.updateInventory();
}
}
/* SALVAGE CHECKS */
if (Permissions.salvage(player) && block.getTypeId() == Config.getInstance().getSalvageAnvilId()) {
if (Salvage.isSalvageable(inHand)) {
final Location location = block.getLocation();
Salvage.handleSalvage(player, location, inHand);
else if (blockID == Salvage.anvilID && Permissions.salvage(player) && Salvage.isSalvageable(heldItem)) {
Salvage.handleSalvage(player, block.getLocation(), heldItem);
event.setCancelled(true);
player.updateInventory();
}
}
/* ACTIVATION CHECKS */
if (Config.getInstance().getAbilitiesEnabled() && BlockChecks.abilityBlockCheck(block)) {
if (!material.equals(Material.DIRT) && !material.equals(Material.GRASS) && !material.equals(Material.SOIL)) {
Skills.activationCheck(player, SkillType.HERBALISM);
}
Skills.activationCheck(player, SkillType.AXES);
Skills.activationCheck(player, SkillType.EXCAVATION);
Skills.activationCheck(player, SkillType.MINING);
Skills.activationCheck(player, SkillType.SWORDS);
Skills.activationCheck(player, SkillType.UNARMED);
Skills.activationCheck(player, SkillType.WOODCUTTING);
}
/* GREEN THUMB CHECK */
if (inHand.getType().equals(Material.SEEDS) && BlockChecks.makeMossy(block) && Permissions.greenThumbBlocks(player)) {
Herbalism.greenThumbBlocks(inHand, player, block);
}
/* ITEM CHECKS */
if (BlockChecks.abilityBlockCheck(block)) {
Item.itemChecks(player);
}
/* BLAST MINING CHECK */
if (player.isSneaking() && inHand.getTypeId() == Config.getInstance().getDetonatorItemID() && Permissions.blastMining(player)) {
else if (player.isSneaking() && Permissions.blastMining(player) && heldItem.getTypeId() == BlastMining.detonatorID) {
MiningManager miningManager = new MiningManager(player);
miningManager.detonate(event);
}
break;
case RIGHT_CLICK_AIR:
/* BLAST MINING CHECK */
if (player.isSneaking() && Permissions.blastMining(player) && heldItem.getTypeId() == BlastMining.detonatorID) {
MiningManager miningManager = new MiningManager(player);
miningManager.detonate(event);
}
break;
default:
break;
}
}
/**
* Monitor PlayerInteract events.
*
* @param event The event to watch
*/
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerInteract(PlayerInteractEvent event) {
Player player = event.getPlayer();
if (Misc.isNPC(player)) {
return;
}
Block block = event.getClickedBlock();
ItemStack heldItem = event.getItem();
switch (event.getAction()) {
case RIGHT_CLICK_BLOCK:
/* ACTIVATION & ITEM CHECKS */
if (BlockChecks.canActivateAbilities(block)) {
if (Skills.abilitiesEnabled) {
if (BlockChecks.canActivateHerbalism(block)) {
Skills.activationCheck(player, SkillType.HERBALISM);
}
Skills.activationCheck(player, SkillType.AXES);
Skills.activationCheck(player, SkillType.EXCAVATION);
Skills.activationCheck(player, SkillType.MINING);
Skills.activationCheck(player, SkillType.SWORDS);
Skills.activationCheck(player, SkillType.UNARMED);
Skills.activationCheck(player, SkillType.WOODCUTTING);
}
Item.itemChecks(player);
}
/* GREEN THUMB CHECK */
if (heldItem.getType() == Material.SEEDS && BlockChecks.canMakeMossy(block) && Permissions.greenThumbBlocks(player)) {
Herbalism.greenThumbBlocks(heldItem, player, block);
}
break;
case RIGHT_CLICK_AIR:
/* ACTIVATION CHECKS */
if (Config.getInstance().getAbilitiesEnabled()) {
if (Skills.abilitiesEnabled) {
Skills.activationCheck(player, SkillType.AXES);
Skills.activationCheck(player, SkillType.EXCAVATION);
Skills.activationCheck(player, SkillType.HERBALISM);
@ -281,12 +303,6 @@ public class PlayerListener implements Listener {
/* ITEM CHECKS */
Item.itemChecks(player);
/* BLAST MINING CHECK */
if (player.isSneaking() && inHand.getTypeId() == Config.getInstance().getDetonatorItemID() && Permissions.blastMining(player)) {
MiningManager miningManager = new MiningManager(player);
miningManager.detonate(event);
}
break;
case LEFT_CLICK_AIR:
@ -294,7 +310,7 @@ public class PlayerListener implements Listener {
/* CALL OF THE WILD CHECKS */
if (player.isSneaking()) {
Material type = inHand.getType();
Material type = heldItem.getType();
if (type == Material.RAW_FISH) {
TamingManager tamingManager = new TamingManager(player);
@ -328,48 +344,12 @@ public class PlayerListener implements Listener {
}
if (profile.getPartyChatMode()) {
Party party = profile.getParty();
if (party == null) {
player.sendMessage(LocaleLoader.getString("Commands.Party.None"));
return;
}
String partyName = party.getName();
String playerName = player.getName();
McMMOPartyChatEvent chatEvent = new McMMOPartyChatEvent(playerName, partyName, event.getMessage());
plugin.getServer().getPluginManager().callEvent(chatEvent);
if (chatEvent.isCancelled()) {
return;
}
plugin.getLogger().info("[P](" + partyName + ")" + "<" + playerName + "> " + chatEvent.getMessage());
for (Player member : party.getOnlineMembers()) {
member.sendMessage(LocaleLoader.getString("Commands.Party.Chat.Prefix", new Object[] {playerName}) + chatEvent.getMessage());
}
event.setCancelled(true);
ChatManager chatManager = new ChatManager(plugin, player, event);
chatManager.handlePartyChat();
}
else if (profile.getAdminChatMode()) {
String playerName = player.getName();
McMMOAdminChatEvent chatEvent = new McMMOAdminChatEvent(playerName, event.getMessage());
plugin.getServer().getPluginManager().callEvent(chatEvent);
if (chatEvent.isCancelled()) {
return;
}
plugin.getLogger().info("[A]<" + playerName + "> " + chatEvent.getMessage());
for (Player otherPlayer : plugin.getServer().getOnlinePlayers()) {
if (Permissions.adminChat(otherPlayer) || otherPlayer.isOp()) {
otherPlayer.sendMessage(LocaleLoader.getString("Commands.AdminChat.Prefix", new Object[] {playerName}) + chatEvent.getMessage());
}
}
event.setCancelled(true);
ChatManager chatManager = new ChatManager(plugin, player, event);
chatManager.handleAdminChat();
}
}
@ -385,8 +365,10 @@ public class PlayerListener implements Listener {
String lowerCaseCommand = command.toLowerCase();
if (plugin.commandIsAliased(lowerCaseCommand)) {
//We should find a better way to avoid string replacement where the alias is equals to the command
if (command.equals(plugin.getCommandAlias(lowerCaseCommand))) {
String commandAlias = plugin.getCommandAlias(lowerCaseCommand);
//TODO: We should find a better way to avoid string replacement where the alias is equals to the command
if (command.equals(commandAlias)) {
return;
}

View File

@ -119,6 +119,9 @@ public class mcMMO extends JavaPlugin {
//Spout Check
public static boolean spoutEnabled;
//XP Event Check
private boolean xpEventEnabled = false;
/**
* Things to be run when the plugin is enabled.
*/
@ -560,5 +563,13 @@ public class mcMMO extends JavaPlugin {
public static Database getPlayerDatabase() {
return database;
}
public boolean isXPEventEnabled() {
return xpEventEnabled;
}
public void setXPEventEnabled(boolean enabled) {
this.xpEventEnabled = enabled;
}
}

View File

@ -193,7 +193,7 @@ public enum AbilityType {
return BlockChecks.canBeGigaDrillBroken(block);
case GREEN_TERRA:
return BlockChecks.makeMossy(block);
return BlockChecks.canMakeMossy(block);
case LEAF_BLOWER:
return block.getType() == Material.LEAVES;

View File

@ -23,6 +23,7 @@ import com.gmail.nossr50.util.Users;
public class Skills {
static AdvancedConfig advancedConfig = AdvancedConfig.getInstance();
public static int abilityLengthIncreaseLevel = advancedConfig.getAbilityLength();
public static boolean abilitiesEnabled = Config.getInstance().getAbilitiesEnabled();
/**
* Checks to see if the cooldown for an item or ability is expired.

View File

@ -1,6 +1,7 @@
package com.gmail.nossr50.skills.mining;
import com.gmail.nossr50.config.AdvancedConfig;
import com.gmail.nossr50.config.Config;
public class BlastMining {
public static int rank1 = AdvancedConfig.getInstance().getBlastMiningRank1();
@ -12,5 +13,7 @@ public class BlastMining {
public static int rank7 = AdvancedConfig.getInstance().getBlastMiningRank7();
public static int rank8 = AdvancedConfig.getInstance().getBlastMiningRank8();
public static int detonatorID = Config.getInstance().getDetonatorItemID();
public final static int MAXIMUM_REMOTE_DETONATION_DISTANCE = 100;
}

View File

@ -27,10 +27,6 @@ public class MiningManager extends SkillManager{
* @param plugin mcMMO plugin instance
*/
public void detonate(PlayerInteractEvent event) {
if (Misc.isNPC(player)) {
return;
}
if (skillLevel < BlastMining.rank1) {
return;
}

View File

@ -12,6 +12,7 @@ import org.getspout.spoutapi.player.SpoutPlayer;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.config.AdvancedConfig;
import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.datatypes.PlayerProfile;
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.skills.SkillType;
@ -33,6 +34,8 @@ public class Repair {
public static boolean arcaneForgingDowngrades = advancedConfig.getArcaneForgingDowngradeEnabled();
public static boolean arcaneForgingEnchantLoss = advancedConfig.getArcaneForgingEnchantLossEnabled();
public static int anvilID = Config.getInstance().getRepairAnvilId();
/**
* Handle the XP gain for repair events.
*

View File

@ -15,15 +15,15 @@ import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.skills.SkillType;
import com.gmail.nossr50.util.ItemChecks;
import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.Users;
public class Salvage {
private static Config configInstance = Config.getInstance();
public static int salvageUnlockLevel = Config.getInstance().getSalvageUnlockLevel();
public static int anvilID = Config.getInstance().getSalvageAnvilId();
public static void handleSalvage(final Player player, final Location location, final ItemStack inHand) {
if (!Permissions.salvage(player) || !configInstance.getSalvageEnabled()) {
if (!configInstance.getSalvageEnabled()) {
return;
}

View File

@ -162,9 +162,6 @@ public class TamingManager extends SkillManager {
* @param summonAmount The amount of material needed to summon the entity
*/
private void callOfTheWild(EntityType type, int summonAmount) {
if (player == null)
return;
if (!Permissions.callOfTheWild(player)) {
return;
}

View File

@ -76,7 +76,7 @@ public class BlockChecks {
* @param block Block to check
* @return true if the block should allow ability activation, false otherwise
*/
public static boolean abilityBlockCheck(Block block) {
public static boolean canActivateAbilities(Block block) {
ItemStack item = (new MaterialData(block.getTypeId(), block.getData())).toItemStack(1);
if (customBlocksEnabled && CustomBlocksConfig.getInstance().customAbilityBlocks.contains(item)) {
@ -151,7 +151,7 @@ public class BlockChecks {
* @param block The block to check
* @return true if the block can be made mossy, false otherwise
*/
public static boolean makeMossy(Block block) {
public static boolean canMakeMossy(Block block) {
switch (block.getType()) {
case COBBLESTONE:
case DIRT:
@ -314,4 +314,16 @@ public class BlockChecks {
return false;
}
}
public static boolean canActivateHerbalism(Block block) {
switch (block.getType()) {
case DIRT:
case GRASS:
case SOIL:
return false;
default:
return true;
}
}
}

View File

@ -0,0 +1,73 @@
package com.gmail.nossr50.util;
import org.bukkit.entity.Player;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.events.chat.McMMOAdminChatEvent;
import com.gmail.nossr50.events.chat.McMMOPartyChatEvent;
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.party.Party;
public class ChatManager {
private mcMMO plugin;
private Player player;
private String playerName;
private AsyncPlayerChatEvent event;
public ChatManager (mcMMO plugin, Player player, AsyncPlayerChatEvent event) {
this.plugin = plugin;
this.player = player;
this.playerName = player.getName();
this.event = event;
}
public void handleAdminChat() {
McMMOAdminChatEvent chatEvent = new McMMOAdminChatEvent(playerName, event.getMessage());
plugin.getServer().getPluginManager().callEvent(chatEvent);
if (chatEvent.isCancelled()) {
return;
}
String adminMessage = chatEvent.getMessage();
plugin.getLogger().info("[A]<" + playerName + "> " + adminMessage);
for (Player otherPlayer : plugin.getServer().getOnlinePlayers()) {
if (Permissions.adminChat(otherPlayer) || otherPlayer.isOp()) {
otherPlayer.sendMessage(LocaleLoader.getString("Commands.AdminChat.Prefix", new Object[] {playerName}) + adminMessage);
}
}
event.setCancelled(true);
}
public void handlePartyChat() {
Party party = Users.getProfile(player).getParty();
if (party == null) {
player.sendMessage(LocaleLoader.getString("Commands.Party.None"));
return;
}
String partyName = party.getName();
McMMOPartyChatEvent chatEvent = new McMMOPartyChatEvent(playerName, partyName, event.getMessage());
plugin.getServer().getPluginManager().callEvent(chatEvent);
if (chatEvent.isCancelled()) {
return;
}
String partyMessage = chatEvent.getMessage();
plugin.getLogger().info("[P](" + partyName + ")" + "<" + playerName + "> " + partyMessage);
for (Player member : party.getOnlineMembers()) {
member.sendMessage(LocaleLoader.getString("Commands.Party.Chat.Prefix", new Object[] {playerName}) + partyMessage);
}
event.setCancelled(true);
}
}