正确读取SELECTED_WEAPON,避免崩溃

This commit is contained in:
Light_Quanta 2025-03-12 21:06:35 +08:00
parent 6f9ecd0c51
commit fd555460dd
No known key found for this signature in database
GPG key ID: 11A39A1B8C890959

View file

@ -235,21 +235,26 @@ public abstract class VehicleEntity extends Entity {
this.entityData.define(LAST_DRIVER_UUID, "undefined"); this.entityData.define(LAST_DRIVER_UUID, "undefined");
if (this instanceof WeaponVehicleEntity weaponVehicle && weaponVehicle.getAllWeapons().length > 0) { if (this instanceof WeaponVehicleEntity weaponVehicle && weaponVehicle.getAllWeapons().length > 0) {
this.availableWeapons = new VehicleWeapon[this.getMaxPassengers()][]; this.entityData.define(SELECTED_WEAPON, IntList.of(initSelectedWeaponArray(weaponVehicle)));
var weapons = weaponVehicle.getAllWeapons();
for (int i = 0; i < weapons.length && i < this.getMaxPassengers(); i++) {
this.availableWeapons[i] = weapons[i];
}
var selected = new int[this.getMaxPassengers()];
for (int i = 0; i < this.getMaxPassengers(); i++) {
selected[i] = weaponVehicle.hasWeapon(i) ? 0 : -1;
}
this.entityData.define(SELECTED_WEAPON, IntList.of(selected));
} }
} }
private int[] initSelectedWeaponArray(WeaponVehicleEntity weaponVehicle) {
this.availableWeapons = new VehicleWeapon[this.getMaxPassengers()][];
var weapons = weaponVehicle.getAllWeapons();
for (int i = 0; i < weapons.length && i < this.getMaxPassengers(); i++) {
this.availableWeapons[i] = weapons[i];
}
var selected = new int[this.getMaxPassengers()];
for (int i = 0; i < this.getMaxPassengers(); i++) {
selected[i] = weaponVehicle.hasWeapon(i) ? 0 : -1;
}
return selected;
}
@Override @Override
protected void readAdditionalSaveData(CompoundTag compound) { protected void readAdditionalSaveData(CompoundTag compound) {
this.entityData.set(LAST_ATTACKER_UUID, compound.getString("LastAttacker")); this.entityData.set(LAST_ATTACKER_UUID, compound.getString("LastAttacker"));
@ -258,7 +263,12 @@ public abstract class VehicleEntity extends Entity {
if (this instanceof WeaponVehicleEntity weaponVehicle && weaponVehicle.getAllWeapons().length > 0) { if (this instanceof WeaponVehicleEntity weaponVehicle && weaponVehicle.getAllWeapons().length > 0) {
var selected = compound.getIntArray("SelectedWeapon"); var selected = compound.getIntArray("SelectedWeapon");
this.entityData.set(SELECTED_WEAPON, IntList.of(selected));
if (selected.length != this.getMaxPassengers()) {
this.entityData.set(SELECTED_WEAPON, IntList.of(initSelectedWeaponArray(weaponVehicle)));
} else {
this.entityData.set(SELECTED_WEAPON, IntList.of(selected));
}
} }
} }