From 59c18e26f5c149ec8f17d670c8b3925cb79f8e45 Mon Sep 17 00:00:00 2001 From: tastybento Date: Fri, 9 Feb 2024 22:21:22 -0800 Subject: [PATCH] Reduce duplications --- .../bentobox/bentobox/nms/PasteHandler.java | 23 +++++++++++++-- .../nms/fallback/PasteHandlerImpl.java | 7 ++++- .../nms/v1_20_R1/PasteHandlerImpl.java | 26 ++--------------- .../nms/v1_20_R1/WorldRegeneratorImpl.java | 7 +---- .../nms/v1_20_R2/PasteHandlerImpl.java | 27 ++---------------- .../nms/v1_20_R2/WorldRegeneratorImpl.java | 7 +---- .../nms/v1_20_R3/PasteHandlerImpl.java | 28 ++----------------- .../nms/v1_20_R3/WorldRegeneratorImpl.java | 7 +---- 8 files changed, 37 insertions(+), 95 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/nms/PasteHandler.java b/src/main/java/world/bentobox/bentobox/nms/PasteHandler.java index 345e1c796..e4e72d494 100644 --- a/src/main/java/world/bentobox/bentobox/nms/PasteHandler.java +++ b/src/main/java/world/bentobox/bentobox/nms/PasteHandler.java @@ -3,18 +3,25 @@ package world.bentobox.bentobox.nms; import java.util.List; import java.util.Map; import java.util.concurrent.CompletableFuture; +import java.util.stream.Collectors; +import org.bukkit.Bukkit; import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.World; +import org.bukkit.block.data.BlockData; 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.util.DefaultPasteUtil; /** * A helper class for {@link world.bentobox.bentobox.blueprints.BlueprintPaster} */ public interface PasteHandler { + + static final BlockData AIR_BLOCKDATA = Bukkit.createBlockData(Material.AIR); /** * Create a future to paste the blocks * @@ -23,7 +30,13 @@ public interface PasteHandler { * @param blockMap the block map * @return the future */ - CompletableFuture pasteBlocks(Island island, World world, Map blockMap); + default CompletableFuture pasteBlocks(Island island, World world, Map 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 setBlock(Island island, Location location, BlueprintBlock bpBlock); /** * Create a future to paste the entities @@ -33,5 +46,11 @@ public interface PasteHandler { * @param entityMap the entities map * @return the future */ - CompletableFuture pasteEntities(Island island, World world, Map> entityMap); + default CompletableFuture pasteEntities(Island island, World world, + Map> 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])))); + } } diff --git a/src/main/java/world/bentobox/bentobox/nms/fallback/PasteHandlerImpl.java b/src/main/java/world/bentobox/bentobox/nms/fallback/PasteHandlerImpl.java index 5d73926e4..81aef5a4f 100644 --- a/src/main/java/world/bentobox/bentobox/nms/fallback/PasteHandlerImpl.java +++ b/src/main/java/world/bentobox/bentobox/nms/fallback/PasteHandlerImpl.java @@ -18,7 +18,7 @@ public class PasteHandlerImpl implements PasteHandler { @Override public CompletableFuture pasteBlocks(Island island, World world, Map blockMap) { return blockMap.entrySet().stream() - .map(entry -> DefaultPasteUtil.setBlock(island, entry.getKey(), entry.getValue())) + .map(entry -> setBlock(island, entry.getKey(), entry.getValue())) .collect( Collectors.collectingAndThen( Collectors.toList(), @@ -38,4 +38,9 @@ public class PasteHandlerImpl implements PasteHandler { ) ); } + + @Override + public CompletableFuture setBlock(Island island, Location location, BlueprintBlock bpBlock) { + return DefaultPasteUtil.setBlock(island, location, bpBlock); + } } diff --git a/src/main/java/world/bentobox/bentobox/nms/v1_20_R1/PasteHandlerImpl.java b/src/main/java/world/bentobox/bentobox/nms/v1_20_R1/PasteHandlerImpl.java index 423574eec..449c3b788 100644 --- a/src/main/java/world/bentobox/bentobox/nms/v1_20_R1/PasteHandlerImpl.java +++ b/src/main/java/world/bentobox/bentobox/nms/v1_20_R1/PasteHandlerImpl.java @@ -1,14 +1,8 @@ package world.bentobox.bentobox.nms.v1_20_R1; -import java.util.List; -import java.util.Map; import java.util.concurrent.CompletableFuture; -import java.util.stream.Collectors; -import org.bukkit.Bukkit; import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.block.data.BlockData; 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.chunk.Chunk; 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.nms.PasteHandler; import world.bentobox.bentobox.util.DefaultPasteUtil; @@ -26,23 +19,8 @@ import world.bentobox.bentobox.util.Util; public class PasteHandlerImpl implements PasteHandler { - protected static final IBlockData AIR = ((CraftBlockData) Bukkit.createBlockData(Material.AIR)).getState(); - @Override - public CompletableFuture pasteBlocks(Island island, World world, Map 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 pasteEntities(Island island, World world, - Map> 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])))); - } + protected static final IBlockData AIR = ((CraftBlockData) AIR_BLOCKDATA).getState(); /** * Set the block to the location @@ -51,7 +29,7 @@ public class PasteHandlerImpl implements PasteHandler { * @param location - location * @param bpBlock - blueprint block */ - public static CompletableFuture setBlock(Island island, Location location, BlueprintBlock bpBlock) { + public CompletableFuture setBlock(Island island, Location location, BlueprintBlock bpBlock) { return Util.getChunkAtAsync(location).thenRun(() -> { Block block = location.getBlock(); // Set the block data - default is AIR diff --git a/src/main/java/world/bentobox/bentobox/nms/v1_20_R1/WorldRegeneratorImpl.java b/src/main/java/world/bentobox/bentobox/nms/v1_20_R1/WorldRegeneratorImpl.java index 1219ddc42..59a9e5d53 100644 --- a/src/main/java/world/bentobox/bentobox/nms/v1_20_R1/WorldRegeneratorImpl.java +++ b/src/main/java/world/bentobox/bentobox/nms/v1_20_R1/WorldRegeneratorImpl.java @@ -1,21 +1,16 @@ 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.craftbukkit.v1_20_R1.CraftWorld; import org.bukkit.craftbukkit.v1_20_R1.block.data.CraftBlockData; import net.minecraft.core.BlockPosition; import net.minecraft.world.level.World; -import net.minecraft.world.level.block.state.IBlockData; import net.minecraft.world.level.chunk.Chunk; import world.bentobox.bentobox.nms.CopyWorldRegenerator; public class WorldRegeneratorImpl extends CopyWorldRegenerator { - private static final IBlockData AIR = ((CraftBlockData) Bukkit.createBlockData(Material.AIR)).getState(); - @Override public void setBlockInNativeChunk(org.bukkit.Chunk chunk, int x, int y, int z, BlockData blockData, boolean applyPhysics) { @@ -24,7 +19,7 @@ public class WorldRegeneratorImpl extends CopyWorldRegenerator { Chunk nmsChunk = nmsWorld.d(chunk.getX(), chunk.getZ()); 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 - nmsChunk.a(bp, AIR, applyPhysics); + nmsChunk.a(bp, PasteHandlerImpl.AIR, applyPhysics); nmsChunk.a(bp, craft.getState(), applyPhysics); } diff --git a/src/main/java/world/bentobox/bentobox/nms/v1_20_R2/PasteHandlerImpl.java b/src/main/java/world/bentobox/bentobox/nms/v1_20_R2/PasteHandlerImpl.java index a3c837f9c..9d7f3c5fc 100644 --- a/src/main/java/world/bentobox/bentobox/nms/v1_20_R2/PasteHandlerImpl.java +++ b/src/main/java/world/bentobox/bentobox/nms/v1_20_R2/PasteHandlerImpl.java @@ -1,14 +1,8 @@ package world.bentobox.bentobox.nms.v1_20_R2; -import java.util.List; -import java.util.Map; import java.util.concurrent.CompletableFuture; -import java.util.stream.Collectors; -import org.bukkit.Bukkit; import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.block.data.BlockData; 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.chunk.Chunk; 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.nms.PasteHandler; import world.bentobox.bentobox.util.DefaultPasteUtil; @@ -26,23 +19,7 @@ import world.bentobox.bentobox.util.Util; public class PasteHandlerImpl implements PasteHandler { - protected static final IBlockData AIR = ((CraftBlockData) Bukkit.createBlockData(Material.AIR)).getState(); - - @Override - public CompletableFuture pasteBlocks(Island island, World world, Map 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 pasteEntities(Island island, World world, - Map> 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])))); - } + protected static final IBlockData AIR = ((CraftBlockData) AIR_BLOCKDATA).getState(); /** * Set the block to the location @@ -51,7 +28,7 @@ public class PasteHandlerImpl implements PasteHandler { * @param location - location * @param bpBlock - blueprint block */ - public static CompletableFuture setBlock(Island island, Location location, BlueprintBlock bpBlock) { + public CompletableFuture setBlock(Island island, Location location, BlueprintBlock bpBlock) { return Util.getChunkAtAsync(location).thenRun(() -> { Block block = location.getBlock(); // Set the block data - default is AIR diff --git a/src/main/java/world/bentobox/bentobox/nms/v1_20_R2/WorldRegeneratorImpl.java b/src/main/java/world/bentobox/bentobox/nms/v1_20_R2/WorldRegeneratorImpl.java index 1646892f0..2263c01c8 100644 --- a/src/main/java/world/bentobox/bentobox/nms/v1_20_R2/WorldRegeneratorImpl.java +++ b/src/main/java/world/bentobox/bentobox/nms/v1_20_R2/WorldRegeneratorImpl.java @@ -1,21 +1,16 @@ 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.craftbukkit.v1_20_R2.CraftWorld; import org.bukkit.craftbukkit.v1_20_R2.block.data.CraftBlockData; import net.minecraft.core.BlockPosition; import net.minecraft.world.level.World; -import net.minecraft.world.level.block.state.IBlockData; import net.minecraft.world.level.chunk.Chunk; import world.bentobox.bentobox.nms.CopyWorldRegenerator; public class WorldRegeneratorImpl extends CopyWorldRegenerator { - private static final IBlockData AIR = ((CraftBlockData) Bukkit.createBlockData(Material.AIR)).getState(); - @Override public void setBlockInNativeChunk(org.bukkit.Chunk chunk, int x, int y, int z, BlockData blockData, boolean applyPhysics) { @@ -24,7 +19,7 @@ public class WorldRegeneratorImpl extends CopyWorldRegenerator { Chunk nmsChunk = nmsWorld.d(chunk.getX(), chunk.getZ()); 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 - nmsChunk.a(bp, AIR, applyPhysics); + nmsChunk.a(bp, PasteHandlerImpl.AIR, applyPhysics); nmsChunk.a(bp, craft.getState(), applyPhysics); } diff --git a/src/main/java/world/bentobox/bentobox/nms/v1_20_R3/PasteHandlerImpl.java b/src/main/java/world/bentobox/bentobox/nms/v1_20_R3/PasteHandlerImpl.java index 6a2f1d8b5..e06b032fa 100644 --- a/src/main/java/world/bentobox/bentobox/nms/v1_20_R3/PasteHandlerImpl.java +++ b/src/main/java/world/bentobox/bentobox/nms/v1_20_R3/PasteHandlerImpl.java @@ -1,14 +1,8 @@ package world.bentobox.bentobox.nms.v1_20_R3; -import java.util.List; -import java.util.Map; import java.util.concurrent.CompletableFuture; -import java.util.stream.Collectors; -import org.bukkit.Bukkit; import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.block.data.BlockData; 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.chunk.Chunk; 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.nms.PasteHandler; import world.bentobox.bentobox.util.DefaultPasteUtil; @@ -26,23 +19,7 @@ import world.bentobox.bentobox.util.Util; public class PasteHandlerImpl implements PasteHandler { - protected static final IBlockData AIR = ((CraftBlockData) Bukkit.createBlockData(Material.AIR)).getState(); - - @Override - public CompletableFuture pasteBlocks(Island island, World world, Map 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 pasteEntities(Island island, World world, - Map> 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])))); - } + protected static final IBlockData AIR = ((CraftBlockData) AIR_BLOCKDATA).getState(); /** * Set the block to the location @@ -51,7 +28,8 @@ public class PasteHandlerImpl implements PasteHandler { * @param location - location * @param bpBlock - blueprint block */ - public static CompletableFuture setBlock(Island island, Location location, BlueprintBlock bpBlock) { + @Override + public CompletableFuture setBlock(Island island, Location location, BlueprintBlock bpBlock) { return Util.getChunkAtAsync(location).thenRun(() -> { Block block = location.getBlock(); // Set the block data - default is AIR diff --git a/src/main/java/world/bentobox/bentobox/nms/v1_20_R3/WorldRegeneratorImpl.java b/src/main/java/world/bentobox/bentobox/nms/v1_20_R3/WorldRegeneratorImpl.java index 677fe90dd..90592f736 100644 --- a/src/main/java/world/bentobox/bentobox/nms/v1_20_R3/WorldRegeneratorImpl.java +++ b/src/main/java/world/bentobox/bentobox/nms/v1_20_R3/WorldRegeneratorImpl.java @@ -1,21 +1,16 @@ 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.craftbukkit.v1_20_R3.CraftWorld; import org.bukkit.craftbukkit.v1_20_R3.block.data.CraftBlockData; import net.minecraft.core.BlockPosition; import net.minecraft.world.level.World; -import net.minecraft.world.level.block.state.IBlockData; import net.minecraft.world.level.chunk.Chunk; import world.bentobox.bentobox.nms.CopyWorldRegenerator; public class WorldRegeneratorImpl extends CopyWorldRegenerator { - private static final IBlockData AIR = ((CraftBlockData) Bukkit.createBlockData(Material.AIR)).getState(); - @Override public void setBlockInNativeChunk(org.bukkit.Chunk chunk, int x, int y, int z, BlockData blockData, boolean applyPhysics) { @@ -24,7 +19,7 @@ public class WorldRegeneratorImpl extends CopyWorldRegenerator { Chunk nmsChunk = nmsWorld.d(chunk.getX(), chunk.getZ()); 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 - nmsChunk.a(bp, AIR, applyPhysics); + nmsChunk.a(bp, PasteHandlerImpl.AIR, applyPhysics); nmsChunk.a(bp, craft.getState(), applyPhysics); }