use of org.terasology.entitySystem.event.ReceiveEvent in project Terasology by MovingBlocks.
the class PlayerSystem method setSpawnLocationOnRespawnRequest.
@ReceiveEvent(priority = EventPriority.PRIORITY_CRITICAL, components = { ClientComponent.class })
public void setSpawnLocationOnRespawnRequest(RespawnRequestEvent event, EntityRef entity) {
EntityRef clientInfo = entity.getComponent(ClientComponent.class).clientInfo;
Vector3f spawnPosition;
if (clientInfo.hasComponent(StaticSpawnLocationComponent.class)) {
spawnPosition = clientInfo.getComponent(StaticSpawnLocationComponent.class).position;
} else {
spawnPosition = worldGenerator.getSpawnPosition(entity);
}
LocationComponent loc = entity.getComponent(LocationComponent.class);
loc.setWorldPosition(spawnPosition);
// reset rotation
loc.setLocalRotation(new Quat4f());
entity.saveComponent(loc);
}
use of org.terasology.entitySystem.event.ReceiveEvent in project Terasology by MovingBlocks.
the class PlayerSystem method onConnect.
@ReceiveEvent(components = ClientComponent.class)
public void onConnect(ConnectedEvent connected, EntityRef entity) {
LocationComponent loc = entity.getComponent(LocationComponent.class);
// for new clients, the player store will return default values
PlayerStore playerStore = connected.getPlayerStore();
Client owner = networkSystem.getOwner(entity);
Vector3i minViewDist = ViewDistance.LEGALLY_BLIND.getChunkDistance();
if (playerStore.hasCharacter()) {
Vector3f storedLocation = playerStore.getRelevanceLocation();
loc.setWorldPosition(storedLocation);
entity.saveComponent(loc);
if (worldProvider.isBlockRelevant(storedLocation)) {
// chunk for spawning location is ready, so spawn right now
playerStore.restoreEntities();
EntityRef character = playerStore.getCharacter();
Vector3i viewDist = owner.getViewDistance().getChunkDistance();
addRelevanceEntity(entity, viewDist, owner);
restoreCharacter(entity, character);
} else {
// otherwise wait until chunk is ready
addRelevanceEntity(entity, minViewDist, owner);
clientsPreparingToSpawn.add(new SpawningClientInfo(entity, storedLocation, playerStore));
}
} else {
Vector3f spawnPosition = worldGenerator.getSpawnPosition(entity);
loc.setWorldPosition(spawnPosition);
entity.saveComponent(loc);
addRelevanceEntity(entity, minViewDist, owner);
if (worldProvider.isBlockRelevant(spawnPosition)) {
spawnPlayer(entity);
} else {
clientsPreparingToSpawn.add(new SpawningClientInfo(entity, spawnPosition));
}
}
}
use of org.terasology.entitySystem.event.ReceiveEvent in project Terasology by MovingBlocks.
the class BuilderHeldItemAuthoritySystem method onGiveItemToCharacterHoldItem.
@ReceiveEvent
public void onGiveItemToCharacterHoldItem(GiveItemEvent event, EntityRef item, ItemComponent itemComponent) {
if (event.getTargetEntity().hasComponent(CharacterHeldItemComponent.class)) {
event.getTargetEntity().addOrSaveComponent(new CharacterOwnedItemComponent(item));
event.getTargetEntity().send(new ChangeHeldItemRequest(item));
event.setHandled(true);
}
}
use of org.terasology.entitySystem.event.ReceiveEvent in project Terasology by MovingBlocks.
the class BlockDropGrammarSystem method onDestroyed.
@ReceiveEvent
public void onDestroyed(DoDestroyEvent event, EntityRef entity, BlockDropGrammarComponent blockDrop, LocationComponent locationComp) {
BlockDamageModifierComponent blockDamageModifierComponent = event.getDamageType().getComponent(BlockDamageModifierComponent.class);
float chanceOfBlockDrop = 1;
if (blockDamageModifierComponent != null) {
chanceOfBlockDrop = 1 - blockDamageModifierComponent.blockAnnihilationChance;
}
if (random.nextFloat() < chanceOfBlockDrop) {
List<String> blockDrops = blockDrop.blockDrops;
List<String> itemDrops = blockDrop.itemDrops;
if (blockDamageModifierComponent != null && blockDrop.droppedWithTool != null) {
for (String toolType : blockDamageModifierComponent.materialDamageMultiplier.keySet()) {
if (blockDrop.droppedWithTool.containsKey(toolType)) {
BlockDropGrammarComponent.DropDefinition dropDefinition = blockDrop.droppedWithTool.get(toolType);
blockDrops = dropDefinition.blockDrops;
itemDrops = dropDefinition.itemDrops;
break;
}
}
}
if (blockDrops != null) {
for (String drop : blockDrops) {
String dropResult = drop;
boolean dropping = true;
int pipeIndex = dropResult.indexOf('|');
if (pipeIndex > -1) {
float chance = Float.parseFloat(dropResult.substring(0, pipeIndex));
if (random.nextFloat() >= chance) {
dropping = false;
}
dropResult = dropResult.substring(pipeIndex + 1);
}
if (dropping) {
DropParser dropParser = new DropParser(random, dropResult).invoke();
EntityRef dropItem = blockItemFactory.newInstance(blockManager.getBlockFamily(dropParser.getDrop()), dropParser.getCount());
if (shouldDropToWorld(event, blockDamageModifierComponent, dropItem)) {
createDrop(dropItem, locationComp.getWorldPosition(), true);
}
}
}
}
if (itemDrops != null) {
for (String drop : itemDrops) {
String dropResult = drop;
boolean dropping = true;
int pipeIndex = dropResult.indexOf('|');
if (pipeIndex > -1) {
float chance = Float.parseFloat(dropResult.substring(0, pipeIndex));
if (random.nextFloat() >= chance) {
dropping = false;
}
dropResult = dropResult.substring(pipeIndex + 1);
}
if (dropping) {
DropParser dropParser = new DropParser(random, dropResult).invoke();
EntityBuilder dropEntity = entityManager.newBuilder(dropParser.getDrop());
if (dropParser.getCount() > 1) {
ItemComponent itemComponent = dropEntity.getComponent(ItemComponent.class);
itemComponent.stackCount = (byte) dropParser.getCount();
}
EntityRef dropItem = dropEntity.build();
if (shouldDropToWorld(event, blockDamageModifierComponent, dropItem)) {
createDrop(dropItem, locationComp.getWorldPosition(), false);
}
}
}
}
}
}
use of org.terasology.entitySystem.event.ReceiveEvent in project Terasology by MovingBlocks.
the class DoorSystem method openDoor.
@ReceiveEvent
public void openDoor(OpenDoorEvent event, EntityRef player) {
EntityRef entity = event.getDoorEntity();
DoorComponent door = entity.getComponent(DoorComponent.class);
Side newSide = door.openSide;
BlockRegionComponent regionComp = entity.getComponent(BlockRegionComponent.class);
Block bottomBlock = door.bottomBlockFamily.getBlockForPlacement(worldProvider, blockEntityRegistry, regionComp.region.min(), newSide, Side.TOP);
worldProvider.setBlock(regionComp.region.min(), bottomBlock);
Block topBlock = door.topBlockFamily.getBlockForPlacement(worldProvider, blockEntityRegistry, regionComp.region.max(), newSide, Side.TOP);
worldProvider.setBlock(regionComp.region.max(), topBlock);
if (door.openSound != null) {
entity.send(new PlaySoundEvent(door.openSound, 1f));
}
door.isOpen = true;
}
Aggregations