Skip to content

Commit

Permalink
Cleanup entity initial NBT
Browse files Browse the repository at this point in the history
  • Loading branch information
wode490390 committed Feb 11, 2024
1 parent 00c51a9 commit 9d9b62c
Show file tree
Hide file tree
Showing 18 changed files with 82 additions and 189 deletions.
16 changes: 2 additions & 14 deletions src/main/java/cn/nukkit/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
import cn.nukkit.nbt.NBTIO;
import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.nbt.tag.DoubleTag;
import cn.nukkit.nbt.tag.FloatTag;
import cn.nukkit.nbt.tag.ListTag;
import cn.nukkit.network.Network;
import cn.nukkit.network.PacketViolationReason;
Expand Down Expand Up @@ -5911,21 +5910,10 @@ public boolean isForceResources() {
*/
public void startFishing(Item fishingRod) {
Vector3 motion = this.getDirectionVector().multiply(2);

CompoundTag nbt = new CompoundTag()
.putList(new ListTag<DoubleTag>("Pos")
.add(new DoubleTag("", x + motion.x * 0.5))
.add(new DoubleTag("", y + this.getEyeHeight() + motion.y * 0.5))
.add(new DoubleTag("", z + motion.z * 0.5)))
.putList(new ListTag<DoubleTag>("Motion")
.add(new DoubleTag("", motion.x))
.add(new DoubleTag("", motion.y))
.add(new DoubleTag("", motion.z)))
.putList(new ListTag<FloatTag>("Rotation")
.add(new FloatTag("", (float) yaw))
.add(new FloatTag("", (float) pitch)));
CompoundTag nbt = Entity.getDefaultNBT(x + motion.x * 0.5, y + this.getEyeHeight() + motion.y * 0.5, z + motion.z * 0.5, motion, (float) yaw, (float) pitch);
EntityFishingHook fishingHook = new EntityFishingHook(getChunk(), nbt, this);
fishingHook.setMotion(motion);

ProjectileLaunchEvent ev = new ProjectileLaunchEvent(fishingHook);
this.getServer().getPluginManager().callEvent(ev);
if (ev.isCancelled()) {
Expand Down
16 changes: 2 additions & 14 deletions src/main/java/cn/nukkit/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import cn.nukkit.data.ServerConfiguration;
import cn.nukkit.dispenser.DispenseBehaviorRegister;
import cn.nukkit.entity.Entities;
import cn.nukkit.entity.Entity;
import cn.nukkit.entity.attribute.Attribute;
import cn.nukkit.entity.data.Skin;
import cn.nukkit.event.HandlerList;
Expand Down Expand Up @@ -41,8 +42,6 @@
import cn.nukkit.metadata.PlayerMetadataStore;
import cn.nukkit.nbt.NBTIO;
import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.nbt.tag.DoubleTag;
import cn.nukkit.nbt.tag.FloatTag;
import cn.nukkit.nbt.tag.ListTag;
import cn.nukkit.network.CompressBatchedTask;
import cn.nukkit.network.Network;
Expand Down Expand Up @@ -1606,24 +1605,13 @@ public CompoundTag getOfflinePlayerData(String name) {
}

Position spawn = this.getDefaultLevel().getSafeSpawn();
CompoundTag nbt = new CompoundTag()
CompoundTag nbt = Entity.getDefaultNBT(spawn)
.putLong("firstPlayed", System.currentTimeMillis() / 1000)
.putLong("lastPlayed", System.currentTimeMillis() / 1000)
.putList(new ListTag<DoubleTag>("Pos")
.add(new DoubleTag("0", spawn.x))
.add(new DoubleTag("1", spawn.y))
.add(new DoubleTag("2", spawn.z)))
.putString("Level", this.getDefaultLevel().getName())
.putList(new ListTag<>("Inventory"))
// .putCompound("Achievements", new CompoundTag())
.putInt("playerGameType", this.getGamemode())
.putList(new ListTag<DoubleTag>("Motion")
.add(new DoubleTag("0", 0))
.add(new DoubleTag("1", 0))
.add(new DoubleTag("2", 0)))
.putList(new ListTag<FloatTag>("Rotation")
.add(new FloatTag("0", 0))
.add(new FloatTag("1", 0)))
.putFloat("FallDistance", 0)
.putShort("Fire", 0)
.putShort("Air", 300)
Expand Down
16 changes: 1 addition & 15 deletions src/main/java/cn/nukkit/block/BlockTNT.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
import cn.nukkit.math.BlockFace;
import cn.nukkit.math.Mth;
import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.nbt.tag.DoubleTag;
import cn.nukkit.nbt.tag.FloatTag;
import cn.nukkit.nbt.tag.ListTag;
import cn.nukkit.network.protocol.LevelEventPacket;
import cn.nukkit.utils.BlockColor;

Expand Down Expand Up @@ -85,18 +82,7 @@ public void prime(int fuse, Entity source) {
}

double mot = ThreadLocalRandom.current().nextDouble() * Math.PI * 2;
CompoundTag nbt = new CompoundTag()
.putList(new ListTag<DoubleTag>("Pos")
.add(new DoubleTag("", this.x + 0.5))
.add(new DoubleTag("", this.y))
.add(new DoubleTag("", this.z + 0.5)))
.putList(new ListTag<DoubleTag>("Motion")
.add(new DoubleTag("", -Mth.sin(mot) * 0.02))
.add(new DoubleTag("", 0.2))
.add(new DoubleTag("", -Mth.cos(mot) * 0.02)))
.putList(new ListTag<FloatTag>("Rotation")
.add(new FloatTag("", 0))
.add(new FloatTag("", 0)))
CompoundTag nbt = Entity.getDefaultNBT(x + 0.5, y, z + 0.5, -Mth.sin(mot) * 0.02, 0.2, -Mth.cos(mot) * 0.02)
.putByte("Fuse", fuse);
Entity tnt = new EntityPrimedTNT(
this.getLevel().getChunk(this.getFloorX() >> 4, this.getFloorZ() >> 4),
Expand Down
61 changes: 49 additions & 12 deletions src/main/java/cn/nukkit/entity/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -818,26 +818,63 @@ public static CompoundTag getDefaultNBT(Vector3 pos) {
return getDefaultNBT(pos, null);
}

public static CompoundTag getDefaultNBT(Vector3 pos, Vector3 motion) {
Location loc = pos instanceof Location ? (Location) pos : null;
public static CompoundTag getDefaultNBT(double x, double y, double z) {
return getDefaultNBT(x, y, z, null);
}

public static CompoundTag getDefaultNBT(double x, double y, double z, @Nullable Vector3 motion) {
if (motion == null) {
return getDefaultNBT(x, y, z, 0, 0, 0);
}
return getDefaultNBT(x, y, z, motion.x, motion.y, motion.z);
}

public static CompoundTag getDefaultNBT(Vector3 pos, @Nullable Vector3 motion) {
if (motion == null) {
return getDefaultNBT(pos, 0, 0, 0);
}
return getDefaultNBT(pos, motion.x, motion.y, motion.z);
}

public static CompoundTag getDefaultNBT(Vector3 pos, double motionX, double motionY, double motionZ) {
if (pos instanceof Location loc) {
return getDefaultNBT(pos.x, pos.y, pos.z, motionX, motionY, motionZ, (float) loc.getYaw(), (float) loc.getPitch());
}
return getDefaultNBT(pos.x, pos.y, pos.z, motionX, motionY, motionZ);
}

public static CompoundTag getDefaultNBT(double x, double y, double z, double motionX, double motionY, double motionZ) {
return getDefaultNBT(x, y, z, motionX, motionY, motionZ, 0, 0);
}

if (loc != null) {
return getDefaultNBT(pos, motion, (float) loc.getYaw(), (float) loc.getPitch());
public static CompoundTag getDefaultNBT(Vector3 pos, double motionX, double motionY, double motionZ, float yaw, float pitch) {
return getDefaultNBT(pos.x, pos.y, pos.z, motionX, motionY, motionZ, yaw, pitch);
}

public static CompoundTag getDefaultNBT(Vector3 pos, @Nullable Vector3 motion, float yaw, float pitch) {
if (motion == null) {
return getDefaultNBT(pos.x, pos.y, pos.z, 0, 0, 0, yaw, pitch);
}
return getDefaultNBT(pos.x, pos.y, pos.z, motion.x, motion.y, motion.z, yaw, pitch);
}

return getDefaultNBT(pos, motion, 0, 0);
public static CompoundTag getDefaultNBT(double x, double y, double z, @Nullable Vector3 motion, float yaw, float pitch) {
if (motion == null) {
return getDefaultNBT(x, y, z, 0, 0, 0, yaw, pitch);
}
return getDefaultNBT(x, y, z, motion.x, motion.y, motion.z, yaw, pitch);
}

public static CompoundTag getDefaultNBT(Vector3 pos, Vector3 motion, float yaw, float pitch) {
public static CompoundTag getDefaultNBT(double x, double y, double z, double motionX, double motionY, double motionZ, float yaw, float pitch) {
return new CompoundTag()
.putList(new ListTag<DoubleTag>("Pos")
.add(new DoubleTag("", pos.x))
.add(new DoubleTag("", pos.y))
.add(new DoubleTag("", pos.z)))
.add(new DoubleTag("", x))
.add(new DoubleTag("", y))
.add(new DoubleTag("", z)))
.putList(new ListTag<DoubleTag>("Motion")
.add(new DoubleTag("", motion != null ? motion.x : 0))
.add(new DoubleTag("", motion != null ? motion.y : 0))
.add(new DoubleTag("", motion != null ? motion.z : 0)))
.add(new DoubleTag("", motionX))
.add(new DoubleTag("", motionY))
.add(new DoubleTag("", motionZ)))
.putList(new ListTag<FloatTag>("Rotation")
.add(new FloatTag("", yaw))
.add(new FloatTag("", pitch)));
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/cn/nukkit/entity/item/EntityPrimedTNT.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public boolean canCollide() {
}

protected int fuse;
protected double force;
protected int force;

protected Entity source;

Expand Down Expand Up @@ -85,7 +85,7 @@ protected void initEntity() {
fuse = 80;
}
if (namedTag.contains("Force")) {
force = namedTag.getDouble("Force");
force = namedTag.getByte("Force");
} else {
force = 4;
}
Expand All @@ -103,7 +103,7 @@ public boolean canCollideWith(Entity entity) {
public void saveNBT() {
super.saveNBT();
namedTag.putByte("Fuse", fuse);
namedTag.putDouble("Force", force);
namedTag.putByte("Force", force);
}

public boolean onUpdate(int currentTick) {
Expand Down
20 changes: 3 additions & 17 deletions src/main/java/cn/nukkit/item/ItemBoat.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
import cn.nukkit.level.Level;
import cn.nukkit.math.AxisAlignedBB;
import cn.nukkit.math.BlockFace;
import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.nbt.tag.DoubleTag;
import cn.nukkit.nbt.tag.FloatTag;
import cn.nukkit.nbt.tag.ListTag;

/**
* Created by yescallop on 2016/2/13.
Expand Down Expand Up @@ -68,19 +64,9 @@ public boolean onActivate(Level level, Player player, Block block, Block target,
}

Entity boat = getEntityFactory().create(
level.getChunk(block.getFloorX() >> 4, block.getFloorZ() >> 4), new CompoundTag("")
.putList(new ListTag<DoubleTag>("Pos")
.add(new DoubleTag("", block.getX() + 0.5))
.add(new DoubleTag("", y))
.add(new DoubleTag("", block.getZ() + 0.5)))
.putList(new ListTag<DoubleTag>("Motion")
.add(new DoubleTag("", 0))
.add(new DoubleTag("", 0))
.add(new DoubleTag("", 0)))
.putList(new ListTag<FloatTag>("Rotation")
.add(new FloatTag("", (float) ((player.yaw + 90f) % 360)))
.add(new FloatTag("", 0)))
.putByte("woodID", this.getDamage())
level.getChunk(block.getFloorX() >> 4, block.getFloorZ() >> 4),
Entity.getDefaultNBT(block.getX() + 0.5, y, block.getZ() + 0.5, null, ((float) player.yaw + 90f) % 360, 0)
.putByte("woodID", this.getDamage())
);

if (player.isSurvivalLike()) {
Expand Down
16 changes: 1 addition & 15 deletions src/main/java/cn/nukkit/item/ItemEndCrystal.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
import cn.nukkit.math.BlockFace;
import cn.nukkit.math.SimpleAxisAlignedBB;
import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.nbt.tag.DoubleTag;
import cn.nukkit.nbt.tag.FloatTag;
import cn.nukkit.nbt.tag.ListTag;

import java.util.concurrent.ThreadLocalRandom;

Expand Down Expand Up @@ -48,18 +45,7 @@ public boolean onActivate(Level level, Player player, Block block, Block target,
return false;
}

CompoundTag nbt = new CompoundTag()
.putList(new ListTag<DoubleTag>("Pos")
.add(new DoubleTag("", target.x + 0.5))
.add(new DoubleTag("", up.y))
.add(new DoubleTag("", target.z + 0.5)))
.putList(new ListTag<DoubleTag>("Motion")
.add(new DoubleTag("", 0))
.add(new DoubleTag("", 0))
.add(new DoubleTag("", 0)))
.putList(new ListTag<FloatTag>("Rotation")
.add(new FloatTag("", ThreadLocalRandom.current().nextFloat() * 360))
.add(new FloatTag("", 0)));
CompoundTag nbt = Entity.getDefaultNBT(target.x + 0.5, up.y, target.z + 0.5, null, ThreadLocalRandom.current().nextFloat() * 360, 0);

if (this.hasCustomName()) {
nbt.putString("CustomName", this.getCustomName());
Expand Down
18 changes: 2 additions & 16 deletions src/main/java/cn/nukkit/item/ItemMinecart.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
import cn.nukkit.Player;
import cn.nukkit.block.Block;
import cn.nukkit.block.BlockRail;
import cn.nukkit.entity.Entity;
import cn.nukkit.entity.item.EntityMinecartEmpty;
import cn.nukkit.level.Level;
import cn.nukkit.math.BlockFace;
import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.nbt.tag.DoubleTag;
import cn.nukkit.nbt.tag.FloatTag;
import cn.nukkit.nbt.tag.ListTag;
import cn.nukkit.utils.Rail;

/**
Expand Down Expand Up @@ -44,18 +41,7 @@ public boolean onActivate(Level level, Player player, Block block, Block target,
adjacent = 0.5D;
}
EntityMinecartEmpty minecart = new EntityMinecartEmpty(
level.getChunk(target.getFloorX() >> 4, target.getFloorZ() >> 4), new CompoundTag("")
.putList(new ListTag<>("Pos")
.add(new DoubleTag("", target.getX() + 0.5))
.add(new DoubleTag("", target.getY() + 0.0625D + adjacent))
.add(new DoubleTag("", target.getZ() + 0.5)))
.putList(new ListTag<>("Motion")
.add(new DoubleTag("", 0))
.add(new DoubleTag("", 0))
.add(new DoubleTag("", 0)))
.putList(new ListTag<>("Rotation")
.add(new FloatTag("", 0))
.add(new FloatTag("", 0)))
level.getChunk(target.getFloorX() >> 4, target.getFloorZ() >> 4), Entity.getDefaultNBT(target.getX() + 0.5, target.getY() + 0.0625D + adjacent, target.getZ() + 0.5)
);

if (player.isSurvivalLike()) {
Expand Down
18 changes: 2 additions & 16 deletions src/main/java/cn/nukkit/item/ItemMinecartChest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
import cn.nukkit.Player;
import cn.nukkit.block.Block;
import cn.nukkit.block.BlockRail;
import cn.nukkit.entity.Entity;
import cn.nukkit.entity.item.EntityMinecartChest;
import cn.nukkit.level.Level;
import cn.nukkit.math.BlockFace;
import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.nbt.tag.DoubleTag;
import cn.nukkit.nbt.tag.FloatTag;
import cn.nukkit.nbt.tag.ListTag;
import cn.nukkit.utils.Rail;

public class ItemMinecartChest extends Item {
Expand Down Expand Up @@ -40,18 +37,7 @@ public boolean onActivate(Level level, Player player, Block block, Block target,
adjacent = 0.5D;
}
EntityMinecartChest minecart = new EntityMinecartChest(
level.getChunk(target.getFloorX() >> 4, target.getFloorZ() >> 4), new CompoundTag("")
.putList(new ListTag<>("Pos")
.add(new DoubleTag("", target.getX() + 0.5))
.add(new DoubleTag("", target.getY() + 0.0625D + adjacent))
.add(new DoubleTag("", target.getZ() + 0.5)))
.putList(new ListTag<>("Motion")
.add(new DoubleTag("", 0))
.add(new DoubleTag("", 0))
.add(new DoubleTag("", 0)))
.putList(new ListTag<>("Rotation")
.add(new FloatTag("", 0))
.add(new FloatTag("", 0)))
level.getChunk(target.getFloorX() >> 4, target.getFloorZ() >> 4), Entity.getDefaultNBT(target.getX() + 0.5, target.getY() + 0.0625D + adjacent, target.getZ() + 0.5)
);

if (player.isSurvivalLike()) {
Expand Down
18 changes: 2 additions & 16 deletions src/main/java/cn/nukkit/item/ItemMinecartHopper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
import cn.nukkit.Player;
import cn.nukkit.block.Block;
import cn.nukkit.block.BlockRail;
import cn.nukkit.entity.Entity;
import cn.nukkit.entity.item.EntityMinecartHopper;
import cn.nukkit.level.Level;
import cn.nukkit.math.BlockFace;
import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.nbt.tag.DoubleTag;
import cn.nukkit.nbt.tag.FloatTag;
import cn.nukkit.nbt.tag.ListTag;
import cn.nukkit.utils.Rail;

public class ItemMinecartHopper extends Item {
Expand Down Expand Up @@ -40,18 +37,7 @@ public boolean onActivate(Level level, Player player, Block block, Block target,
adjacent = 0.5D;
}
EntityMinecartHopper minecart = new EntityMinecartHopper(
level.getChunk(target.getFloorX() >> 4, target.getFloorZ() >> 4), new CompoundTag("")
.putList(new ListTag<>("Pos")
.add(new DoubleTag("", target.getX() + 0.5))
.add(new DoubleTag("", target.getY() + 0.0625D + adjacent))
.add(new DoubleTag("", target.getZ() + 0.5)))
.putList(new ListTag<>("Motion")
.add(new DoubleTag("", 0))
.add(new DoubleTag("", 0))
.add(new DoubleTag("", 0)))
.putList(new ListTag<>("Rotation")
.add(new FloatTag("", 0))
.add(new FloatTag("", 0)))
level.getChunk(target.getFloorX() >> 4, target.getFloorZ() >> 4), Entity.getDefaultNBT(target.getX() + 0.5, target.getY() + 0.0625D + adjacent, target.getZ() + 0.5)
);

if (player.isSurvivalLike()) {
Expand Down
Loading

0 comments on commit 9d9b62c

Please sign in to comment.