Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add event for players taming an entity #5142

Merged
merged 2 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.gmail.nossr50.events.skills.taming;

import com.gmail.nossr50.datatypes.experience.XPGainReason;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import com.gmail.nossr50.events.experience.McMMOPlayerExperienceEvent;
import com.google.common.base.Preconditions;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

/**
* Called when a player has tamed an entity, and we are about to award them experience for it.
*/
public class McMMOPlayerTameEntityEvent extends McMMOPlayerExperienceEvent {
private static final HandlerList HANDLER_LIST = new HandlerList();

private float xpGained;
private final Entity tamedEntity;

@ApiStatus.Internal
Warriorrrr marked this conversation as resolved.
Show resolved Hide resolved
public McMMOPlayerTameEntityEvent(@NotNull Player player, float xp, @NotNull Entity tamedEntity) {
Warriorrrr marked this conversation as resolved.
Show resolved Hide resolved
super(player, PrimarySkillType.TAMING, XPGainReason.PVE);
this.xpGained = xp;
this.tamedEntity = tamedEntity;
}

/**
* @return The raw experience that the player will receive.
*/
public float getXpGained() {
return this.xpGained;
}

/**
* @param xpGained The raw experience that the player will receive.
* @throws IllegalArgumentException if xpGained is NaN or infinite.
*/
public void setXpGained(float xpGained) {
Preconditions.checkArgument(Float.isFinite(xpGained), "new gained xp must be a number");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't need this check since you are passing in a primitive type, instead it would be a good idea to make sure the float is a positive number

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a check for positive numbers, but the isFinite one is still useful because a primitive float can be NaN/infinite

Copy link
Member

@nossr50 nossr50 Jan 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Warriorrrr Thank you for that info, it seems you are correct on this, my mistake!


this.xpGained = xpGained;
}

@NotNull
public Entity getTamedEntity() {
return tamedEntity;
}

/**
* @apiNote Cancelling this event prevents experience from being awarded, but the entity will remain tamed.
*/
@Override
public void setCancelled(boolean cancelled) {
super.setCancelled(cancelled);
}

public static HandlerList getHandlerList() {
return HANDLER_LIST;
}

@Override
public @NotNull HandlerList getHandlers() {
return HANDLER_LIST;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import com.gmail.nossr50.config.experience.ExperienceConfig;
import com.gmail.nossr50.datatypes.experience.XPGainReason;
import com.gmail.nossr50.datatypes.experience.XPGainSource;
import com.gmail.nossr50.datatypes.interactions.NotificationType;
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import com.gmail.nossr50.datatypes.skills.SubSkillType;
import com.gmail.nossr50.datatypes.skills.subskills.taming.CallOfTheWildType;
import com.gmail.nossr50.datatypes.skills.subskills.taming.TamingSummon;
import com.gmail.nossr50.events.skills.taming.McMMOPlayerTameEntityEvent;
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.metadata.MobMetaFlagType;
Expand Down Expand Up @@ -136,7 +138,13 @@ public boolean canUseBeastLore() {
* @param entity The LivingEntity to award XP for
*/
public void awardTamingXP(@NotNull LivingEntity entity) {
applyXpGain(ExperienceConfig.getInstance().getTamingXP(entity.getType()), XPGainReason.PVE);
int xp = ExperienceConfig.getInstance().getTamingXP(entity.getType());

final McMMOPlayerTameEntityEvent event = new McMMOPlayerTameEntityEvent(getPlayer(), xp, entity);
mcMMO.p.getServer().getPluginManager().callEvent(event);

if (!event.isCancelled())
applyXpGain(event.getXpGained(), XPGainReason.PVE, XPGainSource.SELF);
}

/**
Expand Down
Loading