mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2025-06-11 20:13:55 +00:00
Switch to Gradle. Use git log --follow for history.
This converts the project into a multi-module Gradle build. By default, Git does not show history past a rename, so use git log --follow to see further history.
This commit is contained in:
@ -0,0 +1,65 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.bukkit.util;
|
||||
|
||||
public class CommandInfo {
|
||||
|
||||
private final String[] aliases;
|
||||
private final Object registeredWith;
|
||||
private final String usage, desc;
|
||||
private final String[] permissions;
|
||||
|
||||
public CommandInfo(String usage, String desc, String[] aliases, Object registeredWith) {
|
||||
this(usage, desc, aliases, registeredWith, null);
|
||||
}
|
||||
|
||||
public CommandInfo(String usage, String desc, String[] aliases, Object registeredWith, String[] permissions) {
|
||||
this.usage = usage;
|
||||
this.desc = desc;
|
||||
this.aliases = aliases;
|
||||
this.permissions = permissions;
|
||||
this.registeredWith = registeredWith;
|
||||
}
|
||||
|
||||
public String[] getAliases() {
|
||||
return aliases;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return aliases[0];
|
||||
}
|
||||
|
||||
public String getUsage() {
|
||||
return usage;
|
||||
}
|
||||
|
||||
public String getDesc() {
|
||||
return desc;
|
||||
}
|
||||
|
||||
public String[] getPermissions() {
|
||||
return permissions;
|
||||
}
|
||||
|
||||
public Object getRegisteredWith() {
|
||||
return registeredWith;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.bukkit.util;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public interface CommandInspector {
|
||||
|
||||
String getShortText(Command command);
|
||||
|
||||
String getFullText(Command command);
|
||||
|
||||
boolean testPermission(CommandSender sender, Command command);
|
||||
}
|
@ -0,0 +1,110 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.bukkit.util;
|
||||
|
||||
import com.sk89q.util.ReflectionUtil;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandMap;
|
||||
import org.bukkit.command.SimpleCommandMap;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class CommandRegistration {
|
||||
|
||||
static {
|
||||
Bukkit.getServer().getHelpMap().registerHelpTopicFactory(DynamicPluginCommand.class, new DynamicPluginCommandHelpTopic.Factory());
|
||||
}
|
||||
|
||||
protected final Plugin plugin;
|
||||
protected final CommandExecutor executor;
|
||||
private CommandMap fallbackCommands;
|
||||
|
||||
public CommandRegistration(Plugin plugin) {
|
||||
this(plugin, plugin);
|
||||
}
|
||||
|
||||
public CommandRegistration(Plugin plugin, CommandExecutor executor) {
|
||||
this.plugin = plugin;
|
||||
this.executor = executor;
|
||||
}
|
||||
|
||||
public boolean register(List<CommandInfo> registered) {
|
||||
CommandMap commandMap = getCommandMap();
|
||||
if (registered == null || commandMap == null) {
|
||||
return false;
|
||||
}
|
||||
for (CommandInfo command : registered) {
|
||||
DynamicPluginCommand cmd = new DynamicPluginCommand(command.getAliases(),
|
||||
command.getDesc(), "/" + command.getAliases()[0] + " " + command.getUsage(), executor, command.getRegisteredWith(), plugin);
|
||||
cmd.setPermissions(command.getPermissions());
|
||||
commandMap.register(plugin.getDescription().getName(), cmd);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public CommandMap getCommandMap() {
|
||||
CommandMap commandMap = ReflectionUtil.getField(plugin.getServer().getPluginManager(), "commandMap");
|
||||
if (commandMap == null) {
|
||||
if (fallbackCommands != null) {
|
||||
commandMap = fallbackCommands;
|
||||
} else {
|
||||
Bukkit.getServer().getLogger().severe(plugin.getDescription().getName() +
|
||||
": Could not retrieve server CommandMap, using fallback instead!");
|
||||
fallbackCommands = commandMap = new SimpleCommandMap(Bukkit.getServer());
|
||||
Bukkit.getServer().getPluginManager().registerEvents(new FallbackRegistrationListener(fallbackCommands), plugin);
|
||||
}
|
||||
}
|
||||
return commandMap;
|
||||
}
|
||||
|
||||
public boolean unregisterCommands() {
|
||||
CommandMap commandMap = getCommandMap();
|
||||
List<String> toRemove = new ArrayList<String>();
|
||||
Map<String, org.bukkit.command.Command> knownCommands = ReflectionUtil.getField(commandMap, "knownCommands");
|
||||
Set<String> aliases = ReflectionUtil.getField(commandMap, "aliases");
|
||||
if (knownCommands == null || aliases == null) {
|
||||
return false;
|
||||
}
|
||||
for (Iterator<org.bukkit.command.Command> i = knownCommands.values().iterator(); i.hasNext();) {
|
||||
org.bukkit.command.Command cmd = i.next();
|
||||
if (cmd instanceof DynamicPluginCommand && ((DynamicPluginCommand) cmd).getOwner().equals(executor)) {
|
||||
i.remove();
|
||||
for (String alias : cmd.getAliases()) {
|
||||
org.bukkit.command.Command aliasCmd = knownCommands.get(alias);
|
||||
if (cmd.equals(aliasCmd)) {
|
||||
aliases.remove(alias);
|
||||
toRemove.add(alias);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (String string : toRemove) {
|
||||
knownCommands.remove(string);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.bukkit.util;
|
||||
|
||||
import com.sk89q.minecraft.util.commands.Command;
|
||||
import com.sk89q.minecraft.util.commands.CommandPermissions;
|
||||
import com.sk89q.minecraft.util.commands.CommandsManager;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class CommandsManagerRegistration extends CommandRegistration {
|
||||
|
||||
protected CommandsManager<?> commands;
|
||||
|
||||
public CommandsManagerRegistration(Plugin plugin, CommandsManager<?> commands) {
|
||||
super(plugin);
|
||||
this.commands = commands;
|
||||
}
|
||||
|
||||
public CommandsManagerRegistration(Plugin plugin, CommandExecutor executor, CommandsManager<?> commands) {
|
||||
super(plugin, executor);
|
||||
this.commands = commands;
|
||||
}
|
||||
|
||||
public boolean register(Class<?> clazz) {
|
||||
return registerAll(commands.registerAndReturn(clazz));
|
||||
}
|
||||
|
||||
public boolean registerAll(List<Command> registered) {
|
||||
List<CommandInfo> toRegister = new ArrayList<CommandInfo>();
|
||||
for (Command command : registered) {
|
||||
List<String> permissions = null;
|
||||
Method cmdMethod = commands.getMethods().get(null).get(command.aliases()[0]);
|
||||
Map<String, Method> childMethods = commands.getMethods().get(cmdMethod);
|
||||
|
||||
if (cmdMethod != null && cmdMethod.isAnnotationPresent(CommandPermissions.class)) {
|
||||
permissions = Arrays.asList(cmdMethod.getAnnotation(CommandPermissions.class).value());
|
||||
} else if (cmdMethod != null && childMethods != null && !childMethods.isEmpty()) {
|
||||
permissions = new ArrayList<String>();
|
||||
for (Method m : childMethods.values()) {
|
||||
if (m.isAnnotationPresent(CommandPermissions.class)) {
|
||||
permissions.addAll(Arrays.asList(m.getAnnotation(CommandPermissions.class).value()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
toRegister.add(new CommandInfo(command.usage(), command.desc(), command.aliases(), commands, permissions == null ? null : permissions.toArray(new String[permissions.size()])));
|
||||
}
|
||||
|
||||
return register(toRegister);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,120 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.bukkit.util;
|
||||
|
||||
import com.sk89q.minecraft.util.commands.CommandsManager;
|
||||
import com.sk89q.util.StringUtil;
|
||||
import com.sk89q.wepif.PermissionsResolverManager;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.PluginIdentifiableCommand;
|
||||
import org.bukkit.command.TabCompleter;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* An implementation of a dynamically registered {@link org.bukkit.command.Command} attached to a plugin
|
||||
*/
|
||||
public class DynamicPluginCommand extends org.bukkit.command.Command implements PluginIdentifiableCommand {
|
||||
|
||||
protected final CommandExecutor owner;
|
||||
protected final Object registeredWith;
|
||||
protected final Plugin owningPlugin;
|
||||
protected String[] permissions = new String[0];
|
||||
|
||||
public DynamicPluginCommand(String[] aliases, String desc, String usage, CommandExecutor owner, Object registeredWith, Plugin plugin) {
|
||||
super(aliases[0], desc, usage, Arrays.asList(aliases));
|
||||
this.owner = owner;
|
||||
this.owningPlugin = plugin;
|
||||
this.registeredWith = registeredWith;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(CommandSender sender, String label, String[] args) {
|
||||
return owner.onCommand(sender, this, label, args);
|
||||
}
|
||||
|
||||
public Object getOwner() {
|
||||
return owner;
|
||||
}
|
||||
|
||||
public Object getRegisteredWith() {
|
||||
return registeredWith;
|
||||
}
|
||||
|
||||
public void setPermissions(String[] permissions) {
|
||||
this.permissions = permissions;
|
||||
if (permissions != null) {
|
||||
super.setPermission(StringUtil.joinString(permissions, ";"));
|
||||
}
|
||||
}
|
||||
|
||||
public String[] getPermissions() {
|
||||
return permissions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Plugin getPlugin() {
|
||||
return owningPlugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
|
||||
if (registeredWith instanceof CommandInspector) {
|
||||
return ((TabCompleter) owner).onTabComplete(sender, this, alias, args);
|
||||
} else {
|
||||
return super.tabComplete(sender, alias, args);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public boolean testPermissionSilent(CommandSender sender) {
|
||||
if (permissions == null || permissions.length == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (registeredWith instanceof CommandInspector) {
|
||||
CommandInspector resolver = (CommandInspector) registeredWith;
|
||||
return resolver.testPermission(sender, this);
|
||||
} else if (registeredWith instanceof CommandsManager<?>) {
|
||||
try {
|
||||
for (String permission : permissions) {
|
||||
if (((CommandsManager<CommandSender>) registeredWith).hasPermission(sender, permission)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} catch (Throwable ignore) {
|
||||
}
|
||||
} else if (PermissionsResolverManager.isInitialized() && sender instanceof OfflinePlayer) {
|
||||
for (String permission : permissions) {
|
||||
if (PermissionsResolverManager.getInstance().hasPermission((OfflinePlayer) sender, permission)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return super.testPermissionSilent(sender);
|
||||
}
|
||||
}
|
@ -0,0 +1,147 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.bukkit.util;
|
||||
|
||||
import com.sk89q.minecraft.util.commands.CommandsManager;
|
||||
import com.sk89q.wepif.PermissionsResolverManager;
|
||||
import com.sk89q.wepif.WEPIFRuntimeException;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.help.HelpTopic;
|
||||
import org.bukkit.help.HelpTopicFactory;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class DynamicPluginCommandHelpTopic extends HelpTopic {
|
||||
|
||||
private final DynamicPluginCommand cmd;
|
||||
|
||||
public DynamicPluginCommandHelpTopic(DynamicPluginCommand cmd) {
|
||||
this.cmd = cmd;
|
||||
this.name = "/" + cmd.getName();
|
||||
|
||||
if (cmd.getRegisteredWith() instanceof CommandInspector) {
|
||||
CommandInspector resolver = (CommandInspector) cmd.getRegisteredWith();
|
||||
this.shortText = resolver.getShortText(cmd);
|
||||
this.fullText = resolver.getFullText(cmd);
|
||||
} else {
|
||||
String fullTextTemp = null;
|
||||
StringBuilder fullText = new StringBuilder();
|
||||
|
||||
if (cmd.getRegisteredWith() instanceof CommandsManager) {
|
||||
Map<String, String> helpText = ((CommandsManager<?>) cmd.getRegisteredWith()).getHelpMessages();
|
||||
final String lookupName = cmd.getName().replaceAll("/", "");
|
||||
if (helpText.containsKey(lookupName)) { // We have full help text for this command
|
||||
fullTextTemp = helpText.get(lookupName);
|
||||
}
|
||||
// No full help text, assemble help text from info
|
||||
helpText = ((CommandsManager<?>) cmd.getRegisteredWith()).getCommands();
|
||||
if (helpText.containsKey(cmd.getName())) {
|
||||
final String shortText = helpText.get(cmd.getName());
|
||||
if (fullTextTemp == null) {
|
||||
fullTextTemp = this.name + " " + shortText;
|
||||
}
|
||||
this.shortText = shortText;
|
||||
}
|
||||
} else {
|
||||
this.shortText = cmd.getDescription();
|
||||
}
|
||||
|
||||
// Put the usage in the format: Usage string (newline) Aliases (newline) Help text
|
||||
String[] split = fullTextTemp == null ? new String[2] : fullTextTemp.split("\n", 2);
|
||||
fullText.append(ChatColor.BOLD).append(ChatColor.GOLD).append("Usage: ").append(ChatColor.WHITE);
|
||||
fullText.append(split[0]).append("\n");
|
||||
|
||||
if (!cmd.getAliases().isEmpty()) {
|
||||
fullText.append(ChatColor.BOLD).append(ChatColor.GOLD).append("Aliases: ").append(ChatColor.WHITE);
|
||||
boolean first = true;
|
||||
for (String alias : cmd.getAliases()) {
|
||||
if (!first) {
|
||||
fullText.append(", ");
|
||||
}
|
||||
fullText.append(alias);
|
||||
first = false;
|
||||
}
|
||||
fullText.append("\n");
|
||||
}
|
||||
if (split.length > 1) {
|
||||
fullText.append(split[1]);
|
||||
}
|
||||
this.fullText = fullText.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public boolean canSee(CommandSender player) {
|
||||
if (cmd.getRegisteredWith() instanceof CommandInspector) {
|
||||
CommandInspector resolver = (CommandInspector) cmd.getRegisteredWith();
|
||||
return resolver.testPermission(player, cmd);
|
||||
} else if (cmd.getPermissions() != null && cmd.getPermissions().length > 0) {
|
||||
if (cmd.getRegisteredWith() instanceof CommandsManager) {
|
||||
try {
|
||||
for (String perm : cmd.getPermissions()) {
|
||||
if (((CommandsManager<Object>) cmd.getRegisteredWith()).hasPermission(player, perm)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
// Doesn't take the CommandSender (Hooray for compile-time generics!), we have other methods at our disposal
|
||||
}
|
||||
}
|
||||
if (player instanceof OfflinePlayer) {
|
||||
try {
|
||||
for (String perm : cmd.getPermissions()) {
|
||||
if (PermissionsResolverManager.getInstance().hasPermission((OfflinePlayer) player, perm)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} catch (WEPIFRuntimeException e) {
|
||||
// PermissionsResolverManager not initialized, eat it
|
||||
}
|
||||
}
|
||||
for (String perm : cmd.getPermissions()) {
|
||||
if (player.hasPermission(perm)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFullText(CommandSender forWho) {
|
||||
if (this.fullText == null || this.fullText.isEmpty()) {
|
||||
return getShortText();
|
||||
} else {
|
||||
return this.fullText;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Factory implements HelpTopicFactory<DynamicPluginCommand> {
|
||||
@Override
|
||||
public HelpTopic createTopic(DynamicPluginCommand command) {
|
||||
return new DynamicPluginCommandHelpTopic(command);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.bukkit.util;
|
||||
|
||||
import org.bukkit.command.CommandMap;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
||||
|
||||
public class FallbackRegistrationListener implements Listener {
|
||||
|
||||
private final CommandMap commandRegistration;
|
||||
|
||||
public FallbackRegistrationListener(CommandMap commandRegistration) {
|
||||
this.commandRegistration = commandRegistration;
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
|
||||
if (commandRegistration.dispatch(event.getPlayer(), event.getMessage())) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,180 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.wepif;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import com.sk89q.util.yaml.YAMLNode;
|
||||
import com.sk89q.util.yaml.YAMLProcessor;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
|
||||
public class ConfigurationPermissionsResolver implements PermissionsResolver {
|
||||
private YAMLProcessor config;
|
||||
private Map<String, Set<String>> userPermissionsCache;
|
||||
private Set<String> defaultPermissionsCache;
|
||||
private Map<String, Set<String>> userGroups;
|
||||
|
||||
public ConfigurationPermissionsResolver(YAMLProcessor config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public static YAMLNode generateDefaultPerms(YAMLNode section) {
|
||||
section.setProperty("groups.default.permissions", new String[] {
|
||||
"worldedit.reload",
|
||||
"worldedit.selection",
|
||||
"worlds.creative.worldedit.region"});
|
||||
section.setProperty("groups.admins.permissions", new String[] { "*" });
|
||||
section.setProperty("users.sk89q.permissions", new String[] { "worldedit" });
|
||||
section.setProperty("users.sk89q.groups", new String[] { "admins" });
|
||||
return section;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
userGroups = new HashMap<String, Set<String>>();
|
||||
userPermissionsCache = new HashMap<String, Set<String>>();
|
||||
defaultPermissionsCache = new HashSet<String>();
|
||||
|
||||
Map<String, Set<String>> userGroupPermissions = new HashMap<String, Set<String>>();
|
||||
|
||||
List<String> groupKeys = config.getStringList("permissions.groups", null);
|
||||
|
||||
if (groupKeys != null) {
|
||||
for (String key : groupKeys) {
|
||||
List<String> permissions =
|
||||
config.getStringList("permissions.groups." + key + ".permissions", null);
|
||||
|
||||
if (!permissions.isEmpty()) {
|
||||
Set<String> groupPerms = new HashSet<String>(permissions);
|
||||
userGroupPermissions.put(key, groupPerms);
|
||||
|
||||
if (key.equals("default")) {
|
||||
defaultPermissionsCache.addAll(permissions);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
List<String> userKeys = config.getStringList("permissions.users", null);
|
||||
|
||||
if (userKeys != null) {
|
||||
for (String key : userKeys) {
|
||||
Set<String> permsCache = new HashSet<String>();
|
||||
|
||||
List<String> permissions =
|
||||
config.getStringList("permissions.users." + key + ".permissions", null);
|
||||
|
||||
if (!permissions.isEmpty()) {
|
||||
permsCache.addAll(permissions);
|
||||
}
|
||||
|
||||
List<String> groups =
|
||||
config.getStringList("permissions.users." + key + ".groups", null);
|
||||
groups.add("default");
|
||||
|
||||
if (!groups.isEmpty()) {
|
||||
for (String group : groups) {
|
||||
Set<String> groupPerms = userGroupPermissions.get(group);
|
||||
if (groupPerms != null) {
|
||||
permsCache.addAll(groupPerms);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
userPermissionsCache.put(key.toLowerCase(), permsCache);
|
||||
userGroups.put(key.toLowerCase(), new HashSet<String>(groups));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(String player, String permission) {
|
||||
int dotPos = permission.lastIndexOf(".");
|
||||
if (dotPos > -1) {
|
||||
if (hasPermission(player, permission.substring(0, dotPos))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Set<String> perms = userPermissionsCache.get(player.toLowerCase());
|
||||
if (perms == null) {
|
||||
return defaultPermissionsCache.contains(permission)
|
||||
|| defaultPermissionsCache.contains("*");
|
||||
}
|
||||
|
||||
return perms.contains("*") || perms.contains(permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(String worldName, String player, String permission) {
|
||||
return hasPermission(player, "worlds." + worldName + "." + permission)
|
||||
|| hasPermission(player, permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean inGroup(String player, String group) {
|
||||
Set<String> groups = userGroups.get(player.toLowerCase());
|
||||
if (groups == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return groups.contains(group);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getGroups(String player) {
|
||||
Set<String> groups = userGroups.get(player.toLowerCase());
|
||||
if (groups == null) {
|
||||
return new String[0];
|
||||
}
|
||||
|
||||
return groups.toArray(new String[groups.size()]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(OfflinePlayer player, String permission) {
|
||||
return hasPermission(player.getName(), permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(String worldName, OfflinePlayer player, String permission) {
|
||||
return hasPermission(worldName, player.getName(), permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean inGroup(OfflinePlayer player, String group) {
|
||||
return inGroup(player.getName(), group);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getGroups(OfflinePlayer player) {
|
||||
return getGroups(player.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDetectionMessage() {
|
||||
return "No known permissions plugin detected. Using configuration file for permissions.";
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,165 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.wepif;
|
||||
|
||||
import com.sk89q.util.yaml.YAMLProcessor;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.permissions.Permissible;
|
||||
import org.bukkit.permissions.Permission;
|
||||
import org.bukkit.permissions.PermissionAttachmentInfo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class DinnerPermsResolver implements PermissionsResolver {
|
||||
|
||||
public static final String GROUP_PREFIX = "group.";
|
||||
protected final Server server;
|
||||
|
||||
public DinnerPermsResolver(Server server) {
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
public static PermissionsResolver factory(Server server, YAMLProcessor config) {
|
||||
return new DinnerPermsResolver(server);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(String name, String permission) {
|
||||
return hasPermission(server.getOfflinePlayer(name), permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(String worldName, String name, String permission) {
|
||||
return hasPermission(worldName, server.getOfflinePlayer(name), permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean inGroup(String name, String group) {
|
||||
return inGroup(server.getOfflinePlayer(name), group);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getGroups(String name) {
|
||||
return getGroups(server.getOfflinePlayer(name));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(OfflinePlayer player, String permission) {
|
||||
Permissible perms = getPermissible(player);
|
||||
if (perms == null) {
|
||||
return false; // Permissions are only registered for objects with a Permissible
|
||||
}
|
||||
switch (internalHasPermission(perms, permission)) {
|
||||
case -1:
|
||||
return false;
|
||||
case 1:
|
||||
return true;
|
||||
}
|
||||
int dotPos = permission.lastIndexOf(".");
|
||||
while (dotPos > -1) {
|
||||
switch (internalHasPermission(perms, permission.substring(0, dotPos + 1) + "*")) {
|
||||
case -1:
|
||||
return false;
|
||||
case 1:
|
||||
return true;
|
||||
}
|
||||
dotPos = permission.lastIndexOf(".", dotPos - 1);
|
||||
}
|
||||
return internalHasPermission(perms, "*") == 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(String worldName, OfflinePlayer player, String permission) {
|
||||
return hasPermission(player, permission); // no per-world ability to check permissions in dinnerperms
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean inGroup(OfflinePlayer player, String group) {
|
||||
final Permissible perms = getPermissible(player);
|
||||
if (perms == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final String perm = GROUP_PREFIX + group;
|
||||
return perms.isPermissionSet(perm) && perms.hasPermission(perm);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getGroups(OfflinePlayer player) {
|
||||
Permissible perms = getPermissible(player);
|
||||
if (perms == null) {
|
||||
return new String[0];
|
||||
}
|
||||
List<String> groupNames = new ArrayList<String>();
|
||||
for (PermissionAttachmentInfo permAttach : perms.getEffectivePermissions()) {
|
||||
String perm = permAttach.getPermission();
|
||||
if (!(perm.startsWith(GROUP_PREFIX) && permAttach.getValue())) {
|
||||
continue;
|
||||
}
|
||||
groupNames.add(perm.substring(GROUP_PREFIX.length(), perm.length()));
|
||||
}
|
||||
return groupNames.toArray(new String[groupNames.size()]);
|
||||
}
|
||||
|
||||
public Permissible getPermissible(OfflinePlayer offline) {
|
||||
if (offline == null) return null;
|
||||
Permissible perm = null;
|
||||
if (offline instanceof Permissible) {
|
||||
perm = (Permissible) offline;
|
||||
} else {
|
||||
Player player = offline.getPlayer();
|
||||
if (player != null) perm = player;
|
||||
}
|
||||
return perm;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the permission from dinnerperms
|
||||
*
|
||||
* @param perms Permissible to check for
|
||||
* @param permission The permission to check
|
||||
* @return -1 if the permission is explicitly denied, 1 if the permission is allowed,
|
||||
* 0 if the permission is denied by a default.
|
||||
*/
|
||||
public int internalHasPermission(Permissible perms, String permission) {
|
||||
if (perms.isPermissionSet(permission)) {
|
||||
return perms.hasPermission(permission) ? 1 : -1;
|
||||
} else {
|
||||
Permission perm = server.getPluginManager().getPermission(permission);
|
||||
if (perm != null) {
|
||||
return perm.getDefault().getValue(perms.isOp()) ? 1 : 0;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDetectionMessage() {
|
||||
return "Using the Bukkit Permissions API.";
|
||||
}
|
||||
}
|
@ -0,0 +1,248 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.wepif;
|
||||
|
||||
import com.sk89q.util.yaml.YAMLProcessor;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.Server;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class FlatFilePermissionsResolver implements PermissionsResolver {
|
||||
|
||||
private static final Logger log = Logger.getLogger(FlatFilePermissionsResolver.class.getCanonicalName());
|
||||
|
||||
private Map<String, Set<String>> userPermissionsCache;
|
||||
private Set<String> defaultPermissionsCache;
|
||||
private Map<String, Set<String>> userGroups;
|
||||
|
||||
private final File groupFile;
|
||||
private final File userFile;
|
||||
|
||||
public static PermissionsResolver factory(Server server, YAMLProcessor config) {
|
||||
File groups = new File("perms_groups.txt");
|
||||
File users = new File("perms_users.txt");
|
||||
|
||||
if (!groups.exists() || !users.exists()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new FlatFilePermissionsResolver(groups, users);
|
||||
}
|
||||
|
||||
public FlatFilePermissionsResolver() {
|
||||
this(new File("perms_groups.txt"), new File("perms_users.txt"));
|
||||
}
|
||||
|
||||
public FlatFilePermissionsResolver(File groupFile, File userFile) {
|
||||
this.groupFile = groupFile;
|
||||
this.userFile = userFile;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static boolean filesExists() {
|
||||
return (new File("perms_groups.txt")).exists() && (new File("perms_users.txt")).exists();
|
||||
}
|
||||
|
||||
public Map<String, Set<String>> loadGroupPermissions() {
|
||||
Map<String, Set<String>> userGroupPermissions = new HashMap<String, Set<String>>();
|
||||
|
||||
BufferedReader buff = null;
|
||||
|
||||
try {
|
||||
FileReader input = new FileReader(this.groupFile);
|
||||
buff = new BufferedReader(input);
|
||||
|
||||
String line;
|
||||
while ((line = buff.readLine()) != null) {
|
||||
line = line.trim();
|
||||
|
||||
// Blank line
|
||||
if (line.isEmpty()) {
|
||||
continue;
|
||||
} else if (line.charAt(0) == ';' || line.charAt(0) == '#') {
|
||||
continue;
|
||||
}
|
||||
|
||||
String[] parts = line.split(":");
|
||||
|
||||
String key = parts[0];
|
||||
|
||||
if (parts.length > 1) {
|
||||
String[] perms = parts[1].split(",");
|
||||
|
||||
Set<String> groupPerms = new HashSet<String>(Arrays.asList(perms));
|
||||
userGroupPermissions.put(key, groupPerms);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.log(Level.WARNING, "Failed to load permissions", e);
|
||||
} finally {
|
||||
try {
|
||||
if (buff != null) {
|
||||
buff.close();
|
||||
}
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
return userGroupPermissions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
userGroups = new HashMap<String, Set<String>>();
|
||||
userPermissionsCache = new HashMap<String, Set<String>>();
|
||||
defaultPermissionsCache = new HashSet<String>();
|
||||
|
||||
Map<String, Set<String>> userGroupPermissions = loadGroupPermissions();
|
||||
|
||||
if (userGroupPermissions.containsKey("default")) {
|
||||
defaultPermissionsCache = userGroupPermissions.get("default");
|
||||
}
|
||||
|
||||
BufferedReader buff = null;
|
||||
|
||||
try {
|
||||
FileReader input = new FileReader(this.userFile);
|
||||
buff = new BufferedReader(input);
|
||||
|
||||
String line;
|
||||
while ((line = buff.readLine()) != null) {
|
||||
Set<String> permsCache = new HashSet<String>();
|
||||
|
||||
line = line.trim();
|
||||
|
||||
// Blank line
|
||||
if (line.isEmpty()) {
|
||||
continue;
|
||||
} else if (line.charAt(0) == ';' || line.charAt(0) == '#') {
|
||||
continue;
|
||||
}
|
||||
|
||||
String[] parts = line.split(":");
|
||||
|
||||
String key = parts[0];
|
||||
|
||||
if (parts.length > 1) {
|
||||
String[] groups = (parts[1] + ",default").split(",");
|
||||
String[] perms = parts.length > 2 ? parts[2].split(",") : new String[0];
|
||||
|
||||
permsCache.addAll(Arrays.asList(perms));
|
||||
|
||||
for (String group : groups) {
|
||||
Set<String> groupPerms = userGroupPermissions.get(group);
|
||||
if (groupPerms != null) {
|
||||
permsCache.addAll(groupPerms);
|
||||
}
|
||||
}
|
||||
|
||||
userPermissionsCache.put(key.toLowerCase(), permsCache);
|
||||
userGroups.put(key.toLowerCase(), new HashSet<String>(Arrays.asList(groups)));
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.log(Level.WARNING, "Failed to load permissions", e);
|
||||
} finally {
|
||||
try {
|
||||
if (buff != null) {
|
||||
buff.close();
|
||||
}
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(String player, String permission) {
|
||||
int dotPos = permission.lastIndexOf(".");
|
||||
if (dotPos > -1) {
|
||||
if (hasPermission(player, permission.substring(0, dotPos))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Set<String> perms = userPermissionsCache.get(player.toLowerCase());
|
||||
if (perms == null) {
|
||||
return defaultPermissionsCache.contains(permission)
|
||||
|| defaultPermissionsCache.contains("*");
|
||||
}
|
||||
|
||||
return perms.contains("*") || perms.contains(permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(String worldName, String player, String permission) {
|
||||
return hasPermission(player, "worlds." + worldName + "." + permission)
|
||||
|| hasPermission(player, permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean inGroup(String player, String group) {
|
||||
Set<String> groups = userGroups.get(player.toLowerCase());
|
||||
return groups != null && groups.contains(group);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getGroups(String player) {
|
||||
Set<String> groups = userGroups.get(player.toLowerCase());
|
||||
if (groups == null) {
|
||||
return new String[0];
|
||||
}
|
||||
|
||||
return groups.toArray(new String[groups.size()]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(OfflinePlayer player, String permission) {
|
||||
return hasPermission(player.getName(), permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(String worldName, OfflinePlayer player, String permission) {
|
||||
return hasPermission(worldName, player.getName(), permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean inGroup(OfflinePlayer player, String group) {
|
||||
return inGroup(player.getName(), group);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getGroups(OfflinePlayer player) {
|
||||
return getGroups(player.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDetectionMessage() {
|
||||
return "perms_groups.txt and perms_users.txt detected! Using flat file permissions.";
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,131 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.wepif;
|
||||
|
||||
import com.sk89q.util.yaml.YAMLProcessor;
|
||||
import org.anjocaido.groupmanager.dataholder.worlds.WorldsHolder;
|
||||
import org.anjocaido.groupmanager.permissions.AnjoPermissionsHandler;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.permissions.Permissible;
|
||||
|
||||
public class GroupManagerResolver extends DinnerPermsResolver {
|
||||
private final WorldsHolder worldsHolder;
|
||||
|
||||
public static PermissionsResolver factory(Server server, YAMLProcessor config) {
|
||||
try {
|
||||
WorldsHolder worldsHolder = server.getServicesManager().load(WorldsHolder.class);
|
||||
|
||||
if (worldsHolder == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new GroupManagerResolver(server, worldsHolder);
|
||||
} catch (Throwable t) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public GroupManagerResolver(Server server, WorldsHolder worldsHolder) {
|
||||
super(server);
|
||||
this.worldsHolder = worldsHolder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* True if the string is null or empty
|
||||
*/
|
||||
private boolean nameNotSafe(String perm) {
|
||||
return perm == null || perm.isEmpty();
|
||||
}
|
||||
|
||||
private AnjoPermissionsHandler getPermissionHandler(World world) {
|
||||
if (world != null) {
|
||||
return worldsHolder.getWorldPermissions(world.getName());
|
||||
} else {
|
||||
return worldsHolder.getDefaultWorld().getPermissionsHandler();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getGroups(String name) {
|
||||
AnjoPermissionsHandler permissionHandler = getPermissionHandler(null);
|
||||
if (permissionHandler == null) {
|
||||
return new String[0];
|
||||
}
|
||||
return permissionHandler.getGroups(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(OfflinePlayer player, String permission) {
|
||||
if (nameNotSafe(permission)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Permissible permissible = getPermissible(player);
|
||||
if (permissible == null) {
|
||||
return getPermissionHandler(player.getPlayer().getWorld()).permission(player.getName(), permission);
|
||||
} else {
|
||||
return permissible.hasPermission(permission);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(String worldName, OfflinePlayer player, String permission) {
|
||||
if (nameNotSafe(permission)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String name = player.getName();
|
||||
World world = worldName != null ? server.getWorld(worldName) : player.getPlayer().getWorld();
|
||||
|
||||
AnjoPermissionsHandler permissionHandler = getPermissionHandler(world);
|
||||
return permissionHandler != null && permissionHandler.permission(name, permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean inGroup(OfflinePlayer player, String group) {
|
||||
if (super.inGroup(player, group)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (nameNotSafe(group)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
AnjoPermissionsHandler permissionHandler = getPermissionHandler(null);
|
||||
return permissionHandler != null && permissionHandler.inGroup(player.getName(), group);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getGroups(OfflinePlayer player) {
|
||||
return getGroups(player.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDetectionMessage() {
|
||||
return "GroupManager detected! Using GroupManager for permissions.";
|
||||
}
|
||||
}
|
@ -0,0 +1,178 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.wepif;
|
||||
|
||||
import com.sk89q.util.yaml.YAMLProcessor;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.command.PluginCommand;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import com.nijikokun.bukkit.Permissions.Permissions;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class NijiPermissionsResolver implements PermissionsResolver {
|
||||
|
||||
private static final Logger log = Logger.getLogger(NijiPermissionsResolver.class.getCanonicalName());
|
||||
|
||||
private Server server;
|
||||
private Permissions api;
|
||||
|
||||
public static PermissionsResolver factory(Server server, YAMLProcessor config) {
|
||||
PluginManager pluginManager = server.getPluginManager();
|
||||
try {
|
||||
Class.forName("com.nijikokun.bukkit.Permissions.Permissions");
|
||||
} catch (ClassNotFoundException e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Plugin plugin = pluginManager.getPlugin("Permissions");
|
||||
|
||||
// Check if plugin is loaded and has Permissions interface
|
||||
if (plugin == null || !(plugin instanceof Permissions)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Check for fake permissions
|
||||
if (config.getBoolean("ignore-nijiperms-bridges", true) && isFakeNijiPerms(plugin)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new NijiPermissionsResolver(server, (Permissions) plugin);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
|
||||
}
|
||||
|
||||
public NijiPermissionsResolver(Server server, Permissions plugin) {
|
||||
this.server = server;
|
||||
this.api = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("static-access")
|
||||
public boolean hasPermission(String name, String permission) {
|
||||
try {
|
||||
Player player = server.getPlayerExact(name);
|
||||
if (player == null) return false;
|
||||
try {
|
||||
return api.getHandler().has(player, permission);
|
||||
} catch (Throwable t) {
|
||||
return api.Security.permission(player, permission);
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
log.log(Level.WARNING, "Failed to check permissions", t);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(String worldName, String name, String permission) {
|
||||
try {
|
||||
try {
|
||||
return api.getHandler().has(worldName, name, permission);
|
||||
} catch (Throwable t) {
|
||||
return api.getHandler().has(server.getPlayerExact(name), permission);
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
log.log(Level.WARNING, "Failed to check permissions", t);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("static-access")
|
||||
public boolean inGroup(String name, String group) {
|
||||
try {
|
||||
Player player = server.getPlayerExact(name);
|
||||
if (player == null) return false;
|
||||
try {
|
||||
return api.getHandler().inGroup(player.getWorld().getName(), name, group);
|
||||
} catch (Throwable t) {
|
||||
return api.Security.inGroup(name, group);
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
log.log(Level.WARNING, "Failed to check groups", t);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("static-access")
|
||||
public String[] getGroups(String name) {
|
||||
try {
|
||||
Player player = server.getPlayerExact(name);
|
||||
if (player == null) return new String[0];
|
||||
String[] groups = null;
|
||||
try {
|
||||
groups = api.getHandler().getGroups(player.getWorld().getName(), player.getName());
|
||||
} catch (Throwable t) {
|
||||
String group = api.Security.getGroup(player.getWorld().getName(), player.getName());
|
||||
if (group != null) groups = new String[] { group };
|
||||
}
|
||||
if (groups == null) {
|
||||
return new String[0];
|
||||
} else {
|
||||
return groups;
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
log.log(Level.WARNING, "Failed to get groups", t);
|
||||
return new String[0];
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(OfflinePlayer player, String permission) {
|
||||
return hasPermission(player.getName(), permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(String worldName, OfflinePlayer player, String permission) {
|
||||
return hasPermission(worldName, player.getName(), permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean inGroup(OfflinePlayer player, String group) {
|
||||
return inGroup(player.getName(), group);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getGroups(OfflinePlayer player) {
|
||||
return getGroups(player.getName());
|
||||
}
|
||||
|
||||
public static boolean isFakeNijiPerms(Plugin plugin) {
|
||||
PluginCommand permsCommand = Bukkit.getServer().getPluginCommand("permissions");
|
||||
|
||||
return permsCommand == null || !(permsCommand.getPlugin().equals(plugin));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDetectionMessage() {
|
||||
return "Permissions plugin detected! Using Permissions plugin for permissions.";
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,93 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.wepif;
|
||||
|
||||
import com.sk89q.util.yaml.YAMLProcessor;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.permissions.Permissible;
|
||||
import ru.tehkode.permissions.PermissionManager;
|
||||
import ru.tehkode.permissions.PermissionUser;
|
||||
|
||||
public class PermissionsExResolver extends DinnerPermsResolver {
|
||||
private final PermissionManager manager;
|
||||
|
||||
public static PermissionsResolver factory(Server server, YAMLProcessor config) {
|
||||
try {
|
||||
PermissionManager manager = server.getServicesManager().load(PermissionManager.class);
|
||||
|
||||
if (manager == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new PermissionsExResolver(server, manager);
|
||||
} catch (Throwable t) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public PermissionsExResolver(Server server, PermissionManager manager) {
|
||||
super(server);
|
||||
this.manager = manager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(String worldName, String name, String permission) {
|
||||
return manager.has(name, permission, worldName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(OfflinePlayer player, String permission) {
|
||||
Permissible permissible = getPermissible(player);
|
||||
if (permissible == null) {
|
||||
return manager.has(player.getName(), permission, null);
|
||||
} else {
|
||||
return permissible.hasPermission(permission);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(String worldName, OfflinePlayer player, String permission) {
|
||||
return hasPermission(worldName, player.getName(), permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean inGroup(OfflinePlayer player, String group) {
|
||||
return super.inGroup(player, group) || manager.getUser(player.getName()).inGroup(group);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getGroups(OfflinePlayer player) {
|
||||
if (getPermissible(player) == null) {
|
||||
PermissionUser user = manager.getUser(player.getName());
|
||||
if (user == null) {
|
||||
return new String[0];
|
||||
}
|
||||
return user.getGroupsNames();
|
||||
} else {
|
||||
return super.getGroups(player);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDetectionMessage() {
|
||||
return "PermissionsEx detected! Using PermissionsEx for permissions.";
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.wepif;
|
||||
|
||||
import org.bukkit.OfflinePlayer;
|
||||
|
||||
public interface PermissionsProvider {
|
||||
public boolean hasPermission(String name, String permission);
|
||||
|
||||
public boolean hasPermission(String worldName, String name, String permission);
|
||||
|
||||
public boolean inGroup(String player, String group);
|
||||
|
||||
public String[] getGroups(String player);
|
||||
|
||||
public boolean hasPermission(OfflinePlayer player, String permission);
|
||||
|
||||
public boolean hasPermission(String worldName, OfflinePlayer player, String permission);
|
||||
|
||||
public boolean inGroup(OfflinePlayer player, String group);
|
||||
|
||||
public String[] getGroups(OfflinePlayer player);
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.wepif;
|
||||
|
||||
public interface PermissionsResolver extends PermissionsProvider {
|
||||
public void load();
|
||||
|
||||
public String getDetectionMessage();
|
||||
}
|
@ -0,0 +1,307 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.wepif;
|
||||
|
||||
import com.sk89q.util.yaml.YAMLFormat;
|
||||
import com.sk89q.util.yaml.YAMLProcessor;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.server.PluginDisableEvent;
|
||||
import org.bukkit.event.server.PluginEnableEvent;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class PermissionsResolverManager implements PermissionsResolver {
|
||||
|
||||
private static final String CONFIG_HEADER = "#\r\n" +
|
||||
"# WEPIF Configuration File\r\n" +
|
||||
"#\r\n" +
|
||||
"# This file handles permissions configuration for every plugin using WEPIF\r\n" +
|
||||
"#\r\n" +
|
||||
"# About editing this file:\r\n" +
|
||||
"# - DO NOT USE TABS. You MUST use spaces or Bukkit will complain. If\r\n" +
|
||||
"# you use an editor like Notepad++ (recommended for Windows users), you\r\n" +
|
||||
"# must configure it to \"replace tabs with spaces.\" In Notepad++, this can\r\n" +
|
||||
"# be changed in Settings > Preferences > Language Menu.\r\n" +
|
||||
"# - Don't get rid of the indents. They are indented so some entries are\r\n" +
|
||||
"# in categories (like \"enforce-single-session\" is in the \"protection\"\r\n" +
|
||||
"# category.\r\n" +
|
||||
"# - If you want to check the format of this file before putting it\r\n" +
|
||||
"# into WEPIF, paste it into http://yaml-online-parser.appspot.com/\r\n" +
|
||||
"# and see if it gives \"ERROR:\".\r\n" +
|
||||
"# - Lines starting with # are comments and so they are ignored.\r\n" +
|
||||
"#\r\n" +
|
||||
"# About Configuration Permissions\r\n" +
|
||||
"# - See http://wiki.sk89q.com/wiki/WorldEdit/Permissions/Bukkit\r\n" +
|
||||
"# - Now with multiworld support (see example)\r\n" +
|
||||
"\r\n";
|
||||
|
||||
private static PermissionsResolverManager instance;
|
||||
|
||||
public static void initialize(Plugin plugin) {
|
||||
if (!isInitialized()) {
|
||||
instance = new PermissionsResolverManager(plugin);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isInitialized() {
|
||||
return instance != null;
|
||||
}
|
||||
|
||||
public static PermissionsResolverManager getInstance() {
|
||||
if (!isInitialized()) {
|
||||
throw new WEPIFRuntimeException("WEPIF has not yet been initialized!");
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
private Server server;
|
||||
private PermissionsResolver permissionResolver;
|
||||
private YAMLProcessor config;
|
||||
private Logger logger = Logger.getLogger(getClass().getCanonicalName());
|
||||
private List<Class<? extends PermissionsResolver>> enabledResolvers = new ArrayList<Class<? extends PermissionsResolver>>();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Class<? extends PermissionsResolver>[] availableResolvers = new Class[] {
|
||||
PluginPermissionsResolver.class,
|
||||
PermissionsExResolver.class,
|
||||
bPermissionsResolver.class,
|
||||
GroupManagerResolver.class,
|
||||
NijiPermissionsResolver.class,
|
||||
DinnerPermsResolver.class,
|
||||
FlatFilePermissionsResolver.class
|
||||
};
|
||||
|
||||
protected PermissionsResolverManager(Plugin plugin) {
|
||||
this.server = plugin.getServer();
|
||||
(new ServerListener()).register(plugin); // Register the events
|
||||
|
||||
loadConfig(new File("wepif.yml"));
|
||||
findResolver();
|
||||
}
|
||||
|
||||
public void findResolver() {
|
||||
for (Class<? extends PermissionsResolver> resolverClass : enabledResolvers) {
|
||||
try {
|
||||
Method factoryMethod = resolverClass.getMethod("factory", Server.class, YAMLProcessor.class);
|
||||
|
||||
this.permissionResolver = (PermissionsResolver) factoryMethod.invoke(null, this.server, this.config);
|
||||
|
||||
if (this.permissionResolver != null) {
|
||||
break;
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
logger.log(Level.WARNING, "Error in factory method for " + resolverClass.getSimpleName(), e);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (permissionResolver == null) {
|
||||
permissionResolver = new ConfigurationPermissionsResolver(config);
|
||||
}
|
||||
permissionResolver.load();
|
||||
logger.info("WEPIF: " + permissionResolver.getDetectionMessage());
|
||||
}
|
||||
|
||||
public void setPluginPermissionsResolver(Plugin plugin) {
|
||||
if (!(plugin instanceof PermissionsProvider)) {
|
||||
return;
|
||||
}
|
||||
|
||||
permissionResolver = new PluginPermissionsResolver((PermissionsProvider) plugin, plugin);
|
||||
logger.info("WEPIF: " + permissionResolver.getDetectionMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
findResolver();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(String name, String permission) {
|
||||
return permissionResolver.hasPermission(name, permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(String worldName, String name, String permission) {
|
||||
return permissionResolver.hasPermission(worldName, name, permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean inGroup(String player, String group) {
|
||||
return permissionResolver.inGroup(player, group);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getGroups(String player) {
|
||||
return permissionResolver.getGroups(player);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(OfflinePlayer player, String permission) {
|
||||
return permissionResolver.hasPermission(player, permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(String worldName, OfflinePlayer player, String permission) {
|
||||
return permissionResolver.hasPermission(worldName, player, permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean inGroup(OfflinePlayer player, String group) {
|
||||
return permissionResolver.inGroup(player, group);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getGroups(OfflinePlayer player) {
|
||||
return permissionResolver.getGroups(player);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDetectionMessage() {
|
||||
return "Using WEPIF for permissions";
|
||||
}
|
||||
|
||||
private boolean loadConfig(File file) {
|
||||
boolean isUpdated = false;
|
||||
if (!file.exists()) {
|
||||
try {
|
||||
file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
logger.log(Level.WARNING, "Failed to create new configuration file", e);
|
||||
}
|
||||
}
|
||||
config = new YAMLProcessor(file, false, YAMLFormat.EXTENDED);
|
||||
try {
|
||||
config.load();
|
||||
} catch (IOException e) {
|
||||
logger.log(Level.WARNING, "Error loading WEPIF configuration", e);
|
||||
}
|
||||
List<String> keys = config.getKeys(null);
|
||||
config.setHeader(CONFIG_HEADER);
|
||||
|
||||
if (!keys.contains("ignore-nijiperms-bridges")) {
|
||||
config.setProperty("ignore-nijiperms-bridges", true);
|
||||
isUpdated = true;
|
||||
}
|
||||
|
||||
if (!keys.contains("resolvers")) {
|
||||
//List<String> resolverKeys = config.getKeys("resolvers");
|
||||
List<String> resolvers = new ArrayList<String>();
|
||||
for (Class<?> clazz : availableResolvers) {
|
||||
resolvers.add(clazz.getSimpleName());
|
||||
}
|
||||
enabledResolvers.addAll(Arrays.asList(availableResolvers));
|
||||
config.setProperty("resolvers.enabled", resolvers);
|
||||
isUpdated = true;
|
||||
} else {
|
||||
List<String> disabledResolvers = config.getStringList("resolvers.disabled", new ArrayList<String>());
|
||||
List<String> stagedEnabled = config.getStringList("resolvers.enabled", null);
|
||||
for (Iterator<String> i = stagedEnabled.iterator(); i.hasNext();) {
|
||||
String nextName = i.next();
|
||||
Class<?> next = null;
|
||||
try {
|
||||
next = Class.forName(getClass().getPackage().getName() + "." + nextName);
|
||||
} catch (ClassNotFoundException e) {}
|
||||
|
||||
if (next == null || !PermissionsResolver.class.isAssignableFrom(next)) {
|
||||
logger.warning("WEPIF: Invalid or unknown class found in enabled resolvers: "
|
||||
+ nextName + ". Moving to disabled resolvers list.");
|
||||
i.remove();
|
||||
disabledResolvers.add(nextName);
|
||||
isUpdated = true;
|
||||
continue;
|
||||
}
|
||||
enabledResolvers.add(next.asSubclass(PermissionsResolver.class));
|
||||
}
|
||||
|
||||
for (Class<?> clazz : availableResolvers) {
|
||||
if (!stagedEnabled.contains(clazz.getSimpleName()) &&
|
||||
!disabledResolvers.contains(clazz.getSimpleName())) {
|
||||
disabledResolvers.add(clazz.getSimpleName());
|
||||
logger.info("New permissions resolver: "
|
||||
+ clazz.getSimpleName() + " detected. " +
|
||||
"Added to disabled resolvers list.");
|
||||
isUpdated = true;
|
||||
}
|
||||
}
|
||||
config.setProperty("resolvers.disabled", disabledResolvers);
|
||||
config.setProperty("resolvers.enabled", stagedEnabled);
|
||||
}
|
||||
|
||||
if (keys.contains("dinner-perms") || keys.contains("dinnerperms")) {
|
||||
config.removeProperty("dinner-perms");
|
||||
config.removeProperty("dinnerperms");
|
||||
isUpdated = true;
|
||||
}
|
||||
if (!keys.contains("permissions")) {
|
||||
ConfigurationPermissionsResolver.generateDefaultPerms(
|
||||
config.addNode("permissions"));
|
||||
isUpdated = true;
|
||||
}
|
||||
if (isUpdated) {
|
||||
logger.info("WEPIF: Updated config file");
|
||||
config.save();
|
||||
}
|
||||
return isUpdated;
|
||||
}
|
||||
|
||||
public static class MissingPluginException extends Exception {
|
||||
}
|
||||
|
||||
class ServerListener implements org.bukkit.event.Listener {
|
||||
@EventHandler
|
||||
public void onPluginEnable(PluginEnableEvent event) {
|
||||
Plugin plugin = event.getPlugin();
|
||||
String name = plugin.getDescription().getName();
|
||||
if (plugin instanceof PermissionsProvider) {
|
||||
setPluginPermissionsResolver(plugin);
|
||||
} else if ("permissions".equalsIgnoreCase(name) || "permissionsex".equalsIgnoreCase(name)
|
||||
|| "bpermissions".equalsIgnoreCase(name) || "groupmanager".equalsIgnoreCase(name)) {
|
||||
load();
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPluginDisable(PluginDisableEvent event) {
|
||||
String name = event.getPlugin().getDescription().getName();
|
||||
|
||||
if (event.getPlugin() instanceof PermissionsProvider
|
||||
|| "permissions".equalsIgnoreCase(name) || "permissionsex".equalsIgnoreCase(name)
|
||||
|| "bpermissions".equalsIgnoreCase(name) || "groupmanager".equalsIgnoreCase(name)) {
|
||||
load();
|
||||
}
|
||||
}
|
||||
|
||||
void register(Plugin plugin) {
|
||||
plugin.getServer().getPluginManager().registerEvents(this, plugin);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,105 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.wepif;
|
||||
|
||||
import com.sk89q.util.yaml.YAMLProcessor;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
|
||||
public class PluginPermissionsResolver implements PermissionsResolver {
|
||||
|
||||
protected PermissionsProvider resolver;
|
||||
protected Plugin plugin;
|
||||
|
||||
public static PermissionsResolver factory(Server server, YAMLProcessor config) {
|
||||
// Looking for service
|
||||
RegisteredServiceProvider<PermissionsProvider> serviceProvider = server.getServicesManager().getRegistration(PermissionsProvider.class);
|
||||
|
||||
if (serviceProvider != null) {
|
||||
return new PluginPermissionsResolver(serviceProvider.getProvider(), serviceProvider.getPlugin());
|
||||
}
|
||||
|
||||
// Looking for plugin
|
||||
for (Plugin plugin : server.getPluginManager().getPlugins()) {
|
||||
if (plugin instanceof PermissionsProvider) {
|
||||
return new PluginPermissionsResolver((PermissionsProvider) plugin, plugin);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public PluginPermissionsResolver(PermissionsProvider resolver, Plugin permissionsPlugin) {
|
||||
this.resolver = resolver;
|
||||
this.plugin = permissionsPlugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(String name, String permission) {
|
||||
return resolver.hasPermission(name, permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(String worldName, String name, String permission) {
|
||||
return resolver.hasPermission(worldName, name, permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean inGroup(String player, String group) {
|
||||
return resolver.inGroup(player, group);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getGroups(String player) {
|
||||
return resolver.getGroups(player);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(OfflinePlayer player, String permission) {
|
||||
return resolver.hasPermission(player, permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(String worldName, OfflinePlayer player, String permission) {
|
||||
return resolver.hasPermission(worldName, player, permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean inGroup(OfflinePlayer player, String group) {
|
||||
return resolver.inGroup(player, group);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getGroups(OfflinePlayer player) {
|
||||
return resolver.getGroups(player);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDetectionMessage() {
|
||||
return "Using plugin '" + this.plugin.getDescription().getName() + "' for permissions.";
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.wepif;
|
||||
|
||||
public class WEPIFRuntimeException extends RuntimeException {
|
||||
|
||||
public WEPIFRuntimeException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,111 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.wepif;
|
||||
|
||||
import com.sk89q.util.yaml.YAMLProcessor;
|
||||
import de.bananaco.bpermissions.api.ApiLayer;
|
||||
import de.bananaco.bpermissions.api.util.CalculableType;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class bPermissionsResolver implements PermissionsResolver {
|
||||
|
||||
public static PermissionsResolver factory(Server server, YAMLProcessor config) {
|
||||
try {
|
||||
Class.forName("de.bananaco.bpermissions.api.ApiLayer");
|
||||
} catch (ClassNotFoundException e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new bPermissionsResolver(server);
|
||||
}
|
||||
|
||||
private final Server server;
|
||||
|
||||
public bPermissionsResolver(Server server) {
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDetectionMessage() {
|
||||
return "bPermissions detected! Using bPermissions for permissions";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(String name, String permission) {
|
||||
return hasPermission(server.getOfflinePlayer(name), permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(String worldName, String name, String permission) {
|
||||
return ApiLayer.hasPermission(worldName, CalculableType.USER, name, permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean inGroup(String player, String group) {
|
||||
return inGroup(server.getOfflinePlayer(player), group);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getGroups(String player) {
|
||||
return getGroups(server.getOfflinePlayer(player));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(OfflinePlayer player, String permission) {
|
||||
Player onlinePlayer = player.getPlayer();
|
||||
if (onlinePlayer == null) {
|
||||
return ApiLayer.hasPermission(null, CalculableType.USER, player.getName(), permission);
|
||||
} else {
|
||||
return ApiLayer.hasPermission(onlinePlayer.getWorld().getName(), CalculableType.USER, player.getName(), permission);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(String worldName, OfflinePlayer player, String permission) {
|
||||
return hasPermission(worldName, player.getName(), permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean inGroup(OfflinePlayer player, String group) {
|
||||
Player onlinePlayer = player.getPlayer();
|
||||
if (onlinePlayer == null) {
|
||||
return ApiLayer.hasGroupRecursive(null, CalculableType.USER, player.getName(), group);
|
||||
} else {
|
||||
return ApiLayer.hasGroupRecursive(onlinePlayer.getWorld().getName(), CalculableType.USER, player.getName(), group);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getGroups(OfflinePlayer player) {
|
||||
Player onlinePlayer = player.getPlayer();
|
||||
if (onlinePlayer == null) {
|
||||
return ApiLayer.getGroups(null, CalculableType.USER, player.getName());
|
||||
} else {
|
||||
return ApiLayer.getGroups(onlinePlayer.getWorld().getName(), CalculableType.USER, player.getName());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,165 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.bukkit;
|
||||
|
||||
import com.sk89q.worldedit.Vector;
|
||||
import com.sk89q.worldedit.entity.Entity;
|
||||
import com.sk89q.worldedit.util.Location;
|
||||
import com.sk89q.worldedit.world.World;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
/**
|
||||
* Adapts between Bukkit and WorldEdit equivalent objects.
|
||||
*/
|
||||
final class BukkitAdapter {
|
||||
|
||||
private BukkitAdapter() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert any WorldEdit world into an equivalent wrapped Bukkit world.
|
||||
*
|
||||
* <p>If a matching world cannot be found, a {@link RuntimeException}
|
||||
* will be thrown.</p>
|
||||
*
|
||||
* @param world the world
|
||||
* @return a wrapped Bukkit world
|
||||
*/
|
||||
public static BukkitWorld asBukkitWorld(World world) {
|
||||
if (world instanceof BukkitWorld) {
|
||||
return (BukkitWorld) world;
|
||||
} else {
|
||||
BukkitWorld bukkitWorld = WorldEditPlugin.getInstance().getInternalPlatform().matchWorld(world);
|
||||
if (bukkitWorld == null) {
|
||||
throw new RuntimeException("World '" + world.getName() + "' has no matching version in Bukkit");
|
||||
}
|
||||
return bukkitWorld;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a WorldEdit world from a Bukkit world.
|
||||
*
|
||||
* @param world the Bukkit world
|
||||
* @return a WorldEdit world
|
||||
*/
|
||||
public static World adapt(org.bukkit.World world) {
|
||||
checkNotNull(world);
|
||||
return new BukkitWorld(world);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a Bukkit world from a WorldEdit world.
|
||||
*
|
||||
* @param world the WorldEdit world
|
||||
* @return a Bukkit world
|
||||
*/
|
||||
public static org.bukkit.World adapt(World world) {
|
||||
checkNotNull(world);
|
||||
if (world instanceof BukkitWorld) {
|
||||
return ((BukkitWorld) world).getWorld();
|
||||
} else {
|
||||
org.bukkit.World match = Bukkit.getServer().getWorld(world.getName());
|
||||
if (match != null) {
|
||||
return match;
|
||||
} else {
|
||||
throw new IllegalArgumentException("Can't find a Bukkit world for " + world);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a WorldEdit location from a Bukkit location.
|
||||
*
|
||||
* @param location the Bukkit location
|
||||
* @return a WorldEdit location
|
||||
*/
|
||||
public static Location adapt(org.bukkit.Location location) {
|
||||
checkNotNull(location);
|
||||
Vector position = BukkitUtil.toVector(location);
|
||||
return new com.sk89q.worldedit.util.Location(
|
||||
adapt(location.getWorld()),
|
||||
position,
|
||||
location.getYaw(),
|
||||
location.getPitch());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a Bukkit location from a WorldEdit location.
|
||||
*
|
||||
* @param location the WorldEdit location
|
||||
* @return a Bukkit location
|
||||
*/
|
||||
public static org.bukkit.Location adapt(Location location) {
|
||||
checkNotNull(location);
|
||||
Vector position = location.toVector();
|
||||
return new org.bukkit.Location(
|
||||
adapt((World) location.getExtent()),
|
||||
position.getX(), position.getY(), position.getZ(),
|
||||
location.getYaw(),
|
||||
location.getPitch());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a Bukkit location from a WorldEdit position with a Bukkit world.
|
||||
*
|
||||
* @param world the Bukkit world
|
||||
* @param position the WorldEdit position
|
||||
* @return a Bukkit location
|
||||
*/
|
||||
public static org.bukkit.Location adapt(org.bukkit.World world, Vector position) {
|
||||
checkNotNull(world);
|
||||
checkNotNull(position);
|
||||
return new org.bukkit.Location(
|
||||
world,
|
||||
position.getX(), position.getY(), position.getZ());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a Bukkit location from a WorldEdit location with a Bukkit world.
|
||||
*
|
||||
* @param world the Bukkit world
|
||||
* @param location the WorldEdit location
|
||||
* @return a Bukkit location
|
||||
*/
|
||||
public static org.bukkit.Location adapt(org.bukkit.World world, Location location) {
|
||||
checkNotNull(world);
|
||||
checkNotNull(location);
|
||||
return new org.bukkit.Location(
|
||||
world,
|
||||
location.getX(), location.getY(), location.getZ(),
|
||||
location.getYaw(),
|
||||
location.getPitch());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a WorldEdit entity from a Bukkit entity.
|
||||
*
|
||||
* @param entity the Bukkit entity
|
||||
* @return a WorldEdit entity
|
||||
*/
|
||||
public static Entity adapt(org.bukkit.entity.Entity entity) {
|
||||
checkNotNull(entity);
|
||||
return new BukkitEntity(entity);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,79 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.bukkit;
|
||||
|
||||
import com.sk89q.worldedit.bukkit.adapter.BukkitImplAdapter;
|
||||
import com.sk89q.worldedit.world.biome.BaseBiome;
|
||||
import com.sk89q.worldedit.world.biome.BiomeData;
|
||||
import com.sk89q.worldedit.world.registry.BiomeRegistry;
|
||||
import org.bukkit.block.Biome;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A biome registry for Bukkit.
|
||||
*/
|
||||
class BukkitBiomeRegistry implements BiomeRegistry {
|
||||
|
||||
BukkitBiomeRegistry() {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public BaseBiome createFromId(int id) {
|
||||
return new BaseBiome(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<BaseBiome> getBiomes() {
|
||||
BukkitImplAdapter adapter = WorldEditPlugin.getInstance().getBukkitImplAdapter();
|
||||
if (adapter != null) {
|
||||
List<BaseBiome> biomes = new ArrayList<BaseBiome>();
|
||||
for (Biome biome : Biome.values()) {
|
||||
int biomeId = adapter.getBiomeId(biome);
|
||||
biomes.add(new BaseBiome(biomeId));
|
||||
}
|
||||
return biomes;
|
||||
} else {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public BiomeData getData(BaseBiome biome) {
|
||||
BukkitImplAdapter adapter = WorldEditPlugin.getInstance().getBukkitImplAdapter();
|
||||
if (adapter != null) {
|
||||
final Biome bukkitBiome = adapter.getBiome(biome.getId());
|
||||
return new BiomeData() {
|
||||
@Override
|
||||
public String getName() {
|
||||
return bukkitBiome.name();
|
||||
}
|
||||
};
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.bukkit;
|
||||
|
||||
import com.sk89q.bukkit.util.CommandInspector;
|
||||
import com.sk89q.minecraft.util.commands.CommandLocals;
|
||||
import com.sk89q.worldedit.extension.platform.Actor;
|
||||
import com.sk89q.worldedit.util.command.CommandMapping;
|
||||
import com.sk89q.worldedit.util.command.Description;
|
||||
import com.sk89q.worldedit.util.command.Dispatcher;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
class BukkitCommandInspector implements CommandInspector {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(BukkitCommandInspector.class.getCanonicalName());
|
||||
private final WorldEditPlugin plugin;
|
||||
private final Dispatcher dispatcher;
|
||||
|
||||
BukkitCommandInspector(WorldEditPlugin plugin, Dispatcher dispatcher) {
|
||||
checkNotNull(plugin);
|
||||
checkNotNull(dispatcher);
|
||||
this.plugin = plugin;
|
||||
this.dispatcher = dispatcher;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getShortText(Command command) {
|
||||
CommandMapping mapping = dispatcher.get(command.getName());
|
||||
if (mapping != null) {
|
||||
return mapping.getDescription().getShortDescription();
|
||||
} else {
|
||||
logger.warning("BukkitCommandInspector doesn't know how about the command '" + command + "'");
|
||||
return "Help text not available";
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFullText(Command command) {
|
||||
CommandMapping mapping = dispatcher.get(command.getName());
|
||||
if (mapping != null) {
|
||||
Description description = mapping.getDescription();
|
||||
return "Usage: " + description.getUsage() + (description.getHelp() != null ? "\n" + description.getHelp() : "");
|
||||
} else {
|
||||
logger.warning("BukkitCommandInspector doesn't know how about the command '" + command + "'");
|
||||
return "Help text not available";
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean testPermission(CommandSender sender, Command command) {
|
||||
CommandMapping mapping = dispatcher.get(command.getName());
|
||||
if (mapping != null) {
|
||||
CommandLocals locals = new CommandLocals();
|
||||
locals.put(Actor.class, plugin.wrapCommandSender(sender));
|
||||
return mapping.getCallable().testPermission(locals);
|
||||
} else {
|
||||
logger.warning("BukkitCommandInspector doesn't know how about the command '" + command + "'");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,156 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.bukkit;
|
||||
|
||||
import com.sk89q.worldedit.session.SessionKey;
|
||||
import com.sk89q.worldedit.util.auth.AuthorizationException;
|
||||
import com.sk89q.worldedit.extension.platform.Actor;
|
||||
import com.sk89q.worldedit.internal.cui.CUIEvent;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.io.File;
|
||||
import java.util.UUID;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
public class BukkitCommandSender implements Actor {
|
||||
|
||||
/**
|
||||
* One time generated ID.
|
||||
*/
|
||||
private static final UUID DEFAULT_ID = UUID.fromString("a233eb4b-4cab-42cd-9fd9-7e7b9a3f74be");
|
||||
|
||||
private CommandSender sender;
|
||||
private WorldEditPlugin plugin;
|
||||
|
||||
public BukkitCommandSender(WorldEditPlugin plugin, CommandSender sender) {
|
||||
checkNotNull(plugin);
|
||||
checkNotNull(sender);
|
||||
checkArgument(!(sender instanceof Player), "Cannot wrap a player");
|
||||
|
||||
this.plugin = plugin;
|
||||
this.sender = sender;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID getUniqueId() {
|
||||
return DEFAULT_ID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return sender.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void printRaw(String msg) {
|
||||
for (String part : msg.split("\n")) {
|
||||
sender.sendMessage(part);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void print(String msg) {
|
||||
for (String part : msg.split("\n")) {
|
||||
sender.sendMessage("\u00A7d" + part);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void printDebug(String msg) {
|
||||
for (String part : msg.split("\n")) {
|
||||
sender.sendMessage("\u00A77" + part);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void printError(String msg) {
|
||||
for (String part : msg.split("\n")) {
|
||||
sender.sendMessage("\u00A7c" + part);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canDestroyBedrock() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getGroups() {
|
||||
return new String[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(String perm) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkPermission(String permission) throws AuthorizationException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPlayer() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public File openFileOpenDialog(String[] extensions) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public File openFileSaveDialog(String[] extensions) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispatchCUIEvent(CUIEvent event) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public SessionKey getSessionKey() {
|
||||
return new SessionKey() {
|
||||
@Nullable
|
||||
@Override
|
||||
public String getName() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isActive() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPersistent() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID getUniqueId() {
|
||||
return DEFAULT_ID;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.bukkit;
|
||||
|
||||
import com.sk89q.util.yaml.YAMLProcessor;
|
||||
import com.sk89q.worldedit.util.YAMLConfiguration;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* YAMLConfiguration but with setting for no op permissions and plugin root data folder
|
||||
*/
|
||||
public class BukkitConfiguration extends YAMLConfiguration {
|
||||
|
||||
public boolean noOpPermissions = false;
|
||||
private final WorldEditPlugin plugin;
|
||||
|
||||
public BukkitConfiguration(YAMLProcessor config, WorldEditPlugin plugin) {
|
||||
super(config, plugin.getLogger());
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
super.load();
|
||||
noOpPermissions = config.getBoolean("no-op-permissions", false);
|
||||
migrateLegacyFolders();
|
||||
}
|
||||
|
||||
private void migrateLegacyFolders() {
|
||||
migrate(scriptsDir, "craftscripts");
|
||||
migrate(saveDir, "schematics");
|
||||
migrate("drawings", "draw.js images");
|
||||
}
|
||||
|
||||
private void migrate(String file, String name) {
|
||||
File fromDir = new File(".", file);
|
||||
File toDir = new File(getWorkingDirectory(), file);
|
||||
if (fromDir.exists() & !toDir.exists()) {
|
||||
if (fromDir.renameTo(toDir)) {
|
||||
plugin.getLogger().info("Migrated " + name + " folder '" + file +
|
||||
"' from server root to plugin data folder.");
|
||||
} else {
|
||||
plugin.getLogger().warning("Error while migrating " + name + " folder!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public File getWorkingDirectory() {
|
||||
return plugin.getDataFolder();
|
||||
}
|
||||
}
|
@ -0,0 +1,114 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.bukkit;
|
||||
|
||||
import com.sk89q.worldedit.bukkit.adapter.BukkitImplAdapter;
|
||||
import com.sk89q.worldedit.entity.BaseEntity;
|
||||
import com.sk89q.worldedit.entity.Entity;
|
||||
import com.sk89q.worldedit.entity.Player;
|
||||
import com.sk89q.worldedit.entity.metadata.EntityType;
|
||||
import com.sk89q.worldedit.extent.Extent;
|
||||
import com.sk89q.worldedit.util.Location;
|
||||
import com.sk89q.worldedit.world.NullWorld;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
/**
|
||||
* An adapter to adapt a Bukkit entity into a WorldEdit one.
|
||||
*/
|
||||
class BukkitEntity implements Entity {
|
||||
|
||||
private final WeakReference<org.bukkit.entity.Entity> entityRef;
|
||||
|
||||
/**
|
||||
* Create a new instance.
|
||||
*
|
||||
* @param entity the entity
|
||||
*/
|
||||
BukkitEntity(org.bukkit.entity.Entity entity) {
|
||||
checkNotNull(entity);
|
||||
this.entityRef = new WeakReference<org.bukkit.entity.Entity>(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Extent getExtent() {
|
||||
org.bukkit.entity.Entity entity = entityRef.get();
|
||||
if (entity != null) {
|
||||
return BukkitAdapter.adapt(entity.getWorld());
|
||||
} else {
|
||||
return NullWorld.getInstance();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Location getLocation() {
|
||||
org.bukkit.entity.Entity entity = entityRef.get();
|
||||
if (entity != null) {
|
||||
return BukkitAdapter.adapt(entity.getLocation());
|
||||
} else {
|
||||
return new Location(NullWorld.getInstance());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseEntity getState() {
|
||||
org.bukkit.entity.Entity entity = entityRef.get();
|
||||
if (entity != null) {
|
||||
if (entity instanceof Player) {
|
||||
return null;
|
||||
}
|
||||
|
||||
BukkitImplAdapter adapter = WorldEditPlugin.getInstance().getBukkitImplAdapter();
|
||||
if (adapter != null) {
|
||||
return adapter.getEntity(entity);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove() {
|
||||
org.bukkit.entity.Entity entity = entityRef.get();
|
||||
if (entity != null) {
|
||||
entity.remove();
|
||||
return entity.isDead();
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Nullable
|
||||
@Override
|
||||
public <T> T getFacet(Class<? extends T> cls) {
|
||||
org.bukkit.entity.Entity entity = entityRef.get();
|
||||
if (entity != null && EntityType.class.isAssignableFrom(cls)) {
|
||||
return (T) new BukkitEntityType(entity);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,140 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.bukkit;
|
||||
|
||||
import com.sk89q.worldedit.entity.metadata.EntityType;
|
||||
import com.sk89q.worldedit.util.Enums;
|
||||
import org.bukkit.entity.Ambient;
|
||||
import org.bukkit.entity.Animals;
|
||||
import org.bukkit.entity.Boat;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.ExperienceOrb;
|
||||
import org.bukkit.entity.FallingBlock;
|
||||
import org.bukkit.entity.Golem;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.entity.Item;
|
||||
import org.bukkit.entity.ItemFrame;
|
||||
import org.bukkit.entity.LivingEntity;
|
||||
import org.bukkit.entity.Minecart;
|
||||
import org.bukkit.entity.Painting;
|
||||
import org.bukkit.entity.Projectile;
|
||||
import org.bukkit.entity.TNTPrimed;
|
||||
import org.bukkit.entity.Tameable;
|
||||
import org.bukkit.entity.Villager;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
class BukkitEntityType implements EntityType {
|
||||
|
||||
private static final org.bukkit.entity.EntityType tntMinecartType =
|
||||
Enums.findByValue(org.bukkit.entity.EntityType.class, "MINECART_TNT");
|
||||
|
||||
private final Entity entity;
|
||||
|
||||
BukkitEntityType(Entity entity) {
|
||||
checkNotNull(entity);
|
||||
this.entity = entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPlayerDerived() {
|
||||
return entity instanceof HumanEntity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isProjectile() {
|
||||
return entity instanceof Projectile;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isItem() {
|
||||
return entity instanceof Item;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFallingBlock() {
|
||||
return entity instanceof FallingBlock;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPainting() {
|
||||
return entity instanceof Painting;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isItemFrame() {
|
||||
return entity instanceof ItemFrame;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBoat() {
|
||||
return entity instanceof Boat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMinecart() {
|
||||
return entity instanceof Minecart;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTNT() {
|
||||
return entity instanceof TNTPrimed || entity.getType() == tntMinecartType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isExperienceOrb() {
|
||||
return entity instanceof ExperienceOrb;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLiving() {
|
||||
return entity instanceof LivingEntity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAnimal() {
|
||||
return entity instanceof Animals;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAmbient() {
|
||||
return entity instanceof Ambient;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNPC() {
|
||||
return entity instanceof Villager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isGolem() {
|
||||
return entity instanceof Golem;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTamed() {
|
||||
return entity instanceof Tameable && ((Tameable) entity).isTamed();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTagged() {
|
||||
return entity instanceof LivingEntity && ((LivingEntity) entity).getCustomName() != null;
|
||||
}
|
||||
}
|
@ -0,0 +1,248 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.bukkit;
|
||||
|
||||
import com.sk89q.util.StringUtil;
|
||||
import com.sk89q.worldedit.LocalPlayer;
|
||||
import com.sk89q.worldedit.LocalWorld;
|
||||
import com.sk89q.worldedit.ServerInterface;
|
||||
import com.sk89q.worldedit.Vector;
|
||||
import com.sk89q.worldedit.WorldEditException;
|
||||
import com.sk89q.worldedit.WorldVector;
|
||||
import com.sk89q.worldedit.blocks.BaseBlock;
|
||||
import com.sk89q.worldedit.entity.BaseEntity;
|
||||
import com.sk89q.worldedit.extent.inventory.BlockBag;
|
||||
import com.sk89q.worldedit.internal.cui.CUIEvent;
|
||||
import com.sk89q.worldedit.session.SessionKey;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.UUID;
|
||||
|
||||
public class BukkitPlayer extends LocalPlayer {
|
||||
|
||||
private Player player;
|
||||
private WorldEditPlugin plugin;
|
||||
|
||||
public BukkitPlayer(WorldEditPlugin plugin, ServerInterface server, Player player) {
|
||||
this.plugin = plugin;
|
||||
this.player = player;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID getUniqueId() {
|
||||
return player.getUniqueId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemInHand() {
|
||||
ItemStack itemStack = player.getItemInHand();
|
||||
return itemStack != null ? itemStack.getTypeId() : 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseBlock getBlockInHand() throws WorldEditException {
|
||||
ItemStack itemStack = player.getItemInHand();
|
||||
return BukkitUtil.toBlock(getWorld(), itemStack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return player.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public WorldVector getPosition() {
|
||||
Location loc = player.getLocation();
|
||||
return new WorldVector(BukkitUtil.getLocalWorld(loc.getWorld()),
|
||||
loc.getX(), loc.getY(), loc.getZ());
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getPitch() {
|
||||
return player.getLocation().getPitch();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getYaw() {
|
||||
return player.getLocation().getYaw();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void giveItem(int type, int amt) {
|
||||
player.getInventory().addItem(new ItemStack(type, amt));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void printRaw(String msg) {
|
||||
for (String part : msg.split("\n")) {
|
||||
player.sendMessage(part);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void print(String msg) {
|
||||
for (String part : msg.split("\n")) {
|
||||
player.sendMessage("\u00A7d" + part);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void printDebug(String msg) {
|
||||
for (String part : msg.split("\n")) {
|
||||
player.sendMessage("\u00A77" + part);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void printError(String msg) {
|
||||
for (String part : msg.split("\n")) {
|
||||
player.sendMessage("\u00A7c" + part);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPosition(Vector pos, float pitch, float yaw) {
|
||||
player.teleport(new Location(player.getWorld(), pos.getX(), pos.getY(),
|
||||
pos.getZ(), yaw, pitch));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getGroups() {
|
||||
return plugin.getPermissionsResolver().getGroups(player);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockBag getInventoryBlockBag() {
|
||||
return new BukkitPlayerBlockBag(player);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(String perm) {
|
||||
return (!plugin.getLocalConfiguration().noOpPermissions && player.isOp())
|
||||
|| plugin.getPermissionsResolver().hasPermission(
|
||||
player.getWorld().getName(), player, perm);
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalWorld getWorld() {
|
||||
return BukkitUtil.getLocalWorld(player.getWorld());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispatchCUIEvent(CUIEvent event) {
|
||||
String[] params = event.getParameters();
|
||||
String send = event.getTypeId();
|
||||
if (params.length > 0) {
|
||||
send = send + "|" + StringUtil.joinString(params, "|");
|
||||
}
|
||||
player.sendPluginMessage(plugin, WorldEditPlugin.CUI_PLUGIN_CHANNEL, send.getBytes(CUIChannelListener.UTF_8_CHARSET));
|
||||
}
|
||||
|
||||
public Player getPlayer() {
|
||||
return player;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasCreativeMode() {
|
||||
return player.getGameMode() == GameMode.CREATIVE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void floatAt(int x, int y, int z, boolean alwaysGlass) {
|
||||
if (alwaysGlass || !player.getAllowFlight()) {
|
||||
super.floatAt(x, y, z, alwaysGlass);
|
||||
return;
|
||||
}
|
||||
|
||||
setPosition(new Vector(x + 0.5, y, z + 0.5));
|
||||
player.setFlying(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseEntity getState() {
|
||||
throw new UnsupportedOperationException("Cannot create a state from this object");
|
||||
}
|
||||
|
||||
@Override
|
||||
public com.sk89q.worldedit.util.Location getLocation() {
|
||||
Location nativeLocation = player.getLocation();
|
||||
Vector position = BukkitUtil.toVector(nativeLocation);
|
||||
return new com.sk89q.worldedit.util.Location(
|
||||
getWorld(),
|
||||
position,
|
||||
nativeLocation.getYaw(),
|
||||
nativeLocation.getPitch());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public <T> T getFacet(Class<? extends T> cls) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SessionKey getSessionKey() {
|
||||
return new SessionKeyImpl(this.player.getUniqueId(), player.getName());
|
||||
}
|
||||
|
||||
private static class SessionKeyImpl implements SessionKey {
|
||||
// If not static, this will leak a reference
|
||||
|
||||
private final UUID uuid;
|
||||
private final String name;
|
||||
|
||||
private SessionKeyImpl(UUID uuid, String name) {
|
||||
this.uuid = uuid;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID getUniqueId() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isActive() {
|
||||
// This is a thread safe call on CraftBukkit because it uses a
|
||||
// CopyOnWrite list for the list of players, but the Bukkit
|
||||
// specification doesn't require thread safety (though the
|
||||
// spec is extremely incomplete)
|
||||
return Bukkit.getServer().getPlayerExact(name) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPersistent() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,201 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.bukkit;
|
||||
|
||||
import com.sk89q.worldedit.WorldVector;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import com.sk89q.worldedit.extent.inventory.*;
|
||||
import com.sk89q.worldedit.blocks.BaseItem;
|
||||
import com.sk89q.worldedit.blocks.BaseItemStack;
|
||||
import com.sk89q.worldedit.blocks.BlockID;
|
||||
import com.sk89q.worldedit.blocks.ItemType;
|
||||
|
||||
public class BukkitPlayerBlockBag extends BlockBag {
|
||||
|
||||
private Player player;
|
||||
private ItemStack[] items;
|
||||
|
||||
/**
|
||||
* Construct the object.
|
||||
*
|
||||
* @param player the player
|
||||
*/
|
||||
public BukkitPlayerBlockBag(Player player) {
|
||||
this.player = player;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads inventory on first use.
|
||||
*/
|
||||
private void loadInventory() {
|
||||
if (items == null) {
|
||||
items = player.getInventory().getContents();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the player.
|
||||
*
|
||||
* @return the player
|
||||
*/
|
||||
public Player getPlayer() {
|
||||
return player;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fetchItem(BaseItem item) throws BlockBagException {
|
||||
final int id = item.getType();
|
||||
final int damage = item.getData();
|
||||
int amount = (item instanceof BaseItemStack) ? ((BaseItemStack) item).getAmount() : 1;
|
||||
assert(amount == 1);
|
||||
boolean usesDamageValue = ItemType.usesDamageValue(id);
|
||||
|
||||
if (id == BlockID.AIR) {
|
||||
throw new IllegalArgumentException("Can't fetch air block");
|
||||
}
|
||||
|
||||
loadInventory();
|
||||
|
||||
boolean found = false;
|
||||
|
||||
for (int slot = 0; slot < items.length; ++slot) {
|
||||
ItemStack bukkitItem = items[slot];
|
||||
|
||||
if (bukkitItem == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (bukkitItem.getTypeId() != id) {
|
||||
// Type id doesn't fit
|
||||
continue;
|
||||
}
|
||||
|
||||
if (usesDamageValue && bukkitItem.getDurability() != damage) {
|
||||
// Damage value doesn't fit.
|
||||
continue;
|
||||
}
|
||||
|
||||
int currentAmount = bukkitItem.getAmount();
|
||||
if (currentAmount < 0) {
|
||||
// Unlimited
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentAmount > 1) {
|
||||
bukkitItem.setAmount(currentAmount - 1);
|
||||
found = true;
|
||||
} else {
|
||||
items[slot] = null;
|
||||
found = true;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
throw new OutOfBlocksException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void storeItem(BaseItem item) throws BlockBagException {
|
||||
final int id = item.getType();
|
||||
final int damage = item.getData();
|
||||
int amount = (item instanceof BaseItemStack) ? ((BaseItemStack) item).getAmount() : 1;
|
||||
assert(amount <= 64);
|
||||
boolean usesDamageValue = ItemType.usesDamageValue(id);
|
||||
|
||||
if (id == BlockID.AIR) {
|
||||
throw new IllegalArgumentException("Can't store air block");
|
||||
}
|
||||
|
||||
loadInventory();
|
||||
|
||||
int freeSlot = -1;
|
||||
|
||||
for (int slot = 0; slot < items.length; ++slot) {
|
||||
ItemStack bukkitItem = items[slot];
|
||||
|
||||
if (bukkitItem == null) {
|
||||
// Delay using up a free slot until we know there are no stacks
|
||||
// of this item to merge into
|
||||
|
||||
if (freeSlot == -1) {
|
||||
freeSlot = slot;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (bukkitItem.getTypeId() != id) {
|
||||
// Type id doesn't fit
|
||||
continue;
|
||||
}
|
||||
|
||||
if (usesDamageValue && bukkitItem.getDurability() != damage) {
|
||||
// Damage value doesn't fit.
|
||||
continue;
|
||||
}
|
||||
|
||||
int currentAmount = bukkitItem.getAmount();
|
||||
if (currentAmount < 0) {
|
||||
// Unlimited
|
||||
return;
|
||||
}
|
||||
if (currentAmount >= 64) {
|
||||
// Full stack
|
||||
continue;
|
||||
}
|
||||
|
||||
int spaceLeft = 64 - currentAmount;
|
||||
if (spaceLeft >= amount) {
|
||||
bukkitItem.setAmount(currentAmount + amount);
|
||||
return;
|
||||
}
|
||||
|
||||
bukkitItem.setAmount(64);
|
||||
amount -= spaceLeft;
|
||||
}
|
||||
|
||||
if (freeSlot > -1) {
|
||||
items[freeSlot] = new ItemStack(id, amount);
|
||||
return;
|
||||
}
|
||||
|
||||
throw new OutOfSpaceException(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flushChanges() {
|
||||
if (items != null) {
|
||||
player.getInventory().setContents(items);
|
||||
items = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addSourcePosition(WorldVector pos) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addSingleSourcePosition(WorldVector pos) {
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,188 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.bukkit;
|
||||
|
||||
import com.sk89q.bukkit.util.CommandInfo;
|
||||
import com.sk89q.bukkit.util.CommandRegistration;
|
||||
import com.sk89q.worldedit.LocalConfiguration;
|
||||
import com.sk89q.worldedit.LocalWorld;
|
||||
import com.sk89q.worldedit.ServerInterface;
|
||||
import com.sk89q.worldedit.entity.Player;
|
||||
import com.sk89q.worldedit.extension.platform.Actor;
|
||||
import com.sk89q.worldedit.extension.platform.Capability;
|
||||
import com.sk89q.worldedit.extension.platform.MultiUserPlatform;
|
||||
import com.sk89q.worldedit.extension.platform.Preference;
|
||||
import com.sk89q.worldedit.util.command.CommandMapping;
|
||||
import com.sk89q.worldedit.util.command.Description;
|
||||
import com.sk89q.worldedit.util.command.Dispatcher;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.EntityType;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.EnumMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class BukkitServerInterface extends ServerInterface implements MultiUserPlatform {
|
||||
public Server server;
|
||||
public WorldEditPlugin plugin;
|
||||
private CommandRegistration dynamicCommands;
|
||||
private BukkitBiomeRegistry biomes;
|
||||
private boolean hookingEvents;
|
||||
|
||||
public BukkitServerInterface(WorldEditPlugin plugin, Server server) {
|
||||
this.plugin = plugin;
|
||||
this.server = server;
|
||||
this.biomes = new BukkitBiomeRegistry();
|
||||
dynamicCommands = new CommandRegistration(plugin);
|
||||
}
|
||||
|
||||
boolean isHookingEvents() {
|
||||
return hookingEvents;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int resolveItem(String name) {
|
||||
Material mat = Material.matchMaterial(name);
|
||||
return mat == null ? 0 : mat.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValidMobType(String type) {
|
||||
final EntityType entityType = EntityType.fromName(type);
|
||||
return entityType != null && entityType.isAlive();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reload() {
|
||||
plugin.loadConfiguration();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int schedule(long delay, long period, Runnable task) {
|
||||
return Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, task, delay, period);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<LocalWorld> getWorlds() {
|
||||
List<World> worlds = server.getWorlds();
|
||||
List<LocalWorld> ret = new ArrayList<LocalWorld>(worlds.size());
|
||||
|
||||
for (World world : worlds) {
|
||||
ret.add(BukkitUtil.getLocalWorld(world));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public Player matchPlayer(Player player) {
|
||||
if (player instanceof BukkitPlayer) {
|
||||
return player;
|
||||
} else {
|
||||
org.bukkit.entity.Player bukkitPlayer = server.getPlayerExact(player.getName());
|
||||
return bukkitPlayer != null ? new BukkitPlayer(plugin, this, bukkitPlayer) : null;
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public BukkitWorld matchWorld(com.sk89q.worldedit.world.World world) {
|
||||
if (world instanceof BukkitWorld) {
|
||||
return (BukkitWorld) world;
|
||||
} else {
|
||||
World bukkitWorld = server.getWorld(world.getName());
|
||||
return bukkitWorld != null ? new BukkitWorld(bukkitWorld) : null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerCommands(Dispatcher dispatcher) {
|
||||
List<CommandInfo> toRegister = new ArrayList<CommandInfo>();
|
||||
BukkitCommandInspector inspector = new BukkitCommandInspector(plugin, dispatcher);
|
||||
|
||||
for (CommandMapping command : dispatcher.getCommands()) {
|
||||
Description description = command.getDescription();
|
||||
List<String> permissions = description.getPermissions();
|
||||
String[] permissionsArray = new String[permissions.size()];
|
||||
permissions.toArray(permissionsArray);
|
||||
|
||||
toRegister.add(new CommandInfo(description.getUsage(), description.getShortDescription(), command.getAllAliases(), inspector, permissionsArray));
|
||||
}
|
||||
|
||||
dynamicCommands.register(toRegister);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerGameHooks() {
|
||||
hookingEvents = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalConfiguration getConfiguration() {
|
||||
return plugin.getLocalConfiguration();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getVersion() {
|
||||
return plugin.getDescription().getVersion();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPlatformName() {
|
||||
return "Bukkit-Official";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPlatformVersion() {
|
||||
return plugin.getDescription().getVersion();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Capability, Preference> getCapabilities() {
|
||||
Map<Capability, Preference> capabilities = new EnumMap<Capability, Preference>(Capability.class);
|
||||
capabilities.put(Capability.CONFIGURATION, Preference.NORMAL);
|
||||
capabilities.put(Capability.WORLDEDIT_CUI, Preference.NORMAL);
|
||||
capabilities.put(Capability.GAME_HOOKS, Preference.PREFERRED);
|
||||
capabilities.put(Capability.PERMISSIONS, Preference.PREFERRED);
|
||||
capabilities.put(Capability.USER_COMMANDS, Preference.PREFERRED);
|
||||
capabilities.put(Capability.WORLD_EDITING, Preference.PREFER_OTHERS);
|
||||
return capabilities;
|
||||
}
|
||||
|
||||
public void unregisterCommands() {
|
||||
dynamicCommands.unregisterCommands();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Actor> getConnectedUsers() {
|
||||
List<Actor> users = new ArrayList<Actor>();
|
||||
for (org.bukkit.entity.Player player : Bukkit.getServer().getOnlinePlayers()) {
|
||||
users.add(new BukkitPlayer(plugin, this, player));
|
||||
}
|
||||
return users;
|
||||
}
|
||||
}
|
@ -0,0 +1,196 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.bukkit;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.sk89q.worldedit.NotABlockException;
|
||||
import com.sk89q.worldedit.WorldEditException;
|
||||
import com.sk89q.worldedit.blocks.BaseBlock;
|
||||
import com.sk89q.worldedit.blocks.BlockID;
|
||||
import com.sk89q.worldedit.blocks.BlockType;
|
||||
import com.sk89q.worldedit.blocks.ItemID;
|
||||
import com.sk89q.worldedit.blocks.SkullBlock;
|
||||
import org.bukkit.DyeColor;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockFace;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.ExperienceOrb;
|
||||
import org.bukkit.entity.Item;
|
||||
import org.bukkit.entity.Painting;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.sk89q.worldedit.BlockVector;
|
||||
import com.sk89q.worldedit.BlockWorldVector;
|
||||
import com.sk89q.worldedit.LocalWorld;
|
||||
import com.sk89q.worldedit.Location;
|
||||
import com.sk89q.worldedit.Vector;
|
||||
import com.sk89q.worldedit.WorldVector;
|
||||
import com.sk89q.worldedit.bukkit.entity.BukkitEntity;
|
||||
import com.sk89q.worldedit.bukkit.entity.BukkitExpOrb;
|
||||
import com.sk89q.worldedit.bukkit.entity.BukkitItem;
|
||||
import com.sk89q.worldedit.bukkit.entity.BukkitPainting;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.material.Dye;
|
||||
|
||||
public final class BukkitUtil {
|
||||
|
||||
private BukkitUtil() {
|
||||
}
|
||||
|
||||
public static LocalWorld getLocalWorld(World w) {
|
||||
return new BukkitWorld(w);
|
||||
}
|
||||
|
||||
public static BlockVector toVector(Block block) {
|
||||
return new BlockVector(block.getX(), block.getY(), block.getZ());
|
||||
}
|
||||
|
||||
public static BlockVector toVector(BlockFace face) {
|
||||
return new BlockVector(face.getModX(), face.getModY(), face.getModZ());
|
||||
}
|
||||
|
||||
public static BlockWorldVector toWorldVector(Block block) {
|
||||
return new BlockWorldVector(getLocalWorld(block.getWorld()), block.getX(), block.getY(), block.getZ());
|
||||
}
|
||||
|
||||
public static Vector toVector(org.bukkit.Location loc) {
|
||||
return new Vector(loc.getX(), loc.getY(), loc.getZ());
|
||||
}
|
||||
|
||||
public static Location toLocation(org.bukkit.Location loc) {
|
||||
return new Location(
|
||||
getLocalWorld(loc.getWorld()),
|
||||
new Vector(loc.getX(), loc.getY(), loc.getZ()),
|
||||
loc.getYaw(), loc.getPitch()
|
||||
);
|
||||
}
|
||||
|
||||
public static Vector toVector(org.bukkit.util.Vector vector) {
|
||||
return new Vector(vector.getX(), vector.getY(), vector.getZ());
|
||||
}
|
||||
|
||||
public static org.bukkit.Location toLocation(WorldVector pt) {
|
||||
return new org.bukkit.Location(toWorld(pt), pt.getX(), pt.getY(), pt.getZ());
|
||||
}
|
||||
|
||||
public static org.bukkit.Location toLocation(World world, Vector pt) {
|
||||
return new org.bukkit.Location(world, pt.getX(), pt.getY(), pt.getZ());
|
||||
}
|
||||
|
||||
public static org.bukkit.Location center(org.bukkit.Location loc) {
|
||||
return new org.bukkit.Location(
|
||||
loc.getWorld(),
|
||||
loc.getBlockX() + 0.5,
|
||||
loc.getBlockY() + 0.5,
|
||||
loc.getBlockZ() + 0.5,
|
||||
loc.getPitch(),
|
||||
loc.getYaw()
|
||||
);
|
||||
}
|
||||
|
||||
public static Player matchSinglePlayer(Server server, String name) {
|
||||
List<Player> players = server.matchPlayer(name);
|
||||
if (players.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return players.get(0);
|
||||
}
|
||||
|
||||
public static Block toBlock(BlockWorldVector pt) {
|
||||
return toWorld(pt).getBlockAt(toLocation(pt));
|
||||
}
|
||||
|
||||
public static World toWorld(WorldVector pt) {
|
||||
return ((BukkitWorld) pt.getWorld()).getWorld();
|
||||
}
|
||||
|
||||
/**
|
||||
* Bukkit's Location class has serious problems with floating point
|
||||
* precision.
|
||||
*/
|
||||
@SuppressWarnings("RedundantIfStatement")
|
||||
public static boolean equals(org.bukkit.Location a, org.bukkit.Location b) {
|
||||
if (Math.abs(a.getX() - b.getX()) > EQUALS_PRECISION) return false;
|
||||
if (Math.abs(a.getY() - b.getY()) > EQUALS_PRECISION) return false;
|
||||
if (Math.abs(a.getZ() - b.getZ()) > EQUALS_PRECISION) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public static final double EQUALS_PRECISION = 0.0001;
|
||||
|
||||
public static org.bukkit.Location toLocation(Location location) {
|
||||
Vector pt = location.getPosition();
|
||||
return new org.bukkit.Location(
|
||||
toWorld(location.getWorld()),
|
||||
pt.getX(), pt.getY(), pt.getZ(),
|
||||
location.getYaw(), location.getPitch()
|
||||
);
|
||||
}
|
||||
|
||||
public static World toWorld(final LocalWorld world) {
|
||||
return ((BukkitWorld) world).getWorld();
|
||||
}
|
||||
|
||||
public static BukkitEntity toLocalEntity(Entity e) {
|
||||
switch (e.getType()) {
|
||||
case EXPERIENCE_ORB:
|
||||
return new BukkitExpOrb(toLocation(e.getLocation()), e.getUniqueId(), ((ExperienceOrb)e).getExperience());
|
||||
case PAINTING:
|
||||
Painting paint = (Painting) e;
|
||||
return new BukkitPainting(toLocation(e.getLocation()), paint.getArt(), paint.getFacing(), e.getUniqueId());
|
||||
case DROPPED_ITEM:
|
||||
return new BukkitItem(toLocation(e.getLocation()), ((Item)e).getItemStack(), e.getUniqueId());
|
||||
default:
|
||||
return new BukkitEntity(toLocation(e.getLocation()), e.getType(), e.getUniqueId());
|
||||
}
|
||||
}
|
||||
|
||||
public static BaseBlock toBlock(LocalWorld world, ItemStack itemStack) throws WorldEditException {
|
||||
final int typeId = itemStack.getTypeId();
|
||||
|
||||
switch (typeId) {
|
||||
case ItemID.INK_SACK:
|
||||
final Dye materialData = (Dye) itemStack.getData();
|
||||
if (materialData.getColor() == DyeColor.BROWN) {
|
||||
return new BaseBlock(BlockID.COCOA_PLANT, -1);
|
||||
}
|
||||
break;
|
||||
|
||||
case ItemID.HEAD:
|
||||
return new SkullBlock(0, (byte) itemStack.getDurability());
|
||||
|
||||
default:
|
||||
final BaseBlock baseBlock = BlockType.getBlockForItem(typeId, itemStack.getDurability());
|
||||
if (baseBlock != null) {
|
||||
return baseBlock;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (world.isValidBlockType(typeId)) {
|
||||
return new BaseBlock(typeId, -1);
|
||||
}
|
||||
|
||||
throw new NotABlockException(typeId);
|
||||
}
|
||||
}
|
@ -0,0 +1,464 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.bukkit;
|
||||
|
||||
import com.sk89q.worldedit.BlockVector2D;
|
||||
import com.sk89q.worldedit.EditSession;
|
||||
import com.sk89q.worldedit.LocalWorld;
|
||||
import com.sk89q.worldedit.Vector;
|
||||
import com.sk89q.worldedit.Vector2D;
|
||||
import com.sk89q.worldedit.WorldEdit;
|
||||
import com.sk89q.worldedit.WorldEditException;
|
||||
import com.sk89q.worldedit.blocks.BaseBlock;
|
||||
import com.sk89q.worldedit.blocks.BaseItemStack;
|
||||
import com.sk89q.worldedit.blocks.LazyBlock;
|
||||
import com.sk89q.worldedit.bukkit.adapter.BukkitImplAdapter;
|
||||
import com.sk89q.worldedit.entity.BaseEntity;
|
||||
import com.sk89q.worldedit.regions.Region;
|
||||
import com.sk89q.worldedit.util.TreeGenerator;
|
||||
import com.sk89q.worldedit.world.biome.BaseBiome;
|
||||
import com.sk89q.worldedit.world.registry.WorldData;
|
||||
import org.bukkit.Effect;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.TreeType;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Biome;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockState;
|
||||
import org.bukkit.block.Chest;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.inventory.DoubleChestInventory;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.ArrayList;
|
||||
import java.util.EnumMap;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
public class BukkitWorld extends LocalWorld {
|
||||
|
||||
private static final Logger logger = WorldEdit.logger;
|
||||
|
||||
private static final Map<Integer, Effect> effects = new HashMap<Integer, Effect>();
|
||||
static {
|
||||
for (Effect effect : Effect.values()) {
|
||||
effects.put(effect.getId(), effect);
|
||||
}
|
||||
}
|
||||
|
||||
private final WeakReference<World> worldRef;
|
||||
|
||||
/**
|
||||
* Construct the object.
|
||||
*
|
||||
* @param world the world
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public BukkitWorld(World world) {
|
||||
this.worldRef = new WeakReference<World>(world);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<com.sk89q.worldedit.entity.Entity> getEntities(Region region) {
|
||||
World world = getWorld();
|
||||
|
||||
List<com.sk89q.worldedit.entity.Entity> entities = new ArrayList<com.sk89q.worldedit.entity.Entity>();
|
||||
for (Vector2D pt : region.getChunks()) {
|
||||
if (!world.isChunkLoaded(pt.getBlockX(), pt.getBlockZ())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
final Entity[] ents = world.getChunkAt(pt.getBlockX(), pt.getBlockZ()).getEntities();
|
||||
for (Entity ent : ents) {
|
||||
if (region.contains(BukkitUtil.toVector(ent.getLocation()))) {
|
||||
entities.add(BukkitAdapter.adapt(ent));
|
||||
}
|
||||
}
|
||||
}
|
||||
return entities;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<com.sk89q.worldedit.entity.Entity> getEntities() {
|
||||
List<com.sk89q.worldedit.entity.Entity> list = new ArrayList<com.sk89q.worldedit.entity.Entity>();
|
||||
for (Entity entity : getWorld().getEntities()) {
|
||||
list.add(BukkitAdapter.adapt(entity));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public com.sk89q.worldedit.entity.Entity createEntity(com.sk89q.worldedit.util.Location location, BaseEntity entity) {
|
||||
BukkitImplAdapter adapter = WorldEditPlugin.getInstance().getBukkitImplAdapter();
|
||||
if (adapter != null) {
|
||||
Entity createdEntity = adapter.createEntity(BukkitAdapter.adapt(getWorld(), location), entity);
|
||||
if (createdEntity != null) {
|
||||
return new BukkitEntity(createdEntity);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the world handle.
|
||||
*
|
||||
* @return the world
|
||||
*/
|
||||
public World getWorld() {
|
||||
return checkNotNull(worldRef.get(), "The world was unloaded and the reference is unavailable");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the world handle.
|
||||
*
|
||||
* @return the world
|
||||
*/
|
||||
protected World getWorldChecked() throws WorldEditException {
|
||||
World world = worldRef.get();
|
||||
if (world == null) {
|
||||
throw new WorldUnloadedException();
|
||||
}
|
||||
return world;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return getWorld().getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getBlockLightLevel(Vector pt) {
|
||||
return getWorld().getBlockAt(pt.getBlockX(), pt.getBlockY(), pt.getBlockZ()).getLightLevel();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean regenerate(Region region, EditSession editSession) {
|
||||
BaseBlock[] history = new BaseBlock[16 * 16 * (getMaxY() + 1)];
|
||||
|
||||
for (Vector2D chunk : region.getChunks()) {
|
||||
Vector min = new Vector(chunk.getBlockX() * 16, 0, chunk.getBlockZ() * 16);
|
||||
|
||||
// First save all the blocks inside
|
||||
for (int x = 0; x < 16; ++x) {
|
||||
for (int y = 0; y < (getMaxY() + 1); ++y) {
|
||||
for (int z = 0; z < 16; ++z) {
|
||||
Vector pt = min.add(x, y, z);
|
||||
int index = y * 16 * 16 + z * 16 + x;
|
||||
history[index] = editSession.getBlock(pt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
getWorld().regenerateChunk(chunk.getBlockX(), chunk.getBlockZ());
|
||||
} catch (Throwable t) {
|
||||
logger.log(Level.WARNING, "Chunk generation via Bukkit raised an error", t);
|
||||
}
|
||||
|
||||
// Then restore
|
||||
for (int x = 0; x < 16; ++x) {
|
||||
for (int y = 0; y < (getMaxY() + 1); ++y) {
|
||||
for (int z = 0; z < 16; ++z) {
|
||||
Vector pt = min.add(x, y, z);
|
||||
int index = y * 16 * 16 + z * 16 + x;
|
||||
|
||||
// We have to restore the block if it was outside
|
||||
if (!region.contains(pt)) {
|
||||
editSession.smartSetBlock(pt, history[index]);
|
||||
} else { // Otherwise fool with history
|
||||
editSession.rememberChange(pt, history[index],
|
||||
editSession.rawGetBlock(pt));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the single block inventory for a potentially double chest.
|
||||
* Handles people who have an old version of Bukkit.
|
||||
* This should be replaced with {@link org.bukkit.block.Chest#getBlockInventory()}
|
||||
* in a few months (now = March 2012) // note from future dev - lol
|
||||
*
|
||||
* @param chest The chest to get a single block inventory for
|
||||
* @return The chest's inventory
|
||||
*/
|
||||
private Inventory getBlockInventory(Chest chest) {
|
||||
try {
|
||||
return chest.getBlockInventory();
|
||||
} catch (Throwable t) {
|
||||
if (chest.getInventory() instanceof DoubleChestInventory) {
|
||||
DoubleChestInventory inven = (DoubleChestInventory) chest.getInventory();
|
||||
if (inven.getLeftSide().getHolder().equals(chest)) {
|
||||
return inven.getLeftSide();
|
||||
} else if (inven.getRightSide().getHolder().equals(chest)) {
|
||||
return inven.getRightSide();
|
||||
} else {
|
||||
return inven;
|
||||
}
|
||||
} else {
|
||||
return chest.getInventory();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean clearContainerBlockContents(Vector pt) {
|
||||
Block block = getWorld().getBlockAt(pt.getBlockX(), pt.getBlockY(), pt.getBlockZ());
|
||||
if (block == null) {
|
||||
return false;
|
||||
}
|
||||
BlockState state = block.getState();
|
||||
if (!(state instanceof org.bukkit.inventory.InventoryHolder)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
org.bukkit.inventory.InventoryHolder chest = (org.bukkit.inventory.InventoryHolder) state;
|
||||
Inventory inven = chest.getInventory();
|
||||
if (chest instanceof Chest) {
|
||||
inven = getBlockInventory((Chest) chest);
|
||||
}
|
||||
inven.clear();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public boolean generateTree(EditSession editSession, Vector pt) {
|
||||
return generateTree(TreeGenerator.TreeType.TREE, editSession, pt);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public boolean generateBigTree(EditSession editSession, Vector pt) {
|
||||
return generateTree(TreeGenerator.TreeType.BIG_TREE, editSession, pt);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public boolean generateBirchTree(EditSession editSession, Vector pt) {
|
||||
return generateTree(TreeGenerator.TreeType.BIRCH, editSession, pt);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public boolean generateRedwoodTree(EditSession editSession, Vector pt) {
|
||||
return generateTree(TreeGenerator.TreeType.REDWOOD, editSession, pt);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public boolean generateTallRedwoodTree(EditSession editSession, Vector pt) {
|
||||
return generateTree(TreeGenerator.TreeType.TALL_REDWOOD, editSession, pt);
|
||||
}
|
||||
|
||||
/**
|
||||
* An EnumMap that stores which WorldEdit TreeTypes apply to which Bukkit TreeTypes
|
||||
*/
|
||||
private static final EnumMap<TreeGenerator.TreeType, TreeType> treeTypeMapping =
|
||||
new EnumMap<TreeGenerator.TreeType, TreeType>(TreeGenerator.TreeType.class);
|
||||
|
||||
static {
|
||||
for (TreeGenerator.TreeType type : TreeGenerator.TreeType.values()) {
|
||||
try {
|
||||
TreeType bukkitType = TreeType.valueOf(type.name());
|
||||
treeTypeMapping.put(type, bukkitType);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// Unhandled TreeType
|
||||
}
|
||||
}
|
||||
// Other mappings for WE-specific values
|
||||
treeTypeMapping.put(TreeGenerator.TreeType.SHORT_JUNGLE, TreeType.SMALL_JUNGLE);
|
||||
treeTypeMapping.put(TreeGenerator.TreeType.RANDOM, TreeType.BROWN_MUSHROOM);
|
||||
treeTypeMapping.put(TreeGenerator.TreeType.RANDOM_REDWOOD, TreeType.REDWOOD);
|
||||
treeTypeMapping.put(TreeGenerator.TreeType.PINE, TreeType.REDWOOD);
|
||||
for (TreeGenerator.TreeType type : TreeGenerator.TreeType.values()) {
|
||||
if (treeTypeMapping.get(type) == null) {
|
||||
WorldEdit.logger.severe("No TreeType mapping for TreeGenerator.TreeType." + type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static TreeType toBukkitTreeType(TreeGenerator.TreeType type) {
|
||||
return treeTypeMapping.get(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean generateTree(TreeGenerator.TreeType type, EditSession editSession, Vector pt) {
|
||||
World world = getWorld();
|
||||
TreeType bukkitType = toBukkitTreeType(type);
|
||||
return type != null && world.generateTree(BukkitUtil.toLocation(world, pt), bukkitType,
|
||||
new EditSessionBlockChangeDelegate(editSession));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dropItem(Vector pt, BaseItemStack item) {
|
||||
World world = getWorld();
|
||||
ItemStack bukkitItem = new ItemStack(item.getType(), item.getAmount(),
|
||||
item.getData());
|
||||
world.dropItemNaturally(BukkitUtil.toLocation(world, pt), bukkitItem);
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public boolean isValidBlockType(int type) {
|
||||
return Material.getMaterial(type) != null && Material.getMaterial(type).isBlock();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkLoadedChunk(Vector pt) {
|
||||
World world = getWorld();
|
||||
|
||||
if (!world.isChunkLoaded(pt.getBlockX() >> 4, pt.getBlockZ() >> 4)) {
|
||||
world.loadChunk(pt.getBlockX() >> 4, pt.getBlockZ() >> 4);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (other == null) {
|
||||
return false;
|
||||
} else if ((other instanceof BukkitWorld)) {
|
||||
return ((BukkitWorld) other).getWorld().equals(getWorld());
|
||||
} else if (other instanceof com.sk89q.worldedit.world.World) {
|
||||
return ((com.sk89q.worldedit.world.World) other).getName().equals(getName());
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return getWorld().hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxY() {
|
||||
return getWorld().getMaxHeight() - 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fixAfterFastMode(Iterable<BlockVector2D> chunks) {
|
||||
World world = getWorld();
|
||||
for (BlockVector2D chunkPos : chunks) {
|
||||
world.refreshChunk(chunkPos.getBlockX(), chunkPos.getBlockZ());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean playEffect(Vector position, int type, int data) {
|
||||
World world = getWorld();
|
||||
|
||||
final Effect effect = effects.get(type);
|
||||
if (effect == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
world.playEffect(BukkitUtil.toLocation(world, position), effect, data);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WorldData getWorldData() {
|
||||
return BukkitWorldData.getInstance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void simulateBlockMine(Vector pt) {
|
||||
getWorld().getBlockAt(pt.getBlockX(), pt.getBlockY(), pt.getBlockZ()).breakNaturally();
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseBlock getBlock(Vector position) {
|
||||
BukkitImplAdapter adapter = WorldEditPlugin.getInstance().getBukkitImplAdapter();
|
||||
if (adapter != null) {
|
||||
return adapter.getBlock(BukkitAdapter.adapt(getWorld(), position));
|
||||
} else {
|
||||
Block bukkitBlock = getWorld().getBlockAt(position.getBlockX(), position.getBlockY(), position.getBlockZ());
|
||||
return new BaseBlock(bukkitBlock.getTypeId(), bukkitBlock.getData());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setBlock(Vector position, BaseBlock block, boolean notifyAndLight) throws WorldEditException {
|
||||
BukkitImplAdapter adapter = WorldEditPlugin.getInstance().getBukkitImplAdapter();
|
||||
if (adapter != null) {
|
||||
return adapter.setBlock(BukkitAdapter.adapt(getWorld(), position), block, notifyAndLight);
|
||||
} else {
|
||||
Block bukkitBlock = getWorld().getBlockAt(position.getBlockX(), position.getBlockY(), position.getBlockZ());
|
||||
return bukkitBlock.setTypeIdAndData(block.getType(), (byte) block.getData(), notifyAndLight);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public BaseBlock getLazyBlock(Vector position) {
|
||||
World world = getWorld();
|
||||
Block bukkitBlock = world.getBlockAt(position.getBlockX(), position.getBlockY(), position.getBlockZ());
|
||||
return new LazyBlock(bukkitBlock.getTypeId(), bukkitBlock.getData(), this, position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseBiome getBiome(Vector2D position) {
|
||||
BukkitImplAdapter adapter = WorldEditPlugin.getInstance().getBukkitImplAdapter();
|
||||
if (adapter != null) {
|
||||
int id = adapter.getBiomeId(getWorld().getBiome(position.getBlockX(), position.getBlockZ()));
|
||||
return new BaseBiome(id);
|
||||
} else {
|
||||
return new BaseBiome(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setBiome(Vector2D position, BaseBiome biome) {
|
||||
BukkitImplAdapter adapter = WorldEditPlugin.getInstance().getBukkitImplAdapter();
|
||||
if (adapter != null) {
|
||||
Biome bukkitBiome = adapter.getBiome(biome.getId());
|
||||
getWorld().setBiome(position.getBlockX(), position.getBlockZ(), bukkitBiome);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use {@link #setBlock(Vector, BaseBlock, boolean)}
|
||||
*/
|
||||
@Deprecated
|
||||
public boolean setBlock(Vector pt, com.sk89q.worldedit.foundation.Block block, boolean notifyAdjacent) throws WorldEditException {
|
||||
return setBlock(pt, (BaseBlock) block, notifyAdjacent);
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.bukkit;
|
||||
|
||||
import com.sk89q.worldedit.world.registry.BiomeRegistry;
|
||||
import com.sk89q.worldedit.world.registry.LegacyWorldData;
|
||||
|
||||
/**
|
||||
* World data for the Bukkit platform.
|
||||
*/
|
||||
class BukkitWorldData extends LegacyWorldData {
|
||||
|
||||
private static final BukkitWorldData INSTANCE = new BukkitWorldData();
|
||||
private final BiomeRegistry biomeRegistry = new BukkitBiomeRegistry();
|
||||
|
||||
/**
|
||||
* Create a new instance.
|
||||
*/
|
||||
BukkitWorldData() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public BiomeRegistry getBiomeRegistry() {
|
||||
return biomeRegistry;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a static instance.
|
||||
*
|
||||
* @return an instance
|
||||
*/
|
||||
public static BukkitWorldData getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.bukkit;
|
||||
|
||||
import com.sk89q.worldedit.LocalSession;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.messaging.PluginMessageListener;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/**
|
||||
* Handles incoming WorldEditCui init message.
|
||||
*/
|
||||
public class CUIChannelListener implements PluginMessageListener {
|
||||
|
||||
public static final Charset UTF_8_CHARSET = Charset.forName("UTF-8");
|
||||
private final WorldEditPlugin plugin;
|
||||
|
||||
public CUIChannelListener(WorldEditPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPluginMessageReceived(String channel, Player player, byte[] message) {
|
||||
LocalSession session = plugin.getSession(player);
|
||||
String text = new String(message, UTF_8_CHARSET);
|
||||
session.handleCUIInitializationMessage(text);
|
||||
session.describeCUI(plugin.wrapPlayer(player));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.bukkit;
|
||||
|
||||
import com.sk89q.worldedit.blocks.BlockID;
|
||||
import org.bukkit.BlockChangeDelegate;
|
||||
import com.sk89q.worldedit.EditSession;
|
||||
import com.sk89q.worldedit.Vector;
|
||||
import com.sk89q.worldedit.MaxChangedBlocksException;
|
||||
import com.sk89q.worldedit.blocks.BaseBlock;
|
||||
|
||||
/**
|
||||
* Proxy class to catch calls to set blocks.
|
||||
*/
|
||||
public class EditSessionBlockChangeDelegate implements BlockChangeDelegate {
|
||||
|
||||
private EditSession editSession;
|
||||
|
||||
public EditSessionBlockChangeDelegate(EditSession editSession) {
|
||||
this.editSession = editSession;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setRawTypeId(int x, int y, int z, int typeId) {
|
||||
try {
|
||||
return editSession.setBlock(new Vector(x, y, z), new BaseBlock(typeId));
|
||||
} catch (MaxChangedBlocksException ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setRawTypeIdAndData(int x, int y, int z, int typeId, int data) {
|
||||
try {
|
||||
return editSession.setBlock(new Vector(x, y, z), new BaseBlock(typeId, data));
|
||||
} catch (MaxChangedBlocksException ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setTypeId(int x, int y, int z, int typeId) {
|
||||
return setRawTypeId(x, y, z, typeId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setTypeIdAndData(int x, int y, int z, int typeId, int data) {
|
||||
return setRawTypeIdAndData(x, y, z, typeId, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTypeId(int x, int y, int z) {
|
||||
return editSession.getBlockType(new Vector(x, y, z));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHeight() {
|
||||
return editSession.getWorld().getMaxY() + 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty(int x, int y, int z) {
|
||||
return editSession.getBlockType(new Vector(x, y, z)) == BlockID.AIR;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.bukkit;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import com.sk89q.worldedit.LocalSession;
|
||||
|
||||
/**
|
||||
* @deprecated use the regular API
|
||||
*/
|
||||
@Deprecated
|
||||
public class WorldEditAPI {
|
||||
|
||||
private WorldEditPlugin plugin;
|
||||
|
||||
public WorldEditAPI(WorldEditPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the session for a player.
|
||||
*
|
||||
* @param player the player
|
||||
* @return a session
|
||||
*/
|
||||
public LocalSession getSession(Player player) {
|
||||
return plugin.getWorldEdit().getSession(
|
||||
new BukkitPlayer(plugin, plugin.getServerInterface(), player));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,176 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
// $Id$
|
||||
|
||||
package com.sk89q.worldedit.bukkit;
|
||||
|
||||
import com.sk89q.util.StringUtil;
|
||||
import com.sk89q.worldedit.LocalPlayer;
|
||||
import com.sk89q.worldedit.WorldEdit;
|
||||
import com.sk89q.worldedit.WorldVector;
|
||||
import com.sk89q.worldedit.internal.LocalWorldAdapter;
|
||||
import com.sk89q.worldedit.world.World;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.event.Event.Result;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.Action;
|
||||
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
||||
import org.bukkit.event.player.PlayerGameModeChangeEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
|
||||
/**
|
||||
* Handles all events thrown in relation to a Player
|
||||
*/
|
||||
public class WorldEditListener implements Listener {
|
||||
|
||||
private WorldEditPlugin plugin;
|
||||
private boolean ignoreLeftClickAir = false;
|
||||
|
||||
/**
|
||||
* Called when a player plays an animation, such as an arm swing
|
||||
*
|
||||
* @param event Relevant event details
|
||||
*/
|
||||
|
||||
/**
|
||||
* Construct the object;
|
||||
*
|
||||
* @param plugin the plugin
|
||||
*/
|
||||
public WorldEditListener(WorldEditPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
||||
public void onGamemode(PlayerGameModeChangeEvent event) {
|
||||
if (!plugin.getInternalPlatform().isHookingEvents()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// this will automatically refresh their session, we don't have to do anything
|
||||
WorldEdit.getInstance().getSession(plugin.wrapPlayer(event.getPlayer()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when a player attempts to use a command
|
||||
*
|
||||
* @param event Relevant event details
|
||||
*/
|
||||
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
||||
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
|
||||
String[] split = event.getMessage().split(" ");
|
||||
|
||||
if (split.length > 0) {
|
||||
split[0] = split[0].substring(1);
|
||||
split = plugin.getWorldEdit().getPlatformManager().getCommandManager().commandDetection(split);
|
||||
}
|
||||
|
||||
final String newMessage = "/" + StringUtil.joinString(split, " ");
|
||||
|
||||
if (!newMessage.equals(event.getMessage())) {
|
||||
event.setMessage(newMessage);
|
||||
plugin.getServer().getPluginManager().callEvent(event);
|
||||
|
||||
if (!event.isCancelled()) {
|
||||
if (!event.getMessage().isEmpty()) {
|
||||
plugin.getServer().dispatchCommand(event.getPlayer(), event.getMessage().substring(1));
|
||||
}
|
||||
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when a player interacts
|
||||
*
|
||||
* @param event Relevant event details
|
||||
*/
|
||||
@EventHandler
|
||||
public void onPlayerInteract(PlayerInteractEvent event) {
|
||||
if (!plugin.getInternalPlatform().isHookingEvents()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.useItemInHand() == Result.DENY) {
|
||||
return;
|
||||
}
|
||||
|
||||
final LocalPlayer player = plugin.wrapPlayer(event.getPlayer());
|
||||
final World world = player.getWorld();
|
||||
final WorldEdit we = plugin.getWorldEdit();
|
||||
|
||||
Action action = event.getAction();
|
||||
if (action == Action.LEFT_CLICK_BLOCK) {
|
||||
final Block clickedBlock = event.getClickedBlock();
|
||||
final WorldVector pos = new WorldVector(LocalWorldAdapter.adapt(world), clickedBlock.getX(), clickedBlock.getY(), clickedBlock.getZ());
|
||||
|
||||
if (we.handleBlockLeftClick(player, pos)) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
|
||||
if (we.handleArmSwing(player)) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
|
||||
if (!ignoreLeftClickAir) {
|
||||
final int taskId = Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
ignoreLeftClickAir = false;
|
||||
}
|
||||
}, 2);
|
||||
|
||||
if (taskId != -1) {
|
||||
ignoreLeftClickAir = true;
|
||||
}
|
||||
}
|
||||
} else if (action == Action.LEFT_CLICK_AIR) {
|
||||
if (ignoreLeftClickAir) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (we.handleArmSwing(player)) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
|
||||
|
||||
} else if (action == Action.RIGHT_CLICK_BLOCK) {
|
||||
final Block clickedBlock = event.getClickedBlock();
|
||||
final WorldVector pos = new WorldVector(LocalWorldAdapter.adapt(world), clickedBlock.getX(),
|
||||
clickedBlock.getY(), clickedBlock.getZ());
|
||||
|
||||
if (we.handleBlockRightClick(player, pos)) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
|
||||
if (we.handleRightClick(player)) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
} else if (action == Action.RIGHT_CLICK_AIR) {
|
||||
if (we.handleRightClick(player)) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,472 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.bukkit;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import com.sk89q.util.yaml.YAMLProcessor;
|
||||
import com.sk89q.wepif.PermissionsResolverManager;
|
||||
import com.sk89q.worldedit.EditSession;
|
||||
import com.sk89q.worldedit.IncompleteRegionException;
|
||||
import com.sk89q.worldedit.LocalPlayer;
|
||||
import com.sk89q.worldedit.LocalSession;
|
||||
import com.sk89q.worldedit.ServerInterface;
|
||||
import com.sk89q.worldedit.WorldEdit;
|
||||
import com.sk89q.worldedit.WorldEditOperation;
|
||||
import com.sk89q.worldedit.bukkit.adapter.AdapterLoadException;
|
||||
import com.sk89q.worldedit.bukkit.adapter.BukkitImplAdapter;
|
||||
import com.sk89q.worldedit.bukkit.adapter.BukkitImplLoader;
|
||||
import com.sk89q.worldedit.bukkit.selections.CuboidSelection;
|
||||
import com.sk89q.worldedit.bukkit.selections.CylinderSelection;
|
||||
import com.sk89q.worldedit.bukkit.selections.Polygonal2DSelection;
|
||||
import com.sk89q.worldedit.bukkit.selections.Selection;
|
||||
import com.sk89q.worldedit.event.platform.CommandEvent;
|
||||
import com.sk89q.worldedit.event.platform.CommandSuggestionEvent;
|
||||
import com.sk89q.worldedit.event.platform.PlatformReadyEvent;
|
||||
import com.sk89q.worldedit.extension.platform.Actor;
|
||||
import com.sk89q.worldedit.extension.platform.Capability;
|
||||
import com.sk89q.worldedit.extension.platform.Platform;
|
||||
import com.sk89q.worldedit.extent.inventory.BlockBag;
|
||||
import com.sk89q.worldedit.regions.CuboidRegion;
|
||||
import com.sk89q.worldedit.regions.CylinderRegion;
|
||||
import com.sk89q.worldedit.regions.Polygonal2DRegion;
|
||||
import com.sk89q.worldedit.regions.Region;
|
||||
import com.sk89q.worldedit.regions.RegionSelector;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabCompleter;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.zip.ZipEntry;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
/**
|
||||
* Plugin for Bukkit.
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public class WorldEditPlugin extends JavaPlugin implements TabCompleter {
|
||||
|
||||
private static final Logger log = Logger.getLogger(WorldEditPlugin.class.getCanonicalName());
|
||||
public static final String CUI_PLUGIN_CHANNEL = "WECUI";
|
||||
private static WorldEditPlugin INSTANCE;
|
||||
|
||||
private BukkitImplAdapter bukkitAdapter;
|
||||
private BukkitServerInterface server;
|
||||
private final WorldEditAPI api = new WorldEditAPI(this);
|
||||
private BukkitConfiguration config;
|
||||
|
||||
/**
|
||||
* Called on plugin enable.
|
||||
*/
|
||||
@SuppressWarnings("AccessStaticViaInstance")
|
||||
@Override
|
||||
public void onEnable() {
|
||||
this.INSTANCE = this;
|
||||
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
getDataFolder().mkdirs();
|
||||
|
||||
WorldEdit worldEdit = WorldEdit.getInstance();
|
||||
|
||||
loadConfig(); // Load configuration
|
||||
PermissionsResolverManager.initialize(this); // Setup permission resolver
|
||||
|
||||
// Setup platform
|
||||
server = new BukkitServerInterface(this, getServer());
|
||||
worldEdit.getPlatformManager().register(server);
|
||||
|
||||
// Register CUI
|
||||
getServer().getMessenger().registerIncomingPluginChannel(this, CUI_PLUGIN_CHANNEL, new CUIChannelListener(this));
|
||||
getServer().getMessenger().registerOutgoingPluginChannel(this, CUI_PLUGIN_CHANNEL);
|
||||
|
||||
// Now we can register events
|
||||
getServer().getPluginManager().registerEvents(new WorldEditListener(this), this);
|
||||
|
||||
// If we are on MCPC+/Cauldron, then Forge will have already loaded
|
||||
// Forge WorldEdit and there's (probably) not going to be any other
|
||||
// platforms to be worried about... at the current time of writing
|
||||
WorldEdit.getInstance().getEventBus().post(new PlatformReadyEvent());
|
||||
|
||||
loadAdapter(); // Need an adapter to work with special blocks with NBT data
|
||||
}
|
||||
|
||||
private void loadConfig() {
|
||||
createDefaultConfiguration("config.yml"); // Create the default configuration file
|
||||
|
||||
config = new BukkitConfiguration(new YAMLProcessor(new File(getDataFolder(), "config.yml"), true), this);
|
||||
config.load();
|
||||
}
|
||||
|
||||
private void loadAdapter() {
|
||||
WorldEdit worldEdit = WorldEdit.getInstance();
|
||||
|
||||
// Attempt to load a Bukkit adapter
|
||||
BukkitImplLoader adapterLoader = new BukkitImplLoader();
|
||||
|
||||
try {
|
||||
adapterLoader.addFromPath(getClass().getClassLoader());
|
||||
} catch (IOException e) {
|
||||
log.log(Level.WARNING, "Failed to search path for Bukkit adapters");
|
||||
}
|
||||
|
||||
try {
|
||||
adapterLoader.addFromJar(getFile());
|
||||
} catch (IOException e) {
|
||||
log.log(Level.WARNING, "Failed to search " + getFile() + " for Bukkit adapters", e);
|
||||
}
|
||||
try {
|
||||
bukkitAdapter = adapterLoader.loadAdapter();
|
||||
log.log(Level.INFO, "Using " + bukkitAdapter.getClass().getCanonicalName() + " as the Bukkit adapter");
|
||||
} catch (AdapterLoadException e) {
|
||||
Platform platform = worldEdit.getPlatformManager().queryCapability(Capability.WORLD_EDITING);
|
||||
if (platform instanceof BukkitServerInterface) {
|
||||
log.log(Level.WARNING, e.getMessage());
|
||||
} else {
|
||||
log.log(Level.INFO, "WorldEdit could not find a Bukkit adapter for this MC version, " +
|
||||
"but it seems that you have another implementation of WorldEdit installed (" + platform.getPlatformName() + ") " +
|
||||
"that handles the world editing.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called on plugin disable.
|
||||
*/
|
||||
@Override
|
||||
public void onDisable() {
|
||||
WorldEdit worldEdit = WorldEdit.getInstance();
|
||||
worldEdit.clearSessions();
|
||||
worldEdit.getPlatformManager().unregister(server);
|
||||
if (config != null) {
|
||||
config.unload();
|
||||
}
|
||||
if (server != null) {
|
||||
server.unregisterCommands();
|
||||
}
|
||||
this.getServer().getScheduler().cancelTasks(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads and reloads all configuration.
|
||||
*/
|
||||
protected void loadConfiguration() {
|
||||
config.unload();
|
||||
config.load();
|
||||
getPermissionsResolver().load();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a default configuration file from the .jar.
|
||||
*
|
||||
* @param name the filename
|
||||
*/
|
||||
protected void createDefaultConfiguration(String name) {
|
||||
File actual = new File(getDataFolder(), name);
|
||||
if (!actual.exists()) {
|
||||
InputStream input = null;
|
||||
try {
|
||||
JarFile file = new JarFile(getFile());
|
||||
ZipEntry copy = file.getEntry("defaults/" + name);
|
||||
if (copy == null) throw new FileNotFoundException();
|
||||
input = file.getInputStream(copy);
|
||||
} catch (IOException e) {
|
||||
getLogger().severe("Unable to read default configuration: " + name);
|
||||
}
|
||||
if (input != null) {
|
||||
FileOutputStream output = null;
|
||||
|
||||
try {
|
||||
output = new FileOutputStream(actual);
|
||||
byte[] buf = new byte[8192];
|
||||
int length;
|
||||
while ((length = input.read(buf)) > 0) {
|
||||
output.write(buf, 0, length);
|
||||
}
|
||||
|
||||
getLogger().info("Default configuration file written: " + name);
|
||||
} catch (IOException e) {
|
||||
getLogger().log(Level.WARNING, "Failed to write default config file", e);
|
||||
} finally {
|
||||
try {
|
||||
input.close();
|
||||
} catch (IOException ignored) {}
|
||||
|
||||
try {
|
||||
if (output != null) {
|
||||
output.close();
|
||||
}
|
||||
} catch (IOException ignored) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
|
||||
// Add the command to the array because the underlying command handling
|
||||
// code of WorldEdit expects it
|
||||
String[] split = new String[args.length + 1];
|
||||
System.arraycopy(args, 0, split, 1, args.length);
|
||||
split[0] = cmd.getName();
|
||||
|
||||
CommandEvent event = new CommandEvent(wrapCommandSender(sender), Joiner.on(" ").join(split));
|
||||
getWorldEdit().getEventBus().post(event);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(CommandSender sender, Command cmd, String commandLabel, String[] args) {
|
||||
// Add the command to the array because the underlying command handling
|
||||
// code of WorldEdit expects it
|
||||
String[] split = new String[args.length + 1];
|
||||
System.arraycopy(args, 0, split, 1, args.length);
|
||||
split[0] = cmd.getName();
|
||||
|
||||
CommandSuggestionEvent event = new CommandSuggestionEvent(wrapCommandSender(sender), Joiner.on(" ").join(split));
|
||||
getWorldEdit().getEventBus().post(event);
|
||||
return event.getSuggestions();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the session for the player.
|
||||
*
|
||||
* @param player a player
|
||||
* @return a session
|
||||
*/
|
||||
public LocalSession getSession(Player player) {
|
||||
return WorldEdit.getInstance().getSession(wrapPlayer(player));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the session for the player.
|
||||
*
|
||||
* @param player a player
|
||||
* @return a session
|
||||
*/
|
||||
public EditSession createEditSession(Player player) {
|
||||
LocalPlayer wePlayer = wrapPlayer(player);
|
||||
LocalSession session = WorldEdit.getInstance().getSession(wePlayer);
|
||||
BlockBag blockBag = session.getBlockBag(wePlayer);
|
||||
|
||||
EditSession editSession = WorldEdit.getInstance().getEditSessionFactory()
|
||||
.getEditSession(wePlayer.getWorld(), session.getBlockChangeLimit(), blockBag, wePlayer);
|
||||
editSession.enableQueue();
|
||||
|
||||
return editSession;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remember an edit session.
|
||||
*
|
||||
* @param player a player
|
||||
* @param editSession an edit session
|
||||
*/
|
||||
public void remember(Player player, EditSession editSession) {
|
||||
LocalPlayer wePlayer = wrapPlayer(player);
|
||||
LocalSession session = WorldEdit.getInstance().getSession(wePlayer);
|
||||
|
||||
session.remember(editSession);
|
||||
editSession.flushQueue();
|
||||
|
||||
WorldEdit.getInstance().flushBlockBag(wePlayer, editSession);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap an operation into an EditSession.
|
||||
*
|
||||
* @param player a player
|
||||
* @param op the operation
|
||||
* @throws Throwable on any error
|
||||
* @deprecated use the regular API
|
||||
*/
|
||||
@Deprecated
|
||||
public void perform(Player player, WorldEditOperation op) throws Throwable {
|
||||
LocalPlayer wePlayer = wrapPlayer(player);
|
||||
LocalSession session = WorldEdit.getInstance().getSession(wePlayer);
|
||||
|
||||
EditSession editSession = createEditSession(player);
|
||||
try {
|
||||
op.run(session, wePlayer, editSession);
|
||||
} finally {
|
||||
remember(player, editSession);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the API.
|
||||
*
|
||||
* @return the API
|
||||
* @deprecated use the regular API
|
||||
*/
|
||||
@Deprecated
|
||||
public WorldEditAPI getAPI() {
|
||||
return api;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the configuration used by WorldEdit.
|
||||
*
|
||||
* @return the configuration
|
||||
*/
|
||||
public BukkitConfiguration getLocalConfiguration() {
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the permissions resolver in use.
|
||||
*
|
||||
* @return the permissions resolver
|
||||
*/
|
||||
public PermissionsResolverManager getPermissionsResolver() {
|
||||
return PermissionsResolverManager.getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to wrap a Bukkit Player as a LocalPlayer.
|
||||
*
|
||||
* @param player a player
|
||||
* @return a wrapped player
|
||||
*/
|
||||
public BukkitPlayer wrapPlayer(Player player) {
|
||||
return new BukkitPlayer(this, this.server, player);
|
||||
}
|
||||
|
||||
public Actor wrapCommandSender(CommandSender sender) {
|
||||
if (sender instanceof Player) {
|
||||
return wrapPlayer((Player) sender);
|
||||
}
|
||||
|
||||
return new BukkitCommandSender(this, sender);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the server interface.
|
||||
*
|
||||
* @return the server interface
|
||||
*/
|
||||
public ServerInterface getServerInterface() {
|
||||
return server;
|
||||
}
|
||||
|
||||
BukkitServerInterface getInternalPlatform() {
|
||||
return server;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get WorldEdit.
|
||||
*
|
||||
* @return an instance
|
||||
*/
|
||||
public WorldEdit getWorldEdit() {
|
||||
return WorldEdit.getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the region selection for the player.
|
||||
*
|
||||
* @param player aplayer
|
||||
* @return the selection or null if there was none
|
||||
*/
|
||||
public Selection getSelection(Player player) {
|
||||
if (player == null) {
|
||||
throw new IllegalArgumentException("Null player not allowed");
|
||||
}
|
||||
if (!player.isOnline()) {
|
||||
throw new IllegalArgumentException("Offline player not allowed");
|
||||
}
|
||||
|
||||
LocalSession session = WorldEdit.getInstance().getSession(wrapPlayer(player));
|
||||
RegionSelector selector = session.getRegionSelector(BukkitUtil.getLocalWorld(player.getWorld()));
|
||||
|
||||
try {
|
||||
Region region = selector.getRegion();
|
||||
World world = BukkitAdapter.asBukkitWorld(session.getSelectionWorld()).getWorld();
|
||||
|
||||
if (region instanceof CuboidRegion) {
|
||||
return new CuboidSelection(world, selector, (CuboidRegion) region);
|
||||
} else if (region instanceof Polygonal2DRegion) {
|
||||
return new Polygonal2DSelection(world, selector, (Polygonal2DRegion) region);
|
||||
} else if (region instanceof CylinderRegion) {
|
||||
return new CylinderSelection(world, selector, (CylinderRegion) region);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} catch (IncompleteRegionException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the region selection for a player.
|
||||
*
|
||||
* @param player the player
|
||||
* @param selection a selection
|
||||
*/
|
||||
public void setSelection(Player player, Selection selection) {
|
||||
if (player == null) {
|
||||
throw new IllegalArgumentException("Null player not allowed");
|
||||
}
|
||||
if (!player.isOnline()) {
|
||||
throw new IllegalArgumentException("Offline player not allowed");
|
||||
}
|
||||
if (selection == null) {
|
||||
throw new IllegalArgumentException("Null selection not allowed");
|
||||
}
|
||||
|
||||
LocalSession session = WorldEdit.getInstance().getSession(wrapPlayer(player));
|
||||
RegionSelector sel = selection.getRegionSelector();
|
||||
session.setRegionSelector(BukkitUtil.getLocalWorld(player.getWorld()), sel);
|
||||
session.dispatchCUISelection(wrapPlayer(player));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the instance of this plugin.
|
||||
*
|
||||
* @return an instance of the plugin
|
||||
* @throws NullPointerException if the plugin hasn't been enabled
|
||||
*/
|
||||
static WorldEditPlugin getInstance() {
|
||||
return checkNotNull(INSTANCE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Bukkit implementation adapter.
|
||||
*
|
||||
* @return the adapter
|
||||
*/
|
||||
@Nullable
|
||||
BukkitImplAdapter getBukkitImplAdapter() {
|
||||
return bukkitAdapter;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.bukkit;
|
||||
|
||||
import com.sk89q.worldedit.WorldEditException;
|
||||
|
||||
/**
|
||||
* Thrown if the world has been unloaded.
|
||||
*/
|
||||
class WorldUnloadedException extends WorldEditException {
|
||||
|
||||
/**
|
||||
* Create a new instance.
|
||||
*/
|
||||
WorldUnloadedException() {
|
||||
super("The world was unloaded already");
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.bukkit.adapter;
|
||||
|
||||
/**
|
||||
* Thrown when no adapter can be found.
|
||||
*/
|
||||
public class AdapterLoadException extends Exception {
|
||||
|
||||
public AdapterLoadException() {
|
||||
}
|
||||
|
||||
public AdapterLoadException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public AdapterLoadException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public AdapterLoadException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
@ -0,0 +1,114 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.bukkit.adapter;
|
||||
|
||||
import com.sk89q.worldedit.blocks.BaseBlock;
|
||||
import com.sk89q.worldedit.entity.BaseEntity;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Biome;
|
||||
import org.bukkit.entity.Entity;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* An interface for adapters of various Bukkit implementations.
|
||||
*/
|
||||
public interface BukkitImplAdapter {
|
||||
|
||||
/**
|
||||
* Get the block ID for the given material.
|
||||
*
|
||||
* <p>Returns 0 if it is not known or it doesn't exist.</p>
|
||||
*
|
||||
* @param material the material
|
||||
* @return the block ID
|
||||
*/
|
||||
int getBlockId(Material material);
|
||||
|
||||
/**
|
||||
* Get the material for the given block ID.
|
||||
*
|
||||
* <p>Returns {@link Material#AIR} if it is not known or it doesn't exist.</p>
|
||||
*
|
||||
* @param id the block ID
|
||||
* @return the material
|
||||
*/
|
||||
Material getMaterial(int id);
|
||||
|
||||
/**
|
||||
* Get the biome ID for the given biome.
|
||||
*
|
||||
* <p>Returns 0 if it is not known or it doesn't exist.</p>
|
||||
*
|
||||
* @param biome biome
|
||||
* @return the biome ID
|
||||
*/
|
||||
int getBiomeId(Biome biome);
|
||||
|
||||
/**
|
||||
* Get the biome ID for the given biome ID..
|
||||
*
|
||||
* <p>Returns {@link Biome#OCEAN} if it is not known or it doesn't exist.</p>
|
||||
*
|
||||
* @param id the biome ID
|
||||
* @return the biome
|
||||
*/
|
||||
Biome getBiome(int id);
|
||||
|
||||
/**
|
||||
* Get the block at the given location.
|
||||
*
|
||||
* @param location the location
|
||||
* @return the block
|
||||
*/
|
||||
BaseBlock getBlock(Location location);
|
||||
|
||||
/**
|
||||
* Set the block at the given location.
|
||||
*
|
||||
* @param location the location
|
||||
* @param state the block
|
||||
* @param notifyAndLight notify and light if set
|
||||
* @return true if a block was likely changed
|
||||
*/
|
||||
boolean setBlock(Location location, BaseBlock state, boolean notifyAndLight);
|
||||
|
||||
/**
|
||||
* Get the state for the given entity.
|
||||
*
|
||||
* @param entity the entity
|
||||
* @return the state, or null
|
||||
*/
|
||||
@Nullable
|
||||
BaseEntity getEntity(Entity entity);
|
||||
|
||||
/**
|
||||
* Create the given entity.
|
||||
*
|
||||
* @param location the location
|
||||
* @param state the state
|
||||
* @return the created entity or null
|
||||
*/
|
||||
@Nullable
|
||||
Entity createEntity(Location location, BaseEntity state);
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,178 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.bukkit.adapter;
|
||||
|
||||
import com.sk89q.worldedit.util.io.Closer;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* Loads Bukkit implementation adapters.
|
||||
*/
|
||||
public class BukkitImplLoader {
|
||||
|
||||
private static final Logger log = Logger.getLogger(BukkitImplLoader.class.getCanonicalName());
|
||||
private final List<String> adapterCandidates = new ArrayList<String>();
|
||||
private String customCandidate;
|
||||
|
||||
private static final String SEARCH_PACKAGE = "com.sk89q.worldedit.bukkit.adapter.impl";
|
||||
private static final String SEARCH_PACKAGE_DOT = SEARCH_PACKAGE + ".";
|
||||
private static final String SEARCH_PATH = SEARCH_PACKAGE.replace(".", "/");
|
||||
private static final String CLASS_SUFFIX = ".class";
|
||||
|
||||
private static final String LOAD_ERROR_MESSAGE =
|
||||
"\n**********************************************\n" +
|
||||
"** This WorldEdit version does not fully support your version of Bukkit.\n" +
|
||||
"**\n" +
|
||||
"** When working with blocks or undoing, chests will be empty, signs\n" +
|
||||
"** will be blank, and so on. There will be no support for entity\n" +
|
||||
"** and biome-related functions.\n" +
|
||||
"**\n" +
|
||||
"** Please see http://wiki.sk89q.com/wiki/WorldEdit/Bukkit_adapters\n" +
|
||||
"**********************************************\n";
|
||||
|
||||
/**
|
||||
* Create a new instance.
|
||||
*/
|
||||
public BukkitImplLoader() {
|
||||
addDefaults();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add default candidates, such as any defined with
|
||||
* {@code -Dworldedit.bukkit.adapter}.
|
||||
*/
|
||||
private void addDefaults() {
|
||||
String className = System.getProperty("worldedit.bukkit.adapter");
|
||||
if (className != null) {
|
||||
customCandidate = className;
|
||||
adapterCandidates.add(className);
|
||||
log.log(Level.INFO, "-Dworldedit.bukkit.adapter used to add " + className + " to the list of available Bukkit adapters");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Search the given JAR for candidate implementations.
|
||||
*
|
||||
* @param file the file
|
||||
* @throws IOException thrown on I/O error
|
||||
*/
|
||||
public void addFromJar(File file) throws IOException {
|
||||
Closer closer = Closer.create();
|
||||
JarFile jar = closer.register(new JarFile(file));
|
||||
try {
|
||||
Enumeration entries = jar.entries();
|
||||
while (entries.hasMoreElements()) {
|
||||
JarEntry jarEntry = (JarEntry) entries.nextElement();
|
||||
|
||||
String className = jarEntry.getName().replaceAll("[/\\\\]+", ".");
|
||||
|
||||
if (!className.startsWith(SEARCH_PACKAGE_DOT) || jarEntry.isDirectory()) continue;
|
||||
|
||||
int beginIndex = 0;
|
||||
int endIndex = className.length() - CLASS_SUFFIX.length();
|
||||
className = className.substring(beginIndex, endIndex);
|
||||
adapterCandidates.add(className);
|
||||
}
|
||||
} finally {
|
||||
closer.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for classes stored as separate files available via the given
|
||||
* class loader.
|
||||
*
|
||||
* @param classLoader the class loader
|
||||
* @throws IOException thrown on error
|
||||
*/
|
||||
public void addFromPath(ClassLoader classLoader) throws IOException {
|
||||
Enumeration<URL> resources = classLoader.getResources(SEARCH_PATH);
|
||||
while (resources.hasMoreElements()) {
|
||||
File file = new File(resources.nextElement().getFile());
|
||||
addFromPath(file);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for classes stored as separate files available via the given
|
||||
* path.
|
||||
*
|
||||
* @param file the path
|
||||
*/
|
||||
private void addFromPath(File file) {
|
||||
String resource = SEARCH_PACKAGE_DOT + file.getName();
|
||||
if (file.isDirectory()) {
|
||||
File[] files = file.listFiles();
|
||||
if (files != null) {
|
||||
for (File child : files) {
|
||||
addFromPath(child);
|
||||
}
|
||||
}
|
||||
} else if (resource.endsWith(CLASS_SUFFIX)) {
|
||||
int beginIndex = 0;
|
||||
int endIndex = resource.length() - CLASS_SUFFIX.length();
|
||||
String className = resource.substring(beginIndex, endIndex);
|
||||
adapterCandidates.add(className);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterate through the list of candidates and load an adapter.
|
||||
*
|
||||
* @return an adapter
|
||||
* @throws AdapterLoadException thrown if no adapter could be found
|
||||
*/
|
||||
public BukkitImplAdapter loadAdapter() throws AdapterLoadException {
|
||||
for (String className : adapterCandidates) {
|
||||
try {
|
||||
Class<?> cls = Class.forName(className);
|
||||
if (BukkitImplAdapter.class.isAssignableFrom(cls)) {
|
||||
return (BukkitImplAdapter) cls.newInstance();
|
||||
} else {
|
||||
log.log(Level.WARNING, "Failed to load the Bukkit adapter class '" + className +
|
||||
"' because it does not implement " + BukkitImplAdapter.class.getCanonicalName());
|
||||
}
|
||||
} catch (ClassNotFoundException e) {
|
||||
log.log(Level.WARNING, "Failed to load the Bukkit adapter class '" + className +
|
||||
"' that is not supposed to be missing", e);
|
||||
} catch (IllegalAccessException e) {
|
||||
log.log(Level.WARNING, "Failed to load the Bukkit adapter class '" + className +
|
||||
"' that is not supposed to be raising this error", e);
|
||||
} catch (Throwable e) {
|
||||
if (className.equals(customCandidate)) {
|
||||
log.log(Level.WARNING, "Failed to load the Bukkit adapter class '" + className + "'", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw new AdapterLoadException(LOAD_ERROR_MESSAGE);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.bukkit.entity;
|
||||
|
||||
import com.sk89q.worldedit.LocalEntity;
|
||||
import com.sk89q.worldedit.Location;
|
||||
import com.sk89q.worldedit.bukkit.BukkitUtil;
|
||||
import org.bukkit.entity.EntityType;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class BukkitEntity extends LocalEntity {
|
||||
|
||||
private final EntityType type;
|
||||
private final UUID entityId;
|
||||
|
||||
public BukkitEntity(Location loc, EntityType type, UUID entityId) {
|
||||
super(loc);
|
||||
this.type = type;
|
||||
this.entityId = entityId;
|
||||
}
|
||||
|
||||
public UUID getEntityId() {
|
||||
return entityId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean spawn(Location weLoc) {
|
||||
org.bukkit.Location loc = BukkitUtil.toLocation(weLoc);
|
||||
return loc.getWorld().spawn(loc, type.getEntityClass()) != null;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.bukkit.entity;
|
||||
|
||||
import com.sk89q.worldedit.Location;
|
||||
import com.sk89q.worldedit.bukkit.BukkitUtil;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.ExperienceOrb;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class BukkitExpOrb extends BukkitEntity {
|
||||
|
||||
private final int amount;
|
||||
|
||||
public BukkitExpOrb(Location loc, UUID entityId, int amount) {
|
||||
super(loc, EntityType.EXPERIENCE_ORB, entityId);
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean spawn(Location weLoc) {
|
||||
org.bukkit.Location loc = BukkitUtil.toLocation(weLoc);
|
||||
ExperienceOrb orb = loc.getWorld().spawn(loc, ExperienceOrb.class);
|
||||
if (orb != null) {
|
||||
orb.setExperience(amount);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.bukkit.entity;
|
||||
|
||||
import com.sk89q.worldedit.Location;
|
||||
import com.sk89q.worldedit.bukkit.BukkitUtil;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class BukkitItem extends BukkitEntity {
|
||||
|
||||
private final ItemStack stack;
|
||||
public BukkitItem(Location loc, ItemStack stack, UUID entityId) {
|
||||
super(loc, EntityType.DROPPED_ITEM, entityId);
|
||||
this.stack = stack;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean spawn(Location weLoc) {
|
||||
org.bukkit.Location loc = BukkitUtil.toLocation(weLoc);
|
||||
return loc.getWorld().dropItem(loc, stack) != null;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,110 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.bukkit.entity;
|
||||
|
||||
import com.sk89q.worldedit.Location;
|
||||
import com.sk89q.worldedit.bukkit.BukkitUtil;
|
||||
import org.bukkit.Art;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.block.BlockFace;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.Painting;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Deque;
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class BukkitPainting extends BukkitEntity {
|
||||
|
||||
private static final Logger log = Logger.getLogger(BukkitPainting.class.getCanonicalName());
|
||||
|
||||
private static int spawnTask = -1;
|
||||
private static final Deque<QueuedPaintingSpawn> spawnQueue = new ArrayDeque<QueuedPaintingSpawn>();
|
||||
|
||||
private class QueuedPaintingSpawn {
|
||||
private final Location weLoc;
|
||||
|
||||
private QueuedPaintingSpawn(Location weLoc) {
|
||||
this.weLoc = weLoc;
|
||||
}
|
||||
|
||||
public void spawn() {
|
||||
spawnRaw(weLoc);
|
||||
}
|
||||
}
|
||||
|
||||
private static class PaintingSpawnRunnable implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
synchronized (spawnQueue) {
|
||||
QueuedPaintingSpawn spawn;
|
||||
while ((spawn = spawnQueue.poll()) != null) {
|
||||
try {
|
||||
spawn.spawn();
|
||||
} catch (Throwable t) {
|
||||
log.log(Level.WARNING, "Failed to spawn painting", t);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
spawnTask = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private final Art art;
|
||||
private final BlockFace facingDirection;
|
||||
public BukkitPainting(Location loc, Art art, BlockFace facingDirection, UUID entityId) {
|
||||
super(loc, EntityType.PAINTING, entityId);
|
||||
this.art = art;
|
||||
this.facingDirection = facingDirection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Queue the painting to be spawned at the specified location.
|
||||
* This operation is delayed so that the block changes that may be applied can be applied before the painting spawn is attempted.
|
||||
*
|
||||
* @param weLoc The WorldEdit location
|
||||
* @return Whether the spawn as successful
|
||||
*/
|
||||
@Override
|
||||
public boolean spawn(Location weLoc) {
|
||||
synchronized (spawnQueue) {
|
||||
spawnQueue.add(new QueuedPaintingSpawn(weLoc));
|
||||
if (spawnTask == -1) {
|
||||
spawnTask = Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Bukkit.getServer().getPluginManager().getPlugin("WorldEdit"), new PaintingSpawnRunnable(), 1L);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean spawnRaw(Location weLoc) {
|
||||
org.bukkit.Location loc = BukkitUtil.toLocation(weLoc);
|
||||
Painting paint = loc.getWorld().spawn(loc, Painting.class);
|
||||
if (paint != null) {
|
||||
paint.setFacingDirection(facingDirection, true);
|
||||
paint.setArt(art, true);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.bukkit.selections;
|
||||
|
||||
import com.sk89q.worldedit.Vector;
|
||||
import com.sk89q.worldedit.bukkit.BukkitUtil;
|
||||
import com.sk89q.worldedit.regions.CuboidRegion;
|
||||
import com.sk89q.worldedit.regions.RegionSelector;
|
||||
import com.sk89q.worldedit.regions.selector.CuboidRegionSelector;
|
||||
import com.sk89q.worldedit.regions.selector.limit.PermissiveSelectorLimits;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
|
||||
public class CuboidSelection extends RegionSelection {
|
||||
|
||||
protected CuboidRegion cuboid;
|
||||
|
||||
public CuboidSelection(World world, Location pt1, Location pt2) {
|
||||
this(world, BukkitUtil.toVector(pt1), BukkitUtil.toVector(pt2));
|
||||
}
|
||||
|
||||
public CuboidSelection(World world, Vector pt1, Vector pt2) {
|
||||
super(world);
|
||||
|
||||
// Validate input
|
||||
if (pt1 == null) {
|
||||
throw new IllegalArgumentException("Null point 1 not permitted");
|
||||
}
|
||||
|
||||
if (pt2 == null) {
|
||||
throw new IllegalArgumentException("Null point 2 not permitted");
|
||||
}
|
||||
|
||||
// Create new selector
|
||||
CuboidRegionSelector sel = new CuboidRegionSelector(BukkitUtil.getLocalWorld(world));
|
||||
|
||||
// set up selector
|
||||
sel.selectPrimary(pt1, PermissiveSelectorLimits.getInstance());
|
||||
sel.selectSecondary(pt2, PermissiveSelectorLimits.getInstance());
|
||||
|
||||
// set up CuboidSelection
|
||||
cuboid = sel.getIncompleteRegion();
|
||||
|
||||
// set up RegionSelection
|
||||
setRegionSelector(sel);
|
||||
setRegion(cuboid);
|
||||
}
|
||||
|
||||
public CuboidSelection(World world, RegionSelector sel, CuboidRegion region) {
|
||||
super(world, sel, region);
|
||||
this.cuboid = region;
|
||||
}
|
||||
}
|
@ -0,0 +1,80 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.bukkit.selections;
|
||||
|
||||
import com.sk89q.worldedit.regions.selector.CylinderRegionSelector;
|
||||
import org.bukkit.World;
|
||||
|
||||
import com.sk89q.worldedit.BlockVector2D;
|
||||
import com.sk89q.worldedit.LocalWorld;
|
||||
import com.sk89q.worldedit.bukkit.BukkitUtil;
|
||||
import com.sk89q.worldedit.regions.CylinderRegion;
|
||||
import com.sk89q.worldedit.regions.RegionSelector;
|
||||
|
||||
/**
|
||||
* A selection representing a {@link CylinderRegion}
|
||||
*/
|
||||
public class CylinderSelection extends RegionSelection {
|
||||
|
||||
private CylinderRegion cylRegion;
|
||||
|
||||
public CylinderSelection(World world, RegionSelector selector, CylinderRegion region) {
|
||||
super(world, selector, region);
|
||||
this.cylRegion = region;
|
||||
}
|
||||
|
||||
public CylinderSelection(World world, BlockVector2D center, BlockVector2D radius, int minY, int maxY) {
|
||||
super(world);
|
||||
LocalWorld lWorld = BukkitUtil.getLocalWorld(world);
|
||||
|
||||
// Validate input
|
||||
minY = Math.min(Math.max(0, minY), world.getMaxHeight());
|
||||
maxY = Math.min(Math.max(0, maxY), world.getMaxHeight());
|
||||
|
||||
// Create and set up new selector
|
||||
CylinderRegionSelector sel = new CylinderRegionSelector(lWorld, center, radius, minY, maxY);
|
||||
|
||||
// set up selection
|
||||
cylRegion = sel.getIncompleteRegion();
|
||||
|
||||
// set up RegionSelection
|
||||
setRegionSelector(sel);
|
||||
setRegion(cylRegion);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the center vector of the cylinder
|
||||
*
|
||||
* @return the center
|
||||
*/
|
||||
public BlockVector2D getCenter() {
|
||||
return cylRegion.getCenter().toVector2D().toBlockVector2D();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the radius vector of the cylinder
|
||||
*
|
||||
* @return the radius
|
||||
*/
|
||||
public BlockVector2D getRadius() {
|
||||
return cylRegion.getRadius().toBlockVector2D();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.bukkit.selections;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import com.sk89q.worldedit.LocalWorld;
|
||||
import com.sk89q.worldedit.regions.selector.Polygonal2DRegionSelector;
|
||||
import org.bukkit.World;
|
||||
import com.sk89q.worldedit.BlockVector2D;
|
||||
import com.sk89q.worldedit.bukkit.BukkitUtil;
|
||||
import com.sk89q.worldedit.regions.*;
|
||||
|
||||
public class Polygonal2DSelection extends RegionSelection {
|
||||
|
||||
protected Polygonal2DRegion poly2d;
|
||||
|
||||
public Polygonal2DSelection(World world, RegionSelector sel, Polygonal2DRegion region) {
|
||||
super(world, sel, region);
|
||||
this.poly2d = region;
|
||||
}
|
||||
|
||||
public Polygonal2DSelection(World world, List<BlockVector2D> points, int minY, int maxY) {
|
||||
super(world);
|
||||
LocalWorld lWorld = BukkitUtil.getLocalWorld(world);
|
||||
|
||||
// Validate input
|
||||
minY = Math.min(Math.max(0, minY), world.getMaxHeight());
|
||||
maxY = Math.min(Math.max(0, maxY), world.getMaxHeight());
|
||||
|
||||
// Create and set up new selector
|
||||
Polygonal2DRegionSelector sel = new Polygonal2DRegionSelector(lWorld, points, minY, maxY);
|
||||
|
||||
// set up CuboidSelection
|
||||
poly2d = sel.getIncompleteRegion();
|
||||
|
||||
// set up RegionSelection
|
||||
setRegionSelector(sel);
|
||||
setRegion(poly2d);
|
||||
}
|
||||
|
||||
public List<BlockVector2D> getNativePoints() {
|
||||
return Collections.unmodifiableList(poly2d.getPoints());
|
||||
}
|
||||
}
|
@ -0,0 +1,118 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.bukkit.selections;
|
||||
|
||||
import com.sk89q.worldedit.Vector;
|
||||
import com.sk89q.worldedit.regions.Region;
|
||||
import com.sk89q.worldedit.regions.RegionSelector;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
|
||||
import static com.sk89q.worldedit.bukkit.BukkitUtil.toLocation;
|
||||
import static com.sk89q.worldedit.bukkit.BukkitUtil.toVector;
|
||||
|
||||
public abstract class RegionSelection implements Selection {
|
||||
|
||||
private World world;
|
||||
private RegionSelector selector;
|
||||
private Region region;
|
||||
|
||||
public RegionSelection(World world) {
|
||||
this.world = world;
|
||||
}
|
||||
|
||||
public RegionSelection(World world, RegionSelector selector, Region region) {
|
||||
this.world = world;
|
||||
this.region = region;
|
||||
this.selector = selector;
|
||||
}
|
||||
|
||||
protected Region getRegion() {
|
||||
return region;
|
||||
}
|
||||
|
||||
protected void setRegion(Region region) {
|
||||
this.region = region;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RegionSelector getRegionSelector() {
|
||||
return selector;
|
||||
}
|
||||
|
||||
protected void setRegionSelector(RegionSelector selector) {
|
||||
this.selector = selector;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Location getMinimumPoint() {
|
||||
return toLocation(world, region.getMinimumPoint());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vector getNativeMinimumPoint() {
|
||||
return region.getMinimumPoint();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Location getMaximumPoint() {
|
||||
return toLocation(world, region.getMaximumPoint());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vector getNativeMaximumPoint() {
|
||||
return region.getMaximumPoint();
|
||||
}
|
||||
|
||||
@Override
|
||||
public World getWorld() {
|
||||
return world;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getArea() {
|
||||
return region.getArea();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWidth() {
|
||||
return region.getWidth();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHeight() {
|
||||
return region.getHeight();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLength() {
|
||||
return region.getLength();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Location position) {
|
||||
if (!position.getWorld().equals(world)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return region.contains(toVector(position));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,113 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.bukkit.selections;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import com.sk89q.worldedit.Vector;
|
||||
import com.sk89q.worldedit.regions.RegionSelector;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* An abstraction of WorldEdit regions, which do not use Bukkit objects.
|
||||
*/
|
||||
public interface Selection {
|
||||
|
||||
/**
|
||||
* Get the lower point of a region.
|
||||
*
|
||||
* @return min. point
|
||||
*/
|
||||
public Location getMinimumPoint();
|
||||
|
||||
/**
|
||||
* Get the lower point of a region.
|
||||
*
|
||||
* @return min. point
|
||||
*/
|
||||
public Vector getNativeMinimumPoint();
|
||||
|
||||
/**
|
||||
* Get the upper point of a region.
|
||||
*
|
||||
* @return max. point
|
||||
*/
|
||||
public Location getMaximumPoint();
|
||||
|
||||
/**
|
||||
* Get the upper point of a region.
|
||||
*
|
||||
* @return max. point
|
||||
*/
|
||||
public Vector getNativeMaximumPoint();
|
||||
|
||||
/**
|
||||
* Get the region selector. This is for internal use.
|
||||
*
|
||||
* @return the region selector
|
||||
*/
|
||||
public RegionSelector getRegionSelector();
|
||||
|
||||
/**
|
||||
* Get the world.
|
||||
*
|
||||
* @return the world, which may be null
|
||||
*/
|
||||
@Nullable
|
||||
public World getWorld();
|
||||
|
||||
/**
|
||||
* Get the number of blocks in the region.
|
||||
*
|
||||
* @return number of blocks
|
||||
*/
|
||||
public int getArea();
|
||||
|
||||
/**
|
||||
* Get X-size.
|
||||
*
|
||||
* @return width
|
||||
*/
|
||||
public int getWidth();
|
||||
|
||||
/**
|
||||
* Get Y-size.
|
||||
*
|
||||
* @return height
|
||||
*/
|
||||
public int getHeight();
|
||||
|
||||
/**
|
||||
* Get Z-size.
|
||||
*
|
||||
* @return length
|
||||
*/
|
||||
public int getLength();
|
||||
|
||||
/**
|
||||
* Returns true based on whether the region contains the point,
|
||||
*
|
||||
* @param position a vector
|
||||
* @return true if it is contained
|
||||
*/
|
||||
public boolean contains(Location position);
|
||||
|
||||
}
|
Reference in New Issue
Block a user