mirror of
https://github.com/StarWishsama/Slimefun4.git
synced 2024-09-20 03:35:51 +00:00
Made handler and listener
This commit is contained in:
parent
709b329c78
commit
2e91ebe0d2
@ -29,6 +29,7 @@
|
||||
#### Additions
|
||||
* The speed of the Ancient Altar can now be configured in the `Items.yml` file
|
||||
* The message "You do not have enough knowledge to understand this" now includes the name of the item you need to research.
|
||||
* (API) Added SlimefunItemHitListener
|
||||
|
||||
#### Changes
|
||||
* Some performance optimizations to Cargo networks
|
||||
|
@ -0,0 +1,28 @@
|
||||
package io.github.thebusybiscuit.slimefun4.core.handlers;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import me.mrCookieSlime.Slimefun.Objects.handlers.ItemHandler;
|
||||
|
||||
/**
|
||||
* This is triggered when a {@link Player} attacks an {@link Entity}.
|
||||
*
|
||||
* @author Mooy1
|
||||
*
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface EntityHitHandler extends ItemHandler {
|
||||
|
||||
void onHit(@Nonnull EntityDamageByEntityEvent e, @Nonnull Player player, @Nonnull ItemStack item);
|
||||
|
||||
@Override
|
||||
default Class<? extends ItemHandler> getIdentifier() {
|
||||
return EntityHitHandler.class;
|
||||
}
|
||||
|
||||
}
|
@ -61,7 +61,6 @@ import io.github.thebusybiscuit.slimefun4.implementation.items.electric.reactors
|
||||
import io.github.thebusybiscuit.slimefun4.implementation.items.magical.BeeWings;
|
||||
import io.github.thebusybiscuit.slimefun4.implementation.items.tools.GrapplingHook;
|
||||
import io.github.thebusybiscuit.slimefun4.implementation.items.weapons.SeismicAxe;
|
||||
import io.github.thebusybiscuit.slimefun4.implementation.items.weapons.VampireBlade;
|
||||
import io.github.thebusybiscuit.slimefun4.implementation.listeners.AncientAltarListener;
|
||||
import io.github.thebusybiscuit.slimefun4.implementation.listeners.AutoCrafterListener;
|
||||
import io.github.thebusybiscuit.slimefun4.implementation.listeners.BackpackListener;
|
||||
@ -90,11 +89,11 @@ import io.github.thebusybiscuit.slimefun4.implementation.listeners.SeismicAxeLis
|
||||
import io.github.thebusybiscuit.slimefun4.implementation.listeners.SlimefunBootsListener;
|
||||
import io.github.thebusybiscuit.slimefun4.implementation.listeners.SlimefunBowListener;
|
||||
import io.github.thebusybiscuit.slimefun4.implementation.listeners.SlimefunGuideListener;
|
||||
import io.github.thebusybiscuit.slimefun4.implementation.listeners.SlimefunItemHitListener;
|
||||
import io.github.thebusybiscuit.slimefun4.implementation.listeners.SlimefunItemConsumeListener;
|
||||
import io.github.thebusybiscuit.slimefun4.implementation.listeners.SlimefunItemInteractListener;
|
||||
import io.github.thebusybiscuit.slimefun4.implementation.listeners.SoulboundListener;
|
||||
import io.github.thebusybiscuit.slimefun4.implementation.listeners.TalismanListener;
|
||||
import io.github.thebusybiscuit.slimefun4.implementation.listeners.VampireBladeListener;
|
||||
import io.github.thebusybiscuit.slimefun4.implementation.listeners.VillagerTradingListener;
|
||||
import io.github.thebusybiscuit.slimefun4.implementation.listeners.crafting.AnvilListener;
|
||||
import io.github.thebusybiscuit.slimefun4.implementation.listeners.crafting.BrewingStandListener;
|
||||
@ -645,6 +644,7 @@ public final class SlimefunPlugin extends JavaPlugin implements SlimefunAddon {
|
||||
new TalismanListener(this);
|
||||
new SoulboundListener(this);
|
||||
new AutoCrafterListener(this);
|
||||
new SlimefunItemHitListener(this);
|
||||
|
||||
// Bees were added in 1.15
|
||||
if (minecraftVersion.isAtLeast(MinecraftVersion.MINECRAFT_1_15)) {
|
||||
@ -658,7 +658,6 @@ public final class SlimefunPlugin extends JavaPlugin implements SlimefunAddon {
|
||||
}
|
||||
|
||||
// Item-specific Listeners
|
||||
new VampireBladeListener(this, (VampireBlade) SlimefunItems.BLADE_OF_VAMPIRES.getItem());
|
||||
new CoolerListener(this, (Cooler) SlimefunItems.COOLER.getItem());
|
||||
new SeismicAxeListener(this, (SeismicAxe) SlimefunItems.SEISMIC_AXE.getItem());
|
||||
new AncientAltarListener(this, (AncientAltar) SlimefunItems.ANCIENT_ALTAR.getItem(), (AncientPedestal) SlimefunItems.ANCIENT_PEDESTAL.getItem());
|
||||
|
@ -1,5 +1,7 @@
|
||||
package io.github.thebusybiscuit.slimefun4.implementation.items.weapons;
|
||||
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
|
||||
@ -11,10 +13,11 @@ import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import io.github.thebusybiscuit.slimefun4.api.items.ItemSetting;
|
||||
import io.github.thebusybiscuit.slimefun4.api.items.settings.IntRangeSetting;
|
||||
import io.github.thebusybiscuit.slimefun4.implementation.listeners.VampireBladeListener;
|
||||
import io.github.thebusybiscuit.slimefun4.core.handlers.EntityHitHandler;
|
||||
import io.github.thebusybiscuit.slimefun4.implementation.items.SimpleSlimefunItem;
|
||||
import io.github.thebusybiscuit.slimefun4.implementation.listeners.SlimefunItemHitListener;
|
||||
import me.mrCookieSlime.Slimefun.Lists.RecipeType;
|
||||
import me.mrCookieSlime.Slimefun.Objects.Category;
|
||||
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
|
||||
import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
|
||||
|
||||
/**
|
||||
@ -23,10 +26,10 @@ import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
|
||||
*
|
||||
* @author TheBusyBiscuit
|
||||
*
|
||||
* @see VampireBladeListener
|
||||
* @see SlimefunItemHitListener
|
||||
*
|
||||
*/
|
||||
public class VampireBlade extends SlimefunItem {
|
||||
public class VampireBlade extends SimpleSlimefunItem<EntityHitHandler> {
|
||||
|
||||
private static final double HEALING_AMOUNT = 4.0;
|
||||
|
||||
@ -39,6 +42,19 @@ public class VampireBlade extends SlimefunItem {
|
||||
addItemSetting(chance);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public EntityHitHandler getItemHandler() {
|
||||
return (e, p, item) -> {
|
||||
if (ThreadLocalRandom.current().nextInt(100) < getChance()) {
|
||||
p.playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 0.7F, 0.7F);
|
||||
double health = p.getHealth() + HEALING_AMOUNT;
|
||||
double maxHealth = p.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue();
|
||||
p.setHealth(Math.min(health, maxHealth));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* This method returns the chance of a {@link VampireBlade} to apply its healing effect.
|
||||
*
|
||||
@ -48,11 +64,4 @@ public class VampireBlade extends SlimefunItem {
|
||||
return chance.getValue();
|
||||
}
|
||||
|
||||
public void heal(@Nonnull Player p) {
|
||||
p.playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 0.7F, 0.7F);
|
||||
double health = p.getHealth() + HEALING_AMOUNT;
|
||||
double maxHealth = p.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue();
|
||||
p.setHealth(Math.min(health, maxHealth));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,51 @@
|
||||
package io.github.thebusybiscuit.slimefun4.implementation.listeners;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import io.github.thebusybiscuit.slimefun4.core.handlers.EntityHitHandler;
|
||||
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
|
||||
import io.github.thebusybiscuit.slimefun4.implementation.items.weapons.VampireBlade;
|
||||
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
|
||||
|
||||
/**
|
||||
* This {@link Listener} is responsible for calling the {@link EntityHitHandler}.
|
||||
*
|
||||
* @author Mooy1
|
||||
*
|
||||
* @see VampireBlade
|
||||
*
|
||||
*/
|
||||
public class SlimefunItemHitListener implements Listener {
|
||||
|
||||
public SlimefunItemHitListener(@Nonnull SlimefunPlugin plugin) {
|
||||
plugin.getServer().getPluginManager().registerEvents(this, plugin);
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
|
||||
public void onDamage(EntityDamageByEntityEvent e) {
|
||||
if (!(e.getDamager() instanceof Player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Player p = (Player) e.getDamager();
|
||||
|
||||
ItemStack item = p.getInventory().getItemInMainHand();
|
||||
|
||||
if (item.getType() != Material.AIR) {
|
||||
SlimefunItem sfItem = SlimefunItem.getByItem(item);
|
||||
|
||||
if (sfItem != null && sfItem.canUse(p, true)) {
|
||||
sfItem.callItemHandler(EntityHitHandler.class, handler -> handler.onHit(e, p, item));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
package io.github.thebusybiscuit.slimefun4.implementation.listeners;
|
||||
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
||||
import org.bukkit.potion.PotionEffect;
|
||||
|
||||
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
|
||||
import io.github.thebusybiscuit.slimefun4.implementation.items.weapons.VampireBlade;
|
||||
|
||||
/**
|
||||
* This {@link Listener} is exclusively used for the {@link VampireBlade}.
|
||||
* It handles the {@link PotionEffect}
|
||||
*
|
||||
* @author TheBusyBiscuit
|
||||
*
|
||||
* @see VampireBlade
|
||||
*
|
||||
*/
|
||||
public class VampireBladeListener implements Listener {
|
||||
|
||||
private final VampireBlade blade;
|
||||
|
||||
public VampireBladeListener(@Nonnull SlimefunPlugin plugin, @Nonnull VampireBlade blade) {
|
||||
plugin.getServer().getPluginManager().registerEvents(this, plugin);
|
||||
|
||||
this.blade = blade;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
|
||||
public void onDamage(EntityDamageByEntityEvent e) {
|
||||
if (blade.isDisabled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (e.getDamager() instanceof Player && ThreadLocalRandom.current().nextInt(100) < blade.getChance()) {
|
||||
Player p = (Player) e.getDamager();
|
||||
|
||||
if (blade.isItem(p.getInventory().getItemInMainHand())) {
|
||||
if (blade.canUse(p, true)) {
|
||||
blade.heal(p);
|
||||
} else {
|
||||
e.setCancelled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user