use of org.terasology.engine.world.block.BlockComponent in project Terasology by MovingBlocks.
the class EntityDestructionAuthoritySystem method recordDestroyed.
private void recordDestroyed(DestroyEvent event, EntityRef entityRef) {
EntityRef instigator = event.getInstigator();
if (instigator != null) {
if (entityRef.hasComponent(BlockComponent.class)) {
BlockComponent blockComponent = entityRef.getComponent(BlockComponent.class);
String blockName = blockComponent.getBlock().getDisplayName();
if (instigator.hasComponent(GamePlayStatsComponent.class)) {
GamePlayStatsComponent gamePlayStatsComponent = instigator.getComponent(GamePlayStatsComponent.class);
Map<String, Integer> blockDestroyedMap = gamePlayStatsComponent.blockDestroyedMap;
if (blockDestroyedMap.containsKey(blockName)) {
blockDestroyedMap.put(blockName, blockDestroyedMap.get(blockName) + 1);
} else {
blockDestroyedMap.put(blockName, 1);
}
instigator.saveComponent(gamePlayStatsComponent);
} else {
GamePlayStatsComponent gamePlayStatsComponent = new GamePlayStatsComponent();
Map<String, Integer> blockDestroyedMap = gamePlayStatsComponent.blockDestroyedMap;
blockDestroyedMap.put(blockName, 1);
instigator.addOrSaveComponent(gamePlayStatsComponent);
}
} else if (entityRef.hasComponent(CharacterComponent.class)) {
String creatureName = entityRef.getParentPrefab().getName();
if (instigator.hasComponent(GamePlayStatsComponent.class)) {
GamePlayStatsComponent gamePlayStatsComponent = instigator.getComponent(GamePlayStatsComponent.class);
Map<String, Integer> creatureKilled = gamePlayStatsComponent.creatureKilled;
if (creatureKilled.containsKey(creatureName)) {
creatureKilled.put(creatureName, creatureKilled.get(creatureName) + 1);
} else {
creatureKilled.put(creatureName, 1);
}
instigator.saveComponent(gamePlayStatsComponent);
} else {
GamePlayStatsComponent gamePlayStatsComponent = new GamePlayStatsComponent();
Map<String, Integer> creatureKilled = gamePlayStatsComponent.creatureKilled;
creatureKilled.put(creatureName, 1);
instigator.addOrSaveComponent(gamePlayStatsComponent);
}
}
}
}
use of org.terasology.engine.world.block.BlockComponent in project Terasology by MovingBlocks.
the class NetClient method sendInitialEntities.
private void sendInitialEntities(NetData.NetMessage.Builder message) {
int[] initial = netInitial.toArray();
netInitial.clear();
Arrays.sort(initial);
for (int netId : initial) {
netRelevant.add(netId);
EntityRef entity = networkSystem.getEntity(netId);
if (!entity.hasComponent(NetworkComponent.class)) {
logger.error("Sending net entity with no network component: {} - {}", netId, entity);
continue;
}
// Note: Send owner->server fields on initial create
Client owner = networkSystem.getOwner(entity);
EntityData.PackedEntity entityData = entitySerializer.serialize(entity, true, new ServerComponentFieldCheck(owner == this, true)).build();
NetData.CreateEntityMessage.Builder createMessage = NetData.CreateEntityMessage.newBuilder().setEntity(entityData);
BlockComponent blockComponent = entity.getComponent(BlockComponent.class);
if (blockComponent != null) {
createMessage.setBlockPos(NetMessageUtil.convert(blockComponent.getPosition()));
}
message.addCreateEntity(createMessage);
}
}
use of org.terasology.engine.world.block.BlockComponent in project Terasology by MovingBlocks.
the class CameraTargetSystem method update.
public void update(float delta) {
// Repair lost target
// TODO: Improvements to temporary chunk handling will remove the need for this
boolean lostTarget = false;
updateTarget();
if (!target.exists()) {
targetBlockPos = null;
lostTarget = true;
}
HitResult hitInfo = physics.rayTrace(localPlayer.getViewPosition(new Vector3f()), localPlayer.getViewDirection(new Vector3f()), targetDistance, filter);
updateFocalDistance(hitInfo, delta);
Vector3i newBlockPos = null;
EntityRef newTarget = EntityRef.NULL;
if (hitInfo.isHit()) {
newTarget = hitInfo.getEntity();
hitPosition = hitInfo.getHitPoint();
hitNormal = hitInfo.getHitNormal();
if (hitInfo.isWorldHit()) {
newBlockPos = new Vector3i(hitInfo.getBlockPosition());
}
}
if (!Objects.equal(target, newTarget) || lostTarget) {
EntityRef oldTarget = target;
oldTarget.send(new CameraOutEvent());
newTarget.send(new CameraOverEvent());
localPlayer.getCharacterEntity().send(new CameraTargetChangedEvent(oldTarget, newTarget));
// Set isBlock to false if the hit-entity does not have a BlockComponent
isBlock = !(isTargetAvailable() && !newTarget.hasComponent(BlockComponent.class));
}
target = newTarget;
targetBlockPos = newBlockPos;
}
use of org.terasology.engine.world.block.BlockComponent in project Terasology by MovingBlocks.
the class NetEntityRefTypeHandler method serializeNonNull.
@Override
public PersistedData serializeNonNull(EntityRef value, PersistedDataSerializer serializer) {
BlockComponent blockComponent = value.getComponent(BlockComponent.class);
if (blockComponent != null) {
Vector3ic pos = blockComponent.getPosition();
return serializer.serialize(pos.x(), pos.y(), pos.z());
}
NetworkComponent netComponent = value.getComponent(NetworkComponent.class);
if (netComponent != null) {
return serializer.serialize(netComponent.getNetworkId());
}
return serializer.serializeNull();
}
use of org.terasology.engine.world.block.BlockComponent in project Terasology by MovingBlocks.
the class NeighbourBlockFamilyUpdateSystem method onBlockPlaced.
/**
* Notifies the adjacent block families when a block is placed next to them.
* @param event
* @param entity
*/
@ReceiveEvent
public void onBlockPlaced(OnBlockItemPlaced event, EntityRef entity) {
BlockComponent blockComponent = event.getPlacedBlock().getComponent(BlockComponent.class);
if (blockComponent == null) {
return;
}
processUpdateForBlockLocation(blockComponent.getPosition());
}
Aggregations