Skip to content

Commit

Permalink
Fixed stacked blocks not registered correctly (#1068)
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed Apr 28, 2022
1 parent 25ac275 commit d052ba9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
Expand Up @@ -13,15 +13,15 @@ public final class StackedBlocksDatabaseBridge {

public static void saveStackedBlock(StackedBlocksManager stackedBlocks, StackedBlock stackedBlock) {
stackedBlocks.getDatabaseBridge().insertObject("stacked_blocks",
new Pair<>("location", Serializers.LOCATION_SPACED_SERIALIZER.serialize(stackedBlock.getLocation())),
new Pair<>("location", Serializers.STACKED_BLOCK_SERIALIZER.serialize(stackedBlock.getLocation())),
new Pair<>("amount", stackedBlock.getAmount()),
new Pair<>("block_type", stackedBlock.getBlockKey().toString())
);
}

public static void deleteStackedBlock(StackedBlocksManager stackedBlocks, StackedBlock stackedBlock) {
stackedBlocks.getDatabaseBridge().deleteObject("stacked_blocks",
createFilter(new Pair<>("location", Serializers.LOCATION_SPACED_SERIALIZER.serialize(stackedBlock.getLocation()))));
createFilter(new Pair<>("location", Serializers.STACKED_BLOCK_SERIALIZER.serialize(stackedBlock.getLocation()))));
}

public static void deleteStackedBlocks(StackedBlocksManager stackedBlocks) {
Expand Down
Expand Up @@ -5,6 +5,8 @@
import com.bgsoftware.superiorskyblock.serialization.impl.ItemStackSerializer;
import com.bgsoftware.superiorskyblock.serialization.impl.LocationSerializer;
import com.bgsoftware.superiorskyblock.serialization.impl.OffsetSerializer;
import com.bgsoftware.superiorskyblock.serialization.impl.StackedBlockSerializer;
import com.bgsoftware.superiorskyblock.world.blocks.stacked.StackedBlock;
import org.bukkit.Location;
import org.bukkit.inventory.ItemStack;

Expand All @@ -16,6 +18,7 @@ public final class Serializers {
public static final ISerializer<Location, String> LOCATION_SERIALIZER = new LocationSerializer(",");
public static final ISerializer<BlockOffset, String> OFFSET_SPACED_SERIALIZER = new OffsetSerializer(", ");
public static final ISerializer<BlockOffset, String> OFFSET_SERIALIZER = new OffsetSerializer(",");
public static final ISerializer<Location, String> STACKED_BLOCK_SERIALIZER = StackedBlockSerializer.getInstance();

private Serializers() {

Expand Down
@@ -0,0 +1,49 @@
package com.bgsoftware.superiorskyblock.serialization.impl;

import com.bgsoftware.superiorskyblock.serialization.ISerializer;
import com.bgsoftware.superiorskyblock.utils.StringUtils;
import com.bgsoftware.superiorskyblock.utils.locations.SmartLocation;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.jetbrains.annotations.NotNull;

import javax.annotation.Nullable;

public final class StackedBlockSerializer implements ISerializer<Location, String> {

private static final StackedBlockSerializer INSTANCE = new StackedBlockSerializer();

public static StackedBlockSerializer getInstance() {
return INSTANCE;
}

private StackedBlockSerializer() {
}

@NotNull
@Override
public String serialize(@Nullable Location serializable) {
return serializable == null ? "" :
serializable.getWorld().getName() + ", " +
serializable.getBlockX() + ", " +
serializable.getBlockY() + ", " +
serializable.getBlockZ();
}

@Nullable
@Override
public Location deserialize(@Nullable String element) {
if (StringUtils.isBlank(element))
return null;

// Due to issues with commit #feacca9ec, we must parse the values as decimals

String[] positionSections = element.split(", ");

return positionSections.length < 4 ? null : new Location(Bukkit.getWorld(positionSections[0]),
Double.parseDouble(positionSections[1]),
Double.parseDouble(positionSections[2]),
Double.parseDouble(positionSections[3]));
}

}
Expand Up @@ -262,7 +262,7 @@ public void saveStackedBlocks() {
}

private void loadStackedBlock(DatabaseResult resultSet) {
Optional<Location> location = resultSet.getString("location").map(Serializers.LOCATION_SPACED_SERIALIZER::deserialize);
Optional<Location> location = resultSet.getString("location").map(Serializers.STACKED_BLOCK_SERIALIZER::deserialize);
if (!location.isPresent()) {
SuperiorSkyblockPlugin.log("&cCannot load stacked block from null location, skipping...");
return;
Expand Down

0 comments on commit d052ba9

Please sign in to comment.