Reduce duplications

This commit is contained in:
tastybento 2024-02-09 22:21:22 -08:00
parent b63aef5589
commit 59c18e26f5
8 changed files with 37 additions and 95 deletions

View File

@ -3,18 +3,25 @@ package world.bentobox.bentobox.nms;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.data.BlockData;
import world.bentobox.bentobox.blueprints.dataobjects.BlueprintBlock; import world.bentobox.bentobox.blueprints.dataobjects.BlueprintBlock;
import world.bentobox.bentobox.blueprints.dataobjects.BlueprintEntity; import world.bentobox.bentobox.blueprints.dataobjects.BlueprintEntity;
import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.DefaultPasteUtil;
/** /**
* A helper class for {@link world.bentobox.bentobox.blueprints.BlueprintPaster} * A helper class for {@link world.bentobox.bentobox.blueprints.BlueprintPaster}
*/ */
public interface PasteHandler { public interface PasteHandler {
static final BlockData AIR_BLOCKDATA = Bukkit.createBlockData(Material.AIR);
/** /**
* Create a future to paste the blocks * Create a future to paste the blocks
* *
@ -23,7 +30,13 @@ public interface PasteHandler {
* @param blockMap the block map * @param blockMap the block map
* @return the future * @return the future
*/ */
CompletableFuture<Void> pasteBlocks(Island island, World world, Map<Location, BlueprintBlock> blockMap); default CompletableFuture<Void> pasteBlocks(Island island, World world, Map<Location, BlueprintBlock> blockMap) {
return blockMap.entrySet().stream().map(entry -> setBlock(island, entry.getKey(), entry.getValue()))
.collect(Collectors.collectingAndThen(Collectors.toList(),
list -> CompletableFuture.allOf(list.toArray(new CompletableFuture[0]))));
}
CompletableFuture<Void> setBlock(Island island, Location location, BlueprintBlock bpBlock);
/** /**
* Create a future to paste the entities * Create a future to paste the entities
@ -33,5 +46,11 @@ public interface PasteHandler {
* @param entityMap the entities map * @param entityMap the entities map
* @return the future * @return the future
*/ */
CompletableFuture<Void> pasteEntities(Island island, World world, Map<Location, List<BlueprintEntity>> entityMap); default CompletableFuture<Void> pasteEntities(Island island, World world,
Map<Location, List<BlueprintEntity>> entityMap) {
return entityMap.entrySet().stream()
.map(entry -> DefaultPasteUtil.setEntity(island, entry.getKey(), entry.getValue()))
.collect(Collectors.collectingAndThen(Collectors.toList(),
list -> CompletableFuture.allOf(list.toArray(new CompletableFuture[0]))));
}
} }

View File

@ -18,7 +18,7 @@ public class PasteHandlerImpl implements PasteHandler {
@Override @Override
public CompletableFuture<Void> pasteBlocks(Island island, World world, Map<Location, BlueprintBlock> blockMap) { public CompletableFuture<Void> pasteBlocks(Island island, World world, Map<Location, BlueprintBlock> blockMap) {
return blockMap.entrySet().stream() return blockMap.entrySet().stream()
.map(entry -> DefaultPasteUtil.setBlock(island, entry.getKey(), entry.getValue())) .map(entry -> setBlock(island, entry.getKey(), entry.getValue()))
.collect( .collect(
Collectors.collectingAndThen( Collectors.collectingAndThen(
Collectors.toList(), Collectors.toList(),
@ -38,4 +38,9 @@ public class PasteHandlerImpl implements PasteHandler {
) )
); );
} }
@Override
public CompletableFuture<Void> setBlock(Island island, Location location, BlueprintBlock bpBlock) {
return DefaultPasteUtil.setBlock(island, location, bpBlock);
}
} }

View File

@ -1,14 +1,8 @@
package world.bentobox.bentobox.nms.v1_20_R1; package world.bentobox.bentobox.nms.v1_20_R1;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData; import org.bukkit.block.data.BlockData;
import org.bukkit.craftbukkit.v1_20_R1.CraftWorld; import org.bukkit.craftbukkit.v1_20_R1.CraftWorld;
@ -18,7 +12,6 @@ import net.minecraft.core.BlockPosition;
import net.minecraft.world.level.block.state.IBlockData; import net.minecraft.world.level.block.state.IBlockData;
import net.minecraft.world.level.chunk.Chunk; import net.minecraft.world.level.chunk.Chunk;
import world.bentobox.bentobox.blueprints.dataobjects.BlueprintBlock; import world.bentobox.bentobox.blueprints.dataobjects.BlueprintBlock;
import world.bentobox.bentobox.blueprints.dataobjects.BlueprintEntity;
import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.nms.PasteHandler; import world.bentobox.bentobox.nms.PasteHandler;
import world.bentobox.bentobox.util.DefaultPasteUtil; import world.bentobox.bentobox.util.DefaultPasteUtil;
@ -26,23 +19,8 @@ import world.bentobox.bentobox.util.Util;
public class PasteHandlerImpl implements PasteHandler { public class PasteHandlerImpl implements PasteHandler {
protected static final IBlockData AIR = ((CraftBlockData) Bukkit.createBlockData(Material.AIR)).getState();
@Override protected static final IBlockData AIR = ((CraftBlockData) AIR_BLOCKDATA).getState();
public CompletableFuture<Void> pasteBlocks(Island island, World world, Map<Location, BlueprintBlock> blockMap) {
return blockMap.entrySet().stream().map(entry -> setBlock(island, entry.getKey(), entry.getValue()))
.collect(Collectors.collectingAndThen(Collectors.toList(),
list -> CompletableFuture.allOf(list.toArray(new CompletableFuture[0]))));
}
@Override
public CompletableFuture<Void> pasteEntities(Island island, World world,
Map<Location, List<BlueprintEntity>> entityMap) {
return entityMap.entrySet().stream()
.map(entry -> DefaultPasteUtil.setEntity(island, entry.getKey(), entry.getValue()))
.collect(Collectors.collectingAndThen(Collectors.toList(),
list -> CompletableFuture.allOf(list.toArray(new CompletableFuture[0]))));
}
/** /**
* Set the block to the location * Set the block to the location
@ -51,7 +29,7 @@ public class PasteHandlerImpl implements PasteHandler {
* @param location - location * @param location - location
* @param bpBlock - blueprint block * @param bpBlock - blueprint block
*/ */
public static CompletableFuture<Void> setBlock(Island island, Location location, BlueprintBlock bpBlock) { public CompletableFuture<Void> setBlock(Island island, Location location, BlueprintBlock bpBlock) {
return Util.getChunkAtAsync(location).thenRun(() -> { return Util.getChunkAtAsync(location).thenRun(() -> {
Block block = location.getBlock(); Block block = location.getBlock();
// Set the block data - default is AIR // Set the block data - default is AIR

View File

@ -1,21 +1,16 @@
package world.bentobox.bentobox.nms.v1_20_R1; package world.bentobox.bentobox.nms.v1_20_R1;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.data.BlockData; import org.bukkit.block.data.BlockData;
import org.bukkit.craftbukkit.v1_20_R1.CraftWorld; import org.bukkit.craftbukkit.v1_20_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_20_R1.block.data.CraftBlockData; import org.bukkit.craftbukkit.v1_20_R1.block.data.CraftBlockData;
import net.minecraft.core.BlockPosition; import net.minecraft.core.BlockPosition;
import net.minecraft.world.level.World; import net.minecraft.world.level.World;
import net.minecraft.world.level.block.state.IBlockData;
import net.minecraft.world.level.chunk.Chunk; import net.minecraft.world.level.chunk.Chunk;
import world.bentobox.bentobox.nms.CopyWorldRegenerator; import world.bentobox.bentobox.nms.CopyWorldRegenerator;
public class WorldRegeneratorImpl extends CopyWorldRegenerator { public class WorldRegeneratorImpl extends CopyWorldRegenerator {
private static final IBlockData AIR = ((CraftBlockData) Bukkit.createBlockData(Material.AIR)).getState();
@Override @Override
public void setBlockInNativeChunk(org.bukkit.Chunk chunk, int x, int y, int z, BlockData blockData, public void setBlockInNativeChunk(org.bukkit.Chunk chunk, int x, int y, int z, BlockData blockData,
boolean applyPhysics) { boolean applyPhysics) {
@ -24,7 +19,7 @@ public class WorldRegeneratorImpl extends CopyWorldRegenerator {
Chunk nmsChunk = nmsWorld.d(chunk.getX(), chunk.getZ()); Chunk nmsChunk = nmsWorld.d(chunk.getX(), chunk.getZ());
BlockPosition bp = new BlockPosition((chunk.getX() << 4) + x, y, (chunk.getZ() << 4) + z); BlockPosition bp = new BlockPosition((chunk.getX() << 4) + x, y, (chunk.getZ() << 4) + z);
// Setting the block to air before setting to another state prevents some console errors // Setting the block to air before setting to another state prevents some console errors
nmsChunk.a(bp, AIR, applyPhysics); nmsChunk.a(bp, PasteHandlerImpl.AIR, applyPhysics);
nmsChunk.a(bp, craft.getState(), applyPhysics); nmsChunk.a(bp, craft.getState(), applyPhysics);
} }

View File

@ -1,14 +1,8 @@
package world.bentobox.bentobox.nms.v1_20_R2; package world.bentobox.bentobox.nms.v1_20_R2;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData; import org.bukkit.block.data.BlockData;
import org.bukkit.craftbukkit.v1_20_R2.CraftWorld; import org.bukkit.craftbukkit.v1_20_R2.CraftWorld;
@ -18,7 +12,6 @@ import net.minecraft.core.BlockPosition;
import net.minecraft.world.level.block.state.IBlockData; import net.minecraft.world.level.block.state.IBlockData;
import net.minecraft.world.level.chunk.Chunk; import net.minecraft.world.level.chunk.Chunk;
import world.bentobox.bentobox.blueprints.dataobjects.BlueprintBlock; import world.bentobox.bentobox.blueprints.dataobjects.BlueprintBlock;
import world.bentobox.bentobox.blueprints.dataobjects.BlueprintEntity;
import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.nms.PasteHandler; import world.bentobox.bentobox.nms.PasteHandler;
import world.bentobox.bentobox.util.DefaultPasteUtil; import world.bentobox.bentobox.util.DefaultPasteUtil;
@ -26,23 +19,7 @@ import world.bentobox.bentobox.util.Util;
public class PasteHandlerImpl implements PasteHandler { public class PasteHandlerImpl implements PasteHandler {
protected static final IBlockData AIR = ((CraftBlockData) Bukkit.createBlockData(Material.AIR)).getState(); protected static final IBlockData AIR = ((CraftBlockData) AIR_BLOCKDATA).getState();
@Override
public CompletableFuture<Void> pasteBlocks(Island island, World world, Map<Location, BlueprintBlock> blockMap) {
return blockMap.entrySet().stream().map(entry -> setBlock(island, entry.getKey(), entry.getValue()))
.collect(Collectors.collectingAndThen(Collectors.toList(),
list -> CompletableFuture.allOf(list.toArray(new CompletableFuture[0]))));
}
@Override
public CompletableFuture<Void> pasteEntities(Island island, World world,
Map<Location, List<BlueprintEntity>> entityMap) {
return entityMap.entrySet().stream()
.map(entry -> DefaultPasteUtil.setEntity(island, entry.getKey(), entry.getValue()))
.collect(Collectors.collectingAndThen(Collectors.toList(),
list -> CompletableFuture.allOf(list.toArray(new CompletableFuture[0]))));
}
/** /**
* Set the block to the location * Set the block to the location
@ -51,7 +28,7 @@ public class PasteHandlerImpl implements PasteHandler {
* @param location - location * @param location - location
* @param bpBlock - blueprint block * @param bpBlock - blueprint block
*/ */
public static CompletableFuture<Void> setBlock(Island island, Location location, BlueprintBlock bpBlock) { public CompletableFuture<Void> setBlock(Island island, Location location, BlueprintBlock bpBlock) {
return Util.getChunkAtAsync(location).thenRun(() -> { return Util.getChunkAtAsync(location).thenRun(() -> {
Block block = location.getBlock(); Block block = location.getBlock();
// Set the block data - default is AIR // Set the block data - default is AIR

View File

@ -1,21 +1,16 @@
package world.bentobox.bentobox.nms.v1_20_R2; package world.bentobox.bentobox.nms.v1_20_R2;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.data.BlockData; import org.bukkit.block.data.BlockData;
import org.bukkit.craftbukkit.v1_20_R2.CraftWorld; import org.bukkit.craftbukkit.v1_20_R2.CraftWorld;
import org.bukkit.craftbukkit.v1_20_R2.block.data.CraftBlockData; import org.bukkit.craftbukkit.v1_20_R2.block.data.CraftBlockData;
import net.minecraft.core.BlockPosition; import net.minecraft.core.BlockPosition;
import net.minecraft.world.level.World; import net.minecraft.world.level.World;
import net.minecraft.world.level.block.state.IBlockData;
import net.minecraft.world.level.chunk.Chunk; import net.minecraft.world.level.chunk.Chunk;
import world.bentobox.bentobox.nms.CopyWorldRegenerator; import world.bentobox.bentobox.nms.CopyWorldRegenerator;
public class WorldRegeneratorImpl extends CopyWorldRegenerator { public class WorldRegeneratorImpl extends CopyWorldRegenerator {
private static final IBlockData AIR = ((CraftBlockData) Bukkit.createBlockData(Material.AIR)).getState();
@Override @Override
public void setBlockInNativeChunk(org.bukkit.Chunk chunk, int x, int y, int z, BlockData blockData, public void setBlockInNativeChunk(org.bukkit.Chunk chunk, int x, int y, int z, BlockData blockData,
boolean applyPhysics) { boolean applyPhysics) {
@ -24,7 +19,7 @@ public class WorldRegeneratorImpl extends CopyWorldRegenerator {
Chunk nmsChunk = nmsWorld.d(chunk.getX(), chunk.getZ()); Chunk nmsChunk = nmsWorld.d(chunk.getX(), chunk.getZ());
BlockPosition bp = new BlockPosition((chunk.getX() << 4) + x, y, (chunk.getZ() << 4) + z); BlockPosition bp = new BlockPosition((chunk.getX() << 4) + x, y, (chunk.getZ() << 4) + z);
// Setting the block to air before setting to another state prevents some console errors // Setting the block to air before setting to another state prevents some console errors
nmsChunk.a(bp, AIR, applyPhysics); nmsChunk.a(bp, PasteHandlerImpl.AIR, applyPhysics);
nmsChunk.a(bp, craft.getState(), applyPhysics); nmsChunk.a(bp, craft.getState(), applyPhysics);
} }

View File

@ -1,14 +1,8 @@
package world.bentobox.bentobox.nms.v1_20_R3; package world.bentobox.bentobox.nms.v1_20_R3;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData; import org.bukkit.block.data.BlockData;
import org.bukkit.craftbukkit.v1_20_R3.CraftWorld; import org.bukkit.craftbukkit.v1_20_R3.CraftWorld;
@ -18,7 +12,6 @@ import net.minecraft.core.BlockPosition;
import net.minecraft.world.level.block.state.IBlockData; import net.minecraft.world.level.block.state.IBlockData;
import net.minecraft.world.level.chunk.Chunk; import net.minecraft.world.level.chunk.Chunk;
import world.bentobox.bentobox.blueprints.dataobjects.BlueprintBlock; import world.bentobox.bentobox.blueprints.dataobjects.BlueprintBlock;
import world.bentobox.bentobox.blueprints.dataobjects.BlueprintEntity;
import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.nms.PasteHandler; import world.bentobox.bentobox.nms.PasteHandler;
import world.bentobox.bentobox.util.DefaultPasteUtil; import world.bentobox.bentobox.util.DefaultPasteUtil;
@ -26,23 +19,7 @@ import world.bentobox.bentobox.util.Util;
public class PasteHandlerImpl implements PasteHandler { public class PasteHandlerImpl implements PasteHandler {
protected static final IBlockData AIR = ((CraftBlockData) Bukkit.createBlockData(Material.AIR)).getState(); protected static final IBlockData AIR = ((CraftBlockData) AIR_BLOCKDATA).getState();
@Override
public CompletableFuture<Void> pasteBlocks(Island island, World world, Map<Location, BlueprintBlock> blockMap) {
return blockMap.entrySet().stream().map(entry -> setBlock(island, entry.getKey(), entry.getValue()))
.collect(Collectors.collectingAndThen(Collectors.toList(),
list -> CompletableFuture.allOf(list.toArray(new CompletableFuture[0]))));
}
@Override
public CompletableFuture<Void> pasteEntities(Island island, World world,
Map<Location, List<BlueprintEntity>> entityMap) {
return entityMap.entrySet().stream()
.map(entry -> DefaultPasteUtil.setEntity(island, entry.getKey(), entry.getValue()))
.collect(Collectors.collectingAndThen(Collectors.toList(),
list -> CompletableFuture.allOf(list.toArray(new CompletableFuture[0]))));
}
/** /**
* Set the block to the location * Set the block to the location
@ -51,7 +28,8 @@ public class PasteHandlerImpl implements PasteHandler {
* @param location - location * @param location - location
* @param bpBlock - blueprint block * @param bpBlock - blueprint block
*/ */
public static CompletableFuture<Void> setBlock(Island island, Location location, BlueprintBlock bpBlock) { @Override
public CompletableFuture<Void> setBlock(Island island, Location location, BlueprintBlock bpBlock) {
return Util.getChunkAtAsync(location).thenRun(() -> { return Util.getChunkAtAsync(location).thenRun(() -> {
Block block = location.getBlock(); Block block = location.getBlock();
// Set the block data - default is AIR // Set the block data - default is AIR

View File

@ -1,21 +1,16 @@
package world.bentobox.bentobox.nms.v1_20_R3; package world.bentobox.bentobox.nms.v1_20_R3;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.data.BlockData; import org.bukkit.block.data.BlockData;
import org.bukkit.craftbukkit.v1_20_R3.CraftWorld; import org.bukkit.craftbukkit.v1_20_R3.CraftWorld;
import org.bukkit.craftbukkit.v1_20_R3.block.data.CraftBlockData; import org.bukkit.craftbukkit.v1_20_R3.block.data.CraftBlockData;
import net.minecraft.core.BlockPosition; import net.minecraft.core.BlockPosition;
import net.minecraft.world.level.World; import net.minecraft.world.level.World;
import net.minecraft.world.level.block.state.IBlockData;
import net.minecraft.world.level.chunk.Chunk; import net.minecraft.world.level.chunk.Chunk;
import world.bentobox.bentobox.nms.CopyWorldRegenerator; import world.bentobox.bentobox.nms.CopyWorldRegenerator;
public class WorldRegeneratorImpl extends CopyWorldRegenerator { public class WorldRegeneratorImpl extends CopyWorldRegenerator {
private static final IBlockData AIR = ((CraftBlockData) Bukkit.createBlockData(Material.AIR)).getState();
@Override @Override
public void setBlockInNativeChunk(org.bukkit.Chunk chunk, int x, int y, int z, BlockData blockData, public void setBlockInNativeChunk(org.bukkit.Chunk chunk, int x, int y, int z, BlockData blockData,
boolean applyPhysics) { boolean applyPhysics) {
@ -24,7 +19,7 @@ public class WorldRegeneratorImpl extends CopyWorldRegenerator {
Chunk nmsChunk = nmsWorld.d(chunk.getX(), chunk.getZ()); Chunk nmsChunk = nmsWorld.d(chunk.getX(), chunk.getZ());
BlockPosition bp = new BlockPosition((chunk.getX() << 4) + x, y, (chunk.getZ() << 4) + z); BlockPosition bp = new BlockPosition((chunk.getX() << 4) + x, y, (chunk.getZ() << 4) + z);
// Setting the block to air before setting to another state prevents some console errors // Setting the block to air before setting to another state prevents some console errors
nmsChunk.a(bp, AIR, applyPhysics); nmsChunk.a(bp, PasteHandlerImpl.AIR, applyPhysics);
nmsChunk.a(bp, craft.getState(), applyPhysics); nmsChunk.a(bp, craft.getState(), applyPhysics);
} }