Core/src/main/java/eu/univento/core/api/entity/EntityModifier.java

197 lines
6.8 KiB
Java

/*
* Copyright (c) 2017 univento.eu - All rights reserved
* You are not allowed to use, distribute or modify this code
*/
package eu.univento.core.api.entity;
import eu.univento.core.Core;
import net.minecraft.server.v1_12_R1.EntityLiving;
import net.minecraft.server.v1_12_R1.NBTTagCompound;
import org.bukkit.Bukkit;
import org.bukkit.EntityEffect;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_12_R1.entity.CraftCreature;
import org.bukkit.craftbukkit.v1_12_R1.entity.CraftEntity;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import java.lang.reflect.Field;
/**
* @author GerVorbis
* @version 1.0
*/
public class EntityModifier {
private static org.bukkit.entity.Entity entity;
private static CraftEntity craftentity;
private static net.minecraft.server.v1_12_R1.Entity entityS;
private static int scheduler;
private static Plugin plugin = Core.getInstance();
private static Player player = null;
private static float Speed;
public EntityModifier(org.bukkit.entity.Entity entity) {
EntityModifier.entity = entity;
craftentity = (CraftEntity) entity;
entityS = craftentity.getHandle();
}
public Builder modify() {
return new Builder();
}
public static final class Builder {
public Builder setDisplayName(String display) {
EntityModifier.entity.setCustomName(display);
EntityModifier.entity.setCustomNameVisible(true);
return this;
}
public Builder setDisplayNameVisible(Boolean visible) {
EntityModifier.entity.setCustomNameVisible(visible);
return this;
}
public Builder playEffect(EntityEffect entityeffect) {
EntityModifier.entity.playEffect(entityeffect);
return this;
}
public Builder remove() {
EntityModifier.entity.remove();
return this;
}
public Builder setPassenger(org.bukkit.entity.Entity passenger) {
EntityModifier.entity.setPassenger(passenger);
return this;
}
public Builder setFireTicks(int ticks) {
EntityModifier.entity.setFireTicks(ticks);
return this;
}
public Builder setLocation(Location location) {
teleport(location);
return this;
}
public Builder setYawPitch(float yaw, float pitch) {
Location loc = EntityModifier.entity.getLocation().clone();
teleport(new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ(), yaw, pitch));
return this;
}
public void teleport(Location location) {
EntityModifier.entity.teleport(location);
}
public Builder die() {
EntityModifier.entityS.die();
return this;
}
public Builder setInvisible(boolean invisible) {
EntityModifier.entityS.setInvisible(invisible);
return this;
}
public Builder noClip(boolean noClip) {
EntityModifier.entityS.noclip = noClip;
return this;
}
public Builder setInvulnerable(boolean invulnerable) {
try {
Field invulnerableField = net.minecraft.server.v1_12_R1.Entity.class.getDeclaredField("invulnerable");
invulnerableField.setAccessible(true);
invulnerableField.setBoolean(EntityModifier.entityS, invulnerable);
} catch (Exception ex) {
ex.printStackTrace();
}
return this;
}
public Builder setNoAI(boolean noAI) {
NBTTagCompound tag = new NBTTagCompound();
EntityModifier.entityS.c(tag);
tag.setBoolean("NoAI", noAI);
EntityLiving el = (EntityLiving) EntityModifier.entityS;
el.a(tag);
return this;
}
public Builder setSilent(boolean silent) {
NBTTagCompound tag = new NBTTagCompound();
EntityModifier.entityS.c(tag);
tag.setBoolean("Silent", silent);
EntityLiving el = (EntityLiving) EntityModifier.entityS;
el.a(tag);
return this;
}
public Builder setCanPickUpLoot(boolean canpickuploot) {
NBTTagCompound tag = new NBTTagCompound();
EntityModifier.entityS.c(tag);
tag.setBoolean("CanPickUpLoot", canpickuploot);
EntityLiving el = (EntityLiving) EntityModifier.entityS;
el.a(tag);
return this;
}
public Builder setHealth(float health) {
NBTTagCompound tag = new NBTTagCompound();
EntityModifier.entityS.c(tag);
tag.setFloat("HealF", health);
EntityLiving el = (EntityLiving) EntityModifier.entityS;
el.a(tag);
return this;
}
public Builder setCanDespawn(boolean candespawn) {
candespawn = !candespawn;
NBTTagCompound tag = new NBTTagCompound();
EntityModifier.entityS.c(tag);
tag.setBoolean("PersistenceRequired", candespawn);
EntityLiving el = (EntityLiving) EntityModifier.entityS;
el.a(tag);
return this;
}
public Builder walkToLocation(Location location, float speed) {
((CraftCreature) EntityModifier.entity).getHandle().getNavigation().a(location.getX(), location.getY(), location.getZ(), speed);
return this;
}
public Builder followPlayer(Player target, float speed) {
EntityModifier.player = target;
EntityModifier.Speed = speed;
EntityModifier.scheduler = Bukkit.getScheduler().scheduleSyncRepeatingTask(EntityModifier.plugin,
() -> {
double distance = EntityModifier.entity.getLocation().distance(EntityModifier.player.getLocation());
if (distance < 11.0D) {
float speed1 = EntityModifier.Speed;
if (distance < 3.0D) {
speed1 = 0.0F;
}
((CraftCreature) EntityModifier.entity).getHandle().getNavigation().a(EntityModifier.player.getLocation().getX(),EntityModifier.player.getLocation().getY(),EntityModifier.player.getLocation().getZ(), speed1);
} else if (EntityModifier.player.isOnGround()) {
EntityModifier.entity.teleport(EntityModifier.player);
}
}
, 0L, 1L);
return this;
}
public Builder stopFollowingPlayer() {
Bukkit.getScheduler().cancelTask(EntityModifier.scheduler);
return this;
}
public org.bukkit.entity.Entity build() {
return EntityModifier.entity;
}
}
}