use of io.xol.chunkstories.api.entity.interfaces.EntityControllable in project chunkstories-api by Hugobros3.
the class EntityComponentVelocity method addVelocity.
public void addVelocity(Vector3dc delta) {
this.velocity.add(delta);
this.pushComponentEveryoneButController();
// Notify the controller otherwise:
if (entity instanceof EntityControllable) {
Controller controller = ((EntityControllable) entity).getControllerComponent().getController();
if (controller != null) {
PacketVelocityDelta packet = new PacketVelocityDelta(entity.getWorld(), delta);
controller.pushPacket(packet);
}
}
}
use of io.xol.chunkstories.api.entity.interfaces.EntityControllable in project chunkstories-api by Hugobros3.
the class ItemVoxel method onControllerInput.
@Override
public boolean onControllerInput(Entity entity, ItemPile pile, Input input, Controller controller) {
try {
if (entity.getWorld() instanceof WorldMaster && input.getName().equals("mouse.right")) {
// Require entities to be of the right kind
if (!(entity instanceof EntityWorldModifier))
return true;
if (!(entity instanceof EntityControllable))
return true;
EntityWorldModifier modifierEntity = (EntityWorldModifier) entity;
EntityControllable playerEntity = (EntityControllable) entity;
boolean isEntityCreativeMode = (entity instanceof EntityCreative) && (((EntityCreative) entity).isCreativeMode());
Location blockLocation = null;
blockLocation = playerEntity.getBlockLookingAt(false);
if (blockLocation != null) {
FutureCell fvc = new FutureCell(entity.getWorld().peekSafely(blockLocation));
fvc.setVoxel(voxel);
// Opaque blocks overwrite the original light with zero.
if (voxel.getDefinition().isOpaque()) {
fvc.setBlocklight(0);
fvc.setSunlight(0);
}
// Glowy stuff should glow
// if(voxel.getDefinition().getEmittedLightLevel() > 0)
fvc.setBlocklight(voxel.getEmittedLightLevel(fvc));
// Player events mod
if (controller instanceof Player) {
Player player = (Player) controller;
CellData ctx = entity.getWorld().peek(blockLocation);
PlayerVoxelModificationEvent event = new PlayerVoxelModificationEvent(ctx, fvc, isEntityCreativeMode ? EntityCreative.CREATIVE_MODE : this, player);
// Anyone has objections ?
entity.getWorld().getGameContext().getPluginManager().fireEvent(event);
if (event.isCancelled())
return true;
entity.getWorld().getSoundManager().playSoundEffect("sounds/gameplay/voxel_place.ogg", Mode.NORMAL, fvc.getLocation(), 1.0f, 1.0f);
}
entity.getWorld().poke(fvc, modifierEntity);
// Decrease stack size
if (!isEntityCreativeMode) {
int currentAmount = pile.getAmount();
currentAmount--;
pile.setAmount(currentAmount);
}
} else {
// No space found :/
return true;
}
}
} catch (WorldException e) {
}
return false;
}
use of io.xol.chunkstories.api.entity.interfaces.EntityControllable in project chunkstories by Hugobros3.
the class ServerPlayer method updateTrackedEntities.
// Entity tracking
public void updateTrackedEntities() {
EntityControllable controlledEntity = this.controlledEntity;
if (controlledEntity == null)
return;
// Cache (idk if HotSpot makes it redudant but whatever)
double world_size = controlledEntity.getWorld().getWorldSize();
Location controlledEntityLocation = controlledEntity.getLocation();
double ENTITY_VISIBILITY_SIZE = 192;
Iterator<Entity> inRangeEntitiesIterator = controlledEntity.getWorld().getEntitiesInBox(controlledEntityLocation, new Vector3d(ENTITY_VISIBILITY_SIZE, ENTITY_VISIBILITY_SIZE, ENTITY_VISIBILITY_SIZE));
while (inRangeEntitiesIterator.hasNext()) {
Entity e = inRangeEntitiesIterator.next();
// && chunk != null;
boolean shouldTrack = e.shouldBeTrackedBy(this);
boolean contains = subscribedEntities.contains(e);
if (shouldTrack && !contains)
this.subscribe(e);
if (!shouldTrack && contains)
this.unsubscribe(e);
}
Iterator<Entity> subscribedEntitiesIterator = subscribedEntities.iterator();
while (subscribedEntitiesIterator.hasNext()) {
Entity e = subscribedEntitiesIterator.next();
Location loc = e.getLocation();
// Distance calculations
double dx = LoopingMathHelper.moduloDistance(controlledEntityLocation.x(), loc.x(), world_size);
double dy = Math.abs(controlledEntityLocation.y() - loc.y());
double dz = LoopingMathHelper.moduloDistance(controlledEntityLocation.z(), loc.z(), world_size);
boolean inRange = (dx < ENTITY_VISIBILITY_SIZE && dz < ENTITY_VISIBILITY_SIZE && dy < ENTITY_VISIBILITY_SIZE);
// Reasons other than distance to stop tracking this entity
if (!e.shouldBeTrackedBy(this) || !inRange)
this.unsubscribe(e);
// No need to do anything as the component system handles the updates
}
}
use of io.xol.chunkstories.api.entity.interfaces.EntityControllable in project chunkstories by Hugobros3.
the class ServerPlayer method unsubscribeAll.
@Override
public void unsubscribeAll() {
Iterator<Entity> iterator = getSubscribedToList();
while (iterator.hasNext()) {
Entity entity = iterator.next();
// If one of the entities is controllable ...
if (entity instanceof EntityControllable) {
EntityControllable controllableEntity = (EntityControllable) entity;
Controller entityController = controllableEntity.getControllerComponent().getController();
// If said entity is controlled by this subscriber/player
if (entityController == this) {
// Set the controller to null
controllableEntity.getControllerComponent().setController(null);
}
}
((EntityBase) entity).unsubscribe(this);
iterator.remove();
}
}
use of io.xol.chunkstories.api.entity.interfaces.EntityControllable in project chunkstories by Hugobros3.
the class WorldImplementation method spawnPlayer.
public void spawnPlayer(Player player) {
if (!(this instanceof WorldMaster))
throw new UnsupportedOperationException("Only Master Worlds can do this");
Entity savedEntity = null;
SerializedEntityFile playerEntityFile = new SerializedEntityFile(this.getFolderPath() + "/players/" + player.getName().toLowerCase() + ".csf");
if (playerEntityFile.exists())
savedEntity = playerEntityFile.read(this);
Location previousLocation = null;
if (savedEntity != null)
previousLocation = savedEntity.getLocation();
PlayerSpawnEvent playerSpawnEvent = new PlayerSpawnEvent(player, (WorldMaster) this, savedEntity, previousLocation);
getGameContext().getPluginManager().fireEvent(playerSpawnEvent);
if (!playerSpawnEvent.isCancelled()) {
Entity entity = playerSpawnEvent.getEntity();
Location actualSpawnLocation = playerSpawnEvent.getSpawnLocation();
if (actualSpawnLocation == null)
actualSpawnLocation = this.getDefaultSpawnLocation();
// TODO EntitySimplePlayer ?
if (entity == null || ((entity instanceof EntityLiving) && (((EntityLiving) entity).isDead())))
entity = this.gameContext.getContent().entities().getEntityDefinition("player").create(actualSpawnLocation);
else
// entity = new EntityPlayer(this, 0d, 0d, 0d, player.getName()); //Default entity
entity.setUUID(-1);
// Name your player !
if (entity instanceof EntityNameable)
((EntityNameable) entity).getNameComponent().setName(player.getName());
entity.setLocation(actualSpawnLocation);
addEntity(entity);
if (entity instanceof EntityControllable)
player.setControlledEntity((EntityControllable) entity);
else
System.out.println("Error : entity is not controllable");
}
}
Aggregations