New (basic) WorldGuard Integration (eventdriven) for less mem usage

This commit is contained in:
Jascha Starke 2012-02-11 13:05:49 +01:00
parent d0aa780a80
commit d4ee873f77
12 changed files with 395 additions and 68 deletions

View file

@ -28,7 +28,6 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import de.jaschastarke.minecraft.utils.Util;
import de.jaschastarke.minecraft.worldguard.CCommand;
import static de.jaschastarke.minecraft.utils.Locale.L;
public class Commands {
@ -185,9 +184,7 @@ public class Commands {
public static void register(LimitedCreativeCore pplugin) {
plugin = pplugin;
plugin.getCommand("limitedcreative").setExecutor(new MainCommandExecutor());
if (plugin.worldguard != null) {
plugin.getCommand("/region").setExecutor(new CCommand(plugin, plugin.worldguard.getRegionManager()));
} else {
if (plugin.worldguard == null) {
plugin.getCommand("/region").setExecutor(new NotAvailableCommandExecutor());
}
}

View file

@ -1,5 +1,9 @@
package de.jaschastarke.minecraft.limitedcreative.regions;
import java.util.Arrays;
import java.util.List;
import com.sk89q.worldguard.protection.flags.Flag;
import com.sk89q.worldguard.protection.flags.RegionGroupFlag;
import com.sk89q.worldguard.protection.flags.StateFlag;
@ -10,4 +14,11 @@ public final class Flags {
static {
CREATIVE.setGroupFlag(CREATIVE_GROUP);
}
public static List<Flag<?>> getList() {
return Arrays.asList(new Flag<?>[]{
SPAWNDROPS,
CREATIVE,
CREATIVE_GROUP
});
}
}

View file

@ -5,7 +5,6 @@ import static de.jaschastarke.minecraft.utils.Locale.L;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Item;
import org.bukkit.event.EventHandler;
@ -15,51 +14,38 @@ import org.bukkit.event.block.BlockPistonExtendEvent;
import org.bukkit.event.block.BlockPistonRetractEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.ItemSpawnEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.managers.RegionManager;
import de.jaschastarke.minecraft.limitedcreative.LCPlayer;
import de.jaschastarke.minecraft.limitedcreative.LimitedCreativeCore;
import de.jaschastarke.minecraft.utils.Util;
import de.jaschastarke.minecraft.worldguard.ApplicableRegions;
import de.jaschastarke.minecraft.worldguard.CRegionManager;
import de.jaschastarke.minecraft.worldguard.events.PlayerChangedAreaEvent;
public class RegionListener implements Listener {
private static LimitedCreativeCore plugin = WorldGuardIntegration.plugin;
private static WorldGuardPlugin wg = WorldGuardIntegration.wg;
private CRegionManager rm;
public RegionListener(WorldGuardIntegration wgi) {
rm = wgi.getRegionManager();
}
private ApplicableRegions regionSet(World world, Location loc) {
RegionManager mgr = wg.getGlobalRegionManager().get(world);
return new ApplicableRegions(mgr.getApplicableRegions(loc), rm.world(world));
}
private ApplicableRegions regionSet(Location loc) {
return regionSet(loc.getWorld(), loc);
return rm.getRegionSet(loc);
}
private ApplicableRegions regionSet(Block block) {
return regionSet(block.getWorld(), block.getLocation());
return rm.getRegionSet(block);
}
@EventHandler
public void onBlockBreak(BlockBreakEvent event) {
if (event.isCancelled())
return;
LCPlayer player = LCPlayer.get(event.getPlayer());
RegionManager mgr = wg.getGlobalRegionManager().get(event.getPlayer().getWorld());
Vector pt = new Vector(event.getBlock().getLocation().getBlockX(), event.getBlock().getLocation().getBlockY(), event.getBlock().getLocation().getBlockZ());
ApplicableRegions set = new ApplicableRegions(mgr.getApplicableRegions(pt), rm.world(event.getPlayer().getWorld()));
ApplicableRegions set = rm.getRegionSet(event.getBlock());
if (player.isRegionCreative() && !set.allows(Flags.CREATIVE, player)) {
event.getPlayer().sendMessage(L("blocked.outside_creative_break"));
event.setCancelled(true);
} else if (set.allows(Flags.CREATIVE) && player.getRaw().getGameMode() != GameMode.CREATIVE) {
} else if (player.getRaw().getGameMode() != GameMode.CREATIVE && set.allows(Flags.CREATIVE)) {
plugin.spawnblock.block(event.getBlock(), player);
}
}
@ -69,11 +55,9 @@ public class RegionListener implements Listener {
if (event.isCancelled())
return;
LCPlayer player = LCPlayer.get(event.getPlayer());
if (player.isRegionCreative()) {
if (player.isRegionCreative() && rm.isDiffrentRegion(event.getPlayer(), event.getBlock().getLocation())) {
// do not build outside of creative regions, when in the region
RegionManager mgr = wg.getGlobalRegionManager().get(event.getPlayer().getWorld());
Vector pt = new Vector(event.getBlock().getLocation().getBlockX(), event.getBlock().getLocation().getBlockY(), event.getBlock().getLocation().getBlockZ());
ApplicableRegions set = new ApplicableRegions(mgr.getApplicableRegions(pt), rm.world(event.getPlayer().getWorld()));
ApplicableRegions set = rm.getRegionSet(event.getBlock());
if (!set.allows(Flags.CREATIVE, player)) {
event.getPlayer().sendMessage(L("blocked.outside_creative"));
event.setCancelled(true);
@ -82,38 +66,8 @@ public class RegionListener implements Listener {
}
@EventHandler
public void onPlayerMove(PlayerMoveEvent event) {
if (event.isCancelled())
return;
if (event.getFrom().getBlockX() != event.getTo().getBlockX()
|| event.getFrom().getBlockY() != event.getTo().getBlockY()
|| event.getFrom().getBlockZ() != event.getTo().getBlockZ()) {
LCPlayer player = LCPlayer.get(event.getPlayer());
RegionManager mgr = wg.getGlobalRegionManager().get(event.getPlayer().getWorld());
ApplicableRegionSet applicableRegions = mgr.getApplicableRegions(event.getTo());
LimitedCreativeCore.debug(applicableRegions.toString());
ApplicableRegions set = new ApplicableRegions(applicableRegions, rm.world(event.getPlayer().getWorld()));
player.setRegionCreativeAllowed(set.allows(Flags.CREATIVE, player), event);
}
}
@EventHandler
public void onPlayerTeleport(PlayerTeleportEvent event) {
if (event.isCancelled())
return;
if (event.getFrom().getBlockX() != event.getTo().getBlockX()
|| event.getFrom().getBlockY() != event.getTo().getBlockY()
|| event.getFrom().getBlockZ() != event.getTo().getBlockZ()) {
LCPlayer player = LCPlayer.get(event.getPlayer());
RegionManager mgr = wg.getGlobalRegionManager().get(event.getPlayer().getWorld());
Vector pt = new Vector(event.getTo().getBlockX(), event.getTo().getBlockY(), event.getTo().getBlockZ());
ApplicableRegions set = new ApplicableRegions(mgr.getApplicableRegions(pt), rm.world(event.getPlayer().getWorld()));
player.setRegionCreativeAllowed(set.allows(Flags.CREATIVE, player), event);
}
public void onPlayerChangedArea(PlayerChangedAreaEvent event) {
LCPlayer.get(event.getPlayer()).setRegionCreativeAllowed(event.getNewRegionSet().allows(Flags.CREATIVE), event.getMoveEvent());
}
@EventHandler

View file

@ -17,33 +17,44 @@
*/
package de.jaschastarke.minecraft.limitedcreative.regions;
import java.io.File;
import java.util.List;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.protection.flags.Flag;
import de.jaschastarke.minecraft.limitedcreative.LimitedCreativeCore;
import de.jaschastarke.minecraft.worldguard.CRegionManager;
import de.jaschastarke.minecraft.worldguard.FlagList;
import de.jaschastarke.minecraft.worldguard.Integration;
import de.jaschastarke.minecraft.worldguard.Interface;
public class WorldGuardIntegration {
public class WorldGuardIntegration implements Integration {
public static LimitedCreativeCore plugin;
public static WorldGuardPlugin wg;
private CRegionManager rm;
public WorldGuardIntegration(LimitedCreativeCore plugin) {
WorldGuardIntegration.plugin = plugin;
wg = (WorldGuardPlugin) plugin.getServer().getPluginManager().getPlugin("WorldGuard");
rm = new CRegionManager(new File(plugin.getDataFolder(), "regions.yml"));
/*
* This should be only done by the "API"-Plugin itself (when its done). You don't need to that again if you
* like to interact with LimitedCreative's WorldGuard-Integration
*/
new Interface(plugin);
FlagList.addFlag(Flags.CREATIVE);
FlagList.addFlag(Flags.CREATIVE_GROUP);
FlagList.addFlag(Flags.SPAWNDROPS);
/*
* You only need to do this one, to make your Flags available.
*/
Interface.getInstance().register(this);
plugin.getServer().getPluginManager().registerEvents(new RegionListener(this), plugin);
}
public CRegionManager getRegionManager() {
return rm;
return Interface.getInstance().getRegionManager();
}
@Override
public List<Flag<?>> getFlags() {
return Flags.getList();
}
}

View file

@ -0,0 +1,40 @@
/*
* Limited Creative - (Bukkit Plugin)
* Copyright (C) 2012 jascha@ja-s.de
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.jaschastarke.minecraft.worldguard;
import org.bukkit.plugin.java.JavaPlugin;
/**
* This Namespace may be used as a Standalone-Plugin just providing the WorldGuard-API
* TODO: Not done yet
*/
public class API extends JavaPlugin {
@Override
public void onDisable() {
// TODO Auto-generated method stub
}
@Override
public void onEnable() {
throw new IllegalArgumentException("Not yet supported");
// TODO Auto-generated method stub
}
}

View file

@ -0,0 +1,75 @@
/*
* Limited Creative - (Bukkit Plugin)
* Copyright (C) 2012 jascha@ja-s.de
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.jaschastarke.minecraft.worldguard;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import de.jaschastarke.minecraft.worldguard.events.PlayerChangedAreaEvent;
public class CListener implements Listener {
private Interface com;
public CListener(Interface com) {
this.com = com;
}
@EventHandler
public void onPlayerMove(PlayerMoveEvent event) {
if (event.isCancelled())
return;
if (event.getFrom().getBlockX() != event.getTo().getBlockX()
|| event.getFrom().getBlockY() != event.getTo().getBlockY()
|| event.getFrom().getBlockZ() != event.getTo().getBlockZ()) { // he really moved, and not just looked around
if (com.getRegionManager().isDiffrentRegion(event.getPlayer(), event.getTo())) {
Bukkit.getServer().getPluginManager().callEvent(new PlayerChangedAreaEvent(event));
CPlayer.get(event.getPlayer()).setHash(com.getRegionManager().getRegionsHash(event.getTo()));
}
}
}
@EventHandler
public void onPlayerTeleport(PlayerTeleportEvent event) {
if (event.isCancelled())
return;
if (event.getFrom().getBlockX() != event.getTo().getBlockX()
|| event.getFrom().getBlockY() != event.getTo().getBlockY()
|| event.getFrom().getBlockZ() != event.getTo().getBlockZ()) { // he really moved, and not just looked around
if (com.getRegionManager().isDiffrentRegion(event.getPlayer(), event.getTo())) {
Bukkit.getServer().getPluginManager().callEvent(new PlayerChangedAreaEvent(event));
CPlayer.get(event.getPlayer()).setHash(com.getRegionManager().getRegionsHash(event.getTo()));
}
}
}
@EventHandler
public void onPlayerLogin(PlayerJoinEvent event) {
CPlayer.get(event.getPlayer()).setHash(com.getRegionManager().getRegionsHash(event.getPlayer().getLocation()));
}
@EventHandler
public void onPlayerLogout(PlayerQuitEvent event) {
CPlayer.remove(event.getPlayer());
}
}

View file

@ -0,0 +1,48 @@
/*
* Limited Creative - (Bukkit Plugin)
* Copyright (C) 2012 jascha@ja-s.de
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.jaschastarke.minecraft.worldguard;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.entity.Player;
public class CPlayer {
private static Map<String, CPlayer> players = new HashMap<String, CPlayer>();
public static CPlayer get(Player p) {
if (!players.containsKey(p.getName())) {
CPlayer pl = new CPlayer();
players.put(p.getName(), pl);
return pl;
} else {
return players.get(p.getName());
}
}
public static void remove(Player p) {
players.remove(p.getName());
}
private String hash = null;
public String getHash() {
return hash;
}
public void setHash(String hash) {
this.hash = hash;
}
}

View file

@ -20,17 +20,26 @@ package de.jaschastarke.minecraft.worldguard;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import com.sk89q.worldguard.bukkit.BukkitUtil;
import com.sk89q.worldguard.protection.GlobalRegionManager;
import com.sk89q.worldguard.protection.flags.Flag;
import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import de.jaschastarke.minecraft.utils.Util;
public class CRegionManager {
protected YamlConfiguration c;
protected File file;
@ -118,4 +127,30 @@ public class CRegionManager {
return list;
}
}
public GlobalRegionManager getWGGlobalManager() {
return Interface.getInstance().getWorldGuard().getGlobalRegionManager();
}
public RegionManager getWGManager(World world) {
return Interface.getInstance().getWorldGuard().getRegionManager(world);
}
public String getRegionsHash(Location loc) {
List<String> idlist = getWGGlobalManager().get(loc.getWorld()).getApplicableRegionsIDs(BukkitUtil.toVector(loc));
String[] ids = idlist.toArray(new String[idlist.size()]);
Arrays.sort(ids);
return Util.join(ids, "|");
}
public ApplicableRegions getRegionSet(Location loc) {
return new ApplicableRegions(getWGManager(loc.getWorld()).getApplicableRegions(loc), this.world(loc.getWorld()));
}
public ApplicableRegions getRegionSet(Block block) {
return getRegionSet(block.getLocation());
}
public boolean isDiffrentRegion(Player player, Location loc) {
return !getRegionsHash(loc).equals(CPlayer.get(player).getHash());
}
}

View file

@ -18,6 +18,7 @@
package de.jaschastarke.minecraft.worldguard;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.bukkit.command.CommandSender;
@ -33,6 +34,9 @@ public class FlagList {
public static void addFlag(Flag<?> flag) {
list.add(flag);
}
public static void addFlags(Collection<Flag<?>> flags) {
list.addAll(flags);
}
public static Flag<?> getFlag(String flag) {
for (Flag <?> f : list) {
if (f.getName().replace("-", "").equalsIgnoreCase(flag.replace("-", ""))) {

View file

@ -0,0 +1,26 @@
/*
* Limited Creative - (Bukkit Plugin)
* Copyright (C) 2012 jascha@ja-s.de
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.jaschastarke.minecraft.worldguard;
import java.util.List;
import com.sk89q.worldguard.protection.flags.Flag;
public interface Integration {
public List<Flag<?>> getFlags();
}

View file

@ -0,0 +1,58 @@
/*
* Limited Creative - (Bukkit Plugin)
* Copyright (C) 2012 jascha@ja-s.de
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.jaschastarke.minecraft.worldguard;
import java.io.File;
import org.bukkit.plugin.java.JavaPlugin;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
public class Interface {
//private JavaPlugin plugin;
private CRegionManager mgr;
private WorldGuardPlugin wg;
private static Interface _instance = null;
public Interface(JavaPlugin plugin) {
if (_instance != null)
throw new RuntimeException("The Interface is Singleton!");
//this.plugin = plugin;
_instance = this;
wg = (WorldGuardPlugin) plugin.getServer().getPluginManager().getPlugin("WorldGuard");
mgr = new CRegionManager(new File(plugin.getDataFolder(), "regions.yml"));
plugin.getCommand("/region").setExecutor(new CCommand(plugin, mgr, wg));
plugin.getServer().getPluginManager().registerEvents(new CListener(this), plugin);
}
public static Interface getInstance() {
return _instance;
}
public void register(Integration integration) {
FlagList.addFlags(integration.getFlags());
}
public WorldGuardPlugin getWorldGuard() {
return wg;
}
public CRegionManager getRegionManager() {
return mgr;
}
}

View file

@ -0,0 +1,68 @@
/*
* Limited Creative - (Bukkit Plugin)
* Copyright (C) 2012 jascha@ja-s.de
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.jaschastarke.minecraft.worldguard.events;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import de.jaschastarke.minecraft.limitedcreative.LimitedCreativeCore;
import de.jaschastarke.minecraft.worldguard.ApplicableRegions;
import de.jaschastarke.minecraft.worldguard.Interface;
@SuppressWarnings("serial")
public class PlayerChangedAreaEvent extends Event {
private PlayerMoveEvent event;
public PlayerChangedAreaEvent(PlayerMoveEvent moveevent) {
event = moveevent;
}
public Player getPlayer() {
return event.getPlayer();
}
public boolean isTeleport() {
return event instanceof PlayerTeleportEvent;
}
public ApplicableRegions getPreviousRegionSet() {
return Interface.getInstance().getRegionManager().getRegionSet(event.getFrom());
}
public ApplicableRegions getNewRegionSet() {
return Interface.getInstance().getRegionManager().getRegionSet(event.getTo());
}
private static final HandlerList handlers = new HandlerList();
public HandlerList getHandlers() {
LimitedCreativeCore.debug("getHandlers");
return handlers;
}
public static HandlerList getHandlerList() {
LimitedCreativeCore.debug("getHandlerList");
return handlers;
}
public PlayerMoveEvent getMoveEvent() {
return event;
}
}