fix and improve camera system

This commit is contained in:
Sen 2025-08-07 02:17:59 +02:00
parent a76c0c66e1
commit 72dc4a1305
Signed by: sen
GPG key ID: 3AC50A6F47D1B722
15 changed files with 180 additions and 108 deletions

View file

@ -28,6 +28,6 @@ public class CommandCamera extends Command {
}
public void exec(CommandEnvironment env, Executor exec, Vec3 position, Dimension dim, double yaw, double pitch, int radius, double speed, CameraType type, boolean fixed, Player player) {
player.setCamera(position.xCoord, position.yCoord, position.zCoord, (float)yaw, (float)pitch, radius, (float)speed, type, fixed);
player.setCamera(dim, position.xCoord, position.yCoord, position.zCoord, (float)yaw, (float)pitch, radius, (float)speed, type, fixed);
}
}

View file

@ -14,6 +14,6 @@ public class CommandNocam extends Command {
}
public void exec(CommandEnvironment env, Executor exec, Player player) {
player.removeCamera();
player.unsetCamera();
}
}

View file

@ -24,6 +24,7 @@ import common.entity.item.EntityCamera.CameraType;
import common.entity.item.EntityItem;
import common.entity.item.EntityXp;
import common.entity.npc.Alignment;
import common.entity.npc.EntityCameraHolder;
import common.entity.npc.EntityHuman;
import common.entity.npc.EntityNPC;
import common.entity.npc.PlayerCharacter;
@ -164,6 +165,7 @@ public class Player extends User implements Executor, IPlayer
private int lastMoved;
private int pingKey;
private int selected = -1;
private int lastSelected = -1;
private long lastPingTime = System.nanoTime() / 1000000L;
private int lastSentPingPacket;
private double lastPosX;
@ -217,6 +219,8 @@ public class Player extends User implements Executor, IPlayer
private Executor forcedExec;
private Position deathPos;
private Position teleportPos;
private Position lastDeathPos;
private Position lastTeleportPos;
private EntityCamera camera;
public Player(Server server, NetConnection connection, String user)
@ -359,16 +363,39 @@ public class Player extends User implements Executor, IPlayer
public void setCamera(double x, double y, double z, float yaw, float pitch, int radius, float speed, CameraType type, boolean fixed) {
public boolean setCamera(Dimension dim, double x, double y, double z, float yaw, float pitch, int radius, float speed, CameraType type, boolean fixed) {
if(this.charEditor || this.characters.isEmpty() || this.selected < 0 || this.lastSelected >= 0)
return false;
this.lastDeathPos = this.deathPos;
this.lastTeleportPos = this.teleportPos;
String name = this.entity.getCustomNameTag();
TagObject tag = this.server.swapPlayer(this, null, EntityCameraHolder.class);
this.characters.set(this.selected, tag);
this.lastSelected = this.selected;
this.selected = this.characters.size();
this.characters.add(new TagObject());
WorldServer world = this.server.getWorld(dim);
world = world == null ? this.server.getSpace() : world;
this.entity.teleport(x, y, z, yaw, pitch, world);
this.entity.setNoclip(true);
this.entity.setCustomNameTag("Kamera von " + name);
this.teleportPos = this.deathPos = null;
this.camera = new EntityCamera(this.entity.worldObj, x, y, z, yaw, pitch, radius, speed, type, fixed);
this.sendPacket(new SPacketCamera(this.camera));
return true;
}
public void removeCamera() {
if(this.camera != null) {
this.camera = null;
this.sendPacket(new SPacketCamera(null));
}
public boolean unsetCamera() {
if(this.charEditor || this.lastSelected >= this.characters.size() || this.lastSelected < 0)
return false;
this.server.swapPlayer(this, this.characters.get(this.selected = this.lastSelected), null);
this.characters.removeLast();
this.lastSelected = -1;
this.deathPos = this.lastDeathPos;
this.teleportPos = this.lastTeleportPos;
this.camera = null;
this.sendPacket(new SPacketCamera(null));
return true;
}
@ -626,11 +653,13 @@ public class Player extends User implements Executor, IPlayer
public void writeTags(TagObject tag) {
if(!this.characters.isEmpty()) {
tag.setInt("selected", this.selected);
tag.setInt("selected", this.lastSelected >= 0 ? this.lastSelected : this.selected);
List<TagObject> list = Lists.newArrayList();
for(TagObject etag : this.characters) {
list.add(etag);
}
if(this.lastSelected >= 0)
list.removeLast();
tag.setList("characters", list);
}
}
@ -653,14 +682,17 @@ public class Player extends User implements Executor, IPlayer
public void travelToDimension(Dimension dimensionId, BlockPos pos, float yaw, float pitch, PortalType portal)
{
if(this.camera != null)
return;
this.server.transferToDimension(this.entity, dimensionId, pos, yaw, pitch, portal);
this.lastExperience = -1;
this.lastHealth = -1.0F;
}
public void teleport(double x, double y, double z, float yaw, float pitch, Dimension dimension) {
if(this.camera != null)
this.unsetCamera();
this.teleportPos = this.entity.getPos();
this.removeCamera();
x = ExtMath.clampd(x, -World.MAX_SIZE + 1, World.MAX_SIZE - 1);
z = ExtMath.clampd(z, -World.MAX_SIZE + 1, World.MAX_SIZE - 1);
// this.setLastTeleport(this.getLocation());
@ -2036,8 +2068,14 @@ public class Player extends User implements Executor, IPlayer
if(this.camera != null) {
this.camera.setPositionClamped(packetIn.getPositionX(), packetIn.getPositionY(), packetIn.getPositionZ());
this.camera.setRotationClamped(packetIn.getYaw(), packetIn.getPitch());
if(!this.camera.fixed && packetIn.isOnGround())
this.removeCamera();
if(!this.camera.fixed && packetIn.isOnGround()) {
this.unsetCamera();
}
else {
this.setPlayerLocation(this.camera.posX, this.camera.posY, this.camera.posZ, this.camera.rotYaw, this.camera.rotPitch);
this.entity.setRotationYawHead(this.camera.rotYaw);
this.getEntityWorld().updateMountedMovingPlayer(this.entity);
}
}
return;
}
@ -2274,7 +2312,7 @@ public class Player extends User implements Executor, IPlayer
public void processBreak(CPacketBreak packetIn)
{
NetHandler.checkThread(packetIn, this, this.server);
if(this.charEditor)
if(this.charEditor || this.entity instanceof EntityCameraHolder)
return;
WorldServer worldserver = this.getEntityWorld(); // this.serverController.getWorld(this.playerEntity.dimension);
BlockPos blockpos = packetIn.getPosition();
@ -2360,7 +2398,7 @@ public class Player extends User implements Executor, IPlayer
public void processPlace(CPacketPlace packetIn)
{
NetHandler.checkThread(packetIn, this, this.server);
if(this.charEditor)
if(this.charEditor || this.entity instanceof EntityCameraHolder)
return;
WorldServer worldserver = this.getEntityWorld(); // this.serverController.getWorld(this.playerEntity.dimension);
ItemStack itemstack = this.entity.getHeldItem();
@ -2466,7 +2504,7 @@ public class Player extends User implements Executor, IPlayer
NetHandler.checkThread(packetIn, this, this.server);
CPacketAction.Action action = packetIn.getAction();
if(this.charEditor != (action == Action.SET_ALIGN || action == Action.SET_SPECIES || action == Action.SET_CLASS || action == Action.SET_HEIGHT || action == Action.CLOSE_EDITOR || action == Action.CANCEL_EDITOR)) // {
if((this.charEditor != (action == Action.SET_ALIGN || action == Action.SET_SPECIES || action == Action.SET_CLASS || action == Action.SET_HEIGHT || action == Action.CLOSE_EDITOR || action == Action.CANCEL_EDITOR)) || (this.entity instanceof EntityCameraHolder && action != Action.WARP_MODE && action != Action.PERF && action != Action.START_PROFILING && action != Action.STOP_PROFILING)) // {
// if(this.local && action == Action.CLOSE_EDITOR)
// this.server.setDone();
return;
@ -2484,7 +2522,6 @@ public class Player extends User implements Executor, IPlayer
int last = this.selected;
this.selected = -1;
this.teleportPos = this.deathPos = null;
this.removeCamera();
this.sendPacket(!this.characters.isEmpty() && last >= 0 ? new SPacketCharacterList(this.selected, last, this.getCharacterInfo(tag)) : new SPacketCharacterList(this.selected));
break;
}
@ -2500,7 +2537,6 @@ public class Player extends User implements Executor, IPlayer
Position pos = this.server.getRandomSpawnPosition(origin);
this.entity.teleport(pos);
this.teleportPos = this.deathPos = null;
this.removeCamera();
this.sendPacket(new SPacketCharacterList(this.selected, this.selected, new PlayerCharacter(this.entity.getCustomNameTag(), this.entity.getDescription(), this.entity.getAlignment(), this.entity.worldObj.dimension.getDisplay(), this.entity.getPosition(), EntityRegistry.getEntityName(EntityRegistry.getEntityString(this.entity)), this.entity.experienceLevel, world.dimension == Space.INSTANCE ? null : world.dimension.getDisplay())));
// if(this.local)
// this.server.setDone();
@ -2515,7 +2551,6 @@ public class Player extends User implements Executor, IPlayer
this.server.swapPlayer(this, this.characters.get(index), null);
this.selected = index;
this.teleportPos = this.deathPos = null;
this.removeCamera();
this.sendPacket(new SPacketCharacterList(this.selected));
break;
}
@ -2530,7 +2565,6 @@ public class Player extends User implements Executor, IPlayer
int last = this.selected;
this.selected = index;
this.teleportPos = this.deathPos = null;
this.removeCamera();
this.sendPacket(!this.characters.isEmpty() && last >= 0 ? new SPacketCharacterList(this.selected, last, this.getCharacterInfo(etag)) : new SPacketCharacterList(this.selected));
break;
}
@ -2802,7 +2836,7 @@ public class Player extends User implements Executor, IPlayer
case SET_SPECIES:
Class<? extends Entity> clazz = EntityRegistry.getEntityClass(packetIn.getAuxData());
if(EntityNPC.class.isAssignableFrom(clazz))
if(EntityNPC.class.isAssignableFrom(clazz) && clazz != EntityCameraHolder.class)
this.server.swapPlayer(this, null, (Class<? extends EntityNPC>)clazz);
// Log.CONSOLE.info("" + this.entity.height + "(" + this.entity.getHeight() + ")");
break;
@ -2867,7 +2901,7 @@ public class Player extends User implements Executor, IPlayer
public void processClick(CPacketClick packetIn)
{
NetHandler.checkThread(packetIn, this, this.server);
if(this.charEditor)
if(this.charEditor || this.entity instanceof EntityCameraHolder)
return;
if (this.entity.openContainer.windowId == packetIn.getWindowId() && this.entity.openContainer.getCanCraft(this.entity))
@ -2916,7 +2950,7 @@ public class Player extends User implements Executor, IPlayer
public void processCheat(CPacketCheat packet)
{
NetHandler.checkThread(packet, this, this.server);
if(this.charEditor || !this.isAdmin())
if(this.charEditor || !this.isAdmin() || this.entity instanceof EntityCameraHolder)
return;
Item item = packet.getItem();
if(item == null)
@ -2978,7 +3012,7 @@ public class Player extends User implements Executor, IPlayer
public void processSign(CPacketSign packetIn)
{
NetHandler.checkThread(packetIn, this, this.server);
if(this.charEditor)
if(this.charEditor || this.entity instanceof EntityCameraHolder)
return;
WorldServer worldserver = this.getEntityWorld(); // this.serverController.getWorld(this.playerEntity.dimension);