#1181: Consolidate Location conversion code

By: Doc <nachito94@msn.com>
This commit is contained in:
CraftBukkit/Spigot
2023-04-19 19:59:19 +10:00
parent 2ffb1d2479
commit b99d3df2d8
43 changed files with 424 additions and 316 deletions

View File

@@ -7,6 +7,7 @@ import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.inventory.CraftInventory;
import org.bukkit.craftbukkit.util.CraftLocation;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.Inventory;
@@ -70,7 +71,7 @@ public class CraftAllay extends CraftCreature implements org.bukkit.entity.Allay
public void startDancing(Location location) {
Preconditions.checkArgument(location != null, "Location cannot be null");
Preconditions.checkArgument(location.getBlock().getType().equals(Material.JUKEBOX), "The Block in the Location need to be a JukeBox");
getHandle().setJukeboxPlaying(BlockPosition.containing(location.getX(), location.getY(), location.getZ()), true);
getHandle().setJukeboxPlaying(CraftLocation.toBlockPosition(location), true);
}
@Override
@@ -94,6 +95,6 @@ public class CraftAllay extends CraftCreature implements org.bukkit.entity.Allay
public Location getJukebox() {
BlockPosition nmsJukeboxPos = getHandle().jukeboxPos;
return (nmsJukeboxPos != null) ? new Location(getWorld(), nmsJukeboxPos.getX(), nmsJukeboxPos.getY(), nmsJukeboxPos.getZ()) : null;
return (nmsJukeboxPos != null) ? CraftLocation.toBukkit(nmsJukeboxPos, getWorld()) : null;
}
}

View File

@@ -5,6 +5,7 @@ import net.minecraft.core.BlockPosition;
import net.minecraft.world.entity.animal.EntityBee;
import org.bukkit.Location;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.util.CraftLocation;
import org.bukkit.entity.Bee;
import org.bukkit.entity.EntityType;
@@ -32,25 +33,25 @@ public class CraftBee extends CraftAnimals implements Bee {
@Override
public Location getHive() {
BlockPosition hive = getHandle().getHivePos();
return (hive == null) ? null : new Location(getWorld(), hive.getX(), hive.getY(), hive.getZ());
return (hive == null) ? null : CraftLocation.toBukkit(hive, getWorld());
}
@Override
public void setHive(Location location) {
Preconditions.checkArgument(location == null || this.getWorld().equals(location.getWorld()), "Hive must be in same world");
getHandle().hivePos = (location == null) ? null : new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ());
getHandle().hivePos = (location == null) ? null : CraftLocation.toBlockPosition(location);
}
@Override
public Location getFlower() {
BlockPosition flower = getHandle().getSavedFlowerPos();
return (flower == null) ? null : new Location(getWorld(), flower.getX(), flower.getY(), flower.getZ());
return (flower == null) ? null : CraftLocation.toBukkit(flower, getWorld());
}
@Override
public void setFlower(Location location) {
Preconditions.checkArgument(location == null || this.getWorld().equals(location.getWorld()), "Flower must be in same world");
getHandle().setSavedFlowerPos(location == null ? null : new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ()));
getHandle().setSavedFlowerPos(location == null ? null : CraftLocation.toBlockPosition(location));
}
@Override

View File

@@ -4,6 +4,7 @@ import net.minecraft.core.BlockPosition;
import net.minecraft.world.entity.boss.enderdragon.EntityEnderCrystal;
import org.bukkit.Location;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.util.CraftLocation;
import org.bukkit.entity.EnderCrystal;
import org.bukkit.entity.EntityType;
@@ -25,7 +26,7 @@ public class CraftEnderCrystal extends CraftEntity implements EnderCrystal {
@Override
public Location getBeamTarget() {
BlockPosition pos = getHandle().getBeamTarget();
return pos == null ? null : new Location(getWorld(), pos.getX(), pos.getY(), pos.getZ());
return pos == null ? null : CraftLocation.toBukkit(pos, getWorld());
}
@Override
@@ -35,7 +36,7 @@ public class CraftEnderCrystal extends CraftEntity implements EnderCrystal {
} else if (location.getWorld() != getWorld()) {
throw new IllegalArgumentException("Cannot set beam target location to different world");
} else {
getHandle().setBeamTarget(new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ()));
getHandle().setBeamTarget(CraftLocation.toBlockPosition(location));
}
}

View File

@@ -7,6 +7,7 @@ import net.minecraft.world.item.Items;
import org.bukkit.Location;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.inventory.CraftItemStack;
import org.bukkit.craftbukkit.util.CraftLocation;
import org.bukkit.entity.EnderSignal;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;
@@ -39,7 +40,7 @@ public class CraftEnderSignal extends CraftEntity implements EnderSignal {
@Override
public void setTargetLocation(Location location) {
Preconditions.checkArgument(getWorld().equals(location.getWorld()), "Cannot target EnderSignal across worlds");
getHandle().signalTo(BlockPosition.containing(location.getX(), location.getY(), location.getZ()));
getHandle().signalTo(CraftLocation.toBlockPosition(location));
}
@Override

View File

@@ -175,6 +175,7 @@ import org.bukkit.craftbukkit.block.CraftBlock;
import org.bukkit.craftbukkit.persistence.CraftPersistentDataContainer;
import org.bukkit.craftbukkit.persistence.CraftPersistentDataTypeRegistry;
import org.bukkit.craftbukkit.util.CraftChatMessage;
import org.bukkit.craftbukkit.util.CraftLocation;
import org.bukkit.craftbukkit.util.CraftSpawnCategory;
import org.bukkit.craftbukkit.util.CraftVector;
import org.bukkit.entity.Player;
@@ -437,7 +438,7 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity {
@Override
public Location getLocation() {
return new Location(getWorld(), entity.getX(), entity.getY(), entity.getZ(), entity.getBukkitYaw(), entity.getXRot());
return CraftLocation.toBukkit(entity.position(), getWorld(), entity.getBukkitYaw(), entity.getXRot());
}
@Override
@@ -537,7 +538,7 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity {
if (location.getWorld() != null && !location.getWorld().equals(getWorld())) {
// Prevent teleportation to an other world during world generation
Preconditions.checkState(!entity.generation, "Cannot teleport entity to an other world during world generation");
entity.teleportTo(((CraftWorld) location.getWorld()).getHandle(), new Position(location.getX(), location.getY(), location.getZ()));
entity.teleportTo(((CraftWorld) location.getWorld()).getHandle(), CraftLocation.toPosition(location));
return true;
}

View File

@@ -156,7 +156,7 @@ public class CraftFishHook extends CraftProjectile implements FishHook {
EntityFishingHook hook = getHandle();
if (this.biteChance == -1) {
if (hook.level.isRainingAt(new BlockPosition(MathHelper.floor(hook.getX()), MathHelper.floor(hook.getY()) + 1, MathHelper.floor(hook.getZ())))) {
if (hook.level.isRainingAt(BlockPosition.containing(hook.position()).offset(0, 1, 0))) {
return 1 / 300.0;
}
return 1 / 500.0;

View File

@@ -48,6 +48,7 @@ import org.bukkit.craftbukkit.inventory.CraftInventoryView;
import org.bukkit.craftbukkit.inventory.CraftItemStack;
import org.bukkit.craftbukkit.inventory.CraftMerchantCustom;
import org.bukkit.craftbukkit.util.CraftChatMessage;
import org.bukkit.craftbukkit.util.CraftLocation;
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
import org.bukkit.entity.Firework;
@@ -136,7 +137,7 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
Preconditions.checkArgument(location.getWorld() != null, "Location needs to be in a world");
Preconditions.checkArgument(location.getWorld().equals(getWorld()), "Cannot sleep across worlds");
BlockPosition blockposition = new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ());
BlockPosition blockposition = CraftLocation.toBlockPosition(location);
IBlockData iblockdata = getHandle().level.getBlockState(blockposition);
if (!(iblockdata.getBlock() instanceof BlockBed)) {
return false;
@@ -147,7 +148,7 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
}
// From BlockBed
iblockdata = (IBlockData) iblockdata.setValue(BlockBed.OCCUPIED, true);
iblockdata = iblockdata.setValue(BlockBed.OCCUPIED, true);
getHandle().level.setBlock(blockposition, iblockdata, 4);
return true;
@@ -165,7 +166,7 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
Preconditions.checkState(isSleeping(), "Not sleeping");
BlockPosition bed = getHandle().getSleepingPos().get();
return new Location(getWorld(), bed.getX(), bed.getY(), bed.getZ());
return CraftLocation.toBukkit(bed, getWorld());
}
@Override
@@ -340,7 +341,7 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
return null;
}
}
getHandle().openMenu(((BlockWorkbench) Blocks.CRAFTING_TABLE).getMenuProvider(null, getHandle().level, new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ())));
getHandle().openMenu(((BlockWorkbench) Blocks.CRAFTING_TABLE).getMenuProvider(null, getHandle().level, CraftLocation.toBlockPosition(location)));
if (force) {
getHandle().containerMenu.checkReachable = false;
}
@@ -360,7 +361,7 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
}
// If there isn't an enchant table we can force create one, won't be very useful though.
BlockPosition pos = new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ());
BlockPosition pos = CraftLocation.toBlockPosition(location);
getHandle().openMenu(((BlockEnchantmentTable) Blocks.ENCHANTING_TABLE).getMenuProvider(null, getHandle().level, pos));
if (force) {

View File

@@ -137,6 +137,7 @@ import org.bukkit.craftbukkit.map.RenderData;
import org.bukkit.craftbukkit.profile.CraftPlayerProfile;
import org.bukkit.craftbukkit.scoreboard.CraftScoreboard;
import org.bukkit.craftbukkit.util.CraftChatMessage;
import org.bukkit.craftbukkit.util.CraftLocation;
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
import org.bukkit.entity.EntityType;
@@ -380,7 +381,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
if (getHandle().connection == null) return;
// Do not directly assign here, from the packethandler we'll assign it.
getHandle().connection.send(new PacketPlayOutSpawnPosition(new BlockPosition(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()), loc.getYaw()));
getHandle().connection.send(new PacketPlayOutSpawnPosition(CraftLocation.toBlockPosition(loc), loc.getYaw()));
}
@Override
@@ -597,7 +598,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
if (getHandle().connection == null) return;
int packetData = effect.getId();
PacketPlayOutWorldEvent packet = new PacketPlayOutWorldEvent(packetData, new BlockPosition(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()), data, false);
PacketPlayOutWorldEvent packet = new PacketPlayOutWorldEvent(packetData, CraftLocation.toBlockPosition(loc), data, false);
getHandle().connection.send(packet);
}
@@ -626,7 +627,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
public void sendBlockChange(Location loc, Material material, byte data) {
if (getHandle().connection == null) return;
PacketPlayOutBlockChange packet = new PacketPlayOutBlockChange(new BlockPosition(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()), CraftMagicNumbers.getBlock(material, data));
PacketPlayOutBlockChange packet = new PacketPlayOutBlockChange(CraftLocation.toBlockPosition(loc), CraftMagicNumbers.getBlock(material, data));
getHandle().connection.send(packet);
}
@@ -634,7 +635,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
public void sendBlockChange(Location loc, BlockData block) {
if (getHandle().connection == null) return;
PacketPlayOutBlockChange packet = new PacketPlayOutBlockChange(new BlockPosition(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()), ((CraftBlockData) block).getState());
PacketPlayOutBlockChange packet = new PacketPlayOutBlockChange(CraftLocation.toBlockPosition(loc), ((CraftBlockData) block).getState());
getHandle().connection.send(packet);
}
@@ -699,7 +700,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
stage = -1; // The protocol states that any other value will reset the damage, which this API promises
}
PacketPlayOutBlockBreakAnimation packet = new PacketPlayOutBlockBreakAnimation(sourceId, new BlockPosition(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()), stage);
PacketPlayOutBlockBreakAnimation packet = new PacketPlayOutBlockBreakAnimation(sourceId, CraftLocation.toBlockPosition(loc), stage);
getHandle().connection.send(packet);
}
@@ -730,7 +731,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
}
IChatBaseComponent[] components = CraftSign.sanitizeLines(lines);
TileEntitySign sign = new TileEntitySign(new BlockPosition(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()), Blocks.OAK_SIGN.defaultBlockState());
TileEntitySign sign = new TileEntitySign(CraftLocation.toBlockPosition(loc), Blocks.OAK_SIGN.defaultBlockState());
sign.setColor(EnumColor.byId(dyeColor.getWoolData()));
sign.setHasGlowingText(hasGlowingText);
for (int i = 0; i < components.length; i++) {
@@ -1018,7 +1019,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
Optional<Vec3D> spawnLoc = EntityHuman.findRespawnPositionAndUseSpawnBlock(world, bed, getHandle().getRespawnAngle(), getHandle().isRespawnForced(), true);
if (spawnLoc.isPresent()) {
Vec3D vec = spawnLoc.get();
return new Location(world.getWorld(), vec.x, vec.y, vec.z, getHandle().getRespawnAngle(), 0);
return CraftLocation.toBukkit(vec, world.getWorld(), getHandle().getRespawnAngle(), 0);
}
}
return null;
@@ -1034,7 +1035,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
if (location == null) {
getHandle().setRespawnPosition(null, null, 0.0F, override, false, PlayerSpawnChangeEvent.Cause.PLUGIN);
} else {
getHandle().setRespawnPosition(((CraftWorld) location.getWorld()).getHandle().dimension(), new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ()), location.getYaw(), override, false, PlayerSpawnChangeEvent.Cause.PLUGIN);
getHandle().setRespawnPosition(((CraftWorld) location.getWorld()).getHandle().dimension(), CraftLocation.toBlockPosition(location), location.getYaw(), override, false, PlayerSpawnChangeEvent.Cause.PLUGIN);
}
}
@@ -1043,7 +1044,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
Preconditions.checkState(isSleeping(), "Not sleeping");
BlockPosition bed = getHandle().getRespawnPosition();
return new Location(getWorld(), bed.getX(), bed.getY(), bed.getZ());
return CraftLocation.toBukkit(bed, getWorld());
}
@Override

View File

@@ -34,11 +34,10 @@ public abstract class CraftRaider extends CraftMonster implements Raider {
@Override
public void setPatrolTarget(Block block) {
if (block == null) {
getHandle().setPatrolTarget((BlockPosition) null);
getHandle().setPatrolTarget(null);
} else {
Preconditions.checkArgument(block.getWorld().equals(this.getWorld()), "Block must be in same world");
getHandle().setPatrolTarget(new BlockPosition(block.getX(), block.getY(), block.getZ()));
getHandle().setPatrolTarget(((CraftBlock) block).getPosition());
}
}

View File

@@ -7,6 +7,7 @@ import net.minecraft.core.BlockPosition;
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
import org.bukkit.Location;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.util.CraftLocation;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Sniffer;
@@ -33,7 +34,7 @@ public class CraftSniffer extends CraftAnimals implements Sniffer {
@Override
public Collection<Location> getExploredLocations() {
return this.getHandle().getExploredPositions().map(blockPosition -> new Location(this.getLocation().getWorld(), blockPosition.getX(), blockPosition.getY(), blockPosition.getZ())).collect(Collectors.toList());
return this.getHandle().getExploredPositions().map(blockPosition -> CraftLocation.toBukkit(blockPosition, this.getLocation().getWorld())).collect(Collectors.toList());
}
@Override
@@ -43,7 +44,7 @@ public class CraftSniffer extends CraftAnimals implements Sniffer {
return;
}
BlockPosition blockPosition = new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ());
BlockPosition blockPosition = CraftLocation.toBlockPosition(location);
this.getHandle().getBrain().setMemory(MemoryModuleType.SNIFFER_EXPLORED_POSITIONS, this.getHandle().getExploredPositions().filter(blockPositionExplored -> !blockPositionExplored.equals(blockPosition)).collect(Collectors.toList()));
}
@@ -54,7 +55,7 @@ public class CraftSniffer extends CraftAnimals implements Sniffer {
return;
}
this.getHandle().storeExploredPosition(new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ()));
this.getHandle().storeExploredPosition(CraftLocation.toBlockPosition(location));
}
@Override
@@ -70,7 +71,7 @@ public class CraftSniffer extends CraftAnimals implements Sniffer {
@Override
public Location findPossibleDigLocation() {
return this.getHandle().calculateDigPosition().map(blockPosition -> new Location(this.getLocation().getWorld(), blockPosition.getX(), blockPosition.getY(), blockPosition.getZ())).orElse(null);
return this.getHandle().calculateDigPosition().map(blockPosition -> CraftLocation.toBukkit(blockPosition, this.getLocation().getWorld())).orElse(null);
}
@Override

View File

@@ -5,6 +5,7 @@ import net.minecraft.core.BlockPosition;
import net.minecraft.world.entity.monster.EntityVex;
import org.bukkit.Location;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.util.CraftLocation;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Vex;
@@ -42,7 +43,7 @@ public class CraftVex extends CraftMonster implements Vex {
@Override
public Location getBound() {
BlockPosition blockPosition = getHandle().getBoundOrigin();
return (blockPosition == null) ? null : new Location(getWorld(), blockPosition.getX(), blockPosition.getY(), blockPosition.getZ());
return (blockPosition == null) ? null : CraftLocation.toBukkit(blockPosition, getWorld());
}
@Override
@@ -51,7 +52,7 @@ public class CraftVex extends CraftMonster implements Vex {
getHandle().setBoundOrigin(null);
} else {
Preconditions.checkArgument(getWorld().equals(location.getWorld()), "The bound world cannot be different to the entity's world.");
getHandle().setBoundOrigin(new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ()));
getHandle().setBoundOrigin(CraftLocation.toBlockPosition(location));
}
}

View File

@@ -13,6 +13,7 @@ import net.minecraft.world.level.block.state.IBlockData;
import org.apache.commons.lang.Validate;
import org.bukkit.Location;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.util.CraftLocation;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Villager;
@@ -100,7 +101,7 @@ public class CraftVillager extends CraftAbstractVillager implements Villager {
Preconditions.checkArgument(location.getWorld().equals(getWorld()), "Cannot sleep across worlds");
Preconditions.checkState(!getHandle().generation, "Cannot sleep during world generation");
BlockPosition position = new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ());
BlockPosition position = CraftLocation.toBlockPosition(location);
IBlockData iblockdata = getHandle().level.getBlockState(position);
if (!(iblockdata.getBlock() instanceof BlockBed)) {
return false;