use of io.xol.chunkstories.api.item.inventory.ItemPile 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);
}
use of io.xol.chunkstories.api.item.inventory.ItemPile in project chunkstories-core by Hugobros3.
the class EntityPlayer method tick.
// Server-side updating
@Override
public void tick() {
// This is a controllable entity, take care of controlling
if (world instanceof WorldClient && ((WorldClient) getWorld()).getClient().getPlayer().getControlledEntity() == this)
tickClientController(((WorldClient) getWorld()).getClient().getPlayer());
// Tick item in hand if one such exists
ItemPile pileSelected = getSelectedItemComponent().getSelectedItem();
if (pileSelected != null)
pileSelected.getItem().tickInHand(this, pileSelected);
// Auto-pickups items on the ground
if (world instanceof WorldMaster && (world.getTicksElapsed() % 60L) == 0L) {
// TODO Use more precise, regional functions to not iterate over the entire world like a retard
for (Entity e : world.getEntitiesInBox(getLocation(), new Vector3d(3.0))) {
if (e instanceof EntityGroundItem && e.getLocation().distance(this.getLocation()) < 3.0f) {
EntityGroundItem eg = (EntityGroundItem) e;
if (!eg.canBePickedUpYet())
continue;
world.getSoundManager().playSoundEffect("sounds/item/pickup.ogg", Mode.NORMAL, getLocation(), 1.0f, 1.0f);
ItemPile pile = eg.getItemPile();
if (pile != null) {
ItemPile left = this.inventoryComponent.getInventory().addItemPile(pile);
if (left == null)
world.removeEntity(eg);
else
eg.setItemPile(left);
}
}
}
}
if (world instanceof WorldMaster) {
// Take damage when starving
if ((world.getTicksElapsed() % 100L) == 0L) {
if (this.getFoodLevel() == 0)
this.damage(EntityComponentFoodLevel.HUNGER_DAMAGE_CAUSE, 1);
else {
// 27 minutes to start starving at 0.1 starveFactor
// Takes 100hp / ( 0.6rtps * 0.1 hp/hit )
// Starve slowly if inactive
float starve = 0.03f;
// Walking drains you
if (this.getVelocityComponent().getVelocity().length() > 0.3) {
starve = 0.06f;
// Running is even worse
if (this.getVelocityComponent().getVelocity().length() > 0.7)
starve = 0.15f;
}
float newfoodLevel = this.getFoodLevel() - starve;
this.setFoodLevel(newfoodLevel);
// System.out.println("new food level:"+newfoodLevel);
}
}
// It restores hp
if (getFoodLevel() > 20 && !this.isDead()) {
if (this.getHealth() < this.getMaxHealth()) {
this.setHealth(this.getHealth() + 0.01f);
float newfoodLevel = this.getFoodLevel() - 0.01f;
this.setFoodLevel(newfoodLevel);
}
}
// Being on a ladder resets your jump height
if (onLadder)
lastStandingHeight = this.positionComponent.getLocation().y();
if (this.getFlyingComponent().get())
lastStandingHeight = Double.NaN;
// else
// System.out.println("prout"+(world.getTicksElapsed() % 10L));
// System.out.println(this.getVelocityComponent().getVelocity().length());
}
super.tick();
}
use of io.xol.chunkstories.api.item.inventory.ItemPile in project chunkstories-core by Hugobros3.
the class EntityComponentSelectedItem method pull.
@Override
protected void pull(StreamSource from, DataInputStream dis) throws IOException {
selectedSlot = dis.readInt();
boolean itemIncluded = dis.readBoolean();
if (itemIncluded) {
// System.out.println("reading item from packet for entity"+entity);
// ItemPile pile;
ItemPile itemPile = null;
try {
itemPile = ItemPile.obtainItemPileFromStream(entity.getWorld().getContentTranslator(), dis);
} catch (NullItemException e) {
// Don't do anything about it, no big deal
} catch (UndefinedItemTypeException e) {
// This is slightly more problematic
// Logger logger = this.entity.getWorld().getGameContext().logger();
this.entity.getWorld().getGameContext().logger().info(e.getMessage());
// e.printStackTrace(logger.getPrintWriter());
}
// Ensures only client worlds accepts such pushes
if (!(entity.getWorld() instanceof WorldMaster))
inventory.setItemPileAt(selectedSlot, 0, itemPile);
/*int id = dis.readInt() & 0x00FFFFFF;
ItemDefinition itemType = ItemDefinitions.getItemDefinitionById(id);
if(itemType != null)
{
Item item = itemType.newItem();
pile = new ItemPile(item, dis);
if(pile != null && !(entity.getWorld() instanceof WorldMaster))
{
//System.out.println("got held item for "+entity + " : "+pile);
inventory.setItemPileAt(selectedSlot, 0, pile);
}
}*/
}
this.pushComponentEveryoneButController();
}
use of io.xol.chunkstories.api.item.inventory.ItemPile in project chunkstories-core by Hugobros3.
the class VoxelDoor method getItems.
@Override
public ItemPile[] getItems() {
// Top part shouldn't be placed
if (top)
return new ItemPile[] {};
ItemVoxel itemVoxel = (ItemVoxel) store.parent().items().getItemDefinition("item_voxel_1x2").newItem();
itemVoxel.voxel = this;
return new ItemPile[] { new ItemPile(itemVoxel) };
}
Aggregations