2014-10-08 06:38:26 +02:00
|
|
|
package com.massivecraft.factions.engine;
|
|
|
|
|
2014-10-08 09:24:03 +02:00
|
|
|
import com.massivecraft.factions.entity.MConf;
|
2014-10-08 06:38:26 +02:00
|
|
|
import com.massivecraft.factions.entity.MPlayer;
|
2016-02-25 22:28:09 +01:00
|
|
|
import com.massivecraft.massivecore.Engine;
|
2014-10-08 06:38:26 +02:00
|
|
|
import com.massivecraft.massivecore.event.EventMassiveCorePlayerLeave;
|
2014-10-08 09:24:03 +02:00
|
|
|
import com.massivecraft.massivecore.particleeffect.ParticleEffect;
|
|
|
|
import com.massivecraft.massivecore.ps.PS;
|
2014-12-03 02:13:39 +01:00
|
|
|
import com.massivecraft.massivecore.util.MUtil;
|
2014-10-08 09:24:03 +02:00
|
|
|
import com.massivecraft.massivecore.util.PeriodUtil;
|
2017-03-24 13:05:58 +01:00
|
|
|
import org.bukkit.Location;
|
|
|
|
import org.bukkit.World;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
import org.bukkit.event.EventPriority;
|
|
|
|
import org.bukkit.event.player.PlayerChangedWorldEvent;
|
|
|
|
|
|
|
|
import java.security.InvalidParameterException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2014-10-08 06:38:26 +02:00
|
|
|
|
2016-02-25 22:28:09 +01:00
|
|
|
public class EngineSeeChunk extends Engine
|
2014-10-08 09:24:03 +02:00
|
|
|
{
|
2014-10-08 06:38:26 +02:00
|
|
|
// -------------------------------------------- //
|
|
|
|
// INSTANCE & CONSTRUCT
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
|
|
|
private static EngineSeeChunk i = new EngineSeeChunk();
|
|
|
|
public static EngineSeeChunk get() { return i; }
|
2016-02-25 22:28:09 +01:00
|
|
|
public EngineSeeChunk()
|
2014-10-08 09:24:03 +02:00
|
|
|
{
|
2016-02-25 22:28:09 +01:00
|
|
|
this.setPeriod(1L);
|
2014-10-08 09:24:03 +02:00
|
|
|
}
|
|
|
|
|
2014-10-08 06:38:26 +02:00
|
|
|
// -------------------------------------------- //
|
|
|
|
// LEAVE AND WORLD CHANGE REMOVAL
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
|
|
|
public static void leaveAndWorldChangeRemoval(Player player)
|
|
|
|
{
|
2015-05-16 14:30:49 +02:00
|
|
|
if (MUtil.isntPlayer(player)) return;
|
|
|
|
|
2014-10-08 06:38:26 +02:00
|
|
|
final MPlayer mplayer = MPlayer.get(player);
|
|
|
|
mplayer.setSeeingChunk(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Can't be cancelled
|
|
|
|
@EventHandler(priority = EventPriority.NORMAL)
|
|
|
|
public void leaveAndWorldChangeRemoval(EventMassiveCorePlayerLeave event)
|
|
|
|
{
|
|
|
|
leaveAndWorldChangeRemoval(event.getPlayer());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Can't be cancelled
|
|
|
|
@EventHandler(priority = EventPriority.NORMAL)
|
|
|
|
public void leaveAndWorldChangeRemoval(PlayerChangedWorldEvent event)
|
|
|
|
{
|
|
|
|
leaveAndWorldChangeRemoval(event.getPlayer());
|
|
|
|
}
|
|
|
|
|
2014-10-08 09:24:03 +02:00
|
|
|
// -------------------------------------------- //
|
|
|
|
// MODULO REPEAT TASK
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
// Do we have a new period?
|
|
|
|
final long now = System.currentTimeMillis();
|
|
|
|
final long length = MConf.get().seeChunkPeriodMillis;
|
|
|
|
if ( ! PeriodUtil.isNewPeriod(this, length, now)) return;
|
|
|
|
|
|
|
|
// Get the period number
|
|
|
|
final long period = PeriodUtil.getPeriod(length, now);
|
|
|
|
|
|
|
|
// Calculate the "step" from the period number
|
|
|
|
final int steps = MConf.get().seeChunkSteps; // Example: 4
|
|
|
|
final int step = (int) (period % steps); // Example: 0, 1, 2, 3
|
|
|
|
|
|
|
|
// Load other related config options
|
2014-10-08 11:19:06 +02:00
|
|
|
final float offsetX = 0.0f;
|
2014-10-08 09:24:03 +02:00
|
|
|
final float offsetY = MConf.get().seeChunkParticleOffsetY;
|
2014-10-08 11:19:06 +02:00
|
|
|
final float offsetZ = 0.0f;
|
2014-10-08 09:24:03 +02:00
|
|
|
final float speed = 0;
|
|
|
|
final int amount = MConf.get().seeChunkParticleAmount;
|
|
|
|
|
|
|
|
// For each player
|
2014-12-03 02:13:39 +01:00
|
|
|
for (Player player : MUtil.getOnlinePlayers())
|
2014-10-08 09:24:03 +02:00
|
|
|
{
|
2014-10-08 09:56:23 +02:00
|
|
|
// Hide for dead players since the death screen looks better without.
|
|
|
|
if (player.isDead()) continue;
|
|
|
|
|
|
|
|
// The player must obviously have the feature activated.
|
2014-10-08 09:24:03 +02:00
|
|
|
MPlayer mplayer = MPlayer.get(player);
|
|
|
|
if ( ! mplayer.isSeeingChunk()) continue;
|
|
|
|
|
2014-10-08 09:56:23 +02:00
|
|
|
// Calculate locations and play the effect there.
|
2014-10-08 09:24:03 +02:00
|
|
|
List<Location> locations = getLocations(player, steps, step);
|
|
|
|
for (Location location : locations)
|
|
|
|
{
|
2014-12-12 10:28:36 +01:00
|
|
|
ParticleEffect.EXPLOSION_NORMAL.display(location, offsetX, offsetY, offsetZ, speed, amount, player);
|
2014-10-08 09:24:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static List<Location> getLocations(Player player, int steps, int step)
|
|
|
|
{
|
|
|
|
// Clean Args
|
|
|
|
if (player == null) throw new NullPointerException("player");
|
|
|
|
if (steps < 1) throw new InvalidParameterException("steps must be larger than 0");
|
|
|
|
if (step < 0) throw new InvalidParameterException("step must at least be 0");
|
|
|
|
if (step >= steps) throw new InvalidParameterException("step must be less than steps");
|
|
|
|
|
|
|
|
// Create Ret
|
2017-03-24 14:03:29 +01:00
|
|
|
List<Location> ret = new ArrayList<>();
|
2014-10-08 09:24:03 +02:00
|
|
|
|
|
|
|
final Location location = player.getLocation();
|
|
|
|
final World world = location.getWorld();
|
|
|
|
PS chunk = PS.valueOf(location).getChunk(true);
|
|
|
|
|
|
|
|
final int xmin = chunk.getChunkX() * 16;
|
|
|
|
final int xmax = xmin + 15;
|
|
|
|
final double y = location.getBlockY() + MConf.get().seeChunkParticleDeltaY;
|
|
|
|
final int zmin = chunk.getChunkZ() * 16;
|
|
|
|
final int zmax = zmin + 15;
|
|
|
|
|
2014-10-08 11:19:06 +02:00
|
|
|
int keepEvery = MConf.get().seeChunkKeepEvery;
|
|
|
|
if (keepEvery <= 0) keepEvery = Integer.MAX_VALUE;
|
|
|
|
|
|
|
|
int skipEvery = MConf.get().seeChunkSkipEvery;
|
|
|
|
if (skipEvery <= 0) skipEvery = Integer.MAX_VALUE;
|
|
|
|
|
2014-10-08 09:24:03 +02:00
|
|
|
int x = xmin;
|
|
|
|
int z = zmin;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
// Add #1
|
|
|
|
while (x + 1 <= xmax)
|
|
|
|
{
|
|
|
|
x++;
|
|
|
|
i++;
|
2014-10-08 11:19:06 +02:00
|
|
|
if (i % steps == step && (i % keepEvery == 0 && i % skipEvery != 0)) ret.add(new Location(world, x + 0.5, y + 0.5, z + 0.5));
|
2014-10-08 09:24:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add #2
|
|
|
|
while (z + 1 <= zmax)
|
|
|
|
{
|
|
|
|
z++;
|
|
|
|
i++;
|
2014-10-08 11:19:06 +02:00
|
|
|
if (i % steps == step && (i % keepEvery == 0 && i % skipEvery != 0)) ret.add(new Location(world, x + 0.5, y + 0.5, z + 0.5));
|
2014-10-08 09:24:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add #3
|
|
|
|
while (x - 1 >= xmin)
|
|
|
|
{
|
|
|
|
x--;
|
|
|
|
i++;
|
2014-10-08 11:19:06 +02:00
|
|
|
if (i % steps == step && (i % keepEvery == 0 && i % skipEvery != 0)) ret.add(new Location(world, x + 0.5, y + 0.5, z + 0.5));
|
2014-10-08 09:24:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add #4
|
|
|
|
while (z - 1 >= zmin)
|
|
|
|
{
|
|
|
|
z--;
|
|
|
|
i++;
|
2014-10-08 11:19:06 +02:00
|
|
|
if (i % steps == step && (i % keepEvery == 0 && i % skipEvery != 0)) ret.add(new Location(world, x + 0.5, y + 0.5, z + 0.5));
|
2014-10-08 09:24:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return Ret
|
|
|
|
return ret;
|
|
|
|
}
|
2014-10-08 06:38:26 +02:00
|
|
|
}
|