Set controller after setting spawnedByCore (fix UnitChangeEvent) (#8428)

* CoreBlock.playerSpawn: Set spawnedByCore first
* The call to unit.controller() calls player.unit(unit)
* That function fires UnitChangeEvent
* The unit in UnitChangeEvent has spawnedByCore incorrectly set to false
* This caused me to waste an hour of my life while making a plugin

* Might as well improve code readability
This commit is contained in:
BalaM314
2023-03-25 23:25:26 +05:30
committed by GitHub
parent 355f33b0a4
commit 08f63122ba

View File

@@ -66,27 +66,27 @@ public class CoreBlock extends StorageBlock{
@Remote(called = Loc.server) @Remote(called = Loc.server)
public static void playerSpawn(Tile tile, Player player){ public static void playerSpawn(Tile tile, Player player){
if(player == null || tile == null || !(tile.build instanceof CoreBuild entity)) return; if(player == null || tile == null || !(tile.build instanceof CoreBuild core)) return;
CoreBlock block = (CoreBlock)tile.block(); UnitType spawnType = ((CoreBlock)core.block).unitType;
if(entity.wasVisible){ if(core.wasVisible){
Fx.spawn.at(entity); Fx.spawn.at(core);
} }
player.set(entity); player.set(core);
if(!net.client()){ if(!net.client()){
Unit unit = block.unitType.create(tile.team()); Unit unit = spawnType.create(tile.team());
unit.set(entity); unit.set(core);
unit.rotation(90f); unit.rotation(90f);
unit.impulse(0f, 3f); unit.impulse(0f, 3f);
unit.controller(player);
unit.spawnedByCore(true); unit.spawnedByCore(true);
unit.controller(player);
unit.add(); unit.add();
} }
if(state.isCampaign() && player == Vars.player){ if(state.isCampaign() && player == Vars.player){
block.unitType.unlock(); spawnType.unlock();
} }
} }