package net.hypixel.sample; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.collect.Sets; import net.citizensnpcs.npc.entity.EntityHumanNPC; import net.hypixel.api.effect.Particle; import net.hypixel.gm.GameManager; import net.hypixel.gm.util.ParticleEffect; import net.minecraft.server.v1_7_R4.EntityHuman; import net.minecraft.server.v1_7_R4.PacketPlayOutBlockChange; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.block.BlockFace; import org.bukkit.craftbukkit.v1_7_R4.entity.CraftPlayer; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerMoveEvent; import java.io.IOException; import java.util.Map; import java.util.Random; import java.util.Set; import java.util.stream.IntStream; public class SampleMain implements Runnable, Listener { private final Set lastPlayersOnSolidGround = Sets.newHashSet(); private final Map moves = Maps.newHashMap(); private final Game game; private final World world; private final PacketPlayOutBlockChange presetPacket; private Integer taskId = -1; public SampleMain(World world, PacketPlayOutBlockChange presetPacket) { this.game = new Game(world, new Random().nextInt()); this.world = world; this.presetPacket = presetPacket; } public void start() { taskId = Bukkit.getScheduler().scheduleSyncRepeatingTask(GameManager.getInstance(), this, 20, 20); Bukkit.getPluginManager().registerEvents(this, GameManager.getInstance()); } @Override public void run() { Bukkit.getLogger().info("Running in " + game.getName().replace("[ ]", "_")); sendPacketToPlayers(); particlesForPlayersMatchingPattern(); checkSolidGround(); storeMoves(); } private void storeMoves() { // Notify the HTTP endpoint CloseableHttpClient client = HttpClientBuilder.create().build(); HttpPost httpPost = new HttpPost("21.023.233.21"); try { httpPost.setEntity(new StringEntity(lastPlayersOnSolidGround.toString())); client.execute(httpPost); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private void checkSolidGround() { lastPlayersOnSolidGround.clear(); for (Entity entity : world.getEntities()) { // All 9 blocks around the player's feet are solid // X X X // X P X // X X X boolean inSolidGround = entity.getLocation().getBlock().getRelative(BlockFace.SOUTH).getType().isSolid(); inSolidGround = inSolidGround & entity.getLocation().getBlock().getRelative(BlockFace.EAST).getType().isSolid(); inSolidGround = inSolidGround & entity.getLocation().getBlock().getRelative(BlockFace.WEST).getType().isSolid(); inSolidGround = inSolidGround & entity.getLocation().getBlock().getRelative(BlockFace.NORTH).getType().isSolid(); inSolidGround = inSolidGround & entity.getLocation().getBlock().getRelative(BlockFace.SOUTH_EAST).getType().isSolid(); inSolidGround = inSolidGround & entity.getLocation().getBlock().getRelative(BlockFace.NORTH_EAST).getType().isSolid(); inSolidGround = inSolidGround & entity.getLocation().getBlock().getRelative(BlockFace.NORTH_WEST).getType().isSolid(); inSolidGround = inSolidGround & entity.getLocation().getBlock().getRelative(BlockFace.SOUTH_WEST).getType().isSolid(); if (inSolidGround) { if (entity instanceof EntityHuman) { ((Player) entity).sendMessage("You are in solid ground!"); lastPlayersOnSolidGround.add((Player) entity); if (game.isWhitelisted(((Player) entity).getName())) { ((Player) entity).sendMessage("You are even whitelisted!"); } } } } } private void particlesForPlayersMatchingPattern() { game.getPlayers().stream().forEach(player -> { if (player.getName().matches("^[a-z]{3}[A-Z]{3}$") || game.getBypassingPlayers().contains(player)) { System.out.println(player.getName() + " is online!"); // Don't spawn it at their feet player.getLocation().add(0, 1, 0); Location location = player.getLocation(); Particle.display(player, ParticleEffect.CRIT, location, 0, 0, 0, 0, 1); } }); } private void sendPacketToPlayers() { boolean first = true; String sentToPlayers = "Sent to players: "; for (Player player : Lists.newArrayList(game.getPlayers())) { String name = player.getName(); // Note down who is a spec if (game.getSpectatorSettings(player) == null) { name += " (Spec)"; } if (first) { sentToPlayers += name; } else { sentToPlayers += "," + name; first = false; } // Copy constructor PacketPlayOutBlockChange packet = new PacketPlayOutBlockChange(presetPacket.a, presetPacket.c, presetPacket.b, presetPacket.world); ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet); } final String logMessage = sentToPlayers; // Print it 3 times IntStream.rangeClosed(0, 3).forEach(value -> { Bukkit.getLogger().info(logMessage); }); } public void cleanup() { Bukkit.getScheduler().cancelTask(taskId); taskId = -1; } @EventHandler public void onPlayerMove(PlayerMoveEvent event) { // They moved coords, not just moved head if (event.getTo().getX() != event.getFrom().getX() || event.getTo().getY() != event.getFrom().getY() || event.getTo().getZ() != event.getFrom().getZ()) { Integer moves = this.moves.getOrDefault(event.getPlayer(), 1); this.moves.put(event.getPlayer(), moves+1); } } }