Skip to content

Commit

Permalink
[v1.2.2] tellraw の仕様変更に対応
Browse files Browse the repository at this point in the history
  • Loading branch information
yuttyann committed Dec 7, 2022
1 parent 7b2911c commit 5fb4e3b
Show file tree
Hide file tree
Showing 2 changed files with 199 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* ScriptEntityPlus - Allow you to add script to any entities.
* Copyright (C) 2021 yuttyann44581
*
* This program is free software: 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.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <https://www.gnu.org/licenses/>.
*/
package com.github.yuttyann.scriptentityplus.listener;

import com.github.yuttyann.scriptblockplus.enums.Permission;
import com.github.yuttyann.scriptblockplus.utils.StringUtils;
import com.github.yuttyann.scriptentityplus.ScriptEntity;
import com.github.yuttyann.scriptentityplus.item.ScriptConnection;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.filter.AbstractFilter;
import org.apache.logging.log4j.message.Message;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.LogEvent;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.server.ServerCommandEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class CommandListener implements Listener {

private static final String TELLRAW_PREFIX = "tellraw @s \"";

static {
org.apache.logging.log4j.Logger rootLogger = LogManager.getRootLogger();
if (rootLogger instanceof Logger) {
((Logger) rootLogger).addFilter(new CommandLogFilter());
}
}

@EventHandler(priority = EventPriority.HIGH)
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
Player player = event.getPlayer();
String command = event.getMessage().startsWith("/") ? event.getMessage().substring(1) : event.getMessage();
if (Permission.COMMAND_CHECKVER.has(player) && (command.equals("sbp checkver") || command.equals("scriptblockplus checkver"))) {
ScriptEntity.getInstance().checkUpdate(player, true);
} else if (command.startsWith(TELLRAW_PREFIX)) {
TellrawCatcher.onCatch(command.substring(TELLRAW_PREFIX.length(), command.length() - 1), event);
}
}

@EventHandler(priority = EventPriority.HIGH)
public void onServerCommand(ServerCommandEvent event) {
CommandSender sender = event.getSender();
String command = event.getCommand().startsWith("/") ? event.getCommand().substring(1) : event.getCommand();
if (Permission.COMMAND_CHECKVER.has(sender) && (command.equals("sbp checkver") || command.equals("scriptblockplus checkver"))) {
ScriptEntity.getInstance().checkUpdate(sender, true);
}
}

private static class CommandLogFilter extends AbstractFilter {

private static final boolean USE_RAW_STRING = false;

private CommandLogFilter() {
super(Filter.Result.DENY, Filter.Result.NEUTRAL);
}

@Override
public Result filter(LogEvent event) {
Message message = event == null ? null : event.getMessage();
return doFilter(message == null ? null : (USE_RAW_STRING ? message.getFormat() : message.getFormattedMessage()));
}

@Override
public Result filter(Logger logger, Level level, Marker marker, Object msg, Throwable t) {
return doFilter(msg == null ? null : msg.toString());
}

@Override
public Result filter(Logger logger, Level level, Marker marker, String msg, Object... params) {
return doFilter(msg);
}

@Override
public Result filter(Logger logger, Level level, Marker marker, Message msg, Throwable t) {
return doFilter(msg == null ? null : (USE_RAW_STRING ? msg.getFormat() : msg.getFormattedMessage()));
}

@NotNull
private Result doFilter(@Nullable String message) {
return StringUtils.isNotEmpty(message) && message.contains(TELLRAW_PREFIX) && message.contains(ScriptConnection.KEY_TELLRAW) ? onMatch : onMismatch;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.github.yuttyann.scriptentityplus.listener;

import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.jetbrains.annotations.NotNull;

import com.github.yuttyann.scriptblockplus.BlockCoords;
import com.github.yuttyann.scriptblockplus.ScriptBlock;
import com.github.yuttyann.scriptblockplus.script.ScriptKey;
import com.github.yuttyann.scriptblockplus.utils.StringUtils;
import com.github.yuttyann.scriptentityplus.SEPermission;
import com.github.yuttyann.scriptentityplus.file.SEConfig;
import com.github.yuttyann.scriptentityplus.item.ScriptConnection;
import com.github.yuttyann.scriptentityplus.json.EntityScriptJson;

class TellrawCatcher {

private static final Pattern UUID_PATTERN = Pattern.compile("[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}");

static void onCatch(@NotNull String message, @NotNull Cancellable event) {
if (StringUtils.isEmpty(message)) {
return;
}
int count = count(message, '|');
if (count != 4) {
return;
}
List<String> split = StringUtils.split(message, '|');
if (split.size() != 5 || !UUID_PATTERN.matcher(split.get(4)).matches()) {
return;
}
event.setCancelled(true);
if (ScriptConnection.KEY_TELLRAW.equals(split.get(4))) {
try {
Player player = Bukkit.getPlayerExact(split.get(3));
if (player == null || !SEPermission.TOOL_SCRIPT_CONNECTION.has(player)) {
return;
}
switch (Integer.parseInt(split.get(2))) {
case 0: {
ScriptKey scriptKey = ScriptKey.valueOf(split.get(0));
BlockCoords blockCoords = BlockCoords.fromString(split.get(1));
ScriptBlock.getSBPlayer(player).getObjectMap().put(ScriptConnection.KEY_SCRIPT, new String[] { scriptKey.getName(), blockCoords.getFullCoords() });
SEConfig.SCRIPT_SELECT.replace(scriptKey.getName()).send(player);
break;
}
case 1: {
if (!UUID_PATTERN.matcher(split.get(1)).matches()) {
return;
}
EntityScriptJson entityScriptJson = EntityScriptJson.get(UUID.fromString(split.get(1)));
if (entityScriptJson.exists()) {
List<String> types = StringUtils.split(split.get(0), '=');
SettingType settingType = SettingType.get(types.get(0));
ButtonType buttonType = ButtonType.get(types.get(1));
if (settingType == null || buttonType == null) {
return;
}
switch (buttonType) {
case ENABLED: case DISABLED:
settingType.set(entityScriptJson.load(), buttonType.isEnabled());
SEConfig.SETTING_VALUE.replace(settingType.getType(), buttonType.getType()).send(player);
break;
case VIEW:
String type = ButtonType.get(settingType.is(entityScriptJson.load())).getType();
SEConfig.SETTING_VIEW.replace(settingType.getType(), type).send(player);
break;
}
entityScriptJson.saveJson();
}
break;
}
default:
}
} catch (Exception ignore) { }
}
}

private static int count(@NotNull String str, final char ch) {
int count = 0;
for (int i = 0, l = str.length(); i < l; i++) {
if (str.charAt(i) == ch) {
count++;
}
}
return count;
}
}

0 comments on commit 5fb4e3b

Please sign in to comment.