Last active
February 29, 2020 16:20
-
-
Save MelonCode/ed0f943e50833fc6461b to your computer and use it in GitHub Desktop.
A sourcecode of "Semicolon;" spigot server plugin. https://www.spigotmc.org/resources/semicolon.12273/
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 ru.meloncode.semicolon; | |
import org.bukkit.Bukkit; | |
import org.bukkit.Material; | |
import org.bukkit.block.CommandBlock; | |
import org.bukkit.command.CommandSender; | |
import org.bukkit.event.EventHandler; | |
import org.bukkit.event.EventPriority; | |
import org.bukkit.event.Listener; | |
import org.bukkit.event.block.BlockRedstoneEvent; | |
import org.bukkit.event.player.PlayerCommandPreprocessEvent; | |
import org.bukkit.event.server.ServerCommandEvent; | |
import org.bukkit.plugin.java.JavaPlugin; | |
/* | |
This file is part of "Semicolon;". | |
"Semicolon;" is free spigot server plugin: you can redistribute it | |
and/or modify it under the terms of the GNU General Public License | |
as published by the Free Software Foundation, either version 3 of | |
the License, or (at your option) any later version. | |
"Semicolon;" is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with "Semicolon;". If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
public class Semicolon extends JavaPlugin implements Listener { | |
@Override | |
public void onEnable() { | |
Bukkit.getPluginManager().registerEvents(this, this); | |
} | |
@EventHandler(priority = EventPriority.HIGHEST) | |
public void onPlayerCommand(PlayerCommandPreprocessEvent event) { | |
if (!(event.getPlayer().isOp() && event.getPlayer().hasPermission("semicolon.use"))) { | |
return; | |
} | |
if (event.getMessage().contains(";")) { | |
event.setCancelled(true); | |
processCommand(event.getPlayer(), event.getMessage()); | |
} | |
} | |
@EventHandler(priority = EventPriority.HIGHEST) | |
public void onConsoleCommand(ServerCommandEvent event) { | |
if (event.getCommand().contains(";")) { | |
event.setCancelled(true); | |
processCommand(event.getSender(), event.getCommand()); | |
} | |
} | |
@EventHandler(priority = EventPriority.HIGHEST) | |
public void onCommandBlockRedstonePower(BlockRedstoneEvent event) { | |
if (event.getBlock().getType() != Material.COMMAND) | |
return; | |
if (event.getNewCurrent() <= event.getOldCurrent()) | |
return; | |
CommandBlock block = (CommandBlock) event.getBlock().getState(); | |
if (block.getCommand().contains(";")) { | |
event.setNewCurrent(0); | |
new CommandBlockTask(event.getBlock(), block.getCommand()).runTaskTimer(this, 0, 1); | |
} | |
} | |
private void processCommand(CommandSender sender, String message) { | |
String[] commands = cleanCommand(message).split(";"); | |
if (commands.length > 0) { | |
for (String command : commands) { | |
Bukkit.dispatchCommand(sender, cleanCommand(command)); | |
} | |
} | |
} | |
static String cleanCommand(String command) { | |
if (command.startsWith("/") || command.startsWith(" ")) command = command.substring(1); | |
if (command.endsWith(";")) command = command.substring(0, command.length() - 1); | |
return 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 ru.meloncode.semicolon; | |
import org.bukkit.Material; | |
import org.bukkit.block.Block; | |
import org.bukkit.block.BlockFace; | |
import org.bukkit.block.CommandBlock; | |
import org.bukkit.scheduler.BukkitRunnable; | |
/* | |
This file is part of "Semicolon;". | |
"Semicolon;" is free spigot server plugin: you can redistribute it | |
and/or modify it under the terms of the GNU General Public License | |
as published by the Free Software Foundation, either version 3 of | |
the License, or (at your option) any later version. | |
"Semicolon;" is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with "Semicolon;". If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
public class CommandBlockTask extends BukkitRunnable { | |
String line; | |
String[] commands; | |
Block block; | |
Block down; | |
Material dType; | |
byte dData; | |
int i; | |
public CommandBlockTask(Block block, String line) { | |
this.block = block; | |
this.down = block.getRelative(BlockFace.DOWN); | |
this.line = line; | |
this.commands = line.split(";"); | |
this.i = 0; | |
this.dType = down.getType(); | |
this.dData = down.getData(); | |
} | |
@Override | |
public void run() { | |
block.setType(Material.AIR); // We need this to reset CommandBlock charge. | |
//It's kind of magic, but it works. | |
down.setType(Material.AIR); // Setting block under CommandBlock to air for further activation | |
block.setType(Material.COMMAND); // Setting Block type to CommandBlock back. | |
if (i < commands.length) { | |
setCommand(Semicolon.cleanCommand(commands[i])); | |
down.setType(Material.REDSTONE_BLOCK); //Activating CommandBlock | |
i++; | |
} else { | |
//Job done. Restoring down-block and it's data. Then cancelling task | |
block.setType(Material.AIR); //Preventing recursion | |
down.setType(dType); | |
down.setData(dData); | |
block.setType(Material.COMMAND); | |
setCommand(line); //Setting initial multicommand back | |
cancel(); | |
} | |
} | |
private void setCommand(String command) { | |
CommandBlock commandBlock = ((CommandBlock) block.getState()); | |
commandBlock.setCommand(command); | |
commandBlock.update(true); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated to v1.2
Also added licence text