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
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
public ExperienceManager<BrewPotionExperienceSource> newManager() {
return new ExperienceManager<BrewPotionExperienceSource>() {
@EventHandler(priority = EventPriority.HIGH)
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void a(BrewEvent event) {
if (event.isCancelled())
return;
Optional<Player> playerOpt = getNearbyPlayer(event.getBlock().getLocation(), 10);
if (!playerOpt.isPresent())
return;

View File

@ -26,15 +26,12 @@ public class CraftItemExperienceSource extends SpecificExperienceSource<Material
public ExperienceManager<CraftItemExperienceSource> newManager() {
return new ExperienceManager<CraftItemExperienceSource>() {
@EventHandler(priority = EventPriority.HIGH)
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void a(CraftItemEvent event) {
if (event.isCancelled())
return;
PlayerData data = PlayerData.get((Player) event.getWhoClicked());
for (CraftItemExperienceSource source : getSources())
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() {
return new ExperienceManager<EnchantItemExperienceSource>() {
@EventHandler(priority = EventPriority.HIGH)
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void a(EnchantItemEvent event) {
if (event.isCancelled())
return;
PlayerData player = PlayerData.get(event.getEnchanter());
for (EnchantItemExperienceSource source : getSources())
if (source.matches(player, null)) {

View File

@ -28,9 +28,9 @@ public class FishItemExperienceSource extends SpecificExperienceSource<ItemStack
public ExperienceManager<FishItemExperienceSource> newManager() {
return new ExperienceManager<FishItemExperienceSource>() {
@EventHandler(priority = EventPriority.HIGH)
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
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();
if (caught.hasItemMeta())
return;
@ -38,7 +38,7 @@ public class FishItemExperienceSource extends SpecificExperienceSource<ItemStack
PlayerData data = PlayerData.get(event.getPlayer());
for (FishItemExperienceSource source : getSources())
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
public ExperienceManager<KillMobExperienceSource> newManager() {
return new ExperienceManager<KillMobExperienceSource>() {
@EventHandler(priority = EventPriority.MONITOR)
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void a(EntityKillEntityEvent event) {
Bukkit.getScheduler().runTaskLater(MMOCore.plugin, new Runnable() {
@Override
public void run() {
if (!event.getTarget().isDead()) return;
if (event.getEntity() instanceof Player && !event.getEntity().hasMetadata("NPC")) {
if(event.getTarget().hasMetadata("spawner_spawned")) return;
if (event.getTarget().isDead() && event.getEntity() instanceof Player && !event.getEntity().hasMetadata("NPC")
&& !event.getTarget().hasMetadata("spawner_spawned")) {
PlayerData data = PlayerData.get((Player) event.getEntity());
for (KillMobExperienceSource source : getSources())
if (source.matches(data, event.getTarget()))
source.giveExperience(data, event.getTarget().getLocation());
source.giveExperience(data, 1, event.getTarget().getLocation());
}
}
}, 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.player.PlayerData;
import net.Indyuce.mmocore.manager.profession.ExperienceManager;
import net.mmogroup.mmolib.api.MMOLineConfig;
import net.mmogroup.mmolib.MMOLib;
import net.mmogroup.mmolib.api.MMOLineConfig;
public class MineBlockExperienceSource extends SpecificExperienceSource<Material> {
public final Material material;
@ -35,14 +35,14 @@ public class MineBlockExperienceSource extends SpecificExperienceSource<Material
public ExperienceManager<MineBlockExperienceSource> newManager() {
return new ExperienceManager<MineBlockExperienceSource>() {
@EventHandler(priority = EventPriority.HIGHEST)
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void a(BlockBreakEvent event) {
if (event.isCancelled() || event.getPlayer().getGameMode() != GameMode.SURVIVAL)
if (event.getPlayer().getGameMode() != GameMode.SURVIVAL)
return;
PlayerData data = PlayerData.get(event.getPlayer());
for (MineBlockExperienceSource source : getSources())
{
for (MineBlockExperienceSource source : getSources()) {
if (source.silkTouch && hasSilkTouch(event.getPlayer().getInventory().getItemInMainHand()))
continue;
if (source.crop && !MMOLib.plugin.getVersion().getWrapper().isCropFullyGrown(event.getBlock()))
@ -51,7 +51,7 @@ public class MineBlockExperienceSource extends SpecificExperienceSource<Material
continue;
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() {
return new ExperienceManager<PlaceBlockExperienceSource>() {
@EventHandler(priority = EventPriority.HIGHEST)
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void a(BlockPlaceEvent event) {
if (event.isCancelled() || event.getPlayer().getGameMode() != GameMode.SURVIVAL)
if (event.getPlayer().getGameMode() != GameMode.SURVIVAL)
return;
PlayerData data = PlayerData.get(event.getPlayer());
for (PlaceBlockExperienceSource source : getSources()) {
for (PlaceBlockExperienceSource source : getSources())
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() {
return new ExperienceManager<RepairItemExperienceSource>() {
@EventHandler(priority = EventPriority.HIGH)
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
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();
PlayerData data = PlayerData.get((Player) event.getWhoClicked());
@ -49,18 +49,19 @@ public class RepairItemExperienceSource extends ExperienceSource<ItemStack> {
for (RepairItemExperienceSource source : getSources())
if (source.matches(data, item)) {
if(!(event.getInventory() instanceof AnvilInventory))
if (!(event.getInventory() instanceof AnvilInventory))
return;
if(((AnvilInventory) event.getInventory()).getRepairCost() >
((Player) event.getWhoClicked()).getLevel()) return;
if (((AnvilInventory) event.getInventory()).getRepairCost() > ((Player) event.getWhoClicked()).getLevel())
return;
/*
* make sure the items can actually be repaired
* before getting the amount of durability repaired
*/
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)
return;
@ -72,7 +73,8 @@ public class RepairItemExperienceSource extends ExperienceSource<ItemStack> {
* was repaired, substract damage from old item
* 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);
}
}

View File

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

View File

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

View File

@ -10,9 +10,10 @@ import net.mmogroup.mmolib.api.MMOLineConfig;
public abstract class SpecificExperienceSource<T> extends ExperienceSource<T> {
private final RandomAmount amount;
/*
* not all experience sources have to specify a random experience amount e.g
* ENCHANT and ALCHEMY experience depend on the enchanted item.
/**
* Used to register experience sources with SPECIFIC experience outputs.
* 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) {
super(profession);
@ -29,7 +30,18 @@ public abstract class SpecificExperienceSource<T> extends ExperienceSource<T> {
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());
for (KillMythicFactionExperienceSource source : getSources())
if (source.matches(data, event.getMob().getFaction()))
source.giveExperience(data, event.getEntity().getLocation());
source.giveExperience(data, 1, event.getEntity().getLocation());
}
}, 2);
}

View File

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