forked from LogBlock/LogBlock
Change the api from using byte[] to YamlConfiguration
byte[] is only used for database rows
This commit is contained in:
@@ -75,7 +75,7 @@ public class BlockChange implements LookupCacheElement {
|
|||||||
String typeDetails = null;
|
String typeDetails = null;
|
||||||
if (BlockStateCodecs.hasCodec(type.getMaterial())) {
|
if (BlockStateCodecs.hasCodec(type.getMaterial())) {
|
||||||
try {
|
try {
|
||||||
typeDetails = BlockStateCodecs.toString(type.getMaterial(), typeState);
|
typeDetails = BlockStateCodecs.toString(type.getMaterial(), Utils.deserializeYamlConfiguration(typeState));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LogBlock.getInstance().getLogger().log(Level.SEVERE, "Could not parse BlockState for " + type.getMaterial(), e);
|
LogBlock.getInstance().getLogger().log(Level.SEVERE, "Could not parse BlockState for " + type.getMaterial(), e);
|
||||||
}
|
}
|
||||||
@@ -88,7 +88,7 @@ public class BlockChange implements LookupCacheElement {
|
|||||||
String replacedDetails = null;
|
String replacedDetails = null;
|
||||||
if (BlockStateCodecs.hasCodec(replaced.getMaterial())) {
|
if (BlockStateCodecs.hasCodec(replaced.getMaterial())) {
|
||||||
try {
|
try {
|
||||||
replacedDetails = BlockStateCodecs.toString(replaced.getMaterial(), replacedState);
|
replacedDetails = BlockStateCodecs.toString(replaced.getMaterial(), Utils.deserializeYamlConfiguration(replacedState));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LogBlock.getInstance().getLogger().log(Level.SEVERE, "Could not parse BlockState for " + replaced.getMaterial(), e);
|
LogBlock.getInstance().getLogger().log(Level.SEVERE, "Could not parse BlockState for " + replaced.getMaterial(), e);
|
||||||
}
|
}
|
||||||
|
@@ -34,6 +34,7 @@ import org.bukkit.Material;
|
|||||||
import org.bukkit.block.BlockState;
|
import org.bukkit.block.BlockState;
|
||||||
import org.bukkit.block.Sign;
|
import org.bukkit.block.Sign;
|
||||||
import org.bukkit.block.data.BlockData;
|
import org.bukkit.block.data.BlockData;
|
||||||
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
import org.bukkit.entity.Entity;
|
import org.bukkit.entity.Entity;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.entity.Projectile;
|
import org.bukkit.entity.Projectile;
|
||||||
@@ -325,7 +326,7 @@ public class Consumer extends Thread {
|
|||||||
* @param typeState
|
* @param typeState
|
||||||
* Serialized text data of the sign
|
* Serialized text data of the sign
|
||||||
*/
|
*/
|
||||||
public void queueSignBreak(Actor actor, Location loc, BlockData type, byte[] typeState) {
|
public void queueSignBreak(Actor actor, Location loc, BlockData type, YamlConfiguration typeState) {
|
||||||
queueBlock(actor, loc, type, null, typeState, null, null);
|
queueBlock(actor, loc, type, null, typeState, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -357,7 +358,7 @@ public class Consumer extends Thread {
|
|||||||
if ((type.getMaterial() != Material.SIGN && type.getMaterial() != Material.WALL_SIGN) || lines == null || lines.length != 4) {
|
if ((type.getMaterial() != Material.SIGN && type.getMaterial() != Material.WALL_SIGN) || lines == null || lines.length != 4) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
queueBlock(actor, loc, type, type, null, Utils.serializeYamlConfiguration(BlockStateCodecSign.serialize(lines)), null);
|
queueBlock(actor, loc, type, type, null, BlockStateCodecSign.serialize(lines), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -633,7 +634,7 @@ public class Consumer extends Thread {
|
|||||||
return uncommitedPlayerIds.containsKey(actor);
|
return uncommitedPlayerIds.containsKey(actor);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void queueBlock(Actor actor, Location loc, BlockData typeBefore, BlockData typeAfter, byte[] stateBefore, byte[] stateAfter, ChestAccess ca) {
|
private void queueBlock(Actor actor, Location loc, BlockData typeBefore, BlockData typeAfter, YamlConfiguration stateBefore, YamlConfiguration stateAfter, ChestAccess ca) {
|
||||||
if (typeBefore == null || typeBefore.getMaterial() == Material.CAVE_AIR || typeBefore.getMaterial() == Material.VOID_AIR) {
|
if (typeBefore == null || typeBefore.getMaterial() == Material.CAVE_AIR || typeBefore.getMaterial() == Material.VOID_AIR) {
|
||||||
typeBefore = Bukkit.createBlockData(Material.AIR);
|
typeBefore = Bukkit.createBlockData(Material.AIR);
|
||||||
}
|
}
|
||||||
@@ -642,7 +643,7 @@ public class Consumer extends Thread {
|
|||||||
}
|
}
|
||||||
if (Config.fireCustomEvents) {
|
if (Config.fireCustomEvents) {
|
||||||
// Create and call the event
|
// Create and call the event
|
||||||
BlockChangePreLogEvent event = new BlockChangePreLogEvent(actor, loc, typeBefore, typeAfter, null, ca);
|
BlockChangePreLogEvent event = new BlockChangePreLogEvent(actor, loc, typeBefore, typeAfter, stateBefore, stateAfter, ca);
|
||||||
logblock.getServer().getPluginManager().callEvent(event);
|
logblock.getServer().getPluginManager().callEvent(event);
|
||||||
if (event.isCancelled()) {
|
if (event.isCancelled()) {
|
||||||
return;
|
return;
|
||||||
@@ -653,7 +654,8 @@ public class Consumer extends Thread {
|
|||||||
loc = event.getLocation();
|
loc = event.getLocation();
|
||||||
typeBefore = event.getTypeBefore();
|
typeBefore = event.getTypeBefore();
|
||||||
typeAfter = event.getTypeAfter();
|
typeAfter = event.getTypeAfter();
|
||||||
// signtext = event.getSignText();
|
stateBefore = event.getStateBefore();
|
||||||
|
stateAfter = event.getStateAfter();
|
||||||
ca = event.getChestAccess();
|
ca = event.getChestAccess();
|
||||||
}
|
}
|
||||||
// Do this last so LogBlock still has final say in what is being added
|
// Do this last so LogBlock still has final say in what is being added
|
||||||
@@ -668,7 +670,7 @@ public class Consumer extends Thread {
|
|||||||
int typeMaterialId = MaterialConverter.getOrAddMaterialId(typeString);
|
int typeMaterialId = MaterialConverter.getOrAddMaterialId(typeString);
|
||||||
int typeStateId = MaterialConverter.getOrAddBlockStateId(typeString);
|
int typeStateId = MaterialConverter.getOrAddBlockStateId(typeString);
|
||||||
|
|
||||||
addQueueLast(new BlockRow(loc, actor, replacedMaterialId, replacedStateId, stateBefore, typeMaterialId, typeStateId, stateAfter, ca));
|
addQueueLast(new BlockRow(loc, actor, replacedMaterialId, replacedStateId, Utils.serializeYamlConfiguration(stateBefore), typeMaterialId, typeStateId, Utils.serializeYamlConfiguration(stateAfter), ca));
|
||||||
}
|
}
|
||||||
|
|
||||||
private String playerID(Actor actor) {
|
private String playerID(Actor actor) {
|
||||||
|
@@ -22,6 +22,7 @@ import org.bukkit.inventory.ItemStack;
|
|||||||
|
|
||||||
import de.diddiz.LogBlock.blockstate.BlockStateCodecs;
|
import de.diddiz.LogBlock.blockstate.BlockStateCodecs;
|
||||||
import de.diddiz.util.BukkitUtils;
|
import de.diddiz.util.BukkitUtils;
|
||||||
|
import de.diddiz.util.Utils;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
@@ -205,7 +206,7 @@ public class WorldEditor implements Runnable {
|
|||||||
if (BlockStateCodecs.hasCodec(replacedBlock.getMaterial())) {
|
if (BlockStateCodecs.hasCodec(replacedBlock.getMaterial())) {
|
||||||
state = block.getState();
|
state = block.getState();
|
||||||
try {
|
try {
|
||||||
BlockStateCodecs.deserialize(state, replacedState);
|
BlockStateCodecs.deserialize(state, Utils.deserializeYamlConfiguration(replacedState));
|
||||||
state.update();
|
state.update();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new WorldEditorException("Failed to restore blockstate of " + block.getType() + ": " + e, block.getLocation());
|
throw new WorldEditorException("Failed to restore blockstate of " + block.getType() + ": " + e, block.getLocation());
|
||||||
|
@@ -2,16 +2,11 @@ package de.diddiz.LogBlock.blockstate;
|
|||||||
|
|
||||||
import java.util.EnumMap;
|
import java.util.EnumMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.logging.Level;
|
|
||||||
|
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.block.BlockState;
|
import org.bukkit.block.BlockState;
|
||||||
import org.bukkit.configuration.InvalidConfigurationException;
|
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
|
|
||||||
import de.diddiz.LogBlock.LogBlock;
|
|
||||||
import de.diddiz.util.Utils;
|
|
||||||
|
|
||||||
public class BlockStateCodecs {
|
public class BlockStateCodecs {
|
||||||
private static Map<Material, BlockStateCodec> codecs = new EnumMap<>(Material.class);
|
private static Map<Material, BlockStateCodec> codecs = new EnumMap<>(Material.class);
|
||||||
|
|
||||||
@@ -36,44 +31,28 @@ public class BlockStateCodecs {
|
|||||||
return codecs.containsKey(material);
|
return codecs.containsKey(material);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] serialize(BlockState state) {
|
public static YamlConfiguration serialize(BlockState state) {
|
||||||
BlockStateCodec codec = codecs.get(state.getType());
|
BlockStateCodec codec = codecs.get(state.getType());
|
||||||
if (codec != null) {
|
if (codec != null) {
|
||||||
YamlConfiguration serialized = codec.serialize(state);
|
YamlConfiguration serialized = codec.serialize(state);
|
||||||
if (serialized != null && !serialized.getKeys(false).isEmpty()) {
|
if (serialized != null && !serialized.getKeys(false).isEmpty()) {
|
||||||
return Utils.serializeYamlConfiguration(serialized);
|
return serialized;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void deserialize(BlockState block, byte[] state) {
|
public static void deserialize(BlockState block, YamlConfiguration state) {
|
||||||
BlockStateCodec codec = codecs.get(block.getType());
|
BlockStateCodec codec = codecs.get(block.getType());
|
||||||
if (codec != null) {
|
if (codec != null) {
|
||||||
YamlConfiguration conf = null;
|
codec.deserialize(block, state);
|
||||||
try {
|
|
||||||
if (state != null) {
|
|
||||||
conf = Utils.deserializeYamlConfiguration(state);
|
|
||||||
}
|
|
||||||
} catch (InvalidConfigurationException e) {
|
|
||||||
LogBlock.getInstance().getLogger().log(Level.SEVERE, "Exception while deserializing BlockState", e);
|
|
||||||
}
|
|
||||||
codec.deserialize(block, conf);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String toString(Material material, byte[] state) {
|
public static String toString(Material material, YamlConfiguration state) {
|
||||||
BlockStateCodec codec = codecs.get(material);
|
BlockStateCodec codec = codecs.get(material);
|
||||||
if (codec != null) {
|
if (codec != null) {
|
||||||
YamlConfiguration conf = null;
|
return codec.toString(state);
|
||||||
try {
|
|
||||||
if (state != null) {
|
|
||||||
conf = Utils.deserializeYamlConfiguration(state);
|
|
||||||
}
|
|
||||||
} catch (InvalidConfigurationException e) {
|
|
||||||
LogBlock.getInstance().getLogger().log(Level.SEVERE, "Exception while deserializing BlockState", e);
|
|
||||||
}
|
|
||||||
return codec.toString(conf);
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@@ -2,13 +2,12 @@ package de.diddiz.LogBlock.events;
|
|||||||
|
|
||||||
import de.diddiz.LogBlock.Actor;
|
import de.diddiz.LogBlock.Actor;
|
||||||
import de.diddiz.LogBlock.ChestAccess;
|
import de.diddiz.LogBlock.ChestAccess;
|
||||||
import de.diddiz.util.BukkitUtils;
|
|
||||||
|
|
||||||
import org.apache.commons.lang.Validate;
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.block.data.BlockData;
|
import org.bukkit.block.data.BlockData;
|
||||||
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
import org.bukkit.event.HandlerList;
|
import org.bukkit.event.HandlerList;
|
||||||
|
|
||||||
public class BlockChangePreLogEvent extends PreLogEvent {
|
public class BlockChangePreLogEvent extends PreLogEvent {
|
||||||
@@ -16,17 +15,19 @@ public class BlockChangePreLogEvent extends PreLogEvent {
|
|||||||
private static final HandlerList handlers = new HandlerList();
|
private static final HandlerList handlers = new HandlerList();
|
||||||
private Location location;
|
private Location location;
|
||||||
private BlockData typeBefore, typeAfter;
|
private BlockData typeBefore, typeAfter;
|
||||||
private String signText;
|
|
||||||
private ChestAccess chestAccess;
|
private ChestAccess chestAccess;
|
||||||
|
private YamlConfiguration stateBefore;
|
||||||
|
private YamlConfiguration stateAfter;
|
||||||
|
|
||||||
public BlockChangePreLogEvent(Actor owner, Location location, BlockData typeBefore, BlockData typeAfter,
|
public BlockChangePreLogEvent(Actor owner, Location location, BlockData typeBefore, BlockData typeAfter,
|
||||||
String signText, ChestAccess chestAccess) {
|
YamlConfiguration stateBefore, YamlConfiguration stateAfter, ChestAccess chestAccess) {
|
||||||
|
|
||||||
super(owner);
|
super(owner);
|
||||||
this.location = location;
|
this.location = location;
|
||||||
this.typeBefore = typeBefore;
|
this.typeBefore = typeBefore;
|
||||||
this.typeAfter = typeAfter;
|
this.typeAfter = typeAfter;
|
||||||
this.signText = signText;
|
this.stateBefore = stateBefore;
|
||||||
|
this.stateAfter = stateAfter;
|
||||||
this.chestAccess = chestAccess;
|
this.chestAccess = chestAccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,39 +65,20 @@ public class BlockChangePreLogEvent extends PreLogEvent {
|
|||||||
this.typeAfter = typeAfter;
|
this.typeAfter = typeAfter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getSignText() {
|
public YamlConfiguration getStateBefore() {
|
||||||
|
return stateBefore;
|
||||||
return signText;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSignText(String[] signText) {
|
public YamlConfiguration getStateAfter() {
|
||||||
|
return stateAfter;
|
||||||
if (signText != null) {
|
|
||||||
// Check for block
|
|
||||||
Validate.isTrue(isValidSign(), "Must be valid sign block");
|
|
||||||
|
|
||||||
// Check for problems
|
|
||||||
Validate.noNullElements(signText, "No null lines");
|
|
||||||
Validate.isTrue(signText.length == 4, "Sign text must be 4 strings");
|
|
||||||
|
|
||||||
this.signText = signText[0] + "\0" + signText[1] + "\0" + signText[2] + "\0" + signText[3];
|
|
||||||
} else {
|
|
||||||
this.signText = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isValidSign() {
|
public void setStateBefore(YamlConfiguration stateBefore) {
|
||||||
|
this.stateBefore = stateBefore;
|
||||||
|
}
|
||||||
|
|
||||||
if ((typeAfter.getMaterial() == Material.SIGN || typeAfter.getMaterial() == Material.WALL_SIGN) && BukkitUtils.isEmpty(typeBefore.getMaterial())) {
|
public void setStateAfter(YamlConfiguration stateAfter) {
|
||||||
return true;
|
this.stateAfter = stateAfter;
|
||||||
}
|
|
||||||
if ((typeBefore.getMaterial() == Material.SIGN || typeBefore.getMaterial() == Material.WALL_SIGN) && BukkitUtils.isEmpty(typeAfter.getMaterial())) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if ((typeAfter.getMaterial() == Material.SIGN || typeAfter.getMaterial() == Material.WALL_SIGN) && typeBefore.equals(typeAfter)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ChestAccess getChestAccess() {
|
public ChestAccess getChestAccess() {
|
||||||
|
@@ -11,7 +11,6 @@ import java.text.ParseException;
|
|||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.logging.Level;
|
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import java.util.zip.GZIPInputStream;
|
import java.util.zip.GZIPInputStream;
|
||||||
@@ -236,12 +235,8 @@ public class Utils {
|
|||||||
if (data == null || data.length == 0) {
|
if (data == null || data.length == 0) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
try {
|
YamlConfiguration conf = deserializeYamlConfiguration(data);
|
||||||
return deserializeYamlConfiguration(data).getItemStack("stack");
|
return conf == null ? null : conf.getItemStack("stack");
|
||||||
} catch (InvalidConfigurationException e) {
|
|
||||||
LogBlock.getInstance().getLogger().log(Level.SEVERE, "Exception while deserializing ItemStack", e);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] saveItemStack(ItemStack stack) {
|
public static byte[] saveItemStack(ItemStack stack) {
|
||||||
@@ -253,14 +248,17 @@ public class Utils {
|
|||||||
return serializeYamlConfiguration(conf);
|
return serializeYamlConfiguration(conf);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static YamlConfiguration deserializeYamlConfiguration(byte[] data) throws InvalidConfigurationException {
|
public static YamlConfiguration deserializeYamlConfiguration(byte[] data) {
|
||||||
|
if (data == null || data.length == 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
YamlConfiguration conf = new YamlConfiguration();
|
YamlConfiguration conf = new YamlConfiguration();
|
||||||
try {
|
try {
|
||||||
InputStreamReader reader = new InputStreamReader(new GZIPInputStream(new ByteArrayInputStream(data)), "UTF-8");
|
InputStreamReader reader = new InputStreamReader(new GZIPInputStream(new ByteArrayInputStream(data)), "UTF-8");
|
||||||
conf.load(reader);
|
conf.load(reader);
|
||||||
reader.close();
|
reader.close();
|
||||||
return conf;
|
return conf;
|
||||||
} catch (ZipException e) {
|
} catch (ZipException | InvalidConfigurationException e) {
|
||||||
LogBlock.getInstance().getLogger().warning("Could not deserialize YamlConfiguration: " + e.getMessage());
|
LogBlock.getInstance().getLogger().warning("Could not deserialize YamlConfiguration: " + e.getMessage());
|
||||||
return conf;
|
return conf;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@@ -269,6 +267,9 @@ public class Utils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] serializeYamlConfiguration(YamlConfiguration conf) {
|
public static byte[] serializeYamlConfiguration(YamlConfiguration conf) {
|
||||||
|
if (conf == null || conf.getKeys(false).isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
OutputStreamWriter writer = new OutputStreamWriter(new GZIPOutputStream(baos), "UTF-8");
|
OutputStreamWriter writer = new OutputStreamWriter(new GZIPOutputStream(baos), "UTF-8");
|
||||||
@@ -279,4 +280,8 @@ public class Utils {
|
|||||||
throw new RuntimeException("IOException should be impossible for ByteArrayOutputStream", e);
|
throw new RuntimeException("IOException should be impossible for ByteArrayOutputStream", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String serializeForSQL(YamlConfiguration conf) {
|
||||||
|
return mysqlPrepareBytesForInsertAllowNull(serializeYamlConfiguration(conf));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user