Skip to content

Commit

Permalink
rewrite item_projectile for 1.16
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jul 12, 2020
1 parent 19d57a5 commit 9bea3f7
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 176 deletions.
Expand Up @@ -3,6 +3,7 @@
import com.denizenscript.denizen.nms.v1_16.impl.entities.CraftFakePlayerImpl;
import com.denizenscript.denizen.nms.v1_16.impl.entities.EntityFakeArrowImpl;
import com.denizenscript.denizen.nms.v1_16.impl.entities.EntityFakePlayerImpl;
import com.denizenscript.denizen.nms.v1_16.impl.entities.EntityItemProjectileImpl;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import com.denizenscript.denizen.nms.NMSHandler;
Expand All @@ -18,6 +19,7 @@
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_16_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_16_R1.inventory.CraftItemStack;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team;
Expand All @@ -38,13 +40,9 @@ public FakeArrow spawnFakeArrow(Location location) {

@Override
public ItemProjectile spawnItemProjectile(Location location, ItemStack itemStack) {
// TODO: 1.16
throw new UnsupportedOperationException();
/*
CraftWorld world = (CraftWorld) location.getWorld();
EntityItemProjectileImpl entity = new EntityItemProjectileImpl(world, location, itemStack);
EntityItemProjectileImpl entity = new EntityItemProjectileImpl(world.getHandle(), location, CraftItemStack.asNMSCopy(itemStack));
return entity.getBukkitEntity();
*/
}

@Override
Expand Down
@@ -1,15 +1,14 @@
package com.denizenscript.denizen.nms.v1_16.impl.entities;
/*

import com.denizenscript.denizen.nms.interfaces.ItemProjectile;
import org.bukkit.craftbukkit.v1_16_R1.CraftServer;
import org.bukkit.craftbukkit.v1_16_R1.entity.CraftHumanEntity;
import org.bukkit.craftbukkit.v1_16_R1.entity.CraftItem;
import org.bukkit.craftbukkit.v1_16_R1.entity.CraftLivingEntity;
import org.bukkit.projectiles.ProjectileSource;*/

// TODO: 1.16
import org.bukkit.craftbukkit.v1_16_R1.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_16_R1.inventory.CraftItemStack;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.projectiles.ProjectileSource;

public class CraftItemProjectileImpl {} /* extends CraftItem implements ItemProjectile {
public class CraftItemProjectileImpl extends CraftEntity implements ItemProjectile {

private boolean doesBounce;

Expand All @@ -24,7 +23,27 @@ public EntityItemProjectileImpl getHandle() {

@Override
public String getEntityTypeName() {
return "ITEM_PROJECTILE";
return getType().name();
}

@Override
public ItemStack getItemStack() {
return CraftItemStack.asBukkitCopy(getHandle().getItemStack());
}

@Override
public void setItemStack(ItemStack itemStack) {
getHandle().setItemStack(CraftItemStack.asNMSCopy(itemStack));
}

@Override
public int getPickupDelay() {
return 0;
}

@Override
public void setPickupDelay(int i) {
// Do nothing
}

@Override
Expand All @@ -33,18 +52,13 @@ public ProjectileSource getShooter() {
}

@Override
public void setShooter(ProjectileSource shooter) {
if (shooter instanceof CraftLivingEntity) {
getHandle().shooter = ((CraftLivingEntity) shooter).getHandle();
if (shooter instanceof CraftHumanEntity) {
getHandle().shooterId = ((CraftHumanEntity) shooter).getUniqueId();
}
public void setShooter(ProjectileSource projectileSource) {
if (projectileSource instanceof CraftEntity) {
getHandle().setShooter(((CraftEntity) projectileSource).getHandle());
}
else {
getHandle().shooter = null;
getHandle().shooterId = null;
getHandle().projectileSource = projectileSource;
}
getHandle().projectileSource = shooter;
}

@Override
Expand All @@ -56,5 +70,9 @@ public boolean doesBounce() {
public void setBounce(boolean doesBounce) {
this.doesBounce = doesBounce;
}

@Override
public EntityType getType() {
return EntityType.DROPPED_ITEM;
}
}
*/
@@ -1,187 +1,86 @@
package com.denizenscript.denizen.nms.v1_16.impl.entities;

/*
import com.denizenscript.denizen.nms.v1_16.Handler;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizen.nms.util.ReflectionHelper;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.google.common.base.Preconditions;
import net.minecraft.server.v1_16_R1.*;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_16_R1.CraftServer;
import org.bukkit.craftbukkit.v1_16_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_16_R1.event.CraftEventFactory;
import org.bukkit.craftbukkit.v1_16_R1.inventory.CraftItemStack;

import java.util.Iterator;
import java.util.UUID;
*/
import java.lang.invoke.MethodHandle;

// TODO: 1.16: IProjectile isn't an interface anymore.
public class EntityItemProjectileImpl extends EntityProjectile {

public class EntityItemProjectileImpl { // extends EntityItem implements IProjectile
/*
public Entity shooter;
public UUID shooterId;
public Entity c;
private int aw;
public static MethodHandle setBukkitEntityMethod = ReflectionHelper.getFinalSetter(Entity.class, "bukkitEntity");

public boolean inGround;
public int shake;
public static final DataWatcherObject<ItemStack> ITEM;

public EntityItemProjectileImpl(CraftWorld craftWorld, Location location, org.bukkit.inventory.ItemStack itemStack) {
super(EntityTypes.ITEM, craftWorld.getHandle());
static {
DataWatcherObject<ItemStack> watcher = null;
try {
Handler.ENTITY_BUKKITYENTITY.set(this, new CraftItemProjectileImpl((CraftServer) Bukkit.getServer(), this));
watcher = (DataWatcherObject<ItemStack>) ReflectionHelper.getFields(EntityItem.class).get("ITEM").get(null);
}
catch (Exception ex) {
catch (Throwable ex) {
Debug.echoError(ex);
}
this.pickupDelay = Integer.MAX_VALUE;
setPositionRotation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
//this.setSize(0.25F, 0.25F); as of 1.14, part of EntityTypes
this.setItemStack(CraftItemStack.asNMSCopy(itemStack));
world.addEntity(this);
ITEM = watcher;
}

@Override
public void tick() {
this.E = this.locX();
this.F = this.locY();
this.G = this.locZ();
super.tick();
if (this.shake > 0) {
--this.shake;
}
if (this.inGround) {
this.inGround = false;
this.setMot(this.getMot().d((double) (this.random.nextFloat() * 0.2F), (double) (this.random.nextFloat() * 0.2F), (double) (this.random.nextFloat() * 0.2F)));
}
AxisAlignedBB axisalignedbb = this.getBoundingBox().a(this.getMot()).g(1.0D);
Iterator iterator = this.world.getEntities(this, axisalignedbb, (entityx) -> {
return !entityx.isAlive() && entityx.isInteractable();
}).iterator();
while (iterator.hasNext()) {
Entity entity = (Entity) iterator.next();
if (entity == this.c) {
++this.aw;
break;
}
if (this.shooter != null && this.ticksLived < 2 && this.c == null && this.shooter == entity) {
this.c = entity;
this.aw = 3;
break;
}
}
MovingObjectPosition movingobjectposition = ProjectileHelper.a(this, axisalignedbb, (entity1) -> {
return !entity1.isAlive() && entity1.isInteractable() && entity1 != this.c;
}, RayTrace.BlockCollisionOption.OUTLINE, true);
if (this.c != null && this.aw-- <= 0) {
this.c = null;
}
if (movingobjectposition.getType() != MovingObjectPosition.EnumMovingObjectType.MISS) {
if (movingobjectposition.getType() == MovingObjectPosition.EnumMovingObjectType.BLOCK && this.world.getType(((MovingObjectPositionBlock) movingobjectposition).getBlockPosition()).getBlock() == Blocks.NETHER_PORTAL) {
this.c(((MovingObjectPositionBlock) movingobjectposition).getBlockPosition());
}
else {
this.a(movingobjectposition);
if (this.dead) {
CraftEventFactory.callProjectileHitEvent(this, movingobjectposition);
}
}
}
Vec3D vec3d = this.getMot();
this.setPositionRaw(this.locX() + vec3d.x, this.locY() + vec3d.y, this.locZ() + vec3d.z);
float f = MathHelper.sqrt(b(vec3d));
this.yaw = (float) (MathHelper.d(vec3d.x, vec3d.z) * 57.2957763671875D);
// TODO: what is this???
for (this.pitch = (float) (MathHelper.d(vec3d.y, (double) f) * 57.2957763671875D); this.pitch - this.lastPitch < -180.0F; this.lastPitch -= 360.0F) {
}
while (this.pitch - this.lastPitch >= 180.0F) {
this.lastPitch += 360.0F;
}
while (this.yaw - this.lastYaw < -180.0F) {
this.lastYaw -= 360.0F;
public EntityItemProjectileImpl(World world, Location location, ItemStack item) {
super((EntityTypes) EntityTypes.ITEM, world);
try {
setBukkitEntityMethod.invoke(this, new CraftItemProjectileImpl(world.getServer(), this));
}
while (this.yaw - this.lastYaw >= 180.0F) {
this.lastYaw += 360.0F;
catch (Throwable ex) {
Debug.echoError(ex);
}
setPositionRotation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
setItemStack(item);
world.addEntity(this);
}

this.pitch = MathHelper.g(0.2F, this.lastPitch, this.pitch);
this.yaw = MathHelper.g(0.2F, this.lastYaw, this.yaw);
float f1;
if (this.isInWater()) {
for (int i = 0; i < 4; ++i) {
float f2 = 0.25F;
this.world.addParticle(Particles.BUBBLE, this.locX() - vec3d.x * 0.25D, this.locY() - vec3d.y * 0.25D, this.locZ() - vec3d.z * 0.25D, vec3d.x, vec3d.y, vec3d.z);
}
f1 = 0.8F;
}
else {
f1 = 0.99F;
}
@Override
protected void initDatawatcher() {
this.getDataWatcher().register(ITEM, ItemStack.b);
}

this.setMot(vec3d.a((double) f1));
if (!this.isNoGravity()) {
Vec3D vec3d1 = this.getMot();
this.setMot(vec3d1.x, vec3d1.y - 0.03, vec3d1.z);
}
public ItemStack getItemStack() {
return this.getDataWatcher().get(ITEM);
}

this.setPosition(this.locX(), this.locY(), this.locZ());
public void setItemStack(ItemStack itemstack) {
Preconditions.checkArgument(!itemstack.isEmpty(), "Cannot drop air");
this.getDataWatcher().set(ITEM, itemstack);
this.getDataWatcher().markDirty(ITEM);
}

@Override
public void shoot(double d0, double d1, double d2, float f, float f1) {
float f2 = MathHelper.sqrt(d0 * d0 + d1 * d1 + d2 * d2);
d0 /= f2;
d1 /= f2;
d2 /= f2;
d0 += random.nextGaussian() * 0.007499999832361937D * f1;
d1 += random.nextGaussian() * 0.007499999832361937D * f1;
d2 += random.nextGaussian() * 0.007499999832361937D * f1;
d0 *= f;
d1 *= f;
d2 *= f;
setMot(d0, d1, d2);
float f3 = MathHelper.sqrt(d0 * d0 + d2 * d2);
lastYaw = yaw = (float) (Math.atan2(d0, d2) * 180.0D / 3.1415927410125732D);
lastPitch = pitch = (float) (Math.atan2(d1, f3) * 180.0D / 3.1415927410125732D);
public void a(DataWatcherObject<?> datawatcherobject) {
super.a(datawatcherobject);
if (ITEM.equals(datawatcherobject)) {
this.getItemStack().a(this);
}
}

protected void a(MovingObjectPosition var1) {
if (var1 instanceof MovingObjectPositionEntity) {
((MovingObjectPositionEntity) var1).getEntity().damageEntity(DamageSource.projectile(this, this.getShooter()), 0);
@Override
public void saveData(NBTTagCompound nbttagcompound) {
if (!this.getItemStack().isEmpty()) {
nbttagcompound.set("Item", this.getItemStack().save(new NBTTagCompound()));
}
this.die();
super.saveData(nbttagcompound);
}

public Entity getShooter() {
if (this.shooter == null && this.shooterId != null && this.world instanceof WorldServer) {
Entity entity = ((WorldServer) this.world).getEntity(this.shooterId);
if (entity instanceof EntityLiving) {
this.shooter = entity;
}
else {
this.shooterId = null;
}
@Override
public void loadData(NBTTagCompound nbttagcompound) {
NBTTagCompound nbttagcompound1 = nbttagcompound.getCompound("Item");
this.setItemStack(ItemStack.a(nbttagcompound1));
if (this.getItemStack().isEmpty()) {
this.die();
}
return this.shooter;
super.loadData(nbttagcompound);
}

@Override
public CraftItemProjectileImpl getBukkitEntity() {
return (CraftItemProjectileImpl) super.getBukkitEntity();
}*/
}
}

0 comments on commit 9bea3f7

Please sign in to comment.