Fixed exp earned when crafting items

This commit is contained in:
Indyuce 2020-08-27 13:18:43 +02:00
parent aecf4f7f83
commit 6cdd7608d5
13 changed files with 76 additions and 72 deletions

View File

@ -36,18 +36,16 @@ public class BrewPotionExperienceSource extends ExperienceSource<PotionMeta> {
@Override @Override
public boolean matches(PlayerData player, PotionMeta meta) { public boolean matches(PlayerData player, PotionMeta meta) {
return hasRightClass(player) && (types.isEmpty() || new ArrayList<>(types).stream().filter(type -> meta.getBasePotionData().getType() == type).count() > 0); return hasRightClass(player)
&& (types.isEmpty() || new ArrayList<>(types).stream().filter(type -> meta.getBasePotionData().getType() == type).count() > 0);
} }
@Override @Override
public ExperienceManager<BrewPotionExperienceSource> newManager() { public ExperienceManager<BrewPotionExperienceSource> newManager() {
return new ExperienceManager<BrewPotionExperienceSource>() { return new ExperienceManager<BrewPotionExperienceSource>() {
@EventHandler(priority = EventPriority.HIGH) @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void a(BrewEvent event) { public void a(BrewEvent event) {
if (event.isCancelled())
return;
Optional<Player> playerOpt = getNearbyPlayer(event.getBlock().getLocation(), 10); Optional<Player> playerOpt = getNearbyPlayer(event.getBlock().getLocation(), 10);
if (!playerOpt.isPresent()) if (!playerOpt.isPresent())
return; return;

View File

@ -26,15 +26,12 @@ public class CraftItemExperienceSource extends SpecificExperienceSource<Material
public ExperienceManager<CraftItemExperienceSource> newManager() { public ExperienceManager<CraftItemExperienceSource> newManager() {
return new ExperienceManager<CraftItemExperienceSource>() { return new ExperienceManager<CraftItemExperienceSource>() {
@EventHandler(priority = EventPriority.HIGH) @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void a(CraftItemEvent event) { public void a(CraftItemEvent event) {
if (event.isCancelled())
return;
PlayerData data = PlayerData.get((Player) event.getWhoClicked()); PlayerData data = PlayerData.get((Player) event.getWhoClicked());
for (CraftItemExperienceSource source : getSources()) for (CraftItemExperienceSource source : getSources())
if (source.matches(data, event.getInventory().getResult().getType())) if (source.matches(data, event.getInventory().getResult().getType()))
source.giveExperience(data, event.getInventory().getLocation()); source.giveExperience(data, event.getInventory().getResult().getAmount(), event.getInventory().getLocation());
} }
}; };
} }

View File

@ -40,11 +40,8 @@ public class EnchantItemExperienceSource extends ExperienceSource<Void> {
public ExperienceManager<EnchantItemExperienceSource> newManager() { public ExperienceManager<EnchantItemExperienceSource> newManager() {
return new ExperienceManager<EnchantItemExperienceSource>() { return new ExperienceManager<EnchantItemExperienceSource>() {
@EventHandler(priority = EventPriority.HIGH) @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void a(EnchantItemEvent event) { public void a(EnchantItemEvent event) {
if (event.isCancelled())
return;
PlayerData player = PlayerData.get(event.getEnchanter()); PlayerData player = PlayerData.get(event.getEnchanter());
for (EnchantItemExperienceSource source : getSources()) for (EnchantItemExperienceSource source : getSources())
if (source.matches(player, null)) { if (source.matches(player, null)) {

View File

@ -28,9 +28,9 @@ public class FishItemExperienceSource extends SpecificExperienceSource<ItemStack
public ExperienceManager<FishItemExperienceSource> newManager() { public ExperienceManager<FishItemExperienceSource> newManager() {
return new ExperienceManager<FishItemExperienceSource>() { return new ExperienceManager<FishItemExperienceSource>() {
@EventHandler(priority = EventPriority.HIGH) @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void a(PlayerFishEvent event) { public void a(PlayerFishEvent event) {
if (!event.isCancelled() && event.getState() == State.CAUGHT_FISH) { if (event.getState() == State.CAUGHT_FISH) {
ItemStack caught = ((Item) event.getCaught()).getItemStack(); ItemStack caught = ((Item) event.getCaught()).getItemStack();
if (caught.hasItemMeta()) if (caught.hasItemMeta())
return; return;
@ -38,7 +38,7 @@ public class FishItemExperienceSource extends SpecificExperienceSource<ItemStack
PlayerData data = PlayerData.get(event.getPlayer()); PlayerData data = PlayerData.get(event.getPlayer());
for (FishItemExperienceSource source : getSources()) for (FishItemExperienceSource source : getSources())
if (source.matches(data, caught)) if (source.matches(data, caught))
source.giveExperience(data, event.getHook().getLocation().add(0, 1.0f, 0)); source.giveExperience(data, caught.getAmount(), event.getHook().getLocation().add(0, 1.0f, 0));
} }
} }
}; };

View File

@ -28,19 +28,18 @@ public class KillMobExperienceSource extends SpecificExperienceSource<Entity> {
@Override @Override
public ExperienceManager<KillMobExperienceSource> newManager() { public ExperienceManager<KillMobExperienceSource> newManager() {
return new ExperienceManager<KillMobExperienceSource>() { return new ExperienceManager<KillMobExperienceSource>() {
@EventHandler(priority = EventPriority.MONITOR) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void a(EntityKillEntityEvent event) { public void a(EntityKillEntityEvent event) {
Bukkit.getScheduler().runTaskLater(MMOCore.plugin, new Runnable() { Bukkit.getScheduler().runTaskLater(MMOCore.plugin, new Runnable() {
@Override @Override
public void run() { public void run() {
if (!event.getTarget().isDead()) return; if (event.getTarget().isDead() && event.getEntity() instanceof Player && !event.getEntity().hasMetadata("NPC")
if (event.getEntity() instanceof Player && !event.getEntity().hasMetadata("NPC")) { && !event.getTarget().hasMetadata("spawner_spawned")) {
if(event.getTarget().hasMetadata("spawner_spawned")) return;
PlayerData data = PlayerData.get((Player) event.getEntity()); PlayerData data = PlayerData.get((Player) event.getEntity());
for (KillMobExperienceSource source : getSources()) for (KillMobExperienceSource source : getSources())
if (source.matches(data, event.getTarget())) if (source.matches(data, event.getTarget()))
source.giveExperience(data, event.getTarget().getLocation()); source.giveExperience(data, 1, event.getTarget().getLocation());
} }
} }
}, 2); }, 2);

View File

@ -12,8 +12,8 @@ import net.Indyuce.mmocore.api.experience.Profession;
import net.Indyuce.mmocore.api.experience.source.type.SpecificExperienceSource; import net.Indyuce.mmocore.api.experience.source.type.SpecificExperienceSource;
import net.Indyuce.mmocore.api.player.PlayerData; import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.manager.profession.ExperienceManager; import net.Indyuce.mmocore.manager.profession.ExperienceManager;
import net.mmogroup.mmolib.api.MMOLineConfig;
import net.mmogroup.mmolib.MMOLib; import net.mmogroup.mmolib.MMOLib;
import net.mmogroup.mmolib.api.MMOLineConfig;
public class MineBlockExperienceSource extends SpecificExperienceSource<Material> { public class MineBlockExperienceSource extends SpecificExperienceSource<Material> {
public final Material material; public final Material material;
@ -35,14 +35,14 @@ public class MineBlockExperienceSource extends SpecificExperienceSource<Material
public ExperienceManager<MineBlockExperienceSource> newManager() { public ExperienceManager<MineBlockExperienceSource> newManager() {
return new ExperienceManager<MineBlockExperienceSource>() { return new ExperienceManager<MineBlockExperienceSource>() {
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void a(BlockBreakEvent event) { public void a(BlockBreakEvent event) {
if (event.isCancelled() || event.getPlayer().getGameMode() != GameMode.SURVIVAL) if (event.getPlayer().getGameMode() != GameMode.SURVIVAL)
return; return;
PlayerData data = PlayerData.get(event.getPlayer()); PlayerData data = PlayerData.get(event.getPlayer());
for (MineBlockExperienceSource source : getSources()) for (MineBlockExperienceSource source : getSources()) {
{
if (source.silkTouch && hasSilkTouch(event.getPlayer().getInventory().getItemInMainHand())) if (source.silkTouch && hasSilkTouch(event.getPlayer().getInventory().getItemInMainHand()))
continue; continue;
if (source.crop && !MMOLib.plugin.getVersion().getWrapper().isCropFullyGrown(event.getBlock())) if (source.crop && !MMOLib.plugin.getVersion().getWrapper().isCropFullyGrown(event.getBlock()))
@ -51,7 +51,7 @@ public class MineBlockExperienceSource extends SpecificExperienceSource<Material
continue; continue;
if (source.matches(data, event.getBlock().getType())) if (source.matches(data, event.getBlock().getType()))
source.giveExperience(data, event.getBlock().getLocation()); source.giveExperience(data, 1, event.getBlock().getLocation());
} }
} }
}; };

View File

@ -26,16 +26,16 @@ public class PlaceBlockExperienceSource extends SpecificExperienceSource<Materia
public ExperienceManager<PlaceBlockExperienceSource> newManager() { public ExperienceManager<PlaceBlockExperienceSource> newManager() {
return new ExperienceManager<PlaceBlockExperienceSource>() { return new ExperienceManager<PlaceBlockExperienceSource>() {
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void a(BlockPlaceEvent event) { public void a(BlockPlaceEvent event) {
if (event.isCancelled() || event.getPlayer().getGameMode() != GameMode.SURVIVAL) if (event.getPlayer().getGameMode() != GameMode.SURVIVAL)
return; return;
PlayerData data = PlayerData.get(event.getPlayer()); PlayerData data = PlayerData.get(event.getPlayer());
for (PlaceBlockExperienceSource source : getSources()) { for (PlaceBlockExperienceSource source : getSources())
if (source.matches(data, event.getBlock().getType())) if (source.matches(data, event.getBlock().getType()))
source.giveExperience(data, event.getBlock().getLocation()); source.giveExperience(data, 1, event.getBlock().getLocation());
}
} }
}; };
} }

View File

@ -39,9 +39,9 @@ public class RepairItemExperienceSource extends ExperienceSource<ItemStack> {
public ExperienceManager<RepairItemExperienceSource> newManager() { public ExperienceManager<RepairItemExperienceSource> newManager() {
return new ExperienceManager<RepairItemExperienceSource>() { return new ExperienceManager<RepairItemExperienceSource>() {
@EventHandler(priority = EventPriority.HIGH) @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void a(InventoryClickEvent event) { public void a(InventoryClickEvent event) {
if (!event.isCancelled() && event.getInventory() != null && event.getInventory().getType() == InventoryType.ANVIL && event.getSlot() == 2) { if (event.getInventory() != null && event.getInventory().getType() == InventoryType.ANVIL && event.getSlot() == 2) {
ItemStack item = event.getCurrentItem(); ItemStack item = event.getCurrentItem();
PlayerData data = PlayerData.get((Player) event.getWhoClicked()); PlayerData data = PlayerData.get((Player) event.getWhoClicked());
@ -49,18 +49,19 @@ public class RepairItemExperienceSource extends ExperienceSource<ItemStack> {
for (RepairItemExperienceSource source : getSources()) for (RepairItemExperienceSource source : getSources())
if (source.matches(data, item)) { if (source.matches(data, item)) {
if(!(event.getInventory() instanceof AnvilInventory)) if (!(event.getInventory() instanceof AnvilInventory))
return; return;
if(((AnvilInventory) event.getInventory()).getRepairCost() > if (((AnvilInventory) event.getInventory()).getRepairCost() > ((Player) event.getWhoClicked()).getLevel())
((Player) event.getWhoClicked()).getLevel()) return; return;
/* /*
* make sure the items can actually be repaired * make sure the items can actually be repaired
* before getting the amount of durability repaired * before getting the amount of durability repaired
*/ */
ItemStack old = event.getInventory().getItem(0); ItemStack old = event.getInventory().getItem(0);
if (old == null || old.getType() == Material.AIR) return; if (old == null || old.getType() == Material.AIR)
return;
if (old.getType().getMaxDurability() < 30 || item.getType().getMaxDurability() < 10) if (old.getType().getMaxDurability() < 30 || item.getType().getMaxDurability() < 10)
return; return;
@ -72,7 +73,8 @@ public class RepairItemExperienceSource extends ExperienceSource<ItemStack> {
* was repaired, substract damage from old item * was repaired, substract damage from old item
* durability. * durability.
*/ */
double exp = MMOCore.plugin.smithingManager.getBaseExperience(item.getType()) * Math.max(0, ((Damageable) old.getItemMeta()).getDamage() - ((Damageable) item.getItemMeta()).getDamage()) / 100; double exp = MMOCore.plugin.smithingManager.getBaseExperience(item.getType())
* Math.max(0, ((Damageable) old.getItemMeta()).getDamage() - ((Damageable) item.getItemMeta()).getDamage()) / 100;
giveExperience(data, (int) exp, null); giveExperience(data, (int) exp, null);
} }
} }

View File

@ -30,9 +30,8 @@ public class SmeltItemExperienceSource extends SpecificExperienceSource<ItemStac
public ExperienceManager<SmeltItemExperienceSource> newManager() { public ExperienceManager<SmeltItemExperienceSource> newManager() {
return new ExperienceManager<SmeltItemExperienceSource>() { return new ExperienceManager<SmeltItemExperienceSource>() {
@EventHandler(priority = EventPriority.HIGH) @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void a(BlockCookEvent event) { public void a(BlockCookEvent event) {
if (!event.isCancelled()) {
Optional<Player> player = getNearestPlayer(event.getBlock().getLocation(), 10); Optional<Player> player = getNearestPlayer(event.getBlock().getLocation(), 10);
if (!player.isPresent()) if (!player.isPresent())
return; return;
@ -44,21 +43,20 @@ public class SmeltItemExperienceSource extends SpecificExperienceSource<ItemStac
PlayerData data = PlayerData.get(player.get()); PlayerData data = PlayerData.get(player.get());
for (SmeltItemExperienceSource source : getSources()) for (SmeltItemExperienceSource source : getSources())
if (source.matches(data, caught)) if (source.matches(data, caught))
source.giveExperience(data, event.getBlock().getLocation()); source.giveExperience(data, 1, event.getBlock().getLocation());
}
} }
}; };
} }
private Optional<Player> getNearestPlayer(Location loc, double d) { private Optional<Player> getNearestPlayer(Location loc, double d) {
final double d2 = d * d; final double d2 = d * d;
final Player[] nearby = loc.getWorld().getPlayers().stream() final Player[] nearby = loc.getWorld().getPlayers().stream().filter(player -> player.getLocation().distanceSquared(loc) < d2)
.filter(player -> player.getLocation().distanceSquared(loc) < d2).toArray(Player[]::new); .toArray(Player[]::new);
Player selected = null; Player selected = null;
double lastDist = d2; double lastDist = d2;
for(Player p : nearby) { for (Player p : nearby) {
double currDist = p.getLocation().distance(loc); double currDist = p.getLocation().distance(loc);
if(currDist < lastDist) { if (currDist < lastDist) {
lastDist = currDist; lastDist = currDist;
selected = p; selected = p;
} }

View File

@ -25,6 +25,7 @@ public abstract class ExperienceSource<T> {
this.profess = profess; this.profess = profess;
} }
// TODO remove setter
public void setClass(PlayerClass profess) { public void setClass(PlayerClass profess) {
this.profess = profess; this.profess = profess;
} }

View File

@ -10,9 +10,10 @@ import net.mmogroup.mmolib.api.MMOLineConfig;
public abstract class SpecificExperienceSource<T> extends ExperienceSource<T> { public abstract class SpecificExperienceSource<T> extends ExperienceSource<T> {
private final RandomAmount amount; private final RandomAmount amount;
/* /**
* not all experience sources have to specify a random experience amount e.g * Used to register experience sources with SPECIFIC experience outputs.
* ENCHANT and ALCHEMY experience depend on the enchanted item. * Other experience sources like ENCHANT have their exp output depend on the
* enchanted item. ALCHEMY exp outputs depend on the potion crafted
*/ */
public SpecificExperienceSource(Profession profession, MMOLineConfig config) { public SpecificExperienceSource(Profession profession, MMOLineConfig config) {
super(profession); super(profession);
@ -29,7 +30,18 @@ public abstract class SpecificExperienceSource<T> extends ExperienceSource<T> {
return amount.calculateInt(); return amount.calculateInt();
} }
public void giveExperience(PlayerData player, Location loc) { /**
giveExperience(player, rollAmount(), loc); * Used when a player needs to gain experience after performing the action
* corresponding to this exp source
*
* @param multiplier
* Used by the CraftItem experience source, multiplies the exp
* earned by a certain factor. When crafting an item, the
* multiplier is equal to the amount of items crafted
* @param loc
* Location used to display the exp hologram
*/
public void giveExperience(PlayerData player, int multiplier, Location loc) {
giveExperience(player, rollAmount() * multiplier, loc);
} }
} }

View File

@ -37,7 +37,7 @@ public class KillMythicFactionExperienceSource extends SpecificExperienceSource<
PlayerData data = PlayerData.get((Player) event.getKiller()); PlayerData data = PlayerData.get((Player) event.getKiller());
for (KillMythicFactionExperienceSource source : getSources()) for (KillMythicFactionExperienceSource source : getSources())
if (source.matches(data, event.getMob().getFaction())) if (source.matches(data, event.getMob().getFaction()))
source.giveExperience(data, event.getEntity().getLocation()); source.giveExperience(data, 1, event.getEntity().getLocation());
} }
}, 2); }, 2);
} }

View File

@ -36,7 +36,7 @@ public class KillMythicMobExperienceSource extends SpecificExperienceSource<Stri
PlayerData data = PlayerData.get((Player) event.getKiller()); PlayerData data = PlayerData.get((Player) event.getKiller());
for (KillMythicMobExperienceSource source : getSources()) for (KillMythicMobExperienceSource source : getSources())
if (source.matches(data, event.getMobType().getInternalName())) if (source.matches(data, event.getMobType().getInternalName()))
source.giveExperience(data, event.getEntity().getLocation()); source.giveExperience(data, 1, event.getEntity().getLocation());
} }
}, 2); }, 2);
} }