Added basic gate block sending mechanism.
This commit is contained in:
parent
e5ef266f04
commit
d23a9b7650
@ -1,8 +1,7 @@
|
||||
package de.craftinc.gates.gates;
|
||||
package de.craftinc.gates;
|
||||
|
||||
import de.craftinc.gates.Plugin;
|
||||
import de.craftinc.gates.util.FloodUtil;
|
||||
import de.craftinc.gates.gates.persistence.LocationUtil;
|
||||
import de.craftinc.gates.persistence.LocationUtil;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
@ -1,4 +1,4 @@
|
||||
package de.craftinc.gates.gates;
|
||||
package de.craftinc.gates;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
@ -7,8 +7,7 @@ import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import de.craftinc.gates.Plugin;
|
||||
import de.craftinc.gates.gates.persistence.MigrationUtil;
|
||||
import de.craftinc.gates.persistence.MigrationUtil;
|
||||
import org.bukkit.Chunk;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
@ -5,14 +5,13 @@ import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
|
||||
import de.craftinc.gates.gates.Gate;
|
||||
import de.craftinc.gates.listeners.PlayerMoveListener;
|
||||
import de.craftinc.gates.gates.GatesManager;
|
||||
import de.craftinc.gates.listeners.*;
|
||||
import net.milkbowl.vault.permission.Permission;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerialization;
|
||||
import org.bukkit.event.player.PlayerChangedWorldEvent;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
@ -29,10 +28,15 @@ public class Plugin extends JavaPlugin
|
||||
private static Plugin instance;
|
||||
private static Permission permission;
|
||||
|
||||
private String baseCommand;
|
||||
private PlayerMoveListener playerListener = new PlayerMoveListener();
|
||||
private List<BaseCommand> commands = new ArrayList<BaseCommand>();
|
||||
private GatesManager gatesManager = new GatesManager();
|
||||
protected String baseCommand;
|
||||
protected List<BaseCommand> commands = new ArrayList<BaseCommand>();
|
||||
protected GatesManager gatesManager = new GatesManager();
|
||||
|
||||
protected PlayerMoveListener moveListener = new PlayerMoveListener();
|
||||
protected PlayerTeleportListener teleportListener = new PlayerTeleportListener();
|
||||
protected PlayerRespawnListener respawnListener = new PlayerRespawnListener();
|
||||
protected PlayerChangedWorldListener worldChangeListener = new PlayerChangedWorldListener();
|
||||
protected PlayerJoinListener joinListener = new PlayerJoinListener();
|
||||
|
||||
|
||||
public Plugin()
|
||||
@ -60,7 +64,7 @@ public class Plugin extends JavaPlugin
|
||||
}
|
||||
|
||||
|
||||
private void setupPermissions()
|
||||
protected void setupPermissions()
|
||||
{
|
||||
if (getServer().getPluginManager().getPlugin("Vault") == null) {
|
||||
return;
|
||||
@ -112,8 +116,7 @@ public class Plugin extends JavaPlugin
|
||||
|
||||
|
||||
// Register events
|
||||
PluginManager pm = this.getServer().getPluginManager();
|
||||
pm.registerEvents(this.playerListener, this);
|
||||
this.registerEventListeners();
|
||||
|
||||
// Load gates
|
||||
gatesManager.loadGatesFromDisk();
|
||||
@ -122,6 +125,19 @@ public class Plugin extends JavaPlugin
|
||||
}
|
||||
|
||||
|
||||
protected void registerEventListeners()
|
||||
{
|
||||
PluginManager pm = this.getServer().getPluginManager();
|
||||
|
||||
pm.registerEvents(this.moveListener, this);
|
||||
pm.registerEvents(this.teleportListener, this);
|
||||
pm.registerEvents(this.respawnListener, this);
|
||||
pm.registerEvents(this.worldChangeListener, this);
|
||||
pm.registerEvents(this.joinListener, this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// -------------------------------------------- //
|
||||
// Commands
|
||||
// -------------------------------------------- //
|
||||
|
@ -7,8 +7,8 @@ import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import de.craftinc.gates.gates.Gate;
|
||||
import de.craftinc.gates.gates.GatesManager;
|
||||
import de.craftinc.gates.Gate;
|
||||
import de.craftinc.gates.GatesManager;
|
||||
import de.craftinc.gates.Plugin;
|
||||
import de.craftinc.gates.util.TextUtil;
|
||||
|
||||
|
@ -3,8 +3,8 @@ package de.craftinc.gates.commands;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Location;
|
||||
|
||||
import de.craftinc.gates.gates.Gate;
|
||||
import de.craftinc.gates.gates.GatesManager;
|
||||
import de.craftinc.gates.Gate;
|
||||
import de.craftinc.gates.GatesManager;
|
||||
import de.craftinc.gates.Plugin;
|
||||
|
||||
|
||||
|
@ -8,7 +8,7 @@ import java.util.List;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import de.craftinc.gates.gates.Gate;
|
||||
import de.craftinc.gates.Gate;
|
||||
import de.craftinc.gates.Plugin;
|
||||
import de.craftinc.gates.util.TextUtil;
|
||||
|
||||
|
@ -2,7 +2,7 @@ package de.craftinc.gates.commands;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
import de.craftinc.gates.gates.GatesManager;
|
||||
import de.craftinc.gates.GatesManager;
|
||||
import de.craftinc.gates.Plugin;
|
||||
|
||||
|
||||
|
@ -0,0 +1,19 @@
|
||||
package de.craftinc.gates.listeners;
|
||||
|
||||
|
||||
import de.craftinc.gates.util.GateBlockChangeSender;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerChangedWorldEvent;
|
||||
|
||||
|
||||
public class PlayerChangedWorldListener implements Listener
|
||||
{
|
||||
@EventHandler(priority = EventPriority.NORMAL)
|
||||
public void onPlayerChangeWorld(PlayerChangedWorldEvent event)
|
||||
{
|
||||
System.out.println("location: " + event.getPlayer().getLocation());
|
||||
GateBlockChangeSender.updateGateBlocks(event.getPlayer());
|
||||
}
|
||||
}
|
17
src/de/craftinc/gates/listeners/PlayerJoinListener.java
Normal file
17
src/de/craftinc/gates/listeners/PlayerJoinListener.java
Normal file
@ -0,0 +1,17 @@
|
||||
package de.craftinc.gates.listeners;
|
||||
|
||||
|
||||
import de.craftinc.gates.util.GateBlockChangeSender;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
|
||||
public class PlayerJoinListener implements Listener
|
||||
{
|
||||
@EventHandler(priority = EventPriority.NORMAL)
|
||||
public void onPlayerJoin(PlayerJoinEvent event)
|
||||
{
|
||||
GateBlockChangeSender.updateGateBlocks(event.getPlayer());
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@ package de.craftinc.gates.listeners;
|
||||
import java.util.Calendar;
|
||||
import java.util.HashMap;
|
||||
|
||||
import de.craftinc.gates.util.GateBlockChangeSender;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
@ -11,8 +12,8 @@ import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerMoveEvent;
|
||||
|
||||
import de.craftinc.gates.gates.Gate;
|
||||
import de.craftinc.gates.gates.GatesManager;
|
||||
import de.craftinc.gates.Gate;
|
||||
import de.craftinc.gates.GatesManager;
|
||||
import de.craftinc.gates.Plugin;
|
||||
|
||||
|
||||
@ -27,6 +28,11 @@ public class PlayerMoveListener implements Listener
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.getFrom().getChunk() != event.getTo().getChunk()) {
|
||||
GateBlockChangeSender.updateGateBlocks(event.getPlayer(), event.getTo());
|
||||
}
|
||||
|
||||
|
||||
GatesManager gateManager = Plugin.getPlugin().getGatesManager();
|
||||
Gate gateAtLocation = gateManager.getGateAtLocation(event.getTo());
|
||||
|
||||
|
22
src/de/craftinc/gates/listeners/PlayerRespawnListener.java
Normal file
22
src/de/craftinc/gates/listeners/PlayerRespawnListener.java
Normal file
@ -0,0 +1,22 @@
|
||||
package de.craftinc.gates.listeners;
|
||||
|
||||
|
||||
import de.craftinc.gates.util.GateBlockChangeSender;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerRespawnEvent;
|
||||
|
||||
|
||||
public class PlayerRespawnListener implements Listener
|
||||
{
|
||||
@EventHandler(priority = EventPriority.NORMAL)
|
||||
public void onPlayerRespawn(PlayerRespawnEvent event)
|
||||
{
|
||||
System.out.println("in onPlayerRespawn");
|
||||
System.out.println("player: " + event.getPlayer());
|
||||
System.out.println("position: " + event.getPlayer().getLocation());
|
||||
|
||||
GateBlockChangeSender.updateGateBlocks(event.getPlayer(), event.getRespawnLocation());
|
||||
}
|
||||
}
|
21
src/de/craftinc/gates/listeners/PlayerTeleportListener.java
Normal file
21
src/de/craftinc/gates/listeners/PlayerTeleportListener.java
Normal file
@ -0,0 +1,21 @@
|
||||
package de.craftinc.gates.listeners;
|
||||
|
||||
|
||||
import de.craftinc.gates.util.GateBlockChangeSender;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerTeleportEvent;
|
||||
|
||||
public class PlayerTeleportListener implements Listener
|
||||
{
|
||||
@EventHandler(priority = EventPriority.NORMAL)
|
||||
public void onPlayerTeleport(PlayerTeleportEvent event)
|
||||
{
|
||||
if (event.isCancelled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
GateBlockChangeSender.updateGateBlocks(event.getPlayer(), event.getTo());
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package de.craftinc.gates.gates.persistence;
|
||||
package de.craftinc.gates.persistence;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
@ -1,6 +1,6 @@
|
||||
package de.craftinc.gates.gates.persistence;
|
||||
package de.craftinc.gates.persistence;
|
||||
|
||||
import de.craftinc.gates.gates.Gate;
|
||||
import de.craftinc.gates.Gate;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
@ -13,7 +13,7 @@ import de.craftinc.gates.Plugin;
|
||||
|
||||
public class FloodUtil
|
||||
{
|
||||
private final static int frameBlockSearchLimit = 100;
|
||||
private final static int frameBlockSearchLimit = 100; // TODO: move search radius into a config file / get value from config class
|
||||
|
||||
private static final Set<BlockFace> exp1 = new HashSet<BlockFace>();
|
||||
private static final Set<BlockFace> exp2 = new HashSet<BlockFace>();
|
||||
|
98
src/de/craftinc/gates/util/GateBlockChangeSender.java
Normal file
98
src/de/craftinc/gates/util/GateBlockChangeSender.java
Normal file
@ -0,0 +1,98 @@
|
||||
package de.craftinc.gates.util;
|
||||
|
||||
|
||||
import de.craftinc.gates.Plugin;
|
||||
import de.craftinc.gates.Gate;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
|
||||
public class GateBlockChangeSender
|
||||
{
|
||||
protected static final int searchRadius = 16; // TODO: move search radius into a config file / get value from config class
|
||||
|
||||
|
||||
public static void updateGateBlocks(final Player player)
|
||||
{
|
||||
if (player == null) {
|
||||
throw new IllegalArgumentException("'player' must not be 'null'!");
|
||||
}
|
||||
|
||||
updateGateBlocks(player, player.getLocation());
|
||||
}
|
||||
|
||||
|
||||
public static void updateGateBlocks(final Player player, final Location location)
|
||||
{
|
||||
if (player == null) {
|
||||
throw new IllegalArgumentException("'player' must not be 'null'!");
|
||||
}
|
||||
|
||||
if (location == null) {
|
||||
throw new IllegalArgumentException("'location' must not be 'null'!");
|
||||
}
|
||||
|
||||
Set<Gate> gatesNearby = Plugin.getPlugin().getGatesManager().getNearbyGates(location.getChunk());
|
||||
|
||||
if (gatesNearby == null) {
|
||||
return; // no gates nearby
|
||||
}
|
||||
|
||||
for (Gate g : gatesNearby) {
|
||||
|
||||
if (!g.isOpen() || g.isHidden()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (Location l : g.getGateBlockLocations()) {
|
||||
|
||||
if (l.getBlock().getType() == Material.AIR) {
|
||||
player.sendBlockChange(l, Material.PORTAL, (byte)0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void updateGateBlocks(final Gate gate)
|
||||
{
|
||||
if (gate == null) {
|
||||
throw new IllegalArgumentException("'gate must not be 'null'!");
|
||||
}
|
||||
|
||||
Location gateLocation = gate.getLocation();
|
||||
ArrayList<Player> playersNearby = new ArrayList<Player>();
|
||||
|
||||
for (Player p : Plugin.getPlugin().getServer().getOnlinePlayers()) {
|
||||
|
||||
if (p.getLocation().distance(gateLocation) < searchRadius) {
|
||||
playersNearby.add(p);
|
||||
}
|
||||
}
|
||||
|
||||
Material material;
|
||||
|
||||
if (gate.isOpen() && !gate.isHidden()) {
|
||||
material = Material.PORTAL;
|
||||
}
|
||||
else {
|
||||
material = Material.AIR;
|
||||
}
|
||||
|
||||
for (Player p : playersNearby) {
|
||||
|
||||
for (Location l : gate.getGateBlockLocations()) {
|
||||
|
||||
if (l.getBlock().getType() == Material.AIR) { // on server-side a gate is always made out of AIR
|
||||
p.sendBlockChange(l, material, (byte)0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user