@PUT
@Path("/{entity}")
@Permission("modify")
@ApiOperation(
value = "Modify an entity",
notes = "Modify the properties of an existing entity.")
public CachedEntity modifyEntity(
@PathParam("entity") @ApiParam("The uuid of the entity") UUID uuid,
UpdateEntityRequest req)
throws NotFoundException, BadRequestException {
if (req == null) {
throw new BadRequestException("Request body is required");
}
Optional<CachedEntity> optEntity = WebAPI.getCacheService().getEntity(uuid);
if (!optEntity.isPresent()) {
throw new NotFoundException("Entity with UUID '" + uuid + "' could not be found");
}
return WebAPI.runOnMain(() -> {
Optional<Entity> optLive = optEntity.get().getLive();
if (!optLive.isPresent())
throw new InternalServerErrorException("Could not get live entity");
Entity live = optLive.get();
if (req.getWorld().isPresent()) {
Optional<World> optWorld = req.getWorld().get().getLive();
if (!optWorld.isPresent())
throw new InternalServerErrorException("Could not get live world");
if (req.getPosition() != null) {
live.transferToWorld(optWorld.get(), req.getPosition());
} else {
live.transferToWorld(optWorld.get());
}
} else if (req.getPosition() != null) {
live.setLocation(new Location<>(live.getWorld(), req.getPosition()));
}
if (req.getVelocity() != null) {
live.setVelocity(req.getVelocity());
}
if (req.getRotation() != null) {
live.setRotation(req.getRotation());
}
if (req.getScale() != null) {
live.setRotation(req.getScale());
}
if (req.getDamage() != null) {
DamageRequest dmgReq = req.getDamage();
DamageSource.Builder builder = DamageSource.builder();
if (dmgReq.getType().isPresent()) {
Optional<DamageType> optDmgType = dmgReq.getType().get().getLive(DamageType.class);
if (!optDmgType.isPresent())
throw new InternalServerErrorException("Could not get live damage type");
builder.type(optDmgType.get());
}
live.damage(req.getDamage().getAmount(), builder.build());
}
if (req.hasInventory()) {
if (!(live instanceof Carrier)) {
throw new BadRequestException("Entity does not have an inventory!");
}
try {
Inventory inv = ((Carrier) live).getInventory();
for (SlotRequest slotReq : req.getInventory()) {
for (Inventory slot : inv.slots()) {
Optional<SlotIndex> optIndex = slot.getInventoryProperty(SlotIndex.class);
if (!optIndex.isPresent() || !slotReq.getSlotIndex().equals(optIndex.get().getValue())) {
continue;
}
if (slotReq.getStack().isPresent()) {
slot.set(slotReq.getStack().get());
} else {
slot.clear();
}
}
}
} catch (Exception e) {
throw new InternalServerErrorException(e.getMessage());
}
}
return new CachedEntity(live);
});
}
@ApiModelProperty(dataType = "string", value = "The type of damage that should be dealt")
public Optional<CachedCatalogType<DamageType>> getType() {
return type != null ? Optional.of(type) : Optional.empty();
}