improve block state display

This commit is contained in:
Brokkonaut
2023-06-12 05:29:06 +02:00
parent 05269c6978
commit 5fdecb77cd
10 changed files with 81 additions and 45 deletions

View File

@ -83,25 +83,27 @@ public class BlockChange implements LookupCacheElement {
ca = catemp;
}
private String getTypeDetails(BlockData type, byte[] typeState) {
private BaseComponent getTypeDetails(BlockData type, byte[] typeState) {
return getTypeDetails(type, typeState, null, null);
}
private String getTypeDetails(BlockData type, byte[] typeState, BlockData oldType, byte[] oldTypeState) {
String typeDetails = null;
private BaseComponent getTypeDetails(BlockData type, byte[] typeState, BlockData oldType, byte[] oldTypeState) {
BaseComponent typeDetails = null;
if (BlockStateCodecs.hasCodec(type.getMaterial())) {
try {
typeDetails = BlockStateCodecs.toString(type.getMaterial(), Utils.deserializeYamlConfiguration(typeState), type.equals(oldType) ? Utils.deserializeYamlConfiguration(oldTypeState) : null);
typeDetails = BlockStateCodecs.getChangesAsComponent(type.getMaterial(), Utils.deserializeYamlConfiguration(typeState), type.equals(oldType) ? Utils.deserializeYamlConfiguration(oldTypeState) : null);
} catch (Exception e) {
LogBlock.getInstance().getLogger().log(Level.SEVERE, "Could not parse BlockState for " + type.getMaterial(), e);
}
}
if (typeDetails == null) {
return "";
return new TextComponent("");
} else {
return " " + typeDetails;
TextComponent component = new TextComponent(" ");
component.addExtra(typeDetails);
return component;
}
}
@ -129,8 +131,8 @@ public class BlockChange implements LookupCacheElement {
}
// Process type details once for later use.
String typeDetails = getTypeDetails(type, typeState, replaced, replacedState);
String replacedDetails = getTypeDetails(replaced, replacedState);
BaseComponent typeDetails = getTypeDetails(type, typeState, replaced, replacedState);
BaseComponent replacedDetails = getTypeDetails(replaced, replacedState);
if (type.getMaterial().equals(replaced.getMaterial()) || (type.getMaterial() == Material.CAKE && BukkitUtils.isCandleCake(replaced.getMaterial()))) {
if (BukkitUtils.isEmpty(type.getMaterial())) {
@ -201,7 +203,7 @@ public class BlockChange implements LookupCacheElement {
msg.addExtra(createTextComponentWithColor("changed the book on a ", INTERACT.getColor()));
msg.addExtra(prettyMaterial(type));
msg.addExtra(" to");
msg.addExtra(prettyState(typeDetails.length() == 0 ? " empty" : typeDetails));
msg.addExtra(prettyState(typeDetails));
} else if (type instanceof Powerable) {
msg.addExtra(createTextComponentWithColor("stepped on ", INTERACT.getColor()));
msg.addExtra(prettyMaterial(type));

View File

@ -1,5 +1,6 @@
package de.diddiz.LogBlock.blockstate;
import net.md_5.bungee.api.chat.BaseComponent;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
import org.bukkit.configuration.file.YamlConfiguration;
@ -11,5 +12,5 @@ public interface BlockStateCodec {
void deserialize(BlockState state, YamlConfiguration conf);
String toString(YamlConfiguration conf, YamlConfiguration oldState);
BaseComponent getChangesAsComponent(YamlConfiguration conf, YamlConfiguration oldState);
}

View File

@ -1,7 +1,7 @@
package de.diddiz.LogBlock.blockstate;
import java.util.List;
import net.md_5.bungee.api.chat.BaseComponent;
import org.bukkit.DyeColor;
import org.bukkit.Material;
import org.bukkit.block.Banner;
@ -64,7 +64,7 @@ public class BlockStateCodecBanner implements BlockStateCodec {
}
@Override
public String toString(YamlConfiguration conf, YamlConfiguration oldState) {
public BaseComponent getChangesAsComponent(YamlConfiguration conf, YamlConfiguration oldState) {
return null;
}
}

View File

@ -1,5 +1,7 @@
package de.diddiz.LogBlock.blockstate;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
import org.bukkit.block.Lectern;
@ -43,12 +45,10 @@ public class BlockStateCodecLectern implements BlockStateCodec {
}
@Override
public String toString(YamlConfiguration conf, YamlConfiguration oldState) {
public BaseComponent getChangesAsComponent(YamlConfiguration conf, YamlConfiguration oldState) {
if (conf != null) {
StringBuilder sb = new StringBuilder();
sb.append("[").append("book").append("]");
return sb.toString();
return new TextComponent("[book]");
}
return null;
return new TextComponent("empty");
}
}

View File

@ -5,6 +5,8 @@ import static de.diddiz.LogBlock.config.Config.getWorldConfig;
import de.diddiz.LogBlock.Logging;
import de.diddiz.LogBlock.config.WorldConfig;
import de.diddiz.LogBlock.util.BukkitUtils;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
import org.bukkit.block.ShulkerBox;
@ -60,7 +62,7 @@ public class BlockStateCodecShulkerBox implements BlockStateCodec {
}
@Override
public String toString(YamlConfiguration conf, YamlConfiguration oldState) {
public BaseComponent getChangesAsComponent(YamlConfiguration conf, YamlConfiguration oldState) {
if (conf != null) {
StringBuilder sb = new StringBuilder();
sb.append("[");
@ -78,7 +80,7 @@ public class BlockStateCodecShulkerBox implements BlockStateCodec {
}
}
sb.append("]");
return anySlot ? sb.toString() : null;
return anySlot ? new TextComponent(sb.toString()) : null;
}
return null;
}

View File

@ -2,9 +2,13 @@ package de.diddiz.LogBlock.blockstate;
import de.diddiz.LogBlock.util.BukkitUtils;
import de.diddiz.LogBlock.util.Reflections;
import java.awt.Color;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.DyeColor;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
@ -123,20 +127,21 @@ public class BlockStateCodecSign implements BlockStateCodec {
}
@Override
public String toString(YamlConfiguration state, YamlConfiguration oldState) {
public BaseComponent getChangesAsComponent(YamlConfiguration state, YamlConfiguration oldState) {
if (state != null) {
StringBuilder sb = new StringBuilder();
TextComponent tc = new TextComponent();
// StringBuilder sb = new StringBuilder();
boolean isWaxed = state.getBoolean("waxed");
boolean oldWaxed = oldState != null && oldState.getBoolean("waxed");
if (isWaxed != oldWaxed) {
sb.append(isWaxed ? "(waxed)" : "(not waxed)");
tc.addExtra(isWaxed ? "(waxed)" : "(not waxed)");
}
for (Side side : Side.values()) {
ConfigurationSection sideSection = side == Side.FRONT ? state : state.getConfigurationSection(side.name().toLowerCase());
if (!sb.isEmpty()) {
sb.append(" ");
if (tc.getExtra() != null && !tc.getExtra().isEmpty()) {
tc.addExtra(" ");
}
sb.append(side.name()).append(":");
tc.addExtra(side.name() + ":");
List<String> lines = sideSection == null ? Collections.emptyList() : sideSection.getStringList("lines");
List<String> oldLines = Collections.emptyList();
@ -168,30 +173,38 @@ public class BlockStateCodecSign implements BlockStateCodec {
if (!lines.equals(oldLines)) {
for (String line : lines) {
if (sb.length() > 0) {
sb.append(" ");
if (tc.getExtra() != null && !tc.getExtra().isEmpty()) {
tc.addExtra(" ");
}
sb.append("[").append(line).append("]");
tc.addExtra("[");
if (line != null && !line.isEmpty()) {
tc.addExtra(new TextComponent(TextComponent.fromLegacyText(line)));
}
tc.addExtra("]");
}
}
if (signColor != oldSignColor) {
if (sb.length() > 0) {
sb.append(" ");
if (tc.getExtra() != null && !tc.getExtra().isEmpty()) {
tc.addExtra(" ");
}
sb.append("(color: " + signColor.name().toLowerCase() + ")");
tc.addExtra("(color: ");
TextComponent colorText = new TextComponent(signColor.name().toLowerCase());
colorText.setColor(ChatColor.of(new Color(signColor.getColor().asARGB())));
tc.addExtra(colorText);
tc.addExtra(")");
}
if (glowing != oldGlowing) {
if (sb.length() > 0) {
sb.append(" ");
if (tc.getExtra() != null && !tc.getExtra().isEmpty()) {
tc.addExtra(" ");
}
if (glowing) {
sb.append("(glowing)");
tc.addExtra("(glowing)");
} else {
sb.append("(not glowing)");
tc.addExtra("(not glowing)");
}
}
}
return sb.toString();
return tc;
}
return null;
}

View File

@ -1,7 +1,10 @@
package de.diddiz.LogBlock.blockstate;
import java.util.UUID;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.chat.hover.content.Text;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
@ -65,18 +68,22 @@ public class BlockStateCodecSkull implements BlockStateCodec {
}
@Override
public String toString(YamlConfiguration conf, YamlConfiguration oldState) {
public BaseComponent getChangesAsComponent(YamlConfiguration conf, YamlConfiguration oldState) {
if (HAS_PROFILE_API && conf != null) {
PlayerProfile profile = (PlayerProfile) conf.get("profile");
if (profile != null) {
return "[" + (profile.getName() != null ? profile.getName() : (profile.getUniqueId() != null ? profile.getUniqueId().toString() : "~unknown~")) + "]";
TextComponent tc = new TextComponent("[" + (profile.getName() != null ? profile.getName() : (profile.getUniqueId() != null ? profile.getUniqueId().toString() : "~unknown~")) + "]");
if (profile.getName() != null) {
tc.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text("UUID: " + profile.getUniqueId().toString())));
}
return tc;
}
}
String ownerIdString = conf == null ? null : conf.getString("owner");
UUID ownerId = ownerIdString == null ? null : UUID.fromString(ownerIdString);
if (ownerId != null) {
OfflinePlayer owner = Bukkit.getOfflinePlayer(ownerId);
return "[" + (owner.getName() != null ? owner.getName() : owner.getUniqueId().toString()) + "]";
return new TextComponent("[" + (owner.getName() != null ? owner.getName() : owner.getUniqueId().toString()) + "]");
}
return null;
}

View File

@ -1,5 +1,7 @@
package de.diddiz.LogBlock.blockstate;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
import org.bukkit.block.CreatureSpawner;
@ -48,11 +50,11 @@ public class BlockStateCodecSpawner implements BlockStateCodec {
}
@Override
public String toString(YamlConfiguration conf, YamlConfiguration oldState) {
public BaseComponent getChangesAsComponent(YamlConfiguration conf, YamlConfiguration oldState) {
if (conf != null) {
EntityType entity = EntityType.valueOf(conf.getString("spawnedType"));
if (entity != null) {
return "[" + entity + "]";
return new TextComponent("[" + entity.getKey().getKey() + "]");
}
}
return null;

View File

@ -2,7 +2,7 @@ package de.diddiz.LogBlock.blockstate;
import java.util.HashMap;
import java.util.Map;
import net.md_5.bungee.api.chat.BaseComponent;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
import org.bukkit.configuration.file.YamlConfiguration;
@ -51,10 +51,10 @@ public class BlockStateCodecs {
}
}
public static String toString(Material material, YamlConfiguration state, YamlConfiguration oldState) {
public static BaseComponent getChangesAsComponent(Material material, YamlConfiguration state, YamlConfiguration oldState) {
BlockStateCodec codec = codecs.get(material);
if (codec != null) {
return codec.toString(state, oldState);
return codec.getChangesAsComponent(state, oldState);
}
return null;
}

View File

@ -49,6 +49,15 @@ public class MessagingUtil {
return createTextComponentWithColor(stateName, TypeColor.STATE.getColor());
}
public static TextComponent prettyState(BaseComponent stateName) {
TextComponent tc = new TextComponent();
tc.setColor(TypeColor.STATE.getColor());
if (stateName != null) {
tc.addExtra(stateName);
}
return tc;
}
public static TextComponent prettyState(int stateValue) {
return prettyState(Integer.toString(stateValue));
}