Skip to content

Commit

Permalink
Made the 'c show' command make different regions different colors.
Browse files Browse the repository at this point in the history
  • Loading branch information
briannastatic committed Oct 7, 2024
1 parent f937334 commit ad90b2c
Showing 1 changed file with 50 additions and 42 deletions.
92 changes: 50 additions & 42 deletions src/main/java/com/craftaro/ultimateclaims/tasks/VisualizeTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.craftaro.ultimateclaims.UltimateClaims;
import com.craftaro.ultimateclaims.claim.Claim;
import com.craftaro.ultimateclaims.claim.ClaimManager;
import com.craftaro.ultimateclaims.claim.region.ClaimedRegion;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Color;
Expand All @@ -21,13 +22,30 @@
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

public class VisualizeTask extends BukkitRunnable {
private static VisualizeTask instance;
private static UltimateClaims plugin;
private static final Map<OfflinePlayer, Boolean> ACTIVE = new ConcurrentHashMap<>();
private static final Random RANDOM = new Random();

private static final Color[] COLORS = new Color[]{
Color.fromRGB(0, 255, 0), // Green
Color.fromRGB(0, 0, 255), // Blue
Color.fromRGB(255, 255, 0), // Yellow
Color.fromRGB(255, 0, 255), // Magenta
Color.fromRGB(0, 255, 255), // Cyan
Color.fromRGB(255, 165, 0), // Orange
Color.fromRGB(128, 0, 128), // Purple
Color.fromRGB(128, 128, 0), // Olive
Color.fromRGB(0, 128, 128), // Teal
Color.fromRGB(0, 0, 128) // Navy
};

private static final Map<ClaimedRegion, Color> regionColors = new ConcurrentHashMap<>();
private static final AtomicInteger colorIndex = new AtomicInteger(0);

int radius;

public VisualizeTask(UltimateClaims plug) {
Expand All @@ -41,13 +59,12 @@ public static VisualizeTask startTask(UltimateClaims plug) {
instance = new VisualizeTask(plugin);
instance.runTaskTimerAsynchronously(plugin, 60, 10);
}

return instance;
}

public static boolean togglePlayer(Player p) {
Boolean isActive = ACTIVE.get(p);
ACTIVE.put(p, isActive = (isActive == null || isActive == false));
ACTIVE.put(p, isActive = (isActive == null || !isActive));
return isActive;
}

Expand All @@ -66,92 +83,83 @@ void particleTick(Player player) {
final ClaimManager claimManager = plugin.getClaimManager();
final Location playerLocation = player.getLocation();
final World world = playerLocation.getWorld();
// start and stop chunk coordinates
int startY = playerLocation.getBlockY() + 1;
int cxi = (playerLocation.getBlockX() >> 4) - this.radius, cxn = cxi + this.radius * 2;
int czi = (playerLocation.getBlockZ() >> 4) - this.radius, czn = czi + this.radius * 2;
// loop through the chunks to find applicable ones

for (int cx = cxi; cx < cxn; cx++) {
for (int cz = czi; cz < czn; cz++) {
// sanity check
if (!world.isChunkLoaded(cx, cz)) {
continue;
}

// so! Is this a claimed chunk?
Claim claim = claimManager.getClaim(world.getName(), cx, cz);
if (claim != null) {
// we found one!
// now we get to spawn the silly particles for the player
showChunkParticles(player, world.getChunkAt(cx, cz), startY, claim.isOwnerOrMember(player));
Chunk chunk = world.getChunkAt(cx, cz);
ClaimedRegion region = claim.getClaimedRegion(chunk);

// Assign color to region if not already assigned
Color color = regionColors.computeIfAbsent(region, r -> {
int idx = colorIndex.getAndIncrement() % COLORS.length;
return COLORS[idx];
});

showChunkParticles(player, chunk, startY, color);
}
}
}
}

void showChunkParticles(Player player, Chunk chunk, int startY, boolean canBuild) {
// loop through the chunk
void showChunkParticles(Player player, Chunk chunk, int startY, Color color) {
for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
// show about 1/5 of the blocks per tick
boolean show = RANDOM.nextFloat() < .2;
if (!show) {
if (RANDOM.nextFloat() >= .2) {
continue;
}

// Exclude everything over max height
if (startY >= chunk.getWorld().getMaxHeight()) {
continue;
}

// only show if there is a space to show above a solid block
Block b = chunk.getBlock(x, startY, z);
int maxDown = 8;
boolean show;
do {
show = b.getType().isTransparent() && !(b = b.getRelative(BlockFace.DOWN)).getType().isTransparent();
} while (--maxDown > 0 && !show);

// can we do this?
if (show) {
final Location loc = b.getLocation().add(.5, 1.5, .5);

if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13)) {
if (canBuild) {
player.spawnParticle(Particle.VILLAGER_HAPPY, loc, 0, 0, 0, 0, 1);
} else {
player.spawnParticle(Particle.REDSTONE, loc, 0, 0, 0, 0, 1, new Particle.DustOptions(canBuild ? Color.LIME : Color.RED, 2F));
}
player.spawnParticle(Particle.REDSTONE, loc, 1, 0, 0, 0, 0, new Particle.DustOptions(color, 2F));
} else if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_9)) {
if (canBuild) {
player.spawnParticle(Particle.VILLAGER_HAPPY, loc, 0, 0, 0, 0, 1);
} else {
player.spawnParticle(Particle.REDSTONE, loc, 0, 1.0F, 0.1F, 0.1F, 1.0); // xyz = r b g
}
float red = color.getRed() / 255.0F;
float green = color.getGreen() / 255.0F;
float blue = color.getBlue() / 255.0F;
player.spawnParticle(Particle.REDSTONE, loc, 0, red, green, blue, 1.0);
} else if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_8)) {
try {
Object[] data;
if (canBuild) {
data = new Object[]{loc, Effect.valueOf("HAPPY_VILLAGER"), 20, 2, 0F, 0F, 0F, 0F, 1, 16};
} else {
data = new Object[]{loc, Effect.valueOf("COLOURED_DUST"), 30, 2, 1.0F, 0.1F, 0.1F, 0F, 1, 16};
}
float red = color.getRed() / 255.0F;
float green = color.getGreen() / 255.0F;
float blue = color.getBlue() / 255.0F;
Object[] data = new Object[]{loc, Effect.valueOf("COLOURED_DUST"), 30, 2, red, green, blue, 1.0F, 1, 16};
ReflectionUtils.invokePrivateMethod(player.spigot().getClass(), "playEffect", player.spigot(),
new Class[]{Location.class,
Effect.class,
int.class, //id
int.class, //data
float.class, //offset x
float.class, //offset y
float.class, //offset z
float.class, //speed
int.class, //count
int.class}, //radius, how far the player can be away from loc to see the particles
int.class,
int.class,
float.class,
float.class,
float.class,
float.class,
int.class,
int.class},
data);
} catch (Exception ex) {
ex.printStackTrace();
}
}
// 1.8 requires PacketPlayOutWorldParticles for this. todo?
}
}
}
Expand Down

0 comments on commit ad90b2c

Please sign in to comment.