Files
src
build.gradle
settings.gradle
Code
4 125
src/main/java/com/kodari/orbitalwolfcannon/OrbitalWolfCannon.java
package com.kodari.orbitalwolfcannon;
import java.util.concurrent.ThreadLocalRandom;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.entity.Wolf;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.player.PlayerFishEvent;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
public final class OrbitalWolfCannon extends JavaPlugin implements Listener {
private static final int WOLF_COUNT = 50;
private static final int DEFAULT_HEIGHT = 100;
private static final int MIN_HEIGHT = 20;
private static final int MAX_HEIGHT = 300;
private static final int INFINITE_DURATION = Integer.MAX_VALUE;
private static final int MAX_AMPLIFIER = 255;
private NamespacedKey cannonWolfKey;
@Override
public void onEnable() {
cannonWolfKey = new NamespacedKey(this, "orbital_wolf");
getServer().getPluginManager().registerEvents(this, this);
}
@EventHandler
public void onFishingRodCast(PlayerFishEvent event) {
if (event.getState() == PlayerFishEvent.State.FISHING) {
fire(event.getPlayer(), DEFAULT_HEIGHT);
}
}
private void fire(Player player, int height) {
World world = player.getWorld();
Location playerLocation = player.getLocation();
int spawnY = Math.min(playerLocation.getBlockY() + height, world.getMaxHeight() - 2);
spawnY = Math.max(spawnY, world.getMinHeight() + 1);
for (int i = 0; i < WOLF_COUNT; i++) {
double angle = ThreadLocalRandom.current().nextDouble(0, Math.PI * 2);
double distance = ThreadLocalRandom.current().nextDouble(0, 5);
Location spawnLocation = new Location(
world,
playerLocation.getX() + Math.cos(angle) * distance,
spawnY,
playerLocation.getZ() + Math.sin(angle) * distance
);
Wolf wolf = world.spawn(spawnLocation, Wolf.class);
wolf.setCustomName("pug");
wolf.setCustomNameVisible(true);
wolf.getPersistentDataContainer().set(cannonWolfKey, PersistentDataType.BYTE, (byte) 1);
addEffect(wolf, "INCREASE_DAMAGE");
addEffect(wolf, "SPEED");
addEffect(wolf, "FIRE_RESISTANCE");
}
}
private void addEffect(Wolf wolf, String effectName) {
PotionEffectType effectType = PotionEffectType.getByName(effectName);
if (effectType != null) {
wolf.addPotionEffect(new PotionEffect(effectType, INFINITE_DURATION, MAX_AMPLIFIER, true, false, true));
}
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onWolfFallDamage(EntityDamageEvent event) {
if (event.getCause() != EntityDamageEvent.DamageCause.FALL || !(event.getEntity() instanceof Wolf wolf)) {
return;
}
if (wolf.getPersistentDataContainer().has(cannonWolfKey, PersistentDataType.BYTE)) {
event.setCancelled(true);
}
}
}