Last active
December 24, 2020 01:06
-
-
Save BGMP/c9c3ce05d0c8e613437a9dd208047485 to your computer and use it in GitHub Desktop.
Command blocks's @s breaks on Bukkit. This fixes it for close interactions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cl.bgmp.lobbyx.listeners; | |
import cl.bgmp.lobbyx.events.PreciseCommandBlockEvent; | |
import org.bukkit.Bukkit; | |
import org.bukkit.block.Block; | |
import org.bukkit.block.BlockFace; | |
import org.bukkit.block.BlockState; | |
import org.bukkit.block.CommandBlock; | |
import org.bukkit.command.BlockCommandSender; | |
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.PlayerInteractEvent; | |
import org.bukkit.event.server.ServerCommandEvent; | |
/** | |
* Handles commands sent via @{@link BlockCommandSender} and replaces the broken variables in | |
* Bukkit, to then dispatch the involved command via @{@link | |
* org.bukkit.command.ConsoleCommandSender} | |
*/ | |
public class CommandBlockEvents implements Listener { | |
private static final BlockFace[] faces = { | |
BlockFace.DOWN, BlockFace.UP, BlockFace.NORTH, BlockFace.EAST, BlockFace.WEST | |
}; | |
@EventHandler(priority = EventPriority.MONITOR) | |
public void onPlayerInteract(PlayerInteractEvent event) { | |
final Action action = event.getAction(); | |
if (action != Action.PHYSICAL) return; | |
final Block block = event.getClickedBlock(); | |
if (block == null) return; | |
CommandBlock commandBlock = null; | |
for (int i = 0; i < 6; i++) { | |
BlockState nearby = block.getRelative(faces[i]).getState(); | |
if (nearby instanceof CommandBlock) { | |
commandBlock = (CommandBlock) nearby; | |
break; | |
} | |
} | |
if (commandBlock == null) return; | |
final PreciseCommandBlockEvent preciseCommandBlockEvent = | |
new PreciseCommandBlockEvent(event.getPlayer(), commandBlock); | |
Bukkit.getServer().getPluginManager().callEvent(preciseCommandBlockEvent); | |
} | |
@EventHandler(priority = EventPriority.HIGH) | |
public void onCommandBlockEvent(ServerCommandEvent event) {} | |
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) | |
public void onPreciseCommandBlockEvent(PreciseCommandBlockEvent event) { | |
String command = event.getCommand().replaceAll("@s", event.getPlayer().getName()); | |
Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), command); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cl.bgmp.lobbyx.events; | |
import org.bukkit.block.CommandBlock; | |
import org.bukkit.entity.Player; | |
import org.bukkit.event.Event; | |
import org.bukkit.event.HandlerList; | |
import org.jetbrains.annotations.NotNull; | |
public class PreciseCommandBlockEvent extends Event { | |
private static final HandlerList handlerList = new HandlerList(); | |
private final Player player; | |
private final CommandBlock commandBlock; | |
private final String command; | |
public PreciseCommandBlockEvent(Player player, CommandBlock commandBlock) { | |
this.player = player; | |
this.commandBlock = commandBlock; | |
this.command = commandBlock.getCommand(); | |
} | |
public Player getPlayer() { | |
return player; | |
} | |
public CommandBlock getCommandBlock() { | |
return commandBlock; | |
} | |
public String getCommand() { | |
return command; | |
} | |
public static HandlerList getHandlerList() { | |
return handlerList; | |
} | |
@Override | |
public @NotNull HandlerList getHandlers() { | |
return handlerList; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment