/** * Called by client when some action is performed or on finish gathering Called by move observer * on player move * * @param player */ public void finishGathering(Player player) { if (currentGatherer == player.getObjectId()) { if (state == GatherState.GATHERING) { task.abort(); } currentGatherer = 0; state = GatherState.IDLE; } }
/** * Start gathering process * * @param player */ public void onStartUse(final Player player) { // basic actions, need to improve here final GatherableTemplate template = this.getObjectTemplate(); if (!checkPlayerSkill(player, template)) return; List<Material> materials = template.getMaterials().getMaterial(); int index = 0; Material material = materials.get(index); // default is 0 int count = materials.size(); if (count < 1) { // error - theoretically if XML data is correct, this should never happen. return; } else if (count == 1) { // default is 0 } else { if (player.getInventory().isFull()) { PacketSendUtility.sendPacket(player, SM_SYSTEM_MESSAGE.EXTRACT_GATHER_INVENTORY_IS_FULL()); return; } int gatherRate = 1; // 1x rates (probably make config later, if fixed to non-linear statistic probability) float maxrate = 0; int rate = 0; int i = 0; // index counter // sort materials to ascending order SortedMap<Integer, Integer> hasMat = new TreeMap<Integer, Integer>(); for (Material mat : materials) { maxrate += mat.getRate(); // get maxrate hasMat.put( mat.getRate(), i); // sort and save index of materials (key is rate and rate is unique on each // gatherId) i++; } Iterator<Integer> it = hasMat.keySet().iterator(); while (it.hasNext()) { rate = it.next(); float percent = Rnd.get() * 100f; float chance = ((rate / maxrate) * 100f * gatherRate); // default index is to 0, look to up little bit on 'material' if (percent < chance) { index = hasMat.get(rate); // return index material = materials.get(index); break; } } } final Material finalMaterial = material; if (state != GatherState.GATHERING) { state = GatherState.GATHERING; currentGatherer = player.getObjectId(); player .getObserveController() .attach( new StartMovingListener() { @Override public void moved() { finishGathering(player); } }); int skillLvlDiff = player.getSkillList().getSkillLevel(template.getHarvestSkill()) - template.getSkillLevel(); task = new GatheringTask(player, this, finalMaterial, skillLvlDiff); task.start(); } }