UltimateStacker/UltimateStacker-Plugin/src/main/java/com.craftaro.ultimatestacker/listeners/InteractListeners.java

140 lines
5.6 KiB
Java
Raw Normal View History

2023-05-25 19:20:03 +02:00
package com.craftaro.ultimatestacker.listeners;
import com.craftaro.core.compatibility.ServerVersion;
2024-01-07 12:16:27 +01:00
import com.craftaro.third_party.com.cryptomorin.xseries.XMaterial;
2023-05-25 19:20:03 +02:00
import com.craftaro.ultimatestacker.UltimateStacker;
import com.craftaro.ultimatestacker.api.stack.entity.EntityStack;
import com.craftaro.ultimatestacker.settings.Settings;
import com.craftaro.ultimatestacker.stackable.entity.Split;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.*;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractAtEntityEvent;
import org.bukkit.inventory.ItemStack;
2018-12-17 19:32:57 +01:00
import org.bukkit.metadata.FixedMetadataValue;
public class InteractListeners implements Listener {
2019-07-31 06:29:10 +02:00
private final UltimateStacker plugin;
2019-07-31 06:29:10 +02:00
public InteractListeners(UltimateStacker plugin) {
this.plugin = plugin;
}
2023-05-05 10:57:17 +02:00
@EventHandler
public void onInteract(PlayerInteractAtEntityEvent event) {
if (!(event.getRightClicked() instanceof LivingEntity)) return;
Player player = event.getPlayer();
LivingEntity entity = (LivingEntity)event.getRightClicked();
2018-11-30 02:09:59 +01:00
ItemStack item = player.getInventory().getItemInHand();
if (!plugin.getEntityStackManager().isStackedEntity(entity)) return;
2018-12-17 19:32:57 +01:00
if (item.getType() != Material.NAME_TAG && !correctFood(item, entity)) return;
2023-05-25 19:20:03 +02:00
EntityStack stack = plugin.getEntityStackManager().getStackedEntity(entity);
if (stack.getAmount() <= 1
|| item.getType() == Material.NAME_TAG
2019-09-07 23:55:16 +02:00
&& Settings.SPLIT_CHECKS.getStringList().stream().noneMatch(line -> Split.valueOf(line) == Split.NAME_TAG)
|| item.getType() != Material.NAME_TAG
2019-09-07 23:55:16 +02:00
&& Settings.SPLIT_CHECKS.getStringList().stream().noneMatch(line -> Split.valueOf(line) == Split.ENTITY_BREED))
return;
2018-12-17 19:32:57 +01:00
if (item.getType() == Material.NAME_TAG)
event.setCancelled(true);
2019-05-09 23:46:24 +02:00
else if (entity instanceof Ageable && !((Ageable) entity).isAdult())
return;
2020-08-25 01:01:11 +02:00
stack.releaseHost();
2018-12-17 19:32:57 +01:00
if (item.getType() == Material.NAME_TAG) {
entity.setCustomName(item.getItemMeta().getDisplayName());
} else {
2019-05-09 23:46:24 +02:00
if (entity instanceof Ageable
&& !((Ageable) entity).isAdult()) {
return;
}
2019-07-31 06:29:10 +02:00
entity.setMetadata("inLove", new FixedMetadataValue(plugin, true));
2018-12-17 19:32:57 +01:00
2019-07-31 06:29:10 +02:00
Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, () -> {
2018-12-17 19:32:57 +01:00
if (entity.isDead()) return;
2019-07-31 06:29:10 +02:00
entity.removeMetadata("inLove", plugin);
2018-12-17 19:32:57 +01:00
}, 20 * 20);
}
}
2018-12-17 19:32:57 +01:00
private boolean correctFood(ItemStack is, Entity entity) {
Material type = is.getType();
switch (entity.getType().name()) {
case "COW":
2019-10-22 18:43:21 +02:00
case "MUSHROOM_COW":
case "SHEEP":
2018-12-17 19:32:57 +01:00
return type == Material.WHEAT;
case "PIG":
return type == Material.CARROT || type == Material.BEETROOT || type == Material.POTATO;
case "CHICKEN":
return type == Material.WHEAT_SEEDS
2018-12-17 19:32:57 +01:00
|| type == Material.MELON_SEEDS
2019-09-10 23:01:55 +02:00
|| type == Material.PUMPKIN_SEEDS
|| type == Material.BEETROOT_SEEDS;
case "HORSE":
case "DONKEY":
case "MULE":
return (type == Material.GOLDEN_APPLE || type == Material.GOLDEN_CARROT) && ((AbstractHorse) entity).isTamed();
case "WOLF":
return type == Material.BEEF
|| type == Material.CHICKEN
|| type == Material.COD
|| type == Material.MUTTON
|| type == Material.PORKCHOP
|| type == Material.RABBIT
|| type == Material.SALMON
|| type == Material.COOKED_BEEF
|| type == Material.COOKED_CHICKEN
|| type == Material.COOKED_COD
|| type == Material.COOKED_MUTTON
|| type == Material.COOKED_PORKCHOP
|| type == Material.COOKED_RABBIT
|| type == Material.COOKED_SALMON
|| type == Material.ROTTEN_FLESH
2018-12-17 20:20:22 +01:00
&& ((Wolf) entity).isTamed();
case "OCELOT":
case "CAT":
return (type == Material.COD || type == Material.SALMON) && ((Tameable) entity).isTamed();
case "PANDA":
return type == Material.BAMBOO;
case "FOX":
return type == Material.SWEET_BERRIES || type == Material.GLOW_BERRIES;
case "RABBIT":
return type == Material.CARROT || type == Material.GOLDEN_CARROT || type == Material.DANDELION;
case "LLAMA":
case "TRADER_LLAMA":
2018-12-17 19:32:57 +01:00
return type == Material.HAY_BLOCK;
case "TURTLE":
return type == Material.SEAGRASS;
case "HOGLIN":
return type == Material.CRIMSON_FUNGUS;
case "STRIDER":
return type == Material.WARPED_FUNGUS;
case "BEE":
return type == Material.HONEYCOMB || type == Material.HONEY_BOTTLE;
case "AXOLOTL":
return type == Material.TROPICAL_FISH_BUCKET;
case "GOAT":
return type == Material.WHEAT;
case "GLOW_SQUID":
return type == Material.GLOW_INK_SAC;
case "CAMEL":
return type == Material.CACTUS;
2020-09-01 20:54:43 +02:00
default:
return false;
2018-12-17 19:32:57 +01:00
}
}
}