Skip to content

Commit

Permalink
Merge common print functions into abstract class
Browse files Browse the repository at this point in the history
  • Loading branch information
octylFractal committed Aug 12, 2020
1 parent a872c37 commit e588d51
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 302 deletions.
6 changes: 4 additions & 2 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ include("worldedit-libs:core:ap")

include("worldedit-core:doctools")

includeBuild("../Piston") {
dependencySubstitution {
if (gradle.plugins.none { "JetGradlePlugin" in it.javaClass.name }) {
includeBuild("../Piston") {
dependencySubstitution {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@
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.WorldEditText;
import com.sk89q.worldedit.util.formatting.text.Component;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
import com.sk89q.worldedit.util.formatting.text.format.NamedTextColor;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
Expand All @@ -41,23 +35,18 @@
import java.util.Locale;
import java.util.UUID;

import static com.google.common.base.Preconditions.checkNotNull;

public class BukkitBlockCommandSender extends AbstractNonPlayerActor implements Locatable {

private static final String UUID_PREFIX = "CMD";

private final Audience audience;
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);
super(plugin.getAudiences().audience(sender)::sendMessage);

this.audience = BukkitAudiences.create(plugin).audience(sender);
this.plugin = plugin;
this.sender = sender;
this.location = BukkitAdapter.adapt(sender.getBlock().getLocation());
Expand All @@ -69,43 +58,6 @@ public String getName() {
return sender.getName();
}

@Override
@Deprecated
public void printRaw(String msg) {
for (String part : msg.split("\n")) {
sender.sendMessage(part);
}
}

@Override
@Deprecated
public void print(String msg) {
for (String part : msg.split("\n")) {
print(TextComponent.of(part, NamedTextColor.LIGHT_PURPLE));
}
}

@Override
@Deprecated
public void printDebug(String msg) {
for (String part : msg.split("\n")) {
print(TextComponent.of(part, NamedTextColor.GRAY));
}
}

@Override
@Deprecated
public void printError(String msg) {
for (String part : msg.split("\n")) {
print(TextComponent.of(part, NamedTextColor.RED));
}
}

@Override
public void print(Component component) {
audience.sendMessage(WorldEditText.format(component, getLocale()));
}

@Override
public Locale getLocale() {
return WorldEdit.getInstance().getConfiguration().defaultLocale;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,13 @@
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.platform.AbstractNonPlayerActor;
import com.sk89q.worldedit.session.SessionKey;
import com.sk89q.worldedit.util.formatting.WorldEditText;
import com.sk89q.worldedit.util.formatting.text.Component;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.Locale;
import java.util.UUID;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

public class BukkitCommandSender extends AbstractNonPlayerActor {

Expand All @@ -42,15 +37,12 @@ public class BukkitCommandSender extends AbstractNonPlayerActor {
*/
private static final UUID DEFAULT_ID = UUID.fromString("a233eb4b-4cab-42cd-9fd9-7e7b9a3f74be");

private final Audience audience;
private final CommandSender sender;

public BukkitCommandSender(WorldEditPlugin plugin, CommandSender sender) {
checkNotNull(plugin);
checkNotNull(sender);
super(plugin.getAudiences().audience(sender)::sendMessage);
checkArgument(!(sender instanceof Player), "Cannot wrap a player");

this.audience = BukkitAudiences.create(plugin).audience(sender);
this.sender = sender;
}

Expand All @@ -64,43 +56,6 @@ public String getName() {
return sender.getName();
}

@Override
@Deprecated
public void printRaw(String msg) {
for (String part : msg.split("\n")) {
sender.sendMessage(part);
}
}

@Override
@Deprecated
public void print(String msg) {
for (String part : msg.split("\n")) {
sender.sendMessage("§d" + part);
}
}

@Override
@Deprecated
public void printDebug(String msg) {
for (String part : msg.split("\n")) {
sender.sendMessage("§7" + part);
}
}

@Override
@Deprecated
public void printError(String msg) {
for (String part : msg.split("\n")) {
sender.sendMessage("§c" + part);
}
}

@Override
public void print(Component component) {
audience.sendMessage(WorldEditText.format(component, getLocale()));
}

@Override
public String[] getGroups() {
return new String[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@
import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldedit.session.SessionKey;
import com.sk89q.worldedit.util.HandSide;
import com.sk89q.worldedit.util.formatting.WorldEditText;
import com.sk89q.worldedit.util.formatting.component.TextUtils;
import com.sk89q.worldedit.util.formatting.text.Component;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
import com.sk89q.worldedit.util.formatting.text.TranslatableComponent;
import com.sk89q.worldedit.util.formatting.text.event.ClickEvent;
Expand All @@ -44,8 +42,6 @@
import com.sk89q.worldedit.world.block.BlockTypes;
import com.sk89q.worldedit.world.gamemode.GameMode;
import com.sk89q.worldedit.world.gamemode.GameModes;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
Expand All @@ -59,16 +55,15 @@ public class BukkitPlayer extends AbstractPlayerActor {

private final Player player;
private final WorldEditPlugin plugin;
private final Audience audience;

public BukkitPlayer(Player player) {
this(WorldEditPlugin.getInstance(), player);
}

public BukkitPlayer(WorldEditPlugin plugin, Player player) {
super(plugin.getAudiences().player(player)::sendMessage);
this.plugin = plugin;
this.player = player;
this.audience = BukkitAudiences.create(plugin).player(player);
}

@Override
Expand Down Expand Up @@ -107,43 +102,6 @@ public void giveItem(BaseItemStack itemStack) {
player.getInventory().addItem(BukkitAdapter.adapt(itemStack));
}

@Override
@Deprecated
public void printRaw(String msg) {
for (String part : msg.split("\n")) {
player.sendMessage(part);
}
}

@Override
@Deprecated
public void print(String msg) {
for (String part : msg.split("\n")) {
player.sendMessage("§d" + part);
}
}

@Override
@Deprecated
public void printDebug(String msg) {
for (String part : msg.split("\n")) {
player.sendMessage("§7" + part);
}
}

@Override
@Deprecated
public void printError(String msg) {
for (String part : msg.split("\n")) {
player.sendMessage("§c" + part);
}
}

@Override
public void print(Component component) {
audience.sendMessage(WorldEditText.format(component, getLocale()));
}

@Override
public boolean trySetPosition(Vector3 pos, float pitch, float yaw) {
return player.teleport(new Location(player.getWorld(), pos.getX(), pos.getY(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.world.weather.WeatherTypes;
import io.papermc.lib.PaperLib;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
import org.bukkit.Material;
Expand Down Expand Up @@ -114,10 +115,12 @@ public class WorldEditPlugin extends JavaPlugin implements TabCompleter {
private BukkitImplAdapter bukkitAdapter;
private BukkitServerInterface server;
private BukkitConfiguration config;
private BukkitAudiences audiences;

@Override
public void onLoad() {
INSTANCE = this;
audiences = BukkitAudiences.create(this);

//noinspection ResultOfMethodCallIgnored
getDataFolder().mkdirs();
Expand Down Expand Up @@ -431,6 +434,15 @@ public BukkitConfiguration getLocalConfiguration() {
return config;
}

/**
* Get the audience provider for this plugin.
*
* @return the audience provider
*/
public BukkitAudiences getAudiences() {
return audiences;
}

/**
* Get the permissions resolver in use.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.sk89q.worldedit.extension.platform;

import com.sk89q.worldedit.util.formatting.WorldEditText;
import com.sk89q.worldedit.util.formatting.text.Component;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
import com.sk89q.worldedit.util.formatting.text.format.NamedTextColor;

import java.util.function.Consumer;

/**
* Base class for implementing an actor. Provides reasonable defaults.
*/
public abstract class AbstractActor implements Actor {

private final Consumer<Component> sendMessage;

protected AbstractActor(Consumer<Component> sendMessage) {
this.sendMessage = sendMessage;
}

@Override
@Deprecated
public void printRaw(String msg) {
for (String part : msg.split("\n")) {
print(TextComponent.of(part));
}
}

@Override
@Deprecated
public void print(String msg) {
for (String part : msg.split("\n")) {
print(TextComponent.of(part, NamedTextColor.LIGHT_PURPLE));
}
}

@Override
@Deprecated
public void printDebug(String msg) {
for (String part : msg.split("\n")) {
print(TextComponent.of(part, NamedTextColor.GRAY));
}
}

@Override
@Deprecated
public void printError(String msg) {
for (String part : msg.split("\n")) {
print(TextComponent.of(part, NamedTextColor.RED));
}
}

@Override
public void print(Component component) {
sendMessage.accept(WorldEditText.format(component, getLocale()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@
package com.sk89q.worldedit.extension.platform;

import com.sk89q.worldedit.internal.cui.CUIEvent;
import com.sk89q.worldedit.util.formatting.text.Component;

import java.io.File;
import java.util.function.Consumer;

public abstract class AbstractNonPlayerActor implements Actor {
public abstract class AbstractNonPlayerActor extends AbstractActor implements Actor {

protected AbstractNonPlayerActor(Consumer<Component> sendMessage) {
super(sendMessage);
}

@Override
public boolean canDestroyBedrock() {
Expand Down
Loading

0 comments on commit e588d51

Please sign in to comment.