Files
Server/src/engine/devcmd/cmds/SetOwnerCmd.java
T

119 lines
3.2 KiB
Java
Raw Normal View History

2022-04-30 09:41:17 -04:00
package engine.devcmd.cmds;
import engine.mbEnums;
import engine.mbEnums.DbObjectType;
import engine.mbEnums.GameObjectType;
2022-04-30 09:41:17 -04:00
import engine.devcmd.AbstractDevCmd;
import engine.gameManager.DbManager;
import engine.objects.*;
/**
2023-07-15 09:23:48 -04:00
* @author Dev command to set the owner of targeted building.
2022-04-30 09:41:17 -04:00
* Argument is a valid guild UID
*/
public class SetOwnerCmd extends AbstractDevCmd {
2023-07-15 09:23:48 -04:00
Building _targetBuilding = null;
DbObjectType _newOwnerType;
AbstractCharacter outOwner;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
public SetOwnerCmd() {
2022-04-30 09:41:17 -04:00
super("setowner");
}
2023-07-15 09:23:48 -04:00
@Override
protected void _doCmd(PlayerCharacter pcSender, String[] args,
AbstractGameObject target) {
if (validateUserInput(pcSender, target, args) == false) {
this.sendUsage(pcSender);
return;
}
2022-04-30 09:41:17 -04:00
// Valid arguments, attempt to set owner of Building.
2023-07-15 09:23:48 -04:00
2022-04-30 09:41:17 -04:00
_targetBuilding = getTargetAsBuilding(pcSender);
// if it's a tol change ownership of the city
if (_targetBuilding.getBlueprint() != null &&
_targetBuilding.getBlueprint().getBuildingGroup().equals(mbEnums.BuildingGroup.TOL)) {
2022-04-30 09:41:17 -04:00
City city = _targetBuilding.getCity();
if (city != null) {
city.claim(outOwner);
2023-07-15 09:23:48 -04:00
return;
}
2022-04-30 09:41:17 -04:00
}
_targetBuilding.setOwner(outOwner);
2023-07-15 09:23:48 -04:00
2022-04-30 09:41:17 -04:00
DbManager.BuildingQueries.SET_PROPERTY(_targetBuilding, "ownerUUID", args[0]);
2023-07-15 09:23:48 -04:00
2022-04-30 09:41:17 -04:00
_targetBuilding.refreshGuild();
2023-07-15 09:23:48 -04:00
}
@Override
protected String _getUsageString() {
2022-04-30 09:41:17 -04:00
return "' /setowner [UID]";
2023-07-15 09:23:48 -04:00
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
@Override
protected String _getHelpString() {
2022-04-30 09:41:17 -04:00
return "Assigns new owner to building";
2023-07-15 09:23:48 -04:00
}
private boolean validateUserInput(PlayerCharacter pcSender, AbstractGameObject currTarget, String[] userInput) {
2022-04-30 09:41:17 -04:00
// Incorrect number of arguments
2023-07-15 09:23:48 -04:00
2022-04-30 09:41:17 -04:00
if (userInput.length != 1)
2023-07-15 09:23:48 -04:00
return false;
2022-04-30 09:41:17 -04:00
// No target
2023-07-15 09:23:48 -04:00
if (currTarget == null) {
throwbackError(pcSender, "Requires a Building to be targeted");
return false;
}
2022-04-30 09:41:17 -04:00
// Target must be an Building
2023-07-15 09:23:48 -04:00
if (currTarget.getObjectType() != GameObjectType.Building) {
throwbackError(pcSender, "Invalid target. Must be a Building");
return false;
}
2022-04-30 09:41:17 -04:00
// Argument must parse to a long.
2023-07-15 09:23:48 -04:00
try {
Long.parseLong(userInput[0]);
} catch (NumberFormatException | NullPointerException e) {
return false;
}
// Argument must return a valid NPC or PlayerCharacter
_newOwnerType = DbManager.BuildingQueries.GET_UID_ENUM(Long.parseLong(userInput[0]));
switch (_newOwnerType) {
case NPC:
outOwner = (AbstractCharacter) DbManager.getObject(GameObjectType.NPC, Integer.parseInt(userInput[0]));
break;
case CHARACTER:
outOwner = (AbstractCharacter) DbManager.getObject(GameObjectType.PlayerCharacter, Integer.parseInt(userInput[0]));
break;
}
if (outOwner == null) {
throwbackError(pcSender, "Invalid Owner UID");
return false;
2022-04-30 09:41:17 -04:00
}
return true;
2023-07-15 09:23:48 -04:00
}
2022-04-30 09:41:17 -04:00
}