Skip to content

Commit

Permalink
Use a list to store multiblocks instead of metadata
Browse files Browse the repository at this point in the history
Metadata does not persist after server restart.
  • Loading branch information
esotericenderman committed Aug 12, 2024
1 parent 459174b commit 2e9eb5a
Showing 1 changed file with 10 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
package net.slqmy.template_paper_plugin.custom_multiblock;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.event.Listener;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue;

import net.slqmy.template_paper_plugin.TemplatePaperPlugin;
import net.slqmy.template_paper_plugin.util.types.BlockLocation;

public abstract class AbstractCustomMultiblock implements Listener {

private final TemplatePaperPlugin plugin;
protected final TemplatePaperPlugin plugin;

private final CustomMultiblock multiblockId;
private final List<List<BlockLocation>> multiblocks = new ArrayList<>();

public AbstractCustomMultiblock(TemplatePaperPlugin plugin, CustomMultiblockManager customMultiblockManager, CustomMultiblock multiblockId) {
this.plugin = plugin;

this.multiblockId = multiblockId;

Bukkit.getPluginManager().registerEvents(this, plugin);

customMultiblockManager.addCustomMultiblock(multiblockId, this);
Expand All @@ -32,20 +30,18 @@ public AbstractCustomMultiblock(TemplatePaperPlugin plugin, CustomMultiblockMana
public List<Block> getCustomMultiblock(Location placeLocation) {
List<Block> multiblock = generateCustomMultiblock(placeLocation);

for (Block block : multiblock) {
block.setMetadata(plugin.getCustomMultiblockIdKey(), new FixedMetadataValue(plugin, multiblockId.name()));
}
multiblocks.add(multiblock.stream().map((block) -> new BlockLocation(block)).toList());

return multiblock;
}

public boolean isBlock(Block block) {
List<MetadataValue> values = block.getMetadata(plugin.getCustomMultiblockIdKey());

if (values.size() == 0) {
return false;
for (List<BlockLocation> multiblock : multiblocks) {
if (multiblock.contains(new BlockLocation(block))) {
return true;
}
}

return multiblockId.name().equals(values.get(0).asString());
return false;
}
}

0 comments on commit 2e9eb5a

Please sign in to comment.