Java Code Examples for org.bukkit.Chunk#getX()

The following examples show how to use org.bukkit.Chunk#getX() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HologramListener.java    From Holograms with MIT License 6 votes vote down vote up
@EventHandler
public void onChunkLoad(ChunkLoadEvent event) {
    Chunk chunk = event.getChunk();
    if (chunk == null || !chunk.isLoaded()) {
        return;
    }

    Collection<Hologram> holograms = plugin.getHologramManager().getActiveHolograms().values();
    for (Hologram holo : holograms) {
        int chunkX = (int) Math.floor(holo.getLocation().getBlockX() / 16.0D);
        int chunkZ = (int) Math.floor(holo.getLocation().getBlockZ() / 16.0D);
        if (chunkX == chunk.getX() && chunkZ == chunk.getZ()) {
            plugin.getServer().getScheduler().runTaskLater(plugin, holo::spawn, 10L);
        }
    }
}
 
Example 2
Source File: HologramListener.java    From Holograms with MIT License 6 votes vote down vote up
@EventHandler
public void onChunkUnload(ChunkUnloadEvent event) {
    Chunk chunk = event.getChunk();
    for (Entity entity : chunk.getEntities()) {
        HologramEntity hologramEntity = plugin.getEntityController().getHologramEntity(entity);
        if (hologramEntity != null) {
            hologramEntity.remove();
        }
    }
    Collection<Hologram> holograms = plugin.getHologramManager().getActiveHolograms().values();
    for (Hologram holo : holograms) {
        Location loc = holo.getLocation();
        int chunkX = (int) Math.floor(loc.getBlockX() / 16.0D);
        int chunkZ = (int) Math.floor(loc.getBlockZ() / 16.0D);
        if (chunkX == chunk.getX() && chunkZ == chunk.getZ()) {
            holo.despawn();
        }
    }
}
 
Example 3
Source File: TreePopulator.java    From GiantTrees with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void
    populate(final World world, final Random random, final Chunk chunk) {
  final Location refPoint = new Location(world, (chunk.getX() * 16)
                                                + random.nextInt(16), 64,
                                         (chunk.getZ() * 16)
                                             + random.nextInt(16));
  refPoint.setY(this.getHighestSoil(world.getHighestBlockAt(refPoint)));

  final Biome biome = this.simplifyBiome(world.getBiome(refPoint.getBlockX(),
                                                        refPoint.getBlockZ()));
  if (this.isAcceptableBiome(biome) && this.treeCanGrow(random)) {
    final String treeType = biome.name();

    final File treeFile = new File(this.plugin.getDataFolder(), "biome."
                                                                + treeType
                                                                + ".xml");
    final File rootFile = new File(this.plugin.getDataFolder(), "biome."
                                                                + treeType
                                                                + ".root.xml");

    if (treeFile.exists()) {
      final TreeRenderer renderer = new TreeRenderer(this.plugin);
      renderer.renderTree(refPoint, treeFile, rootFile, random.nextInt(),
                          false);
    }
  }
}
 
Example 4
Source File: TradeGoodPopulator.java    From civcraft with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void populate(World world, Random random, Chunk source) {
   	
   	ChunkCoord cCoord = new ChunkCoord(source);
   	TradeGoodPick pick = CivGlobal.preGenerator.goodPicks.get(cCoord);
   	if (pick != null) {
		int centerX = (source.getX() << 4) + 8;
		int centerZ = (source.getZ() << 4) + 8;
		int centerY = world.getHighestBlockYAt(centerX, centerZ);
		BlockCoord coord = new BlockCoord(world.getName(), centerX, centerY, centerZ);

		if (checkForDuplicateTradeGood(world.getName(), centerX, centerY, centerZ)) {
			return;
		}
		
		// Determine if we should be a water good.
		ConfigTradeGood good;
		if (ItemManager.getBlockTypeIdAt(world, centerX, centerY-1, centerZ) == CivData.WATER || 
			ItemManager.getBlockTypeIdAt(world, centerX, centerY-1, centerZ) == CivData.WATER_RUNNING) {
			good = pick.waterPick;
		}  else {
			good = pick.landPick;
		}
		
		// Randomly choose a land or water good.
		if (good == null) {
			System.out.println("Could not find suitable good type during populate! aborting.");
			return;
		}
		
		// Create a copy and save it in the global hash table.
		buildTradeGoodie(good, coord, world, false);
   	}
	
   }
 
Example 5
Source File: UnloadedInventoryHandler.java    From Civs with GNU General Public License v3.0 5 votes vote down vote up
public void syncAllInventoriesInChunk(Chunk chunk) {
    String chunkString = "c:" + chunk.getX() + ":" + chunk.getZ();
    if (!unloadedChestInventories.containsKey(chunkString)) {
        return;
    }
    for (String locationString : unloadedChestInventories.get(chunkString).keySet()) {
        syncInventory(locationString);
    }
}
 
Example 6
Source File: AsyncChunk.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean equals(Object obj) {
    if (obj == null || !(obj instanceof Chunk)) {
        return false;
    }
    Chunk other = (Chunk) obj;
    return other.getX() == x && other.getZ() == z && world.equals(other.getWorld());
}
 
Example 7
Source File: Fire.java    From GlobalWarming with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Set a random set of loaded blocks on fire
 */
private void setFire(World world, int blocks) {
    if (world != null) {
        int count = world.getLoadedChunks().length;
        for (int i = 0; i < blocks; i++) {
            int chunkIndex = GlobalWarming.getInstance().getRandom().nextInt(count);
            Chunk chunk = world.getLoadedChunks()[chunkIndex];
            int x = (chunk.getX() * BLOCKS_PER_CHUNK) + GlobalWarming.getInstance().getRandom().nextInt(BLOCKS_PER_CHUNK);
            int z = (chunk.getZ() * BLOCKS_PER_CHUNK) + GlobalWarming.getInstance().getRandom().nextInt(BLOCKS_PER_CHUNK);
            Block topBlock = world.getHighestBlockAt(x, z);
            topBlock.getRelative(BlockFace.UP).setType(Material.FIRE);
        }
    }
}
 
Example 8
Source File: RegionManager.java    From RedProtect with GNU General Public License v3.0 5 votes vote down vote up
public Set<Region> getRegionsForChunk(Chunk chunk) {
    Set<Region> regions = new HashSet<>();
    for (Region region : RedProtect.get().rm.getRegionsByWorld(chunk.getWorld().getName())) {
        int minChunkX = (int) Math.floor(region.getMinMbrX() / 16f);
        int maxChunkX = (int) Math.floor(region.getMaxMbrX() / 16f);
        int minChunkZ = (int) Math.floor(region.getMinMbrZ() / 16f);
        int maxChunkZ = (int) Math.floor(region.getMaxMbrZ() / 16f);
        if (chunk.getX() >= minChunkX && chunk.getX() <= maxChunkX && chunk.getZ() >= minChunkZ && chunk.getZ() <= maxChunkZ) {
            regions.add(region);
        }
    }
    return regions;
}
 
Example 9
Source File: SubCommand_Find.java    From QuickShop-Reremake with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onCommand(
        @NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] cmdArg) {
    if (!(sender instanceof Player)) {
        MsgUtil.sendMessage(sender, MsgUtil.getMessage("Only player can run this command", sender));
        return;
    }

    if (cmdArg.length < 1) {
        MsgUtil.sendMessage(sender, MsgUtil.getMessage("command.no-type-given", sender));
        return;
    }

    final StringBuilder sb = new StringBuilder(cmdArg[0]);

    for (int i = 1; i < cmdArg.length; i++) {
        sb.append(" ").append(cmdArg[i]);
    }

    final String lookFor = sb.toString().toLowerCase();
    final Player p = (Player) sender;
    final Location loc = p.getEyeLocation().clone();
    final double minDistance = plugin.getConfig().getInt("shop.find-distance");
    double minDistanceSquared = minDistance * minDistance;
    final int chunkRadius = (int) minDistance / 16 + 1;
    Shop closest = null;
    CompletableFuture<Chunk> future = new CompletableFuture<>();
    plugin.getBukkitAPIWrapper().getChunkAt(loc.getWorld(), loc, future);
    final Chunk c;
    try {
        c = future.get();
    } catch (Exception asyncErr) {
        MsgUtil.sendMessage(sender, "Cannot execute the command, see console for details.");
        plugin.getSentryErrorReporter().sendError(asyncErr, "Unknown errors");
        plugin.getSentryErrorReporter().ignoreThrow();
        asyncErr.printStackTrace();
        return;
    }
    for (int x = -chunkRadius + c.getX(); x < chunkRadius + c.getX(); x++) {
        for (int z = -chunkRadius + c.getZ(); z < chunkRadius + c.getZ(); z++) {
            final Chunk d = c.getWorld().getChunkAt(x, z);
            final Map<Location, Shop> inChunk = plugin.getShopManager().getShops(d);

            if (inChunk == null) {
                continue;
            }

            for (Shop shop : inChunk.values()) {
                if (!Util.getItemStackName(shop.getItem()).toLowerCase().contains(lookFor)) {
                    continue;
                }

                if (shop.getLocation().distanceSquared(loc) >= minDistanceSquared) {
                    continue;
                }

                closest = shop;
                minDistanceSquared = shop.getLocation().distanceSquared(loc);
            }
        }
    }

    if (closest == null) {
        MsgUtil.sendMessage(sender, MsgUtil.getMessage("no-nearby-shop", sender, cmdArg[0]));
        return;
    }

    final Location lookat = closest.getLocation().clone().add(0.5, 0.5, 0.5);
    // Hack fix to make /qs find not used by /back
    plugin
            .getBukkitAPIWrapper()
            .teleportEntity(
                    p,
                    Util.lookAt(loc, lookat).add(0, -1.62, 0),
                    PlayerTeleportEvent.TeleportCause.UNKNOWN);
    MsgUtil.sendMessage(p,
            MsgUtil.getMessage(
                    "nearby-shop-this-way", sender, Integer.toString((int) Math.floor(Math.sqrt(minDistanceSquared)))));
}
 
Example 10
Source File: GChunk.java    From GlobalWarming with GNU Lesser General Public License v3.0 4 votes vote down vote up
public GChunk(Chunk chunk) {
    this.world = chunk.getWorld().getName();
    this.x = chunk.getX();
    this.z = chunk.getZ();
}
 
Example 11
Source File: ChunkPos.java    From factions-top with MIT License 4 votes vote down vote up
public static ChunkPos of(Chunk chunk) {
    return new ChunkPos(chunk.getWorld().getName(), chunk.getX(), chunk.getZ());
}
 
Example 12
Source File: Regions.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public static Region forChunk(Chunk chunk) {
    Vector min = new Vector(chunk.getX() * 16, 0, chunk.getZ() * 16);
    return new CuboidRegion(min, new Vector(min.getX() + 16, 256, min.getZ() + 16));
}
 
Example 13
Source File: ChunkVector.java    From PGM with GNU Affero General Public License v3.0 4 votes vote down vote up
public static ChunkVector of(Chunk chunk) {
  return new ChunkVector(chunk.getX(), chunk.getZ());
}
 
Example 14
Source File: ChunkVector.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public static ChunkVector of(Chunk chunk) {
    return new ChunkVector(chunk.getX(), chunk.getZ());
}
 
Example 15
Source File: TradeGoodPostGenTask.java    From civcraft with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void run() {
	World world = Bukkit.getWorld("world");
	BlockCoord bcoord2 = new BlockCoord();

	for(int i = 0; i < amount; i++) {
		TradeGoodPick pick = picksQueue.poll();
		if (pick == null) {
			return;
		}
		
		ChunkCoord coord = pick.chunkCoord;
		Chunk chunk = world.getChunkAt(coord.getX(), coord.getZ());
		
		int centerX = (chunk.getX() << 4) + 8;
		int centerZ = (chunk.getZ() << 4) + 8;
		int centerY = world.getHighestBlockYAt(centerX, centerZ);
		
		
		
		bcoord2.setWorldname("world");
		bcoord2.setX(centerX);
		bcoord2.setY(centerY - 1);
		bcoord2.setZ(centerZ);
		
		/* try to detect already existing trade goods. */
		while(true) {
			Block top = world.getBlockAt(bcoord2.getX(), bcoord2.getY(), bcoord2.getZ());
			
			if (!top.getChunk().isLoaded()) {
				top.getChunk().load();
			}
			
			if (ItemManager.getId(top) == CivData.BEDROCK) {
				ItemManager.setTypeId(top, CivData.AIR);
    			ItemManager.setData(top, 0, true);
    			bcoord2.setY(bcoord2.getY() - 1);
    			
    			top = top.getRelative(BlockFace.NORTH);
    			if (ItemManager.getId(top) == CivData.WALL_SIGN) {
    				ItemManager.setTypeId(top, CivData.AIR);
	    			ItemManager.setData(top, 0, true);	    			
	    		}
    			
    			top = top.getRelative(BlockFace.SOUTH);
    			if (ItemManager.getId(top) == CivData.WALL_SIGN) {
    				ItemManager.setTypeId(top, CivData.AIR);
	    			ItemManager.setData(top, 0, true);	    			
	    		}
    			
    			top = top.getRelative(BlockFace.EAST);
    			if (ItemManager.getId(top) == CivData.WALL_SIGN) {
    				ItemManager.setTypeId(top, CivData.AIR);
	    			ItemManager.setData(top, 0, true);	    			
	    		}
    			
    			top = top.getRelative(BlockFace.WEST);
    			if (ItemManager.getId(top) == CivData.WALL_SIGN) {
    				ItemManager.setTypeId(top, CivData.AIR);
	    			ItemManager.setData(top, 0, true);
	    		}
			} else {
				break;
			}
			
		}
		
		centerY = world.getHighestBlockYAt(centerX, centerZ);
		
		// Determine if we should be a water good.
		ConfigTradeGood good;
		if (ItemManager.getBlockTypeIdAt(world, centerX, centerY-1, centerZ) == CivData.WATER || 
			ItemManager.getBlockTypeIdAt(world, centerX, centerY-1, centerZ) == CivData.WATER_RUNNING) {
			good = pick.waterPick;
		}  else {
			good = pick.landPick;
		}
		
		// Randomly choose a land or water good.
		if (good == null) {
			System.out.println("Could not find suitable good type during populate! aborting.");
			continue;
		}
		
		// Create a copy and save it in the global hash table.
		BlockCoord bcoord = new BlockCoord(world.getName(), centerX, centerY, centerZ);
		TradeGoodPopulator.buildTradeGoodie(good, bcoord, world, true);
		
	}
}
 
Example 16
Source File: Region.java    From BedwarsRel with GNU General Public License v3.0 4 votes vote down vote up
public boolean chunkIsInRegion(Chunk chunk) {
  return (chunk.getX() >= this.minCorner.getX() && chunk.getX() <= this.maxCorner.getX()
      && chunk.getZ() >= this.minCorner.getZ() && chunk.getZ() <= this.maxCorner.getZ());
}
 
Example 17
Source File: PlayerMovementHandler.java    From ClaimChunk with MIT License 4 votes vote down vote up
@SuppressWarnings("unused")
@EventHandler
public void onPlayerMove(PlayerMoveEvent e) {
    if (e != null && !e.isCancelled() && e.getTo() != null) {
        // Get the previous and current chunks
        Chunk prev = e.getFrom().getChunk();
        Chunk to = e.getTo().getChunk();

        // Make sure the player moved into a new chunk
        if (prev.getX() != to.getX() || prev.getZ() != to.getZ()) {
            // If the claim is currently autoclaiming, try to claim this chunk
            if (AutoClaimHandler.inList(e.getPlayer())) {
                claimChunk.getCommandHandler().mainHandler.claimChunk(e.getPlayer(), to);
                return;
            }

            ChunkHandler ch = claimChunk.getChunkHandler();

            // Check if the previous chunk was already claimed
            boolean lastClaimed = ch.isClaimed(prev.getWorld(), prev.getX(), prev.getZ());

            // Check if the new chunk is already claimed
            if (ch.isClaimed(to.getWorld(), to.getX(), to.getZ())) {
                // If the new chunk and the previous chunk were claimed, check if the owners differ
                if (lastClaimed) {
                    UUID prevOwner = ch.getOwner(prev.getWorld(), prev.getX(), prev.getZ());
                    UUID newOwner = ch.getOwner(to.getWorld(), to.getX(), to.getZ());

                    // Only display the new chunk's owner if they differ from the previous chunk's owner
                    if ((prevOwner == null && newOwner == null) || (prevOwner != null && !prevOwner.equals(newOwner))) {
                        showTitle(e.getPlayer(), to);
                    }
                } else {
                    // Show the player the chunk's owner
                    showTitle(e.getPlayer(), to);
                }
            } else {
                // The player entered an unclaimed chunk from a claimed chunk
                if (lastClaimed) {
                    UUID lastOwner = ch.getOwner(prev.getWorld(), prev.getX(), prev.getZ());
                    String name = claimChunk.getPlayerHandler().getChunkName(lastOwner);
                    String msg = (e.getPlayer().getUniqueId().equals(lastOwner) ? claimChunk.getMessages().chunkLeaveSelf : claimChunk.getMessages().chunkLeave)
                            .replace("%%PLAYER%%",
                                    ((name == null) ? claimChunk.getMessages().chunkLeaveUnknown : name));
                    Utils.toPlayer(e.getPlayer(), msg);
                }
            }
        }
    }
}
 
Example 18
Source File: GDClaimManager.java    From GriefDefender with MIT License 4 votes vote down vote up
private long getChunkKey(Chunk chunk) {
    return (long) chunk.getX() & 0xffffffffL | ((long) chunk.getZ() & 0xffffffffL) << 32;
}
 
Example 19
Source File: SignsFeature.java    From AreaShop with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Convert a chunk to a string to use as map key.
 * Use a Location argument to prevent chunk loading!
 * @param chunk The location to get the key for
 * @return A string to use in a map for a chunk
 */
public static String chunkToString(Chunk chunk) {
	return chunk.getWorld().getName() + ";" + chunk.getX() + ";" + chunk.getZ();
}
 
Example 20
Source File: ChunkPos.java    From ClaimChunk with MIT License 2 votes vote down vote up
/**
 * Create an instance of a chunk position from Spigot's chunk position
 * representation.
 *
 * @param chunk The Spigot chunk representation.
 */
public ChunkPos(Chunk chunk) {
    this(chunk.getWorld().getName(), chunk.getX(), chunk.getZ());
}