2013-10-15 19:03:33 +02:00
|
|
|
package com.gmail.nossr50.util;
|
|
|
|
|
2013-10-18 14:31:00 +02:00
|
|
|
import java.util.Map;
|
|
|
|
|
2013-10-15 19:03:33 +02:00
|
|
|
import org.bukkit.block.Block;
|
2013-10-18 14:31:00 +02:00
|
|
|
import org.bukkit.enchantments.Enchantment;
|
|
|
|
import org.bukkit.entity.Fish;
|
2013-10-15 19:03:33 +02:00
|
|
|
import org.bukkit.entity.Player;
|
2013-10-18 14:31:00 +02:00
|
|
|
import org.bukkit.event.player.PlayerFishEvent;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
2013-10-15 19:03:33 +02:00
|
|
|
import org.bukkit.plugin.PluginManager;
|
|
|
|
|
|
|
|
import com.gmail.nossr50.mcMMO;
|
2013-11-11 17:42:57 +01:00
|
|
|
import com.gmail.nossr50.datatypes.party.Party;
|
2013-10-18 14:31:00 +02:00
|
|
|
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
|
|
|
|
import com.gmail.nossr50.datatypes.player.PlayerProfile;
|
|
|
|
import com.gmail.nossr50.datatypes.skills.AbilityType;
|
2013-10-15 19:03:33 +02:00
|
|
|
import com.gmail.nossr50.datatypes.skills.SkillType;
|
2013-10-18 14:31:00 +02:00
|
|
|
import com.gmail.nossr50.events.experience.McMMOPlayerLevelChangeEvent;
|
|
|
|
import com.gmail.nossr50.events.experience.McMMOPlayerLevelDownEvent;
|
2013-10-15 19:03:33 +02:00
|
|
|
import com.gmail.nossr50.events.experience.McMMOPlayerLevelUpEvent;
|
2013-10-18 14:31:00 +02:00
|
|
|
import com.gmail.nossr50.events.experience.McMMOPlayerXpGainEvent;
|
2013-10-15 19:03:33 +02:00
|
|
|
import com.gmail.nossr50.events.fake.FakeBlockBreakEvent;
|
|
|
|
import com.gmail.nossr50.events.fake.FakeBlockDamageEvent;
|
|
|
|
import com.gmail.nossr50.events.fake.FakePlayerAnimationEvent;
|
2013-10-18 14:31:00 +02:00
|
|
|
import com.gmail.nossr50.events.fake.FakePlayerFishEvent;
|
|
|
|
import com.gmail.nossr50.events.hardcore.McMMOPlayerDeathPenaltyEvent;
|
2013-11-11 17:42:57 +01:00
|
|
|
import com.gmail.nossr50.events.party.McMMOPartyLevelUpEvent;
|
2013-10-18 14:31:00 +02:00
|
|
|
import com.gmail.nossr50.events.party.McMMOPartyTeleportEvent;
|
2013-11-11 17:42:57 +01:00
|
|
|
import com.gmail.nossr50.events.party.McMMOPartyXpGainEvent;
|
2013-10-15 19:03:33 +02:00
|
|
|
import com.gmail.nossr50.events.skills.abilities.McMMOPlayerAbilityActivateEvent;
|
2013-10-18 14:31:00 +02:00
|
|
|
import com.gmail.nossr50.events.skills.abilities.McMMOPlayerAbilityDeactivateEvent;
|
|
|
|
import com.gmail.nossr50.events.skills.fishing.McMMOPlayerFishingTreasureEvent;
|
|
|
|
import com.gmail.nossr50.events.skills.fishing.McMMOPlayerMagicHunterEvent;
|
|
|
|
import com.gmail.nossr50.events.skills.repair.McMMOPlayerRepairCheckEvent;
|
|
|
|
import com.gmail.nossr50.events.skills.unarmed.McMMOPlayerDisarmEvent;
|
|
|
|
import com.gmail.nossr50.locale.LocaleLoader;
|
|
|
|
import com.gmail.nossr50.util.player.UserManager;
|
2013-10-15 19:03:33 +02:00
|
|
|
|
|
|
|
public class EventUtils {
|
|
|
|
public static McMMOPlayerAbilityActivateEvent callPlayerAbilityActivateEvent(Player player, SkillType skill) {
|
|
|
|
McMMOPlayerAbilityActivateEvent event = new McMMOPlayerAbilityActivateEvent(player, skill);
|
|
|
|
mcMMO.p.getServer().getPluginManager().callEvent(event);
|
|
|
|
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static FakePlayerAnimationEvent callFakeArmSwingEvent(Player player) {
|
|
|
|
FakePlayerAnimationEvent event = new FakePlayerAnimationEvent(player);
|
|
|
|
mcMMO.p.getServer().getPluginManager().callEvent(event);
|
|
|
|
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
2013-10-18 14:31:00 +02:00
|
|
|
public static boolean handleLevelChangeEvent(Player player, SkillType skill, int levelsChanged, float xpRemoved, boolean isLevelUp) {
|
|
|
|
McMMOPlayerLevelChangeEvent event = isLevelUp ? new McMMOPlayerLevelUpEvent(player, skill, levelsChanged) : new McMMOPlayerLevelDownEvent(player, skill, levelsChanged);
|
2013-10-15 19:03:33 +02:00
|
|
|
mcMMO.p.getServer().getPluginManager().callEvent(event);
|
|
|
|
|
2013-10-18 14:31:00 +02:00
|
|
|
boolean isCancelled = event.isCancelled();
|
|
|
|
|
|
|
|
if (isCancelled) {
|
|
|
|
PlayerProfile profile = UserManager.getPlayer(player).getProfile();
|
|
|
|
|
2013-10-18 15:23:40 +02:00
|
|
|
profile.modifySkill(skill, profile.getSkillLevel(skill) - (isLevelUp ? levelsChanged : -levelsChanged));
|
2013-10-29 16:02:57 +01:00
|
|
|
profile.addXp(skill, xpRemoved);
|
2013-10-18 14:31:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return !isCancelled;
|
2013-10-15 19:03:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 simulateBlockBreak(Block block, Player player, boolean shouldArmSwing) {
|
|
|
|
PluginManager pluginManager = mcMMO.p.getServer().getPluginManager();
|
|
|
|
|
|
|
|
// Support for NoCheat
|
|
|
|
if (shouldArmSwing) {
|
|
|
|
callFakeArmSwingEvent(player);
|
|
|
|
}
|
|
|
|
|
|
|
|
FakeBlockDamageEvent damageEvent = new FakeBlockDamageEvent(player, block, player.getItemInHand(), true);
|
|
|
|
pluginManager.callEvent(damageEvent);
|
|
|
|
|
|
|
|
FakeBlockBreakEvent breakEvent = new FakeBlockBreakEvent(block, player);
|
|
|
|
pluginManager.callEvent(breakEvent);
|
|
|
|
|
|
|
|
return !damageEvent.isCancelled() && !breakEvent.isCancelled();
|
|
|
|
}
|
|
|
|
|
2013-10-18 14:31:00 +02:00
|
|
|
public static void handlePartyTeleportEvent(Player teleportingPlayer, Player targetPlayer) {
|
|
|
|
McMMOPlayer mcMMOPlayer = UserManager.getPlayer(teleportingPlayer);
|
|
|
|
|
|
|
|
McMMOPartyTeleportEvent event = new McMMOPartyTeleportEvent(teleportingPlayer, targetPlayer, mcMMOPlayer.getParty().getName());
|
|
|
|
mcMMO.p.getServer().getPluginManager().callEvent(event);
|
|
|
|
|
|
|
|
if (event.isCancelled()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
teleportingPlayer.teleport(targetPlayer);
|
|
|
|
|
|
|
|
teleportingPlayer.sendMessage(LocaleLoader.getString("Party.Teleport.Player", targetPlayer.getName()));
|
|
|
|
targetPlayer.sendMessage(LocaleLoader.getString("Party.Teleport.Target", teleportingPlayer.getName()));
|
|
|
|
|
2013-10-28 20:47:47 +01:00
|
|
|
mcMMOPlayer.getPartyTeleportRecord().actualizeLastUse();
|
2013-10-18 14:31:00 +02:00
|
|
|
}
|
|
|
|
|
2013-11-11 17:42:57 +01:00
|
|
|
public static boolean handlePartyXpGainEvent(Party party, float xpGained) {
|
|
|
|
McMMOPartyXpGainEvent event = new McMMOPartyXpGainEvent(party, xpGained);
|
|
|
|
mcMMO.p.getServer().getPluginManager().callEvent(event);
|
|
|
|
|
|
|
|
boolean isCancelled = event.isCancelled();
|
|
|
|
|
|
|
|
if (!isCancelled) {
|
|
|
|
party.addXp(event.getRawXpGained());
|
|
|
|
}
|
|
|
|
|
|
|
|
return !isCancelled;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean handlePartyLevelChangeEvent(Party party, int levelsChanged, float xpRemoved) {
|
|
|
|
McMMOPartyLevelUpEvent event = new McMMOPartyLevelUpEvent(party, levelsChanged);
|
|
|
|
mcMMO.p.getServer().getPluginManager().callEvent(event);
|
|
|
|
|
|
|
|
boolean isCancelled = event.isCancelled();
|
|
|
|
|
|
|
|
if (isCancelled) {
|
|
|
|
|
|
|
|
party.setLevel(party.getLevel() + levelsChanged);
|
|
|
|
party.addXp(xpRemoved);
|
|
|
|
}
|
|
|
|
|
|
|
|
return !isCancelled;
|
|
|
|
}
|
|
|
|
|
2013-10-18 14:31:00 +02:00
|
|
|
public static boolean handleXpGainEvent(Player player, SkillType skill, float xpGained) {
|
|
|
|
McMMOPlayerXpGainEvent event = new McMMOPlayerXpGainEvent(player, skill, xpGained);
|
|
|
|
mcMMO.p.getServer().getPluginManager().callEvent(event);
|
|
|
|
|
|
|
|
boolean isCancelled = event.isCancelled();
|
|
|
|
|
|
|
|
if (!isCancelled) {
|
2013-10-29 16:02:57 +01:00
|
|
|
UserManager.getPlayer(player).addXp(skill, event.getRawXpGained());
|
2013-10-18 14:31:00 +02:00
|
|
|
}
|
|
|
|
|
2013-10-19 14:08:04 +02:00
|
|
|
return !isCancelled;
|
2013-10-18 14:31:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static McMMOPlayerAbilityDeactivateEvent callAbilityDeactivateEvent(Player player, AbilityType ability) {
|
|
|
|
McMMOPlayerAbilityDeactivateEvent event = new McMMOPlayerAbilityDeactivateEvent(player, SkillType.byAbility(ability));
|
|
|
|
mcMMO.p.getServer().getPluginManager().callEvent(event);
|
|
|
|
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static McMMOPlayerFishingTreasureEvent callFishingTreasureEvent(Player player, ItemStack treasureDrop, int treasureXp, Map<Enchantment, Integer> enchants) {
|
|
|
|
McMMOPlayerFishingTreasureEvent event = enchants.isEmpty() ? new McMMOPlayerFishingTreasureEvent(player, treasureDrop, treasureXp) : new McMMOPlayerMagicHunterEvent(player, treasureDrop, treasureXp, enchants);
|
|
|
|
mcMMO.p.getServer().getPluginManager().callEvent(event);
|
|
|
|
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static FakePlayerFishEvent callFakeFishEvent(Player player, Fish hook) {
|
|
|
|
FakePlayerFishEvent event = new FakePlayerFishEvent(player, null, hook, PlayerFishEvent.State.FISHING);
|
|
|
|
mcMMO.p.getServer().getPluginManager().callEvent(event);
|
|
|
|
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static McMMOPlayerRepairCheckEvent callRepairCheckEvent(Player player, short durability, ItemStack repairMaterial, ItemStack repairedObject) {
|
|
|
|
McMMOPlayerRepairCheckEvent event = new McMMOPlayerRepairCheckEvent(player, durability, repairMaterial, repairedObject);
|
|
|
|
mcMMO.p.getServer().getPluginManager().callEvent(event);
|
|
|
|
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static McMMOPlayerDeathPenaltyEvent callDeathPenaltyEvent(Player player) {
|
|
|
|
McMMOPlayerDeathPenaltyEvent event = new McMMOPlayerDeathPenaltyEvent(player);
|
|
|
|
mcMMO.p.getServer().getPluginManager().callEvent(event);
|
|
|
|
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static McMMOPlayerDisarmEvent callDisarmEvent(Player defender) {
|
|
|
|
McMMOPlayerDisarmEvent event = new McMMOPlayerDisarmEvent(defender);
|
|
|
|
mcMMO.p.getServer().getPluginManager().callEvent(event);
|
|
|
|
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
}
|