Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change border smoke color based on affiliation #6219

Merged
merged 2 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/com/palmergames/bukkit/towny/TownyTimerHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public static void toggleCooldownTimer(boolean on) {

public static void toggleDrawSmokeTask(boolean on) {
if (on && !isDrawSmokeTaskRunning()) {
drawSmokeTask = BukkitTools.scheduleAsyncRepeatingTask(new DrawSmokeTask(plugin), 0, 100);
drawSmokeTask = BukkitTools.scheduleAsyncRepeatingTask(new DrawSmokeTask(plugin), 0, 40);
if (drawSmokeTask == -1)
TownyMessaging.sendErrorMsg("Could not schedule draw smoke loop");
} else if (!on && isDrawSmokeTaskRunning()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ public void onPlayerChangePlotEvent(PlayerChangePlotEvent event) {
}
if (resident.hasMode("map"))
TownyCommand.showMap(player);
if (resident.hasMode("plotborder"))
BorderUtil.getPlotBorder(to).runBorderedOnSurface(1, 2, DrawSmokeTaskFactory.sendToPlayer(player));
if (resident.hasMode("plotborder") || resident.hasMode("constantplotborder"))
BorderUtil.getPlotBorder(to).runBorderedOnSurface(1, 2, DrawSmokeTaskFactory.showToPlayer(player, to));

// Check if player has entered a new town/wilderness
if (event.isShowingPlotNotifications()) {
Expand Down
28 changes: 19 additions & 9 deletions src/com/palmergames/bukkit/towny/object/CellBorder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import com.palmergames.bukkit.util.DrawUtil;
import com.palmergames.bukkit.util.LocationRunnable;
import org.bukkit.Location;
import org.bukkit.World;

import java.util.Arrays;
import java.util.function.Consumer;

public class CellBorder extends WorldCoord {

Expand Down Expand Up @@ -75,7 +77,7 @@ public int getBlockZ() {
return getZ() * getCellSize();
}

public void runBorderedOnSurface(int wallHeight, int cornerHeight, LocationRunnable runnable) {
public void runBorderedOnSurface(int wallHeight, int cornerHeight, Consumer<Location> locationConsumer) {

int x = getBlockX(); // positive x is east, negative x is west
int z = getBlockZ(); // positive z is south, negative z is north
Expand All @@ -87,35 +89,43 @@ public void runBorderedOnSurface(int wallHeight, int cornerHeight, LocationRunna
if ((section.getType() == Section.Type.WALL && wallHeight > 0) || section.getType() == Section.Type.CORNER && cornerHeight > 0) {
switch (section) {
case N:
DrawUtil.runOnSurface(world, x, z, x, z + w, wallHeight, runnable);
DrawUtil.runOnSurface(world, x, z, x, z + w, wallHeight, locationConsumer);
break;
case NE:
DrawUtil.runOnSurface(world, x, z, x, z, cornerHeight, runnable);
DrawUtil.runOnSurface(world, x, z, x, z, cornerHeight, locationConsumer);
break;
case E:
DrawUtil.runOnSurface(world, x, z, x + w, z, wallHeight, runnable);
DrawUtil.runOnSurface(world, x, z, x + w, z, wallHeight, locationConsumer);
break;
case SE:
DrawUtil.runOnSurface(world, x + w, z, x + w, z, cornerHeight, runnable);
DrawUtil.runOnSurface(world, x + w, z, x + w, z, cornerHeight, locationConsumer);
break;
case S:
DrawUtil.runOnSurface(world, x + w, z, x + w, z + w, wallHeight, runnable);
DrawUtil.runOnSurface(world, x + w, z, x + w, z + w, wallHeight, locationConsumer);
break;
case SW:
DrawUtil.runOnSurface(world, x + w, z + w, x + w, z + w, cornerHeight, runnable);
DrawUtil.runOnSurface(world, x + w, z + w, x + w, z + w, cornerHeight, locationConsumer);
break;
case W:
DrawUtil.runOnSurface(world, x, z + w, x + w, z + w, wallHeight, runnable);
DrawUtil.runOnSurface(world, x, z + w, x + w, z + w, wallHeight, locationConsumer);
break;
case NW:
DrawUtil.runOnSurface(world, x, z + w, x, z + w, cornerHeight, runnable);
DrawUtil.runOnSurface(world, x, z + w, x, z + w, cornerHeight, locationConsumer);
break;
default:
}
}
}
}
}

/**
* @deprecated Deprecated as of 0.98.3.13, please use {@link #runBorderedOnSurface(int, int, Consumer)} instead.
*/
@Deprecated
public void runBorderedOnSurface(int wallHeight, int cornerHeight, LocationRunnable runnable) {
runBorderedOnSurface(wallHeight, cornerHeight, (Consumer<Location>) runnable::run);
}

@Override
public String toString() {
Expand Down
29 changes: 14 additions & 15 deletions src/com/palmergames/bukkit/towny/tasks/DrawSmokeTask.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
package com.palmergames.bukkit.towny.tasks;

import java.util.Collection;

import com.palmergames.bukkit.towny.TownyAPI;
import com.palmergames.bukkit.towny.object.Resident;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import com.palmergames.bukkit.towny.Towny;
import com.palmergames.bukkit.towny.object.CellBorder;
import com.palmergames.bukkit.towny.object.Coord;
import com.palmergames.bukkit.towny.object.WorldCoord;
import com.palmergames.bukkit.towny.utils.BorderUtil;
import com.palmergames.bukkit.util.BukkitTools;
import com.palmergames.bukkit.util.DrawSmokeTaskFactory;

public class DrawSmokeTask extends TownyTimerTask{
public class DrawSmokeTask extends TownyTimerTask {

public DrawSmokeTask(Towny plugin) {

super(plugin);
}

@Override
public void run() {

Collection<? extends Player> players = BukkitTools.getOnlinePlayers();

for (Player player: players) {
if (plugin.hasPlayerMode(player, "constantplotborder")) {
WorldCoord wc = new WorldCoord(player.getWorld().getName(), Coord.parseCoord(player.getLocation()));
CellBorder cellBorder = BorderUtil.getPlotBorder(wc);
cellBorder.runBorderedOnSurface(1, 2, DrawSmokeTaskFactory.sendToPlayer(player));
}
for (Player player : Bukkit.getOnlinePlayers()) {
Resident resident = TownyAPI.getInstance().getResident(player);
if (resident == null || !resident.hasMode("constantplotborder"))
continue;

WorldCoord wc = new WorldCoord(player.getWorld().getName(), Coord.parseCoord(player.getLocation()));
CellBorder cellBorder = BorderUtil.getPlotBorder(wc);

cellBorder.runBorderedOnSurface(1, 2, DrawSmokeTaskFactory.showToPlayer(player, DrawSmokeTaskFactory.getAffiliationColor(resident, wc)));
}
}
}
}
54 changes: 43 additions & 11 deletions src/com/palmergames/bukkit/util/DrawSmokeTaskFactory.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package com.palmergames.bukkit.util;

import com.palmergames.bukkit.towny.TownyAPI;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.WorldCoord;
import com.palmergames.bukkit.towny.utils.CombatUtil;
import org.bukkit.Color;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull;

import java.util.function.Consumer;

/**
* @author Chris H (Zren / Shade)
Expand All @@ -16,17 +24,41 @@ public class DrawSmokeTaskFactory {
*
* @param player - {@link Player} to send smoke effect
* @return {@link LocationRunnable}
* @deprecated Deprecated as of 0.98.3.13, please use {@link #showToPlayer(Player)} instead.
*/
@Deprecated
public static LocationRunnable sendToPlayer(final Player player) {
return new LocationRunnable() {
Vector offset = new Vector(0.5, 1.5, 0.5);

@Override
public void run(Location loc) {
// Considering changing from Smoke to this Green coloured particle. It is difficult to see however.
// Colours could be changed to differentiate between friendly and enemy lands, but we'd have to be passing colour data.
player.spawnParticle(Particle.SMOKE_NORMAL, loc.add(offset), 5,0,0,0,0);
}
};
return location -> player.spawnParticle(Particle.SMOKE_NORMAL, location.add(0.5, 1.5, 0.5), 5, 0, 0, 0, 0);
}

public static Consumer<Location> showToPlayer(@NotNull Player player) {
return showToPlayer(player, WorldCoord.parseWorldCoord(player));
}

public static Consumer<Location> showToPlayer(@NotNull Player player, @NotNull WorldCoord worldCoord) {
Resident resident = TownyAPI.getInstance().getResident(player);

return showToPlayer(player, resident == null ? Color.GRAY : getAffiliationColor(resident, worldCoord));
}

public static Consumer<Location> showToPlayer(@NotNull Player player, @NotNull Color particleColor) {
final Particle.DustOptions dustOptions = new Particle.DustOptions(particleColor, 2);

return location -> player.spawnParticle(Particle.REDSTONE, location.add(0.5, 1.5, 0.5), 5, dustOptions);
}

public static Color getAffiliationColor(Resident resident, WorldCoord coord) {
Town residentTown = resident.getTownOrNull();
Town town = coord.getTownOrNull();

if (residentTown == null || town == null)
return Color.GRAY;

if (CombatUtil.isAlly(residentTown, town))
return Color.GREEN;
else if (CombatUtil.isEnemy(residentTown, town))
return Color.RED;
else
return Color.GRAY;
}
}
28 changes: 19 additions & 9 deletions src/com/palmergames/bukkit/util/DrawUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.bukkit.Location;
import org.bukkit.World;

import java.util.function.Consumer;

/**
* @author Chris H (Zren / Shade)
* Date: 4/15/12
Expand All @@ -13,15 +15,15 @@ public class DrawUtil {
* Run a runnable over the surface of the specified rectangular area. From
* the ground up.
*
* @param world - {@link World}
* @param x1 - X-Coordinate 1 ({@link Integer})
* @param z1 - Z-Coordinate 1 ({@link Integer})
* @param x2 - X-Coordinate 2 ({@link Integer})
* @param z2 - Z-Coordinate 2 ({@link Integer})
* @param height - Y-Coordinate at call ({@link Integer})
* @param runnable - {@link LocationRunnable}
* @param world {@link World}
* @param x1 X-Coordinate 1 ({@link Integer})
* @param z1 Z-Coordinate 1 ({@link Integer})
* @param x2 X-Coordinate 2 ({@link Integer})
* @param z2 Z-Coordinate 2 ({@link Integer})
* @param height Y-Coordinate at call ({@link Integer})
* @param locationConsumer A consumer for accepting locations
*/
public static void runOnSurface(World world, int x1, int z1, int x2, int z2, int height, LocationRunnable runnable) {
public static void runOnSurface(World world, int x1, int z1, int x2, int z2, int height, Consumer<Location> locationConsumer) {

int _x1 = Math.min(x1, x2);
int _x2 = Math.max(x1, x2);
Expand All @@ -33,9 +35,17 @@ public static void runOnSurface(World world, int x1, int z1, int x2, int z2, int
int start = world.getHighestBlockYAt(x, z);
int end = (start + height) < world.getMaxHeight() ? (start + height - 1) : world.getMaxHeight();
for (int y = start; y <= end; y++) {
runnable.run(new Location(world, x, y, z));
locationConsumer.accept(new Location(world, x, y, z));
}
}
}
}

/**
* @deprecated Deprecated as of 0.98.3.13, please use {@link #runOnSurface(World, int, int, int, int, int, Consumer)} instead.
*/
@Deprecated
public static void runOnSurface(World world, int x1, int z1, int x2, int z2, int height, LocationRunnable runnable) {
runOnSurface(world, x1, z1, x2, z2, height, (Consumer<Location>) runnable::run);
}
}
2 changes: 2 additions & 0 deletions src/com/palmergames/bukkit/util/LocationRunnable.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
/**
* @author Chris H (Zren / Shade)
* Date: 4/15/12
* @deprecated Deprecated as of 0.98.3.13, please use a {@link java.util.function.Consumer} instead.
*/
@Deprecated
public interface LocationRunnable {

void run(Location loc);
Expand Down