use of io.xol.chunkstories.core.entity.EntityGroundItem in project chunkstories-core by Hugobros3.
the class ItemMiningTool method tickInHand.
@Override
public void tickInHand(Entity owner, ItemPile itemPile) {
World world = owner.getWorld();
if (owner instanceof EntityControllable && owner instanceof EntityWorldModifier) {
EntityControllable entityControllable = (EntityControllable) owner;
Controller controller = entityControllable.getController();
if (controller != null && controller instanceof Player) {
InputsManager inputs = controller.getInputsManager();
Location lookingAt = entityControllable.getBlockLookingAt(true);
if (lookingAt != null && lookingAt.distance(owner.getLocation()) > 7f)
lookingAt = null;
if (inputs.getInputByName("mouse.left").isPressed() && lookingAt != null) {
WorldCell cell = world.peekSafely(lookingAt);
// Cancel mining if looking away or the block changed by itself
if (lookingAt == null || (progress != null && (lookingAt.distance(progress.loc) > 0 || !cell.getVoxel().sameKind(progress.voxel)))) {
progress = null;
}
if (progress == null) {
// Try starting mining something
if (lookingAt != null)
progress = new MiningProgress(world.peekSafely(lookingAt));
} else {
// Progress using efficiency / ticks per second
progress.progress += ItemMiningTool.this.miningEfficiency / 60f / progress.materialHardnessForThisTool;
if (progress.progress >= 1.0f) {
if (owner.getWorld() instanceof WorldMaster) {
FutureCell future = new FutureCell(cell);
future.setVoxel(this.getDefinition().store().parent().voxels().air());
// Check no one minds
PlayerVoxelModificationEvent event = new PlayerVoxelModificationEvent(cell, future, (WorldModificationCause) entityControllable, (Player) controller);
owner.getWorld().getGameContext().getPluginManager().fireEvent(event);
// Break the block
if (!event.isCancelled()) {
Vector3d rnd = new Vector3d();
for (int i = 0; i < 40; i++) {
rnd.set(progress.loc);
rnd.add(Math.random() * 0.98, Math.random() * 0.98, Math.random() * 0.98);
world.getParticlesManager().spawnParticleAtPosition("voxel_frag", rnd);
}
world.getSoundManager().playSoundEffect("sounds/gameplay/voxel_remove.ogg", Mode.NORMAL, progress.loc, 1.0f, 1.0f);
Location itemSpawnLocation = new Location(world, progress.loc);
itemSpawnLocation.add(0.5, 0.0, 0.5);
// ItemPile droppedItemPile = null;
for (ItemPile droppedItemPile : cell.getVoxel().getLoot(cell, (WorldModificationCause) entityControllable)) {
EntityGroundItem thrownItem = (EntityGroundItem) getDefinition().store().parent().entities().getEntityDefinition("groundItem").create(itemSpawnLocation);
thrownItem.positionComponent.setPosition(itemSpawnLocation);
thrownItem.velocityComponent.setVelocity(new Vector3d(Math.random() * 0.125 - 0.0625, 0.1, Math.random() * 0.125 - 0.0625));
thrownItem.setItemPile(droppedItemPile);
world.addEntity(thrownItem);
}
try {
world.poke(future, (WorldModificationCause) entityControllable);
} catch (WorldException e) {
// Didn't work
// TODO make some ingame effect so as to clue in the player why it failed
}
}
}
progress = null;
}
}
} else {
progress = null;
}
Player player = (Player) controller;
if (player.getContext() instanceof ClientInterface) {
Player me = ((ClientInterface) player.getContext()).getPlayer();
if (me.equals(player)) {
myProgress = progress;
}
}
}
}
}
use of io.xol.chunkstories.core.entity.EntityGroundItem in project chunkstories-core by Hugobros3.
the class ItemsLogicListener method onDroppedItem.
@EventHandler
public void onDroppedItem(EventItemDroppedToWorld event) {
// Create an EntityGroundItem and add it to the event
Location throwLocation = event.getLocation();
Vector3d throwForce = new Vector3d(0.0);
// Throw it when dropping it from a player's inventory ?
System.out.println(event.getInventoryFrom());
if (event.getInventoryFrom() != null && event.getInventoryFrom().getHolder() != null && event.getInventoryFrom().getHolder() instanceof Entity) {
System.out.println("from som 1");
EntityWithInventory entity = ((EntityWithInventory) event.getInventoryFrom().getHolder());
Location pos = entity.getLocation();
if (entity instanceof EntityLiving) {
System.out.println("he l i v e s");
EntityLiving owner = (EntityLiving) entity;
throwLocation = new Location(pos.getWorld(), pos.x(), pos.y() + ((EntityPlayer) owner).eyePosition, pos.z());
throwForce = new Vector3d(((EntityPlayer) owner).getDirectionLookingAt()).mul(0.15 - Math2.clampd(((EntityPlayer) owner).getEntityRotationComponent().getVerticalRotation(), -45, 20) / 45f * 0.0f);
throwForce.add(((EntityPlayer) owner).getVelocityComponent().getVelocity());
}
}
EntityGroundItem thrownItem = (EntityGroundItem) core.getPluginExecutionContext().getContent().entities().getEntityDefinition("groundItem").create(throwLocation);
thrownItem.positionComponent.setPosition(throwLocation);
thrownItem.velocityComponent.setVelocity(throwForce);
thrownItem.setItemPile(event.getItemPile());
// EntityGroundItem entity = new EntityGroundItem(core.getPluginExecutionContext().getContent().entities().getEntityDefinitionByName("groundItem"), event.getLocation(), event.getItemPile());
event.setItemEntity(thrownItem);
}
Aggregations