character selector

This commit is contained in:
Sen 2025-03-27 17:54:03 +01:00
parent 0af8ca57d1
commit 74f730e450
24 changed files with 554 additions and 154 deletions

View file

@ -1,5 +1,6 @@
package game.network;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@ -20,10 +21,12 @@ import game.entity.effect.EntityLightning;
import game.entity.item.EntityBoat;
import game.entity.item.EntityXp;
import game.entity.npc.EntityNPC;
import game.entity.npc.PlayerCharacter;
import game.entity.projectile.EntityProjectile;
import game.entity.types.EntityLiving;
import game.gui.Gui;
import game.gui.GuiConsole;
import game.gui.GuiSkin;
import game.gui.container.GuiMachine;
import game.gui.container.GuiMerchant;
import game.init.EntityRegistry;
@ -73,6 +76,7 @@ import game.packet.SPacketBlockAction;
import game.packet.SPacketBlockBreakAnim;
import game.packet.SPacketBlockChange;
import game.packet.SPacketCamera;
import game.packet.SPacketCharacterList;
import game.packet.SPacketChunkData;
import game.packet.SPacketCollectItem;
import game.packet.SPacketDestroyEntities;
@ -137,8 +141,9 @@ public class ClientPlayer extends NetHandler
private boolean doneLoadingTerrain;
// private boolean travelSound;
private final Map<String, Integer> playerList = Maps.<String, Integer>newTreeMap();
private final Map<Integer, PlayerCharacter> characterList = Maps.<Integer, PlayerCharacter>newHashMap();
// private final List<Entry<String, Integer>> players = Lists.newArrayList();
// private boolean field_147308_k = false;
private int selectedCharacter = -1;
/**
* Just an ordinary random number generator, used to randomize audio pitch of item/orb pickup and randomize both
@ -168,13 +173,14 @@ public class ClientPlayer extends NetHandler
public void handleJoinGame(SPacketJoinGame packetIn)
{
NetHandler.checkThread(packetIn, this, this.gameController);
this.gameController.charEditor = packetIn.isInEditor();
this.gameController.controller = new PlayerController(this.gameController, this);
this.clientWorldController = new WorldClient(this.gameController.debugWorld, packetIn.getDimension());
// this.gameController.gameSettings.difficulty = packetIn.getDifficulty();
this.gameController.loadWorld(this.clientWorldController, packetIn.getEntityType());
// this.gameController.thePlayer.dimension = this.clientWorldController.dimension.getDimensionId();
this.gameController.displayGuiScreen(null);
this.gameController.thePlayer.setId(packetIn.getEntityId());
this.gameController.displayGuiScreen(this.gameController.charEditor ? GuiSkin.INSTANCE : null);
// this.currentServerMaxPlayers = packetIn.getMaxPlayers();
// this.gameController.controller.setCheat(packetIn.getCheat());
// this.gameController.updateViewDistance();
@ -435,6 +441,9 @@ public class ClientPlayer extends NetHandler
if (entity != null && packetIn.func_149376_c() != null)
{
entity.getDataWatcher().updateWatchedObjectsFromList(packetIn.func_149376_c());
if(entity == this.gameController.thePlayer && this.gameController.open instanceof GuiSkin)
((GuiSkin)this.gameController.open).checkReopen();
}
}
@ -993,6 +1002,7 @@ public class ClientPlayer extends NetHandler
public void handleRespawn(SPacketRespawn packetIn)
{
NetHandler.checkThread(packetIn, this, this.gameController, this.clientWorldController);
this.gameController.charEditor = packetIn.isInEditor();
Dimension dim = packetIn.getDimension();
if (dim.getDimensionId() != this.clientWorldController.dimension.getDimensionId()) // this.gameController.thePlayer.dimension)
@ -1006,10 +1016,13 @@ public class ClientPlayer extends NetHandler
// this.clientWorldController.setWorldScoreboard(scoreboard);
this.gameController.loadWorld(this.clientWorldController, packetIn.getEntityType());
// this.gameController.thePlayer.dimension = dim.getDimensionId();
this.gameController.displayGuiScreen(null);
}
// else if(this.gameController.charEditor) {
// this.gameController.displayGuiScreen(GuiSkin.INSTANCE);
// }
this.gameController.setDimensionAndSpawnPlayer(dim.getDimensionId(), packetIn.getEntityType());
this.gameController.displayGuiScreen(this.gameController.charEditor ? GuiSkin.INSTANCE : null);
// this.gameController.controller.setCheat(packetIn.getCheat());
}
@ -1506,6 +1519,19 @@ public class ClientPlayer extends NetHandler
// this.gameController.infolist(list, data);
}
public void handleCharacterList(SPacketCharacterList packet)
{
NetHandler.checkThread(packet, this, this.gameController);
for(Entry<Integer, PlayerCharacter> data : packet.getEntries()) {
if(data.getValue() == null)
this.characterList.remove(data.getKey());
else
this.characterList.put(data.getKey(), data.getValue());
}
this.selectedCharacter = packet.getSelected();
}
public void handleKeepAlive(SPacketKeepAlive packetIn)
{
this.addToSendQueue(new CPacketKeepAlive(packetIn.getValue()));
@ -1858,6 +1884,16 @@ public class ClientPlayer extends NetHandler
return this.playerList.entrySet();
}
public Collection<PlayerCharacter> getCharacterList()
{
return this.characterList.values();
}
public int getSelectedCharacter()
{
return this.selectedCharacter;
}
public Iterable<String> getPlayerNames()
{
return this.playerList.keySet();

View file

@ -58,6 +58,7 @@ import game.packet.SPacketBlockAction;
import game.packet.SPacketBlockBreakAnim;
import game.packet.SPacketBlockChange;
import game.packet.SPacketCamera;
import game.packet.SPacketCharacterList;
import game.packet.SPacketChunkData;
import game.packet.SPacketCollectItem;
import game.packet.SPacketDestroyEntities;
@ -176,6 +177,7 @@ public enum PacketRegistry
this.server(SPacketTrades.class);
// this.server(SPacketNotify.class);
this.server(SPacketDimensionName.class);
this.server(SPacketCharacterList.class);
this.client(CPacketKeepAlive.class);
this.client(CPacketMessage.class);

View file

@ -24,6 +24,7 @@ import game.clipboard.RotationValue;
import game.clipboard.Vector;
import game.color.TextColor;
import game.command.Executor;
import game.dimension.Dimension;
import game.entity.Entity;
import game.entity.animal.EntityHorse;
import game.entity.item.EntityItem;
@ -31,6 +32,7 @@ import game.entity.item.EntityXp;
import game.entity.npc.Alignment;
import game.entity.npc.EntityHuman;
import game.entity.npc.EntityNPC;
import game.entity.npc.PlayerCharacter;
import game.entity.projectile.EntityArrow;
import game.entity.types.EntityLiving;
import game.init.BlockRegistry;
@ -40,6 +42,7 @@ import game.init.EntityRegistry;
import game.init.Items;
import game.init.RotationRegistry;
import game.init.SoundEvent;
import game.init.UniverseRegistry;
import game.inventory.Container;
import game.inventory.ContainerChest;
import game.inventory.ContainerHorseInventory;
@ -54,11 +57,10 @@ import game.item.ItemStack;
import game.log.Log;
import game.material.Material;
import game.nbt.NBTTagCompound;
import game.nbt.NBTTagDouble;
import game.nbt.NBTTagFloat;
import game.nbt.NBTTagList;
import game.nbt.NBTTagString;
import game.packet.CPacketAction;
import game.packet.CPacketAction.Action;
import game.packet.CPacketBook;
import game.packet.CPacketBreak;
import game.packet.CPacketCheat;
@ -88,6 +90,7 @@ import game.packet.S39PacketPlayerAbilities;
import game.packet.S3APacketTabComplete;
import game.packet.SPacketAnimation;
import game.packet.SPacketBlockChange;
import game.packet.SPacketCharacterList;
import game.packet.SPacketChunkData;
import game.packet.SPacketDestroyEntities;
import game.packet.SPacketDisconnect;
@ -140,7 +143,7 @@ public class Player extends NetHandler implements ICrafting, Executor
public static class UserValidator implements CharValidator {
public boolean valid(char ch) {
return /* (ch >= 'A' && ch <= 'Z') || */ (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') || ch == '_' || ch == '-';
return (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') || ch == '_' || ch == '-';
}
}
@ -154,6 +157,7 @@ public class Player extends NetHandler implements ICrafting, Executor
public static final int MAX_NICK_LENGTH = 32;
public static final int MAX_PASS_LENGTH = 64;
public static final int MAX_CMD_LENGTH = 1024;
public static final int MAX_INFO_LENGTH = 4096;
public static final CharValidator VALID_USER = new UserValidator();
public static final CharValidator VALID_NICK = new NickValidator();
@ -176,6 +180,7 @@ public class Player extends NetHandler implements ICrafting, Executor
private double lastPosY;
private double lastPosZ;
private boolean hasMoved = true;
private boolean charEditor = true;
private boolean admin;
private int ping;
@ -310,6 +315,14 @@ public class Player extends NetHandler implements ICrafting, Executor
this.respawnTimer = ExtMath.clampi(Config.respawnTime, 2, 20);
}
public boolean isInEditor() {
return this.charEditor;
}
public EntityNPC getPresentEntity() {
return this.charEditor ? null : this.entity;
}
public EntityNPC getEntity() {
return this.entity;
}
@ -610,6 +623,7 @@ public class Player extends NetHandler implements ICrafting, Executor
this.characters.add(list.getCompoundTagAt(z));
}
this.selected = Math.min(this.selected, this.characters.size() - 1);
this.charEditor = this.selected < 0;
// this.stats.clear();
// if(tag.hasKey("Stats", 10)) {
// NBTTagCompound stats = tag.getCompoundTag("Stats");
@ -1501,40 +1515,15 @@ public class Player extends NetHandler implements ICrafting, Executor
String info = this.getRotationInfo();
this.addHotbar(TextColor.YELLOW + (info.isEmpty() ? "Keine Drehung" : info));
}
public List<NBTTagCompound> getCharacters() {
return this.characters;
}
public NBTTagCompound addCharacter(String id) {
NBTTagCompound tag = new NBTTagCompound();
this.characters.add(tag);
tag.setString("id", id);
return tag;
}
public NBTTagCompound getSelectedCharacter(boolean create) {
NBTTagCompound tag = this.characters.isEmpty() || this.selected < 0 ? null : this.characters.get(this.selected);
if(create && tag == null) {
tag = new NBTTagCompound();
this.selected = this.characters.size();
this.characters.add(tag);
return tag;
}
else if(create) {
tag.getKeySet().clear();
}
return tag;
}
public void selectCharacter(int index) {
if(index == this.selected || index >= this.characters.size())
return;
this.server.swapPlayer(this, index < 0 ? null : this.characters.get(index));
this.selected = index;
public NBTTagCompound readCharacter() {
return this.characters.isEmpty() || this.selected < 0 ? null : this.characters.get(this.selected);
}
public void writeCharacter(NBTTagCompound tag) {
if(!this.characters.isEmpty() && this.selected >= 0)
this.characters.set(this.selected, tag);
}
public void disconnect()
{
@ -1563,7 +1552,27 @@ public class Player extends NetHandler implements ICrafting, Executor
this.server.removePlayer(this);
}
public PlayerCharacter getCharacterInfo(NBTTagCompound tag) {
String name = tag.getString("CustomName");
String info = tag.getString("Description");
if(info != null && info.isEmpty())
info = null;
Alignment align = Alignment.getByName(tag.getString("Align"));
NBTTagList position = tag.getTagList("Pos", 6);
Dimension dimension = UniverseRegistry.getDimension(tag.getInteger("Dimension"));
String dim = dimension == null ? "???" : dimension.getFormattedName(false);
BlockPos pos = new BlockPos(position.getDoubleAt(0), position.getDoubleAt(1), position.getDoubleAt(2));
String type = EntityRegistry.getEntityName(tag.getString("id"));
int level = tag.getInteger("XpLevel");
return new PlayerCharacter(name, info, align, dim, pos, type, level);
}
public void onConnect() {
List<PlayerCharacter> chars = Lists.newArrayList();
for(NBTTagCompound tag : this.characters) {
chars.add(this.getCharacterInfo(tag));
}
this.sendPacket(new SPacketCharacterList(this.selected, chars));
if(!this.local)
this.server.sendPacket(new SPacketMessage(String.format("%s hat das Spiel betreten", this.user), Type.FEED));
}
@ -1946,7 +1955,12 @@ public class Player extends NetHandler implements ICrafting, Executor
if(c < 32 || c == 127)
throw new IllegalArgumentException("Ungültige Zeichen in Nachricht");
}
switch(packetIn.getType()) {
CPacketMessage.Type type = packetIn.getType();
if(this.charEditor != (type == CPacketMessage.Type.DISPLAY))
return;
switch(type) {
case COMMAND:
if(!this.setVar(msg))
this.runCommand(msg); // this.addFeed(TextColor.RED + "Befehl wurde nicht gefunden");
@ -2009,7 +2023,7 @@ public class Player extends NetHandler implements ICrafting, Executor
public void processComplete(CPacketComplete packetIn)
{
NetHandler.checkThread(packetIn, this, this.server);
if(!this.isAdmin()) {
if(this.charEditor || !this.isAdmin()) {
this.entity.connection.sendPacket(new S3APacketTabComplete(new String[0]));
return;
}
@ -2043,6 +2057,8 @@ public class Player extends NetHandler implements ICrafting, Executor
public void processPlayer(CPacketPlayer packetIn)
{
NetHandler.checkThread(packetIn, this, this.server);
if(this.charEditor)
return;
if (!isFinite(packetIn.getPositionX()) || !isFinite(packetIn.getPositionY()) || !isFinite(packetIn.getPositionZ()) || !isFinite(packetIn.getPitch()) || !isFinite(packetIn.getYaw()))
{
@ -2285,6 +2301,8 @@ public class Player extends NetHandler implements ICrafting, Executor
public void processBreak(CPacketBreak packetIn)
{
NetHandler.checkThread(packetIn, this, this.server);
if(this.charEditor)
return;
WorldServer worldserver = this.entity.getServerWorld(); // this.serverController.getWorld(this.playerEntity.dimension);
BlockPos blockpos = packetIn.getPosition();
@ -2369,6 +2387,8 @@ public class Player extends NetHandler implements ICrafting, Executor
public void processPlace(CPacketPlace packetIn)
{
NetHandler.checkThread(packetIn, this, this.server);
if(this.charEditor)
return;
WorldServer worldserver = this.entity.getServerWorld(); // this.serverController.getWorld(this.playerEntity.dimension);
ItemStack itemstack = this.entity.inventory.getCurrentItem();
boolean flag = false;
@ -2472,8 +2492,40 @@ public class Player extends NetHandler implements ICrafting, Executor
{
NetHandler.checkThread(packetIn, this, this.server);
switch (packetIn.getAction())
CPacketAction.Action action = packetIn.getAction();
if(this.charEditor != (action == Action.SET_ALIGN || action == Action.SET_SPECIES || action == Action.SET_HEIGHT || action == Action.CLOSE_EDITOR))
return;
switch (action)
{
case OPEN_EDITOR:
this.charEditor = true;
NBTTagCompound tag = this.server.swapPlayer(this, null, EntityHuman.class);
if(!this.characters.isEmpty() && this.selected >= 0)
this.characters.set(this.selected, tag);
this.selected = -1;
this.sendPacket(new SPacketCharacterList(this.selected));
break;
case CLOSE_EDITOR:
this.charEditor = false;
this.selected = this.characters.size();
this.characters.add(new NBTTagCompound());
this.entity.teleport(this.server.getRandomSpawnPosition());
this.sendPacket(new SPacketCharacterList(this.selected, this.selected, new PlayerCharacter(this.entity.getCustomNameTag(), this.entity.getDescription(), this.entity.getAlignment(), this.entity.worldObj.dimension.getFormattedName(false), this.entity.getPosition(), EntityRegistry.getEntityName(EntityRegistry.getEntityString(this.entity)), this.entity.experienceLevel)));
break;
case SELECT_CHARACTER:
int index = packetIn.getAuxData();
if(index == this.selected || index >= this.characters.size() || index < 0)
return;
NBTTagCompound etag = this.server.swapPlayer(this, this.characters.get(index), null);
if(!this.characters.isEmpty() && this.selected >= 0)
this.characters.set(this.selected, etag);
this.selected = index;
this.sendPacket(new SPacketCharacterList(this.selected));
break;
case START_SNEAKING:
this.entity.setSneaking(true);
break;
@ -2540,7 +2592,8 @@ public class Player extends NetHandler implements ICrafting, Executor
// break;
case SET_HEIGHT:
this.entity.setHeight(((float)ExtMath.clampi(packetIn.getAuxData(), 120, 320) * 0.01f) / this.entity.getSpecies().size);
this.entity.setHeight(((float)ExtMath.clampi(packetIn.getAuxData(), 120, 320) * 0.01f) / this.entity.getSpecies().renderer.height);
// Log.CONSOLE.info("" + this.entity.height + "(" + this.entity.getHeight() + ")");
break;
case SELECT_TRADE:
@ -2759,23 +2812,9 @@ public class Player extends NetHandler implements ICrafting, Executor
case SET_SPECIES:
Class<? extends Entity> clazz = EntityRegistry.getEntityClass(packetIn.getAuxData());
if(EntityNPC.class.isAssignableFrom(clazz)) {
NBTTagCompound tag = this.addCharacter(EntityRegistry.getEntityString(clazz));
tag.setInteger("Dimension", this.entity.worldObj.dimension.getDimensionId());
NBTTagList pos = new NBTTagList();
pos.appendTag(new NBTTagDouble(this.entity.posX));
pos.appendTag(new NBTTagDouble(this.entity.posY));
pos.appendTag(new NBTTagDouble(this.entity.posZ));
tag.setTag("Pos", pos);
NBTTagList rot = new NBTTagList();
rot.appendTag(new NBTTagFloat(this.entity.rotYaw));
rot.appendTag(new NBTTagFloat(this.entity.rotPitch));
tag.setTag("Rotation", rot);
tag.setString("CustomName", this.entity.getCustomNameTag());
tag.setString("Align", this.entity.getAlignment().name);
tag.setFloat("Height", this.entity.getHeight());
this.server.swapPlayer(this, tag);
}
if(EntityNPC.class.isAssignableFrom(clazz))
this.server.swapPlayer(this, null, (Class<? extends EntityNPC>)clazz);
// Log.CONSOLE.info("" + this.entity.height + "(" + this.entity.getHeight() + ")");
break;
default:
@ -2786,12 +2825,16 @@ public class Player extends NetHandler implements ICrafting, Executor
public void processInput(CPacketInput packetIn)
{
NetHandler.checkThread(packetIn, this, this.server);
if(this.charEditor)
return;
this.setEntityActionState(packetIn.getStrafeSpeed(), packetIn.getForwardSpeed(), packetIn.isJumping(), packetIn.isSneaking());
}
public void processClick(CPacketClick packetIn)
{
NetHandler.checkThread(packetIn, this, this.server);
if(this.charEditor)
return;
if (this.entity.openContainer.windowId == packetIn.getWindowId() && this.entity.openContainer.getCanCraft(this.entity))
{
@ -2839,6 +2882,8 @@ public class Player extends NetHandler implements ICrafting, Executor
public void processCheat(CPacketCheat packetIn)
{
NetHandler.checkThread(packetIn, this, this.server);
if(this.charEditor)
return;
if(!this.isAdmin())
return;
@ -2929,6 +2974,8 @@ public class Player extends NetHandler implements ICrafting, Executor
public void processSign(CPacketSign packetIn)
{
NetHandler.checkThread(packetIn, this, this.server);
if(this.charEditor)
return;
WorldServer worldserver = this.entity.getServerWorld(); // this.serverController.getWorld(this.playerEntity.dimension);
BlockPos blockpos = packetIn.getPosition();
@ -2984,6 +3031,8 @@ public class Player extends NetHandler implements ICrafting, Executor
public void processSkin(CPacketSkin packetIn)
{
NetHandler.checkThread(packetIn, this, this.server);
if(!this.charEditor)
return;
// if(!this.netManager.isLocalChannel() && System.currentTimeMillis() - this.lastSkinUpdate < 1000L) {
// String comp = "Bitte warte 1 Sekunde bevor du deinen Skin änderst";
//// comp.setColor();
@ -3003,6 +3052,9 @@ public class Player extends NetHandler implements ICrafting, Executor
public void processBook(CPacketBook packetIn) {
NetHandler.checkThread(packetIn, this, this.server);
if(this.charEditor)
return;
String[] pages = packetIn.getPages();
// if (!ItemWritableBook.isNBTValid(itemstack1.getTagCompound()))
// throw new IOException("Fehlerhafter Buch-Tag!");