/** * fill this VoxelSelection using the serialised VoxelSelection byte array * * @param byteArrayInputStream the bytearray containing the serialised VoxelSelection * @return true for success, false for failure (leaves selection untouched) */ public boolean readFromBytes(ByteArrayInputStream byteArrayInputStream) { try { DataInputStream inputStream = new DataInputStream(byteArrayInputStream); int newXsize = inputStream.readInt(); int newYsize = inputStream.readInt(); int newZsize = inputStream.readInt(); if (newXsize < 1 || newXsize > MAX_X_SIZE || newYsize < 1 || newYsize > MAX_Y_SIZE || newZsize < 1 || newZsize > MAX_Z_SIZE) { return false; } ObjectInputStream objectInputStream = new ObjectInputStream(inputStream); Object newVoxels = objectInputStream.readObject(); if (!(newVoxels instanceof BitSet)) return false; xSize = newXsize; ySize = newYsize; zSize = newZsize; voxels = (BitSet) newVoxels; } catch (ClassNotFoundException cnfe) { ErrorLog.defaultLog().debug("Exception while VoxelSelection.readFromDataArray: " + cnfe); return false; } catch (IOException ioe) { ErrorLog.defaultLog().debug("Exception while VoxelSelection.readFromDataArray: " + ioe); return false; } return true; }
@Override public boolean handlePacket( Packet250MultipartSegmentAcknowledge packetAck, MessageContext ctx) { { EntityPlayerMP entityPlayerMP = ctx.getServerHandler().playerEntity; if (!playerMOATsenders.containsKey(entityPlayerMP)) { ErrorLog.defaultLog() .info("ServerVoxelSelections:: Packet received from player not in playerMOATsenders"); return false; } // System.out.println("ServerVoxelSelections packet received"); return playerMOATsenders.get(entityPlayerMP).handleIncomingPacket(packetAck); } }
/** * serialise the VoxelSelection to a byte array * * @return the serialised VoxelSelection, or null for failure */ public ByteArrayOutputStream writeToBytes() { ByteArrayOutputStream bos = null; try { bos = new ByteArrayOutputStream(); DataOutputStream outputStream = new DataOutputStream(bos); outputStream.writeInt(xSize); outputStream.writeInt(ySize); outputStream.writeInt(zSize); ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream); objectOutputStream.writeObject(voxels); objectOutputStream.close(); } catch (IOException ioe) { ErrorLog.defaultLog().debug("Exception while converting VoxelSelection toDataArray:" + ioe); bos = null; } return bos; }
/** * handle timeouts etc * * @param maximumDurationInNS - the maximum amount of time to spend generating selections for * clients. 0 = don't generate any. */ public void tick(long maximumDurationInNS) { for (MultipartOneAtATimeReceiver receiver : playerMOATreceivers.values()) { receiver.onTick(); } for (MultipartOneAtATimeSender sender : playerMOATsenders.values()) { sender.onTick(); } if (maximumDurationInNS == 0) return; boolean foundSuitable = false; CommandQueueEntry currentCommand; do { currentCommand = commandQueue.peekFirst(); if (currentCommand == null) return; if (currentCommand.entityPlayerMP.get() == null) { commandQueue.removeFirst(); } else { foundSuitable = true; } } while (!foundSuitable); EntityPlayerMP entityPlayerMP = currentCommand.entityPlayerMP.get(); World playerWorld = entityPlayerMP.getEntityWorld(); Packet250ServerSelectionGeneration commandPacket = currentCommand.commandPacket; if (!currentCommand.hasStarted) { BlockVoxelMultiSelector blockVoxelMultiSelector = new BlockVoxelMultiSelector(); playerBlockVoxelMultiSelectors.put(entityPlayerMP, blockVoxelMultiSelector); playerCommandStatus.put(entityPlayerMP, CommandStatus.EXECUTING); currentCommand.blockVoxelMultiSelector = blockVoxelMultiSelector; switch (commandPacket.getCommand()) { case ALL_IN_BOX: { blockVoxelMultiSelector.selectAllInBoxStart( playerWorld, commandPacket.getCorner1(), commandPacket.getCorner2()); break; } case UNBOUND_FILL: { blockVoxelMultiSelector.selectUnboundFillStart( playerWorld, commandPacket.getFillAlgorithmSettings()); break; } case BOUND_FILL: { blockVoxelMultiSelector.selectBoundFillStart( playerWorld, commandPacket.getFillAlgorithmSettings(), commandPacket.getCorner1(), commandPacket.getCorner2()); break; } default: { ErrorLog.defaultLog() .severe("Invalid command in ServerVoxelSelections: " + commandPacket.getCommand()); break; } } currentCommand.hasStarted = true; } else { BlockVoxelMultiSelector blockVoxelMultiSelector = currentCommand.blockVoxelMultiSelector; float progress = blockVoxelMultiSelector.continueSelectionGeneration(playerWorld, maximumDurationInNS); if (progress < 0) { // finished BlockPos origin = blockVoxelMultiSelector.getWorldOrigin(); VoxelSelectionWithOrigin newSelection = new VoxelSelectionWithOrigin( origin.getX(), origin.getY(), origin.getZ(), blockVoxelMultiSelector.getSelection()); // System.out.println("New selection origin: [" + newSelection.getWxOrigin() // + ", " + newSelection.getWyOrigin() // + ", " + newSelection.getWzOrigin()+"]"); playerSelections.put(entityPlayerMP, newSelection); playerBlockVoxelMultiSelectors.remove(entityPlayerMP); playerCommandStatus.put(entityPlayerMP, CommandStatus.COMPLETED); MultipartOneAtATimeSender sender = playerMOATsenders.get(entityPlayerMP); if (sender != null) { SelectionPacket selectionPacket = SelectionPacket.createSenderPacket(blockVoxelMultiSelector, Side.SERVER); SenderLinkage newLinkage = new SenderLinkage(entityPlayerMP, selectionPacket.getUniqueID()); playerSenderLinkages.put(entityPlayerMP, newLinkage); // System.out.println("send new Multipart Selection from server to client, ID = " // + selectionPacket.getUniqueID()); // todo remove sender.sendMultipartPacket(newLinkage, selectionPacket); } assert (commandQueue.peekFirst() == currentCommand); commandQueue.removeFirst(); } } }
public Packet250ServerSelectionGeneration handlePacket( Packet250ServerSelectionGeneration packet, MessageContext ctx) { final float ERROR_STATUS = -10.0F; EntityPlayerMP entityPlayerMP = ctx.getServerHandler().playerEntity; int uniqueID = packet.getUniqueID(); if (!players.containsKey(entityPlayerMP)) { ErrorLog.defaultLog() .info("ServerVoxelSelections:: Packet received from player not in players"); Packet250Base message = Packet250ServerSelectionGeneration.replyFractionCompleted(uniqueID, ERROR_STATUS); sendReplyMessageToClient(message, entityPlayerMP); return null; } Integer lastCommandID = playerLastCommandID.get(entityPlayerMP); if (lastCommandID == null) lastCommandID = Integer.MIN_VALUE; if (uniqueID < lastCommandID) return null; // discard old commands switch (packet.getCommand()) { case STATUS_REQUEST: { if (uniqueID != lastCommandID) return null; // discard old or too-new commands CommandStatus commandStatus = playerCommandStatus.get(entityPlayerMP); if (commandStatus == null) return Packet250ServerSelectionGeneration.replyFractionCompleted( uniqueID, ERROR_STATUS); final float JUST_STARTED_VALUE = 0.01F; Packet250Base message = null; switch (commandStatus) { case QUEUED: { message = Packet250ServerSelectionGeneration.replyFractionCompleted( uniqueID, JUST_STARTED_VALUE); break; } case COMPLETED: { message = Packet250ServerSelectionGeneration.replyFractionCompleted(uniqueID, 1.0F); break; } case EXECUTING: { BlockVoxelMultiSelector blockVoxelMultiSelector = playerBlockVoxelMultiSelectors.get(entityPlayerMP); if (blockVoxelMultiSelector == null) { message = Packet250ServerSelectionGeneration.replyFractionCompleted( uniqueID, ERROR_STATUS); } else { message = Packet250ServerSelectionGeneration.replyFractionCompleted( uniqueID, blockVoxelMultiSelector.getEstimatedFractionComplete()); } break; } default: assert false : "Invalid commandStatus in ServerVoxelSelections:" + commandStatus; } if (message != null) { sendReplyMessageToClient(message, entityPlayerMP); } return null; } case ABORT: { if (uniqueID == lastCommandID) { abortCurrentCommand(entityPlayerMP); } return null; } case ALL_IN_BOX: case UNBOUND_FILL: case BOUND_FILL: { if (uniqueID < lastCommandID) return null; // discard old commands if (uniqueID == lastCommandID) { CommandStatus commandStatus = playerCommandStatus.get(entityPlayerMP); if (commandStatus != CommandStatus.COMPLETED) return null; // ignore this command if we're currently processing it } boolean success = enqueueSelectionCommand(entityPlayerMP, packet); if (success) { playerLastCommandID.put(entityPlayerMP, uniqueID); } Packet250Base message = Packet250ServerSelectionGeneration.replyFractionCompleted( uniqueID, success ? 0.0F : ERROR_STATUS); sendReplyMessageToClient(message, entityPlayerMP); return null; } default: { ErrorLog.defaultLog() .severe("Invalid command received in ServerVoxelSelections:" + packet.getCommand()); return null; } } }