use of org.bukkit.Chunk in project Denizen-For-Bukkit by DenizenScript.
the class ChunkLoadCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag action = scriptEntry.getElement("action");
ListTag chunklocs = scriptEntry.getObjectTag("location");
DurationTag length = scriptEntry.getObjectTag("duration");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), action, chunklocs, length);
}
for (String chunkText : chunklocs) {
Chunk chunk;
if (ChunkTag.matches(chunkText)) {
chunk = ChunkTag.valueOf(chunkText, scriptEntry.context).getChunk();
} else if (LocationTag.matches(chunkText)) {
chunk = LocationTag.valueOf(chunkText, scriptEntry.context).getChunk();
} else {
Debug.echoError("Chunk input '" + chunkText + "' is invalid.");
return;
}
ChunkCoordinate coord = new ChunkCoordinate(chunk);
switch(Action.valueOf(action.asString())) {
case ADD:
if (length.getSeconds() != 0) {
chunkDelays.put(coord, System.currentTimeMillis() + length.getMillis());
} else {
chunkDelays.put(coord, (long) 0);
}
Debug.echoDebug(scriptEntry, "...added chunk " + chunk.getX() + ", " + chunk.getZ() + " with a delay of " + length.getSeconds() + " seconds.");
if (!chunk.isLoaded()) {
chunk.load();
}
chunk.addPluginChunkTicket(Denizen.getInstance());
if (length.getSeconds() > 0) {
Bukkit.getScheduler().scheduleSyncDelayedTask(Denizen.getInstance(), () -> {
if (chunkDelays.containsKey(coord) && chunkDelays.get(coord) <= System.currentTimeMillis()) {
chunk.removePluginChunkTicket(Denizen.getInstance());
chunkDelays.remove(coord);
}
}, length.getTicks() + 20);
}
break;
case REMOVE:
if (chunkDelays.containsKey(coord)) {
chunkDelays.remove(coord);
chunk.removePluginChunkTicket(Denizen.getInstance());
Debug.echoDebug(scriptEntry, "...allowing unloading of chunk " + chunk.getX() + ", " + chunk.getZ());
} else {
Debug.echoDebug(scriptEntry, "Chunk '" + coord + "' was not on the load list, ignoring.");
}
break;
case REMOVEALL:
Debug.echoDebug(scriptEntry, "...allowing unloading of all stored chunks");
for (ChunkCoordinate loopCoord : chunkDelays.keySet()) {
loopCoord.getChunk().getChunk().removePluginChunkTicket(Denizen.getInstance());
}
chunkDelays.clear();
break;
}
}
}
use of org.bukkit.Chunk in project Prism-Bukkit by prism.
the class RadiusParameter method process.
@Override
public void process(QueryParameters query, String alias, String input, CommandSender sender) {
Player player = null;
if (sender instanceof Player) {
player = (Player) sender;
}
String inputValue = input;
final FileConfiguration config = Prism.config;
if (TypeUtils.isNumeric(inputValue) || (inputValue.contains(":") && inputValue.split(":").length >= 1 && TypeUtils.isNumeric(inputValue.split(":")[1]))) {
Location coordsLoc = null;
int desiredRadius;
if (inputValue.contains(":")) {
desiredRadius = Integer.parseInt(inputValue.split(":")[1]);
final String radiusLocOrPlayer = inputValue.split(":")[0];
if (radiusLocOrPlayer.contains(",") && player != null) {
// Coordinates;
// x,y,z
final String[] coordinates = radiusLocOrPlayer.split(",");
if (coordinates.length != 3) {
throw new IllegalArgumentException("Couldn't parse the coordinates '" + radiusLocOrPlayer + "'. Perhaps you have more than two commas?");
}
for (final String s : coordinates) {
if (!TypeUtils.isNumeric(s)) {
throw new IllegalArgumentException("The coordinate '" + s + "' is not a number.");
}
}
coordsLoc = (new Location(player.getWorld(), Integer.parseInt(coordinates[0]), Integer.parseInt(coordinates[1]), Integer.parseInt(coordinates[2])));
} else {
// Try to find an online player
Player p2 = Bukkit.getServer().getPlayer(radiusLocOrPlayer);
if (p2 == null) {
throw new IllegalArgumentException("Couldn't find the player named '" + radiusLocOrPlayer + "'. Perhaps they are not online or you misspelled their name?");
}
player = p2;
}
} else {
desiredRadius = Integer.parseInt(inputValue);
}
if (desiredRadius <= 0) {
throw new IllegalArgumentException("Radius must be greater than zero. Or leave it off to use the default. Use /prism ? for help.");
}
// If neither sender or a named player found, die here
if (player == null) {
throw new IllegalArgumentException("The radius parameter must be used by a player. Use w:worldname if attempting " + "to limit to a world.");
}
// Clamp radius based on perms, configs
int radius = MiscUtils.clampRadius(player, desiredRadius, query.getProcessType(), config);
if (desiredRadius != radius) {
if (sender != null) {
Prism.messenger.sendMessage(sender, Prism.messenger.playerError("Forcing radius to " + radius + " as allowed by config."));
}
}
if (radius > 0) {
query.setRadius(radius);
if (coordsLoc != null) {
// We
query.setMinMaxVectorsFromPlayerLocation(coordsLoc);
// need
// to
// set
// this
// *after*
// the
// radius
// has
// been
// set
// or
// it
// won't
// work.
} else {
query.setMinMaxVectorsFromPlayerLocation(player.getLocation());
}
}
} else {
// If neither sender or a named player found, die here
if (player == null) {
throw new IllegalArgumentException("The radius parameter must be used by a player. " + "Use w:worldname if attempting to limit to a world.");
}
// User wants an area inside of a worldedit selection
switch(inputValue) {
case "we":
if (ApiHandler.worldEditPlugin == null) {
throw new IllegalArgumentException("This feature is disabled because Prism couldn't find WorldEdit.");
} else {
// Load a selection from world edit as our area.
if (!WorldEditBridge.getSelectedArea(Prism.getInstance(), player, query)) {
throw new IllegalArgumentException("Invalid region selected. Make sure you have a region selected," + " and that it doesn't exceed the max radius.");
}
}
break;
// Confine to the chunk
case "c":
case "chunk":
final Chunk ch = player.getLocation().getChunk();
query.setWorld(ch.getWorld().getName());
query.setMinLocation(ChunkUtils.getChunkMinVector(ch));
query.setMaxLocation(ChunkUtils.getChunkMaxVector(ch));
break;
// User wants no radius, but contained within the current world
case "world":
// Do they have permission to override the global lookup radius
if (query.getProcessType().equals(PrismProcessType.LOOKUP) && !player.hasPermission("prism.override-max-lookup-radius")) {
throw new IllegalArgumentException("You do not have permission to override the max radius.");
}
// Do they have permission to override the global applier radius
if (!query.getProcessType().equals(PrismProcessType.LOOKUP) && !player.hasPermission("prism.override-max-applier-radius")) {
throw new IllegalArgumentException("You do not have permission to override the max radius.");
}
// Use the world defined in the w: param
if (query.getWorld() != null) {
inputValue = query.getWorld();
} else {
inputValue = player.getWorld().getName();
}
query.setWorld(inputValue);
query.setAllowNoRadius(true);
break;
// User has asked for a global radius
case "global":
// Do they have permission to override the global lookup radius
if (query.getProcessType().equals(PrismProcessType.LOOKUP) && !player.hasPermission("prism.override-max-lookup-radius")) {
throw new IllegalArgumentException("You do not have permission to override the max radius.");
}
// Do they have permission to override the global applier radius
if (!query.getProcessType().equals(PrismProcessType.LOOKUP) && !player.hasPermission("prism.override-max-applier-radius")) {
throw new IllegalArgumentException("You do not have permission to override the max radius.");
}
// Either they have permission or player is null
query.setWorld(null);
query.setAllowNoRadius(true);
break;
default:
throw new IllegalArgumentException("Radius is invalid. There's a bunch of choice, so use /prism actions for assistance.");
}
}
}
use of org.bukkit.Chunk in project BKCommonLib by bergerhealer.
the class EntityAddRemoveHandler_1_14_to_1_16_5 method replace.
@Override
public void replace(EntityHandle oldEntity, EntityHandle newEntity) {
WorldServerHandle world = oldEntity.getWorldServer();
if (newEntity == null) {
if (world != null) {
world.removeEntity(oldEntity);
world.getEntityTracker().stopTracking(oldEntity.getBukkitEntity());
}
// Works fine, no need to clean up any more
return;
}
Object worldHandle = world.getRaw();
// *** Remove from the entities to add queue ***
Queue<Object> entitiesToAdd = this.entitiesToAddField.get(oldEntity.getWorld().getRaw());
entitiesToAdd.remove(oldEntity.getRaw());
// *** Entities By UUID Map ***
{
Map<UUID, Object> entitiesByUUID = this.entitiesByUUIDField.get(worldHandle);
Object storedEntityHandle = entitiesByUUID.get(oldEntity.getUniqueID());
if (storedEntityHandle != null && storedEntityHandle != newEntity.getRaw()) {
if (!oldEntity.getUniqueID().equals(newEntity.getUniqueID())) {
entitiesByUUID.remove(oldEntity.getUniqueID());
}
entitiesByUUID.put(newEntity.getUniqueID(), newEntity.getRaw());
}
}
// *** Entities by Id Map ***
{
IntHashMapHandle entitiesById = IntHashMapHandle.createHandle(this.entitiesByIdField.get(worldHandle));
Object storedEntityHandle = entitiesById.get(oldEntity.getIdField());
if (storedEntityHandle != null && storedEntityHandle != newEntity.getRaw()) {
if (oldEntity.getIdField() != newEntity.getIdField()) {
entitiesById.remove(oldEntity.getIdField());
}
entitiesById.put(newEntity.getIdField(), newEntity.getRaw());
}
}
// *** Tuinity WorldServer EntityList field ***
if (tuinitySwapEntityInWorldEntityListMethod.isAvailable()) {
tuinitySwapEntityInWorldEntityListMethod.invoke(worldHandle, oldEntity.getRaw(), newEntity.getRaw());
}
// *** Tuinity WorldServer entitiesForIteration field ***
if (tuinitySwapEntityInWorldEntityIterationSetMethod.isAvailable()) {
tuinitySwapEntityInWorldEntityIterationSetMethod.invoke(worldHandle, oldEntity.getRaw(), newEntity.getRaw());
}
// *** EntityTrackerEntry ***
replaceInEntityTracker(oldEntity, oldEntity, newEntity);
if (oldEntity.getVehicle() != null) {
replaceInEntityTracker(oldEntity.getVehicle(), oldEntity, newEntity);
}
if (oldEntity.getPassengers() != null) {
for (EntityHandle passenger : oldEntity.getPassengers()) {
replaceInEntityTracker(passenger, oldEntity, newEntity);
}
}
// *** Entity Current Chunk ***
final int chunkX = newEntity.getChunkX();
final int chunkZ = newEntity.getChunkZ();
PlayerChunkMapHandle playerChunks = WorldServerHandle.T.getPlayerChunkMap.invoke(worldHandle);
Chunk loadedChunk = WorldUtil.getChunk(newEntity.getBukkitWorld(), chunkX, chunkZ);
if (loadedChunk != null) {
replaceInChunk(loadedChunk, oldEntity, newEntity);
} else {
// Chunk isn't loaded at this time. This gets difficult!
// It might still be in the updating chunks mapping
PlayerChunkHandle updatingChunk = playerChunks.getUpdatingChunk(chunkX, chunkZ);
Chunk loadedUpdatingChunk = (updatingChunk == null) ? null : updatingChunk.getChunkIfLoaded();
if (loadedUpdatingChunk == null && updatingChunk != null) {
// Try hard time! This allows any status the chunk is in.
loadedUpdatingChunk = PlayerChunkHandle.T.opt_getChunkTryHard_1_14.invoke(updatingChunk.getRaw());
}
// Let's go!
replaceInChunk(loadedUpdatingChunk, oldEntity, newEntity);
}
// See where the object is still referenced to check we aren't missing any places to replace
// This is SLOW, do not ever have this enabled on a release version!
// com.bergerkiller.bukkit.common.utils.DebugUtil.logInstances(oldEntity.getRaw());
}
use of org.bukkit.Chunk in project BKCommonLib by bergerhealer.
the class RegionHandler_CubicChunks_1_12_2 method getRegions3.
@Override
public Set<IntVector3> getRegions3(World world) {
// First try using the ICubicStorage API, if available
{
Object worldHandle = HandleConversion.toWorldHandle(world);
Object chunkProviderServer = WorldServerHandle.T.getChunkProviderServer.raw.invoke(worldHandle);
final Set<IntVector3> regionIndices = new HashSet<>();
if (handle.forEachCube(chunkProviderServer, wrap(cubeCoordinate -> {
regionIndices.add(new IntVector3(cubeCoordinate.x >> 5, cubeCoordinate.y >> 5, cubeCoordinate.z >> 5));
}))) {
return regionIndices;
}
}
// Fallback for older CubicChunks versions
// Obtain the coordinates using the files stored on disk
Set<IntVector3> regionIndices = getWorldRegionFileCoordinates(world, c -> true);
// Look at all loaded chunks and their cubes of the world and add the regions they are inside of
for (Chunk chunk : world.getLoadedChunks()) {
IntVector2 region = new IntVector2(chunk.getX() >> 5, chunk.getZ() >> 5);
List<Integer> cubes_y = handle.getLoadedCubesY(HandleConversion.toChunkHandle(chunk));
for (Integer y : cubes_y) {
regionIndices.add(region.toIntVector3(y.intValue() >> 5));
}
}
return regionIndices;
}
use of org.bukkit.Chunk in project BKCommonLib by bergerhealer.
the class RegionHandler_CubicChunks_1_12_2 method getRegions3ForXZ.
@Override
public Set<IntVector3> getRegions3ForXZ(World world, Set<IntVector2> regionXZCoordinates) {
// First try using the ICubicStorage API, if available
{
Object worldHandle = HandleConversion.toWorldHandle(world);
Object chunkProviderServer = WorldServerHandle.T.getChunkProviderServer.raw.invoke(worldHandle);
final Set<IntVector3> regionIndices = new HashSet<>();
if (handle.forEachCube(chunkProviderServer, wrap(cubeCoordinate -> {
IntVector2 regionXZ = new IntVector2(cubeCoordinate.x >> 5, cubeCoordinate.z >> 5);
if (regionXZCoordinates.contains(regionXZ)) {
regionIndices.add(regionXZ.toIntVector3(cubeCoordinate.y >> 5));
}
}))) {
return regionIndices;
}
}
// Obtain the coordinates using the files stored on disk
Set<IntVector3> regionIndices = getWorldRegionFileCoordinates(world, c -> {
return regionXZCoordinates.contains(c.toIntVector2());
});
// Look at all loaded chunks and their cubes of the world and add the regions they are inside of
for (Chunk chunk : world.getLoadedChunks()) {
// Check region is filtered
IntVector2 region = new IntVector2(chunk.getX() >> 5, chunk.getZ() >> 5);
if (!regionXZCoordinates.contains(region)) {
continue;
}
List<Integer> cubes_y = handle.getLoadedCubesY(HandleConversion.toChunkHandle(chunk));
for (Integer y : cubes_y) {
regionIndices.add(region.toIntVector3(y.intValue() >> 5));
}
}
return regionIndices;
}
Aggregations