public void resetScore() { /* * score.set 与char2type.set这两个方法,虽然没有以原子的方式执行完成,但是 却不影响整个类的安全性,因为这两个方法没有相互依赖,只需保证单个方法以原子的方式执行即可, 这个情况与 java并发编程中的因式分解那个例子不同, */ score.set(0); char2type.set(-1); setScore(); }
public void newCharacter(CharacterEvent ce) { int oldChar2type; // Previous character not typed correctly - 1 point penalty if (ce.source == generator.get()) { oldChar2type = char2type.getAndSet(ce.character); // 接收随机字母 if (oldChar2type != -1) { score.decrementAndGet(); setScore(); } } // If character is extraneous - 1 point penalty // If character does not match - 1 point penalty else if (ce.source == typist.get()) { // 在此有个假设:即正在使用的该变量值不会被变更且程序代码完成时也是如此 // 所有已经被我们设定的具有特定值的变量就应当是那个值。 while (true) { oldChar2type = char2type.get(); // 获取上次已接收到的随机字母 if (oldChar2type != ce.character) { // ce.character是用户输入的字母,现在作比较 score.decrementAndGet(); break; } else if (char2type.compareAndSet(oldChar2type, -1)) { score.incrementAndGet(); break; } } setScore(); } }
private DarkBotMCSpambot( DarkBot darkBot, String server, String username, String password, String sessionId, String loginProxy, String proxy, String owner) { synchronized (bots) { bots.add(this); // slotsTaken.incrementAndGet(); synchronized (slotsTaken) { slotsTaken.notifyAll(); } } MinecraftBotData.Builder builder = MinecraftBotData.builder(); // botData.nickname = ""; // for(int i = 0; i < 10; i++) // botData.nickname += alphas[random.nextInt(alphas.length)]; if (proxy != null && !proxy.isEmpty()) { int port = 80; ProxyType type = ProxyType.SOCKS; if (proxy.contains(":")) { String[] parts = proxy.split(":"); proxy = parts[0]; port = Integer.parseInt(parts[1]); if (parts.length > 2) type = ProxyType.values()[Integer.parseInt(parts[2]) - 1]; } builder.withSocksProxy(new ProxyData(proxy, port, type)); this.proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(proxy, port)); } if (loginProxy != null && !loginProxy.isEmpty()) { int port = 80; if (loginProxy.contains(":")) { String[] parts = loginProxy.split(":"); loginProxy = parts[0]; port = Integer.parseInt(parts[1]); } builder.withHttpProxy(new ProxyData(loginProxy, port, ProxyType.HTTP)); this.loginProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(loginProxy, port)); } builder.withUsername(username); if (sessionId != null) builder.withSessionId(sessionId); else builder.withPassword(password); if (server != null && !server.isEmpty()) { int port = 25565; if (server.contains(":")) { String[] parts = server.split(":"); server = parts[0]; port = Integer.parseInt(parts[1]); } builder.withServer(server).withPort(port); } else throw new IllegalArgumentException("Unknown server!"); this.owner = owner; MinecraftBotData botData = builder.build(); System.setProperty("socksProxyHost", ""); System.setProperty("socksProxyPort", ""); System.out.println("[" + username + "] Connecting..."); bot = new MinecraftBot(darkBot, botData); bot.setMovementDisabled(true); connectionHandler = bot.getConnectionHandler(); Session session = bot.getSession(); // System.gc(); System.out.println("[" + username + "] Done! (" + amountJoined.incrementAndGet() + ")"); bot.getEventManager().registerListener(this); bot.getGameHandler().registerListener(this); long lastShoutTime = System.currentTimeMillis(); while (bot.isConnected()) { if (die) { connectionHandler.sendPacket(new Packet255KickDisconnect("Goodbye")); return; } try { Thread.sleep(3000 + random.nextInt(1000)); } catch (InterruptedException exception) { exception.printStackTrace(); } if (!bot.hasSpawned()) continue; connectionHandler.sendPacket(new Packet0KeepAlive(random.nextInt())); if (spamMessage == null || !canSpam) continue; String message = spamMessage; if (message.contains("%skill")) message = message.replace("%skill", skills[nextSkill++]); if (nextSkill >= skills.length) nextSkill = 0; if (message.contains("%bot")) { synchronized (bots) { message = message.replace( "%bot", bots.get(nextBot > bots.size() ? (nextBot = 0) * 0 : nextBot++) .bot .getSession() .getUsername()); } } if (message.contains("%spamlist")) message = message.replace("%spamlist", spamList[nextSpamList++]); if (nextSpamList >= spamList.length) nextSpamList = 0; if (message.contains("%rnd")) { int length = 1; int index = message.indexOf("%rnd") + "%rnd".length(); int lastIndex; for (lastIndex = index; lastIndex < message.length(); lastIndex++) if (Character.isDigit(message.charAt(lastIndex))) lastIndex++; else break; if (lastIndex > message.length()) lastIndex--; try { System.out.println(index + "," + lastIndex + "," + message.length()); length = Integer.parseInt(message.substring(index, lastIndex)); } catch (Exception exception) { } String randomChars = ""; for (int i = 0; i < length; i++) randomChars += alphas[random.nextInt(alphas.length)]; message = message.replace("%rnd", randomChars); } if (message.contains("%msg")) message = "/msg " + msgChars[nextMsgChar++] + " " + message.replace("%msg", ""); if (message.contains("%ernd")) { message = message.replace("%ernd", ""); int extraMessageLength = 15 + random.nextInt(6); message = message.substring(0, Math.min(100 - extraMessageLength, message.length())) + " ["; extraMessageLength -= 3; for (int i = 0; i < extraMessageLength; i++) message += alphas[random.nextInt(alphas.length)]; message += "]"; } else message = message.substring(0, Math.min(100, message.length())); connectionHandler.sendPacket(new Packet3Chat(message)); } synchronized (bots) { bots.remove(this); } amountJoined.decrementAndGet(); slotsTaken.decrementAndGet(); synchronized (slotsTaken) { slotsTaken.notifyAll(); } }