added levetation wand mechanic

This commit is contained in:
kai ohara 2025-02-07 17:51:16 -05:00
parent be99719fc6
commit 3d71110f37
15 changed files with 141 additions and 2 deletions

View File

@ -77,7 +77,8 @@ public final class CoswayUtil extends JavaPlugin implements Listener {
getServer().getPluginManager().registerEvents(new PhantomDodge(this), this);
// Register the WitherContract listener
getServer().getPluginManager().registerEvents(new WitherContract(this), this);
Bukkit.getPluginManager().registerEvents(new MobLevitationWand(this), this);
this.getCommand("getwand").setExecutor(new GiveWandCommand());
}
@Override

View File

@ -0,0 +1,23 @@
package CoswayUtil;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
public class GiveWandCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
ItemStack wand = MobLevitationWand.createWand(); // Calls the wand creation method
player.getInventory().addItem(wand);
player.sendMessage("You have received the Levitation Wand!");
return true;
}
sender.sendMessage("Only players can use this command!");
return false;
}
}

View File

@ -0,0 +1,111 @@
package CoswayUtil;
import org.bukkit.*;
import org.bukkit.entity.*;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;
import java.util.HashMap;
import java.util.UUID;
public class MobLevitationWand implements Listener {
private final NamespacedKey wandKey = new NamespacedKey("your_plugin", "levitation_wand");
private final HashMap<UUID, Entity> levitatedMobs = new HashMap<>();
private final CoswayUtil plugin;
public MobLevitationWand(CoswayUtil plugin) {
this.plugin = plugin;
}
@EventHandler
public void onPlayerUseWand(PlayerInteractEvent event) {
Player player = event.getPlayer();
ItemStack item = player.getInventory().getItemInMainHand();
// Check if the player is holding the custom wand
if (!isLevitationWand(item)) return;
event.setCancelled(true); // Prevent default interaction
UUID playerId = player.getUniqueId();
// If already holding a mob, release it
if (levitatedMobs.containsKey(playerId)) {
releaseMob(player);
return;
}
// Try to pick up a nearby mob
Entity target = getNearestEntity(player);
if (target != null) {
levitatedMobs.put(playerId, target);
target.setGravity(false);
target.setInvulnerable(true);
startLevitating(player, target);
} else {
player.sendMessage(ChatColor.RED + "No entity found to pick up!");
}
}
private boolean isLevitationWand(ItemStack item) {
if (item == null || item.getType() != Material.STICK || !item.hasItemMeta()) return false;
ItemMeta meta = item.getItemMeta();
return meta.getPersistentDataContainer().has(wandKey, PersistentDataType.STRING);
}
private Entity getNearestEntity(Player player) {
return player.getNearbyEntities(5, 5, 5).stream()
.filter(entity -> entity instanceof LivingEntity && !(entity instanceof Player))
.findFirst()
.orElse(null);
}
private void startLevitating(Player player, Entity entity) {
new BukkitRunnable() {
@Override
public void run() {
if (!levitatedMobs.containsKey(player.getUniqueId()) || !player.isOnline()) {
releaseMob(player);
cancel();
return;
}
Vector direction = player.getLocation().getDirection().normalize().multiply(5);
Location newLocation = player.getLocation().add(direction);
entity.teleport(newLocation);
// Spawn particle chain
for (double i = 0; i <= 1; i += 0.1) {
double x = player.getLocation().getX() + (newLocation.getX() - player.getLocation().getX()) * i;
double y = player.getLocation().getY() + (newLocation.getY() - player.getLocation().getY()) * i;
double z = player.getLocation().getZ() + (newLocation.getZ() - player.getLocation().getZ()) * i;
player.getWorld().spawnParticle(Particle.SOUL_FIRE_FLAME, new Location(player.getWorld(), x, y, z), 2, 0, 0, 0, 0);
}
}
}.runTaskTimer(plugin, 0L, 2L);
}
private void releaseMob(Player player) {
Entity entity = levitatedMobs.remove(player.getUniqueId());
if (entity != null) {
entity.setGravity(true);
entity.setInvulnerable(false);
}
}
public static ItemStack createWand() {
ItemStack wand = new ItemStack(Material.STICK);
ItemMeta meta = wand.getItemMeta();
meta.setDisplayName(ChatColor.LIGHT_PURPLE + "Levitation Wand");
meta.getPersistentDataContainer().set(new NamespacedKey("CoswayUtil", "levitation_wand"), PersistentDataType.STRING, "true");
wand.setItemMeta(meta);
return wand;
}
}

View File

@ -16,4 +16,8 @@ commands:
gravitygauntlet:
description: "Gives the player a Gravity Gauntlet"
usage: "/gravitygauntlet"
permission: CoswayUtil.GravityGauntlet
permission: CoswayUtil.GravityGauntlet
getwand:
description: "Gives player a levitation wand"
usage: "/getwand"
permission: CoswayUtil.wand