use of io.xol.chunkstories.api.world.WorldMaster in project chunkstories by Hugobros3.
the class InventoryView method handleClick.
private boolean handleClick(MouseButton mouseButton) {
// We assume a player has to be spawned in order to do items manipulation
Player player = gameWindow.getClient().getPlayer();
if (player == null) {
this.gameWindow.setLayer(parentLayer);
// this.mainScene.changeOverlay(parent);
selectedItem = null;
return true;
}
World world = player.getWorld();
for (int i = 0; i < drawers.length; i++) {
// Close button
if (drawers[i].isOverCloseButton()) {
this.gameWindow.setLayer(parentLayer);
selectedItem = null;
} else {
int[] c = drawers[i].getSelectedSlot();
if (c == null)
continue;
else {
int x = c[0];
int y = c[1];
if (selectedItem == null) {
if (mouseButton.equals("mouse.left")) {
selectedItem = inventories[i].getItemPileAt(x, y);
selectedItemAmount = selectedItem == null ? 0 : selectedItem.getAmount();
} else if (mouseButton.equals("mouse.right")) {
selectedItem = inventories[i].getItemPileAt(x, y);
selectedItemAmount = selectedItem == null ? 0 : 1;
} else if (mouseButton.equals("mouse.middle")) {
selectedItem = inventories[i].getItemPileAt(x, y);
selectedItemAmount = selectedItem == null ? 0 : (selectedItem.getAmount() > 1 ? selectedItem.getAmount() / 2 : 1);
}
// selectedItemInv = inventory;
} else if (mouseButton.equals("mouse.right")) {
if (selectedItem.equals(inventories[i].getItemPileAt(x, y))) {
if (selectedItemAmount < inventories[i].getItemPileAt(x, y).getAmount())
selectedItemAmount++;
}
} else if (mouseButton.equals("mouse.left")) {
// Ignore null-sum games
if (selectedItem.getInventory() == inventories[i] && x == selectedItem.getX() && y == selectedItem.getY()) {
selectedItem = null;
return true;
}
if (world instanceof WorldMaster) {
PlayerMoveItemEvent moveItemEvent = new PlayerMoveItemEvent(player, selectedItem, selectedItem.getInventory(), inventories[i], selectedItem.getX(), selectedItem.getY(), x, y, selectedItemAmount);
player.getContext().getPluginManager().fireEvent(moveItemEvent);
// If move was successfull
if (!moveItemEvent.isCancelled())
selectedItem.moveItemPileTo(inventories[i], x, y, selectedItemAmount);
selectedItem = null;
} else if (world instanceof WorldClientNetworkedRemote) {
// When in a remote MP scenario, send a packet
PacketInventoryMoveItemPile packetMove = new PacketInventoryMoveItemPile(world, selectedItem, selectedItem.getInventory(), inventories[i], selectedItem.getX(), selectedItem.getY(), x, y, selectedItemAmount);
((WorldClientNetworkedRemote) world).getRemoteServer().pushPacket(packetMove);
// And unsellect item
selectedItem = null;
}
}
return true;
}
}
}
// Clicked outside of any other inventory (drop!)
if (selectedItem != null) {
// SP scenario, replicated logic in PacketInventoryMoveItemPile
if (world instanceof WorldMaster) {
// For local item drops, we need to make sure we have a sutiable entity
Entity playerEntity = player.getControlledEntity();
if (playerEntity != null) {
PlayerMoveItemEvent moveItemEvent = new PlayerMoveItemEvent(player, selectedItem, selectedItem.getInventory(), null, selectedItem.getX(), selectedItem.getY(), 0, 0, selectedItemAmount);
player.getContext().getPluginManager().fireEvent(moveItemEvent);
if (!moveItemEvent.isCancelled()) {
// If we're pulling this out of an inventory ( and not /dev/null ), we need to
// remove it from that
Inventory sourceInventory = selectedItem.getInventory();
Location loc = playerEntity.getLocation();
EventItemDroppedToWorld dropItemEvent = new EventItemDroppedToWorld(loc, sourceInventory, selectedItem);
player.getContext().getPluginManager().fireEvent(dropItemEvent);
if (!dropItemEvent.isCancelled()) {
if (sourceInventory != null)
sourceInventory.setItemPileAt(selectedItem.getX(), selectedItem.getY(), null);
if (dropItemEvent.getItemEntity() != null)
loc.getWorld().addEntity(dropItemEvent.getItemEntity());
}
}
}
selectedItem = null;
} else // In MP scenario, move into /dev/null
if (world instanceof WorldClientNetworkedRemote) {
PacketInventoryMoveItemPile packetMove = new PacketInventoryMoveItemPile(world, selectedItem, selectedItem.getInventory(), null, selectedItem.getX(), selectedItem.getY(), 0, 0, selectedItemAmount);
((WorldClientNetworkedRemote) world).getRemoteServer().pushPacket(packetMove);
selectedItem = null;
}
}
return true;
}
use of io.xol.chunkstories.api.world.WorldMaster in project chunkstories by Hugobros3.
the class PlayerClientImplementation method setControlledEntity.
@Override
public boolean setControlledEntity(EntityControllable entity) {
if (entity instanceof EntityControllable) {
this.subscribe(entity);
EntityControllable controllableEntity = (EntityControllable) entity;
// If a world master, directly set the entity's controller
if (world instanceof WorldMaster)
controllableEntity.getControllerComponent().setController(this);
// In remote networked worlds, we need to subscribe the server to our player actions to the controlled entity so he gets updates
if (entity.getWorld() instanceof WorldClientNetworkedRemote) {
// When changing controlled entity, first unsubscribe the remote server from the one we no longer own
if (controlledEntity != null && controllableEntity != controlledEntity)
((WorldClientNetworkedRemote) controlledEntity.getWorld()).getRemoteServer().unsubscribe(controlledEntity);
// Let know the server of new changes
((WorldClientNetworkedRemote) controllableEntity.getWorld()).getRemoteServer().subscribe(controllableEntity);
}
controlledEntity = controllableEntity;
} else if (entity == null && getControlledEntity() != null) {
// Directly unset it
if (world instanceof WorldMaster)
getControlledEntity().getControllerComponent().setController(null);
// When loosing control over an entity, stop sending the server info about it
if (controlledEntity != null)
if (controlledEntity.getWorld() instanceof WorldClientNetworkedRemote)
((WorldClientNetworkedRemote) controlledEntity.getWorld()).getRemoteServer().unsubscribe(controlledEntity);
controlledEntity = null;
}
return true;
}
use of io.xol.chunkstories.api.world.WorldMaster in project chunkstories-api by Hugobros3.
the class EntityComponentController method setController.
public void setController(Controller controller) {
// Checks we are entitled to do this
if (!(entity.getWorld() instanceof WorldMaster))
throw new UnauthorizedClientActionException("setController()");
Controller formerController = this.controller;
this.controller = controller;
// Tell the new controller he his
if (controller != null)
pushComponent(controller);
// Tell the former one he's no longer
if (formerController != null && (controller == null || !controller.equals(formerController)))
pushComponent(formerController);
}
use of io.xol.chunkstories.api.world.WorldMaster in project chunkstories-api by Hugobros3.
the class PacketEntity method process.
public void process(PacketSender sender, DataInputStream in, PacketReceptionContext processor) throws IOException, UnknownComponentException {
long entityUUID = in.readLong();
short entityTypeID = in.readShort();
if (entityTypeID == -1)
return;
World world = processor.getWorld();
if (world == null)
return;
Entity entity = world.getEntityByUUID(entityUUID);
boolean addToWorld = false;
// Create an entity if the servers tells you to do so
if (entity == null) {
if (world instanceof WorldMaster && sender instanceof RemotePlayer) {
((Player) sender).sendMessage("You are sending packets to the server about a removed entity. Ignoring those.");
return;
} else {
entity = processor.getWorld().getContentTranslator().getEntityForId(entityTypeID).create(// This is technically wrong
new Location(world, 0, 0, 0));
entity.setUUID(entityUUID);
addToWorld = true;
}
}
int componentId = in.readInt();
// Loop throught all components
while (componentId != 0) {
try {
entity.getComponents().tryPullComponentInStream(componentId, sender, in);
} catch (UnknownComponentException e) {
processor.logger().warn(e.getMessage());
}
componentId = in.readInt();
}
// Add to world if it was missing and we didn't receive the despawn flag
if (addToWorld && entity.exists()) {
// Only the WorldMaster is allowed to spawn new entities in the world
if (processor instanceof ClientPacketsProcessor)
processor.getWorld().addEntity(entity);
}
}
use of io.xol.chunkstories.api.world.WorldMaster 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;
}
Aggregations