Eniripsa96's Enchantments

Enchantment Showcase - Eniripsa96

Dash

public class DashEnchantment extends CustomEnchantment {

    static final int COOLDOWN = 5000;

    Plugin plugin;
    long timer;

    public DashEnchantment(Plugin plugin) {
        super("Dash", null);
        this.plugin = plugin;
    }

    @Override
    public void applyMiscEffect(Player player, int level, PlayerInteractEvent event) {
        if (event.getAction() == Action.RIGHT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_AIR) {
            if (System.currentTimeMillis() - timer < COOLDOWN) return;

            Vector vector = player.getLocation().getDirection();
            vector.setY(0);
            vector.multiply(3 / vector.length());
            vector.setY(0.2);
            player.setVelocity(vector);

            DashTask task = new DashTask(plugin, player, level, 3);
            task.runTask(plugin);

            timer = System.currentTimeMillis();
        }
    }
}
class DashTask extends BukkitRunnable {

    Plugin plugin;
    Player player;
    int damage;
    int remaining;

    public DashTask(Plugin plugin, Player player, int damage, int remaining) {
        this.plugin = plugin;
        this.player = player;
        this.damage = damage;
        this.remaining = remaining;
    }

    public void run() {
        for (Entity entity : player.getNearbyEntities(1, 1, 1)) {
            if (entity instanceof LivingEntity) ((LivingEntity) entity).damage(damage, player);
        }
        if (remaining > 0) new DashTask(plugin, player, damage, remaining - 1).runTaskLater(plugin, 4);
    }
}

Redstone Trap (base class for other trap enchantments)

public abstract class TrapEnchantment extends CustomEnchantment {


    protected int cooldown;
    protected boolean[][] layout;
    protected long timer;
    protected int radius;

    public TrapEnchantment(String name, int radius, int cooldown) {
        super (name, null);
        this.radius = radius;
        this.cooldown = cooldown * 1000;
    }

    @Override
    public void applyMiscEffect(Player player, int enchantLevel, PlayerInteractEvent event) {
        if (event.getAction() == Action.RIGHT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_AIR) {

            if (Trap.isTrapActive(player.getName(), this)) {
                Trap trap = Trap.getTrap(player.getName(), this);
                if (trap.contains(player.getLocation())){
                    trap.remove();
                    timer -= cooldown / 4;
                    return;
                }
            }

            if (System.currentTimeMillis() - timer < cooldown) return;
            if (Trap.isTrapActive(player.getName(), this)) return;

            Location location = player.getLocation();
            location.setX(location.getX() - layout.length / 2);
            location.setZ(location.getZ() - layout[layout.length / 2].length / 2);
            for (int i = 0; i < layout.length; i++) {
                for (int j = 0; j < layout[i].length; j++) {
                    if (!layout[i][j]) {
                        location.setZ(location.getZ() + 1);
                        continue;
                    }
                    Block nonSolid = player.getWorld().getBlockAt(location);
                    location.setY(location.getY() - 1);
                    Block solid = player.getWorld().getBlockAt(location);
                    location.setY(location.getY() + 1);
                    if (nonSolid.getType().isSolid() || !solid.getType().isSolid()
                            || nonSolid.getType() == Material.REDSTONE_WIRE) {
                        player.sendMessage("This is not a suitable place for the trap's seal...");
                        return;
                    }
                    location.setZ(location.getZ() + 1);
                }
                location.setZ(location.getZ() - layout[i].length);
                location.setX(location.getX() + 1);
            }

            location.setX(player.getLocation().getX() - layout.length / 2);
            for (int i = 0; i < layout.length; i++) {
                for (int j = 0; j < layout[i].length; j++) {
                    if (!layout[i][j]) {
                        location.setZ(location.getZ() + 1);
                        continue;
                    }
                    player.getWorld().getBlockAt(location).setType(Material.REDSTONE_WIRE);
                    location.setZ(location.getZ() + 1);
                }
                location.setZ(location.getZ() - layout[i].length);
                location.setX(location.getX() + 1);
            }

            Trap.createTrap(player, this, player.getLocation(), radius);
            timer = System.currentTimeMillis();
        }
    }

    public void removeTrap(Location center) {
        center.setX(center.getX() - layout.length / 2);
        center.setZ(center.getZ() - layout[layout.length / 2].length / 2);
        for (int i = 0; i < layout.length; i++) {
            for (int j = 0; j < layout[i].length; j++) {
                if (!layout[i][j]) {
                    center.setZ(center.getZ() + 1);
                    continue;
                }
                center.getWorld().getBlockAt(center).setType(Material.AIR);
                center.setZ(center.getZ() + 1);
            }
            center.setZ(center.getZ() - layout[i].length);
            center.setX(center.getX() + 1);
        }
    }

    public void onEnter(Trap trap, LivingEntity entity) {}
    public void onLeave(Trap trap, LivingEntity entity) {}
    public void onUpdate(Trap trap) {}
}
public class Trap {

    static final int MAX_DISTANCE = 50;

    static HashMap<String, Trap> traps = new HashMap<String, Trap>();

    List<LivingEntity> inRange;
    Player owner;
    TrapEnchantment enchantment;
    Location center;
    int radius;

    public static boolean isTrapActive(String player, TrapEnchantment enchantment) {
         return traps.containsKey(player + enchantment.name());
    }

    public static Trap getTrap(String player, TrapEnchantment enchantment) {
        if (traps.containsKey(player + enchantment.name()))
            return traps.get(player + enchantment.name());
        else return null;
    }

    public static void createTrap(Player player, TrapEnchantment enchantment, Location center, int radius) {
        traps.put(player.getName() + enchantment.name(), new Trap(player, enchantment, center, radius));
    }

    public Trap(Player owner, TrapEnchantment enchantment, Location center, int radius) {
        inRange = new ArrayList<LivingEntity>();
        this.owner = owner;
        this.enchantment = enchantment;
        this.center = center;
        this.radius = radius;
    }

    public boolean contains(Location location) {
        return location.getWorld() == center.getWorld()
                && location.distanceSquared(center) < radius * radius;
    }

    public void addEntity(LivingEntity entity) {
        if (owner == null) remove();
        else if (!inRange.contains(entity)) {
            inRange.add(entity);
            enchantment.onEnter(this, entity);
        }
    }

    public void removeEntity(LivingEntity entity) {
        if (owner == null) remove();
        else if (inRange.contains(entity)) {
            inRange.remove(entity);
            enchantment.onLeave(this, entity);
        }
    }

    public void update() {
        enchantment.onUpdate(this);
        if (owner == null) remove();
        else if (!owner.isOnline()) remove();
        else if (owner.getLocation().distanceSquared(center) > MAX_DISTANCE * MAX_DISTANCE) remove();
    }

    public void remove() {
        enchantment.removeTrap(center);
        traps.remove(owner.getName() + enchantment.name());
    }
}
public class TrapTask extends BukkitRunnable {

    public void run() {
        for (Trap trap : Trap.traps.values()) {
            for (LivingEntity entity : trap.center.getWorld().getLivingEntities()) {
                if (trap.contains(entity.getLocation())) trap.addEntity(entity);
                else trap.removeEntity(entity);
            }
            trap.update();
        }
    }
}

Fire Redstone Trap

public class FireTrapEnchantment extends TrapEnchantment {

    public FireTrapEnchantment() {
        super("Fire Trap", 4, 20);
        layout = new boolean[][] {
                { false, false,  true,  true,  true },
                { false, false, false,  true },
                {  true, false, false, false, false, false,  true },
                {  true,  true, false,  true, false,  true,  true },
                {  true, false, false, false, false, false,  true },
                { false, false, false,  true },
                { false, false,  true,  true,  true }};
    }

    @Override
    public void onEnter(Trap trap, LivingEntity entity) {
        if (entity != trap.owner) {
            Location loc = entity.getLocation();
            entity.getWorld().createExplosion(loc.getX(), loc.getY(), loc.getZ(), 4.0f, true, false);
            trap.remove();
        }
    }
}

Ice Redstone Trap

public class IceTrapEnchantment extends TrapEnchantment {

    public IceTrapEnchantment() {
        super("Ice Trap", 4, 20);
        layout = new boolean[][] {
                {  true,  true, false,  true,  true },
                {  true, false, false, false,  true },
                { false, false,  true, false, false },
                {  true, false, false, false,  true },
                {  true,  true, false,  true,  true }};
    }

    @Override
    public void onLeave(Trap trap, LivingEntity entity) {
        if (entity != trap.owner) {
            Vector direction = trap.center.toVector().subtract(entity.getLocation().toVector());
            direction = direction.multiply(1 / direction.length());
            entity.setVelocity(direction);
        }
    }
}

Poison Redstone Trap

public class PoisonTrapEnchantment extends TrapEnchantment {

    public PoisonTrapEnchantment() {
        super("Poison Trap", 4, 20);
        layout = new boolean[][] {
                { false, false, false,  true },
                { false, false,  true,  true,  true },
                { false,  true, false, false, false,  true },
                {  true,  true, false,  true, false,  true,  true },
                { false,  true, false, false, false,  true },
                { false, false,  true,  true,  true },
                { false, false, false,  true }};
    }

    @Override
    public void onUpdate(Trap trap) {
        for (LivingEntity entity : trap.inRange) {
            if (entity != trap.owner) {
                entity.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 139, 1), true);
            }
        }
    }
}

Comments

Posts Quoted:
Reply
Clear All Quotes