use of io.xol.chunkstories.api.player.Player in project chunkstories by Hugobros3.
the class DebugWorldDataCommands method handleCommand.
@Override
public boolean handleCommand(CommandEmitter emitter, Command command, String[] arguments) {
if (command.getName().equals("chunk") && emitter.hasPermission("server.debug")) {
Player player = (Player) emitter;
emitter.sendMessage("#00FFD0" + player.getControlledEntity().getChunk());
return true;
} else if (command.getName().equals("region") && emitter.hasPermission("server.debug")) {
Player player = (Player) emitter;
Chunk chunk = player.getControlledEntity().getChunk();
if (chunk != null)
emitter.sendMessage("#00FFD0" + chunk.getRegion());
else
emitter.sendMessage("#00FFD0" + "not within a loaded chunk, so no parent region could be found.");
return true;
} else if (command.getName().equals("heightmap") && emitter.hasPermission("server.debug")) {
Heightmap sum;
if (arguments.length == 2) {
int x = Integer.parseInt(arguments[0]);
int z = Integer.parseInt(arguments[1]);
sum = server.getWorld().getRegionsSummariesHolder().getHeightmap(x, z);
} else {
Player player = (Player) emitter;
sum = player.getWorld().getRegionsSummariesHolder().getHeightmapLocation(player.getLocation());
}
emitter.sendMessage("#00FFD0" + sum);
return true;
} else if (command.getName().equals("heightmaps") && emitter.hasPermission("server.debug")) {
dumpLoadedHeightmap((WorldImplementation) server.getWorld(), emitter);
}
return false;
}
use of io.xol.chunkstories.api.player.Player in project chunkstories by Hugobros3.
the class Ingame method destroy.
@Override
public void destroy() {
// Logout sequence: Save the player entity
if (world instanceof WorldMaster) {
Player player = getPlayer();
PlayerLogoutEvent playerDisconnectionEvent = new PlayerLogoutEvent(player);
world.getPluginManager().fireEvent(playerDisconnectionEvent);
if (this.playerEntity != null) {
SerializedEntityFile playerEntityFile = new SerializedEntityFile(world.getFolderPath() + "/players/" + getPlayer().getName().toLowerCase() + ".csf");
playerEntityFile.write(this.playerEntity);
}
}
// Stop the game logic and save
if (world instanceof WorldMaster) {
// TODO: Stop simulation
Fence fence = ((WorldMaster) world).stopLogic();
// exitButton.text = "#{world.saving}";
fence.traverse();
fence = world.saveEverything();
// exitButton.text = "#{world.saving}";
fence.traverse();
}
// Disables plugins
world.getPluginManager().disablePlugins();
this.world.getWorldRenderer().destroy();
}
use of io.xol.chunkstories.api.player.Player 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.player.Player in project chunkstories-api by Hugobros3.
the class PacketInventoryMoveItemPile method process.
public void process(PacketSender sender, DataInputStream in, PacketReceptionContext processor) throws IOException {
if (!(processor instanceof ServerPlayerPacketsProcessor)) {
processor.logger().warn("Received a " + this.getClass().getSimpleName() + " but this GameContext isn't providen with a packet processor made to deal with it");
return;
}
ServerPlayerPacketsProcessor sppc = (ServerPlayerPacketsProcessor) processor;
Player player = sppc.getPlayer();
EntityControllable playerEntity = player.getControlledEntity();
oldX = in.readInt();
oldY = in.readInt();
newX = in.readInt();
newY = in.readInt();
amount = in.readInt();
from = InventoryTranslator.obtainInventoryHandle(in, processor);
to = InventoryTranslator.obtainInventoryHandle(in, processor);
// If this pile is spawned from the void
if (// || from == InventoryTranslator.INVENTORY_CREATIVE_TRASH)
from == null) {
try {
itemPile = ItemPile.obtainItemPileFromStream(player.getWorld().getContentTranslator(), in);
} catch (NullItemException e) {
// This ... isn't supposed to happen
processor.logger().info("User " + sender + " is trying to spawn a null ItemPile for some reason.");
} catch (UndefinedItemTypeException e) {
// This is slightly more problematic
processor.logger().warn(e.getMessage());
// e.printStackTrace(processor.getLogger().getPrintWriter());
}
} else {
itemPile = from.getItemPileAt(oldX, oldY);
}
// Check access
if (to != null && playerEntity != null) {
if (!to.isAccessibleTo(playerEntity)) {
player.sendMessage("You don't have access to this.");
return;
}
}
// Check using event
PlayerMoveItemEvent moveItemEvent = new PlayerMoveItemEvent(player, itemPile, from, to, oldX, oldY, newX, newY, amount);
player.getContext().getPluginManager().fireEvent(moveItemEvent);
if (!moveItemEvent.isCancelled()) {
// Restrict item spawning
if (// || from instanceof InventoryLocalCreativeMenu)
from == null) {
if (player.hasPermission("items.spawn") || (player.getControlledEntity() != null && player.getControlledEntity() instanceof EntityCreative && ((EntityCreative) player.getControlledEntity()).getCreativeModeComponent().get())) {
// Let it happen when in creative mode or owns items.spawn perm
} else {
player.sendMessage("#C00000You are neither in creative mode nor have the items.spawn permission.");
return;
}
}
// If target inventory is null, this means the item was dropped
if (to == null) {
if (playerEntity == null) {
System.out.println("Dropping items isn't possible if the player doesn't control any entity.");
return;
}
// If we're pulling this out of an inventory ( and not /dev/null ), we need to remove it from that
Inventory sourceInventory = itemPile.getInventory();
Location loc = playerEntity.getLocation();
EventItemDroppedToWorld dropItemEvent = new EventItemDroppedToWorld(loc, sourceInventory, itemPile);
player.getContext().getPluginManager().fireEvent(dropItemEvent);
if (!dropItemEvent.isCancelled()) {
if (sourceInventory != null)
sourceInventory.setItemPileAt(itemPile.getX(), itemPile.getY(), null);
if (dropItemEvent.getItemEntity() != null)
loc.getWorld().addEntity(dropItemEvent.getItemEntity());
}
return;
}
itemPile.moveItemPileTo(to, newX, newY, amount);
}
}
use of io.xol.chunkstories.api.player.Player in project chunkstories-api by Hugobros3.
the class EntityComponentController method pull.
@Override
public void pull(StreamSource from, DataInputStream dis) throws IOException {
long controllerUUID = 0;
boolean isControllerNotNull = dis.readBoolean();
if (isControllerNotNull)
controllerUUID = dis.readLong();
// This is strictly illegal, only remotely connected client worlds will accept such requests
if (!(entity.getWorld() instanceof WorldClientNetworkedRemote)) {
// Terminate connections immediately
if (from instanceof Player)
((Player) from).disconnect("Illegal controller set attempt, terminating client connection for " + from);
return;
}
LocalPlayer player = ((ClientInterface) entity.getWorld().getGameContext()).getPlayer();
assert player != null;
if (isControllerNotNull) {
// long controllerUUID = dis.readLong();
long clientUUID = player.getUUID();
System.out.println("Entity " + entity + " is now in control of " + controllerUUID + " me=" + clientUUID);
// This update tells us we are now in control of this entity
if (clientUUID == controllerUUID) {
EntityControllable controlledEntity = (EntityControllable) entity;
// TODO sort out local hosted worlds properly ?
// Client.getInstance().getServerConnection().subscribe(entity);
controller = player;
player.setControlledEntity(controlledEntity);
System.out.println("The client is now in control of entity " + controlledEntity);
} else {
// If we receive a different UUID than ours in a EntityComponent change, it means that we don't control it anymore and someone else does.
if (player.getControlledEntity() != null && player.getControlledEntity().equals(entity)) {
player.setControlledEntity(null);
// Client.getInstance().getServerConnection().unsubscribe(entity);
controller = null;
System.out.println("Lost control of entity " + entity + " to " + controllerUUID);
}
}
} else {
// If we receive a different UUID than ours in a EntityComponent change, it means that we don't control it anymore and someone else does.
if (player.getControlledEntity() != null && player.getControlledEntity().equals(entity)) {
player.setControlledEntity(null);
// Client.getInstance().getServerConnection().unsubscribe(entity);
controller = null;
System.out.println("Lost control of entity " + entity);
}
}
}
Aggregations