Change the api from using byte[] to YamlConfiguration

byte[] is only used for database rows
This commit is contained in:
Brokkonaut
2018-08-09 05:00:11 +02:00
parent ce1a1c3bd2
commit 572df51d28
6 changed files with 47 additions and 78 deletions

View File

@ -75,7 +75,7 @@ public class BlockChange implements LookupCacheElement {
String typeDetails = null;
if (BlockStateCodecs.hasCodec(type.getMaterial())) {
try {
typeDetails = BlockStateCodecs.toString(type.getMaterial(), typeState);
typeDetails = BlockStateCodecs.toString(type.getMaterial(), Utils.deserializeYamlConfiguration(typeState));
} catch (Exception 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;
if (BlockStateCodecs.hasCodec(replaced.getMaterial())) {
try {
replacedDetails = BlockStateCodecs.toString(replaced.getMaterial(), replacedState);
replacedDetails = BlockStateCodecs.toString(replaced.getMaterial(), Utils.deserializeYamlConfiguration(replacedState));
} catch (Exception e) {
LogBlock.getInstance().getLogger().log(Level.SEVERE, "Could not parse BlockState for " + replaced.getMaterial(), e);
}

View File

@ -34,6 +34,7 @@ import org.bukkit.Material;
import org.bukkit.block.BlockState;
import org.bukkit.block.Sign;
import org.bukkit.block.data.BlockData;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
@ -325,7 +326,7 @@ public class Consumer extends Thread {
* @param typeState
* 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);
}
@ -357,7 +358,7 @@ public class Consumer extends Thread {
if ((type.getMaterial() != Material.SIGN && type.getMaterial() != Material.WALL_SIGN) || lines == null || lines.length != 4) {
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);
}
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) {
typeBefore = Bukkit.createBlockData(Material.AIR);
}
@ -642,7 +643,7 @@ public class Consumer extends Thread {
}
if (Config.fireCustomEvents) {
// 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);
if (event.isCancelled()) {
return;
@ -653,7 +654,8 @@ public class Consumer extends Thread {
loc = event.getLocation();
typeBefore = event.getTypeBefore();
typeAfter = event.getTypeAfter();
// signtext = event.getSignText();
stateBefore = event.getStateBefore();
stateAfter = event.getStateAfter();
ca = event.getChestAccess();
}
// 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 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) {

View File

@ -22,6 +22,7 @@ import org.bukkit.inventory.ItemStack;
import de.diddiz.LogBlock.blockstate.BlockStateCodecs;
import de.diddiz.util.BukkitUtils;
import de.diddiz.util.Utils;
import java.io.File;
import java.io.PrintWriter;
@ -205,7 +206,7 @@ public class WorldEditor implements Runnable {
if (BlockStateCodecs.hasCodec(replacedBlock.getMaterial())) {
state = block.getState();
try {
BlockStateCodecs.deserialize(state, replacedState);
BlockStateCodecs.deserialize(state, Utils.deserializeYamlConfiguration(replacedState));
state.update();
} catch (Exception e) {
throw new WorldEditorException("Failed to restore blockstate of " + block.getType() + ": " + e, block.getLocation());

View File

@ -2,16 +2,11 @@ package de.diddiz.LogBlock.blockstate;
import java.util.EnumMap;
import java.util.Map;
import java.util.logging.Level;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import de.diddiz.LogBlock.LogBlock;
import de.diddiz.util.Utils;
public class BlockStateCodecs {
private static Map<Material, BlockStateCodec> codecs = new EnumMap<>(Material.class);
@ -36,44 +31,28 @@ public class BlockStateCodecs {
return codecs.containsKey(material);
}
public static byte[] serialize(BlockState state) {
public static YamlConfiguration serialize(BlockState state) {
BlockStateCodec codec = codecs.get(state.getType());
if (codec != null) {
YamlConfiguration serialized = codec.serialize(state);
if (serialized != null && !serialized.getKeys(false).isEmpty()) {
return Utils.serializeYamlConfiguration(serialized);
return serialized;
}
}
return null;
}
public static void deserialize(BlockState block, byte[] state) {
public static void deserialize(BlockState block, YamlConfiguration state) {
BlockStateCodec codec = codecs.get(block.getType());
if (codec != null) {
YamlConfiguration conf = null;
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);
codec.deserialize(block, state);
}
}
public static String toString(Material material, byte[] state) {
public static String toString(Material material, YamlConfiguration state) {
BlockStateCodec codec = codecs.get(material);
if (codec != null) {
YamlConfiguration conf = null;
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 codec.toString(state);
}
return null;
}

View File

@ -2,13 +2,12 @@ package de.diddiz.LogBlock.events;
import de.diddiz.LogBlock.Actor;
import de.diddiz.LogBlock.ChestAccess;
import de.diddiz.util.BukkitUtils;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.data.BlockData;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.HandlerList;
public class BlockChangePreLogEvent extends PreLogEvent {
@ -16,17 +15,19 @@ public class BlockChangePreLogEvent extends PreLogEvent {
private static final HandlerList handlers = new HandlerList();
private Location location;
private BlockData typeBefore, typeAfter;
private String signText;
private ChestAccess chestAccess;
private YamlConfiguration stateBefore;
private YamlConfiguration stateAfter;
public BlockChangePreLogEvent(Actor owner, Location location, BlockData typeBefore, BlockData typeAfter,
String signText, ChestAccess chestAccess) {
YamlConfiguration stateBefore, YamlConfiguration stateAfter, ChestAccess chestAccess) {
super(owner);
this.location = location;
this.typeBefore = typeBefore;
this.typeAfter = typeAfter;
this.signText = signText;
this.stateBefore = stateBefore;
this.stateAfter = stateAfter;
this.chestAccess = chestAccess;
}
@ -64,39 +65,20 @@ public class BlockChangePreLogEvent extends PreLogEvent {
this.typeAfter = typeAfter;
}
public String getSignText() {
return signText;
public YamlConfiguration getStateBefore() {
return stateBefore;
}
public void setSignText(String[] signText) {
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;
}
public YamlConfiguration getStateAfter() {
return stateAfter;
}
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())) {
return true;
}
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 void setStateAfter(YamlConfiguration stateAfter) {
this.stateAfter = stateAfter;
}
public ChestAccess getChestAccess() {

View File

@ -11,7 +11,6 @@ import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;
@ -236,12 +235,8 @@ public class Utils {
if (data == null || data.length == 0) {
return null;
}
try {
return deserializeYamlConfiguration(data).getItemStack("stack");
} catch (InvalidConfigurationException e) {
LogBlock.getInstance().getLogger().log(Level.SEVERE, "Exception while deserializing ItemStack", e);
}
return null;
YamlConfiguration conf = deserializeYamlConfiguration(data);
return conf == null ? null : conf.getItemStack("stack");
}
public static byte[] saveItemStack(ItemStack stack) {
@ -253,14 +248,17 @@ public class Utils {
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();
try {
InputStreamReader reader = new InputStreamReader(new GZIPInputStream(new ByteArrayInputStream(data)), "UTF-8");
conf.load(reader);
reader.close();
return conf;
} catch (ZipException e) {
} catch (ZipException | InvalidConfigurationException e) {
LogBlock.getInstance().getLogger().warning("Could not deserialize YamlConfiguration: " + e.getMessage());
return conf;
} catch (IOException e) {
@ -269,6 +267,9 @@ public class Utils {
}
public static byte[] serializeYamlConfiguration(YamlConfiguration conf) {
if (conf == null || conf.getKeys(false).isEmpty()) {
return null;
}
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
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);
}
}
public static String serializeForSQL(YamlConfiguration conf) {
return mysqlPrepareBytesForInsertAllowNull(serializeYamlConfiguration(conf));
}
}