use of io.xol.chunkstories.api.events.player.voxel.PlayerVoxelModificationEvent 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.api.events.player.voxel.PlayerVoxelModificationEvent 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.events.player.voxel.PlayerVoxelModificationEvent in project chunkstories-core by Hugobros3.
the class EntityPlayer method onControllerInput.
@Override
public boolean onControllerInput(Input input, Controller controller) {
// We are moving inventory bringup here !
if (input.getName().equals("inventory") && world instanceof WorldClient) {
if (creativeMode.get()) {
((WorldClient) getWorld()).getClient().openInventories(this.inventoryComponent.getInventory(), new InventoryLocalCreativeMenu(world));
} else {
((WorldClient) getWorld()).getClient().openInventories(this.inventoryComponent.getInventory(), this.armor.getInventory());
}
return true;
}
Location blockLocation = this.getBlockLookingAt(true);
double maxLen = 1024;
if (blockLocation != null) {
Vector3d diff = new Vector3d(blockLocation).sub(this.getLocation());
// Vector3d dir = diff.clone().normalize();
maxLen = diff.length();
}
Vector3d initialPosition = new Vector3d(getLocation());
initialPosition.add(new Vector3d(0, eyePosition, 0));
Vector3dc direction = getDirectionLookingAt();
Iterator<Entity> i = world.collisionsManager().rayTraceEntities(initialPosition, direction, maxLen);
while (i.hasNext()) {
Entity e = i.next();
if (e.handleInteraction(this, input))
return true;
}
ItemPile itemSelected = getSelectedItemComponent().getSelectedItem();
if (itemSelected != null) {
// See if the item handles the interaction
if (itemSelected.getItem().onControllerInput(this, itemSelected, input, controller))
return true;
}
if (getWorld() instanceof WorldMaster) {
// Creative mode features building and picking.
if (this.getCreativeModeComponent().get()) {
if (input.getName().equals("mouse.left")) {
if (blockLocation != null) {
// Player events mod
if (controller instanceof Player) {
Player player = (Player) controller;
WorldCell cell = world.peekSafely(blockLocation);
FutureCell future = new FutureCell(cell);
future.setVoxel(this.getDefinition().store().parent().voxels().air());
future.setBlocklight(0);
future.setSunlight(0);
future.setMetaData(0);
PlayerVoxelModificationEvent event = new PlayerVoxelModificationEvent(cell, future, EntityCreative.CREATIVE_MODE, player);
// Anyone has objections ?
world.getGameContext().getPluginManager().fireEvent(event);
if (event.isCancelled())
return true;
Vector3d rnd = new Vector3d();
for (int k = 0; k < 40; k++) {
rnd.set(blockLocation);
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, blockLocation, 1.0f, 1.0f);
try {
world.poke(future, this);
// world.poke((int)blockLocation.x, (int)blockLocation.y, (int)blockLocation.z, null, 0, 0, 0, this);
} catch (WorldException e) {
// Discard but maybe play some effect ?
}
return true;
}
}
} else if (input.getName().equals("mouse.middle")) {
if (blockLocation != null) {
CellData ctx = this.getWorld().peekSafely(blockLocation);
if (!ctx.getVoxel().isAir()) {
// Spawn new itemPile in his inventory
ItemVoxel item = (ItemVoxel) world.getGameContext().getContent().items().getItemDefinition("item_voxel").newItem();
item.voxel = ctx.getVoxel();
item.voxelMeta = ctx.getMetaData();
ItemPile itemVoxel = new ItemPile(item);
this.getInventory().setItemPileAt(getSelectedItemComponent().getSelectedSlot(), 0, itemVoxel);
return true;
}
}
}
}
}
// Then we check if the world minds being interacted with
return world.handleInteraction(this, blockLocation, input);
}
Aggregations