Skip to content

Commit

Permalink
Added CommandBlock support to Bukkit
Browse files Browse the repository at this point in the history
  • Loading branch information
me4502 committed Jul 27, 2019
1 parent f664190 commit 513f0a0
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* 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 static com.google.common.base.Preconditions.checkNotNull;

import com.sk89q.worldedit.extension.platform.AbstractNonPlayerActor;
import com.sk89q.worldedit.extension.platform.Locatable;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.session.SessionKey;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.util.auth.AuthorizationException;
import com.sk89q.worldedit.util.formatting.text.Component;
import com.sk89q.worldedit.util.formatting.text.adapter.bukkit.TextAdapter;
import org.bukkit.Material;
import org.bukkit.command.BlockCommandSender;

import java.util.UUID;

import javax.annotation.Nullable;

public class BukkitBlockCommandSender extends AbstractNonPlayerActor implements Locatable {

private final BlockCommandSender sender;
private final WorldEditPlugin plugin;
private final Location location;
private final UUID uuid;

public BukkitBlockCommandSender(WorldEditPlugin plugin, BlockCommandSender sender) {
checkNotNull(plugin);
checkNotNull(sender);

this.plugin = plugin;
this.sender = sender;
this.location = BukkitAdapter.adapt(sender.getBlock().getLocation());
this.uuid = new UUID(location.toVector().toBlockPoint().hashCode(), location.getExtent().hashCode());
}

@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 void print(Component component) {
TextAdapter.sendComponent(sender, component);
}

@Override
public Location getLocation() {
return this.location;
}

@Override
public boolean setLocation(Location location) {
return false;
}

@Override
public Extent getExtent() {
return this.location.getExtent();
}

@Override
public UUID getUniqueId() {
return uuid;
}

@Override
public String[] getGroups() {
return new String[0];
}

@Override
public void checkPermission(String permission) throws AuthorizationException {
if (!hasPermission(permission)) {
throw new AuthorizationException();
}
}

@Override
public boolean hasPermission(String permission) {
return sender.hasPermission(permission);
}

@Override
public SessionKey getSessionKey() {
return new SessionKey() {
@Nullable
@Override
public String getName() {
return sender.getName();
}

@Override
public boolean isActive() {
return sender.getBlock().getType() == Material.COMMAND_BLOCK
|| sender.getBlock().getType() == Material.CHAIN_COMMAND_BLOCK
|| sender.getBlock().getType() == Material.REPEATING_COMMAND_BLOCK;
}

@Override
public boolean isPersistent() {
return false;
}

@Override
public UUID getUniqueId() {
return uuid;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,19 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.internal.cui.CUIEvent;
import com.sk89q.worldedit.extension.platform.AbstractNonPlayerActor;
import com.sk89q.worldedit.session.SessionKey;
import com.sk89q.worldedit.util.auth.AuthorizationException;
import com.sk89q.worldedit.util.formatting.text.Component;
import com.sk89q.worldedit.util.formatting.text.adapter.bukkit.TextAdapter;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.io.File;
import java.util.UUID;

import javax.annotation.Nullable;

public class BukkitCommandSender implements Actor {
public class BukkitCommandSender extends AbstractNonPlayerActor {

/**
* One time generated ID.
Expand Down Expand Up @@ -98,11 +96,6 @@ public void print(Component component) {
TextAdapter.sendComponent(sender, component);
}

@Override
public boolean canDestroyBedrock() {
return true;
}

@Override
public String[] getGroups() {
return new String[0];
Expand All @@ -117,42 +110,23 @@ public boolean hasPermission(String perm) {
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;
return sender.getName();
}

@Override
public boolean isActive() {
return false;
return true;
}

@Override
public boolean isPersistent() {
return false;
return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.bukkit.Material;
import org.bukkit.Tag;
import org.bukkit.block.Biome;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
Expand Down Expand Up @@ -429,6 +430,8 @@ public BukkitPlayer wrapPlayer(Player player) {
public Actor wrapCommandSender(CommandSender sender) {
if (sender instanceof Player) {
return wrapPlayer((Player) sender);
} else if (sender instanceof BlockCommandSender) {
return new BukkitBlockCommandSender(this, (BlockCommandSender) sender);
}

return new BukkitCommandSender(this, sender);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -913,8 +913,8 @@ public EditSession createEditSession(Actor actor) {
World world = null;
if (hasWorldOverride()) {
world = getWorldOverride();
} else if (actor.isPlayer() && actor instanceof Player) {
world = ((Player) actor).getWorld();
} else if (actor instanceof Locatable && ((Locatable) actor).getExtent() instanceof World) {
world = (World) ((Locatable) actor).getExtent();
}

// Create an edit session
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.extension.platform;

import com.sk89q.worldedit.internal.cui.CUIEvent;

import java.io.File;

public abstract class AbstractNonPlayerActor implements Actor {

@Override
public boolean canDestroyBedrock() {
return true;
}

@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) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ private void registerAlwaysInjectedValues() {
LocalSession localSession = context.injectedValue(Key.of(LocalSession.class))
.orElseThrow(() -> new IllegalStateException("No LocalSession"));
return context.injectedValue(Key.of(Actor.class))
.map(player -> {
EditSession editSession = localSession.createEditSession(player);
.map(actor -> {
EditSession editSession = localSession.createEditSession(actor);
editSession.enableStandardMode();
return editSession;
});
Expand All @@ -258,8 +258,8 @@ private void registerAlwaysInjectedValues() {
try {
if (localSession.hasWorldOverride()) {
return localSession.getWorldOverride();
} else if (actor.isPlayer() && actor instanceof Player) {
return ((Player) actor).getWorld();
} else if (actor instanceof Locatable && ((Locatable) actor).getExtent() instanceof World) {
return (World) ((Locatable) actor).getExtent();
} else {
throw new MissingWorldException();
}
Expand Down

0 comments on commit 513f0a0

Please sign in to comment.