From 0ce4d5a836fd9ade62b6b653467d326d1cca45e2 Mon Sep 17 00:00:00 2001 From: XiaoMoMi <972454774@qq.com> Date: Sun, 17 Mar 2024 18:05:39 +0800 Subject: [PATCH] change name to BlockPos --- .../world/{ChunkPos.java => BlockPos.java} | 14 +++---- .../world/level/CustomCropsSection.java | 10 ++--- .../customcrops/mechanic/world/CChunk.java | 22 +++++------ .../customcrops/mechanic/world/CSection.java | 14 +++---- .../world/adaptor/BukkitWorldAdaptor.java | 39 +++++++++---------- .../world/adaptor/SlimeWorldAdaptor.java | 30 +++++++------- .../customcrops/scheduler/task/TickTask.java | 12 +++--- 7 files changed, 70 insertions(+), 71 deletions(-) rename api/src/main/java/net/momirealms/customcrops/api/mechanic/world/{ChunkPos.java => BlockPos.java} (86%) diff --git a/api/src/main/java/net/momirealms/customcrops/api/mechanic/world/ChunkPos.java b/api/src/main/java/net/momirealms/customcrops/api/mechanic/world/BlockPos.java similarity index 86% rename from api/src/main/java/net/momirealms/customcrops/api/mechanic/world/ChunkPos.java rename to api/src/main/java/net/momirealms/customcrops/api/mechanic/world/BlockPos.java index aca71ca..14f1c4b 100644 --- a/api/src/main/java/net/momirealms/customcrops/api/mechanic/world/ChunkPos.java +++ b/api/src/main/java/net/momirealms/customcrops/api/mechanic/world/BlockPos.java @@ -19,20 +19,20 @@ package net.momirealms.customcrops.api.mechanic.world; import java.util.Objects; -public class ChunkPos { +public class BlockPos { private final int position; - public ChunkPos(int position) { + public BlockPos(int position) { this.position = position; } - public ChunkPos(int x, int y, int z) { + public BlockPos(int x, int y, int z) { this.position = ((x & 0xF) << 28) | ((z & 0xF) << 24) | (y & 0xFFFFFF); } - public static ChunkPos getByLocation(SimpleLocation location) { - return new ChunkPos(location.getX() % 16, location.getY(), location.getZ() % 16); + public static BlockPos getByLocation(SimpleLocation location) { + return new BlockPos(location.getX() % 16, location.getY(), location.getZ() % 16); } public SimpleLocation getLocation(String world, ChunkCoordinate coordinate) { @@ -67,8 +67,8 @@ public class ChunkPos { public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - ChunkPos chunkPos = (ChunkPos) o; - return position == chunkPos.position; + BlockPos blockPos = (BlockPos) o; + return position == blockPos.position; } @Override diff --git a/api/src/main/java/net/momirealms/customcrops/api/mechanic/world/level/CustomCropsSection.java b/api/src/main/java/net/momirealms/customcrops/api/mechanic/world/level/CustomCropsSection.java index c0b2f3f..848440a 100644 --- a/api/src/main/java/net/momirealms/customcrops/api/mechanic/world/level/CustomCropsSection.java +++ b/api/src/main/java/net/momirealms/customcrops/api/mechanic/world/level/CustomCropsSection.java @@ -17,7 +17,7 @@ package net.momirealms.customcrops.api.mechanic.world.level; -import net.momirealms.customcrops.api.mechanic.world.ChunkPos; +import net.momirealms.customcrops.api.mechanic.world.BlockPos; import net.momirealms.customcrops.api.mechanic.world.CustomCropsBlock; import java.util.Map; @@ -26,15 +26,15 @@ public interface CustomCropsSection { int getSectionID(); - CustomCropsBlock getBlockAt(ChunkPos pos); + CustomCropsBlock getBlockAt(BlockPos pos); - CustomCropsBlock removeBlockAt(ChunkPos pos); + CustomCropsBlock removeBlockAt(BlockPos pos); - CustomCropsBlock addBlockAt(ChunkPos pos, CustomCropsBlock block); + CustomCropsBlock addBlockAt(BlockPos pos, CustomCropsBlock block); boolean canPrune(); CustomCropsBlock[] getBlocks(); - Map getBlockMap(); + Map getBlockMap(); } diff --git a/plugin/src/main/java/net/momirealms/customcrops/mechanic/world/CChunk.java b/plugin/src/main/java/net/momirealms/customcrops/mechanic/world/CChunk.java index 8113939..9257d28 100644 --- a/plugin/src/main/java/net/momirealms/customcrops/mechanic/world/CChunk.java +++ b/plugin/src/main/java/net/momirealms/customcrops/mechanic/world/CChunk.java @@ -26,7 +26,7 @@ import net.momirealms.customcrops.api.mechanic.item.Sprinkler; import net.momirealms.customcrops.api.mechanic.misc.CRotation; import net.momirealms.customcrops.api.mechanic.requirement.State; import net.momirealms.customcrops.api.mechanic.world.ChunkCoordinate; -import net.momirealms.customcrops.api.mechanic.world.ChunkPos; +import net.momirealms.customcrops.api.mechanic.world.BlockPos; import net.momirealms.customcrops.api.mechanic.world.CustomCropsBlock; import net.momirealms.customcrops.api.mechanic.world.SimpleLocation; import net.momirealms.customcrops.api.mechanic.world.level.*; @@ -47,7 +47,7 @@ public class CChunk implements CustomCropsChunk { private final ChunkCoordinate chunkCoordinate; private final ConcurrentHashMap loadedSections; private final PriorityQueue queue; - private final Set tickedBlocks; + private final Set tickedBlocks; private long lastLoadedTime; private int loadedSeconds; private int unloadedSeconds; @@ -69,7 +69,7 @@ public class CChunk implements CustomCropsChunk { long lastLoadedTime, ConcurrentHashMap loadedSections, PriorityQueue queue, - HashSet tickedBlocks + HashSet tickedBlocks ) { this.cWorld = cWorld; this.chunkCoordinate = chunkCoordinate; @@ -124,7 +124,7 @@ public class CChunk implements CustomCropsChunk { while (!queue.isEmpty() && queue.peek().getTime() <= loadedSeconds) { TickTask task = queue.poll(); if (task != null) { - ChunkPos pos = task.getChunkPos(); + BlockPos pos = task.getChunkPos(); CSection section = loadedSections.get(pos.getSectionID()); if (section != null) { CustomCropsBlock block = section.getBlockAt(pos); @@ -161,7 +161,7 @@ public class CChunk implements CustomCropsChunk { int x = random.nextInt(16); int y = random.nextInt(16) + baseY; int z = random.nextInt(16); - CustomCropsBlock block = section.getBlockAt(new ChunkPos(x,y,z)); + CustomCropsBlock block = section.getBlockAt(new BlockPos(x,y,z)); if (block != null) { switch (block.getType()) { case CROP -> { @@ -199,7 +199,7 @@ public class CChunk implements CustomCropsChunk { public void arrangeTasks(int unit) { ThreadLocalRandom random = ThreadLocalRandom.current(); for (CustomCropsSection section : getSections()) { - for (Map.Entry entry : section.getBlockMap().entrySet()) { + for (Map.Entry entry : section.getBlockMap().entrySet()) { this.queue.add(new TickTask( random.nextInt(0, unit), entry.getKey() @@ -209,7 +209,7 @@ public class CChunk implements CustomCropsChunk { } } - public void tryCreatingTaskForNewBlock(ChunkPos pos) { + public void tryCreatingTaskForNewBlock(BlockPos pos) { WorldSetting setting = cWorld.getWorldSetting(); if (setting.isScheduledTick() && !tickedBlocks.contains(pos)) { tickedBlocks.add(pos); @@ -433,7 +433,7 @@ public class CChunk implements CustomCropsChunk { @Override public CustomCropsBlock removeBlockAt(SimpleLocation location) { - ChunkPos pos = ChunkPos.getByLocation(location); + BlockPos pos = BlockPos.getByLocation(location); CSection section = loadedSections.get(pos.getSectionID()); if (section == null) return null; return section.removeBlockAt(pos); @@ -441,7 +441,7 @@ public class CChunk implements CustomCropsChunk { @Override public CustomCropsBlock addBlockAt(CustomCropsBlock block, SimpleLocation location) { - ChunkPos pos = ChunkPos.getByLocation(location); + BlockPos pos = BlockPos.getByLocation(location); CSection section = loadedSections.get(pos.getSectionID()); if (section == null) { section = new CSection(pos.getSectionID()); @@ -453,7 +453,7 @@ public class CChunk implements CustomCropsChunk { @Override public Optional getBlockAt(SimpleLocation location) { - ChunkPos pos = ChunkPos.getByLocation(location); + BlockPos pos = BlockPos.getByLocation(location); CSection section = loadedSections.get(pos.getSectionID()); if (section == null) { return Optional.empty(); @@ -539,7 +539,7 @@ public class CChunk implements CustomCropsChunk { return queue; } - public Set getTickedBlocks() { + public Set getTickedBlocks() { return tickedBlocks; } } diff --git a/plugin/src/main/java/net/momirealms/customcrops/mechanic/world/CSection.java b/plugin/src/main/java/net/momirealms/customcrops/mechanic/world/CSection.java index 804d1c7..454ed95 100644 --- a/plugin/src/main/java/net/momirealms/customcrops/mechanic/world/CSection.java +++ b/plugin/src/main/java/net/momirealms/customcrops/mechanic/world/CSection.java @@ -17,7 +17,7 @@ package net.momirealms.customcrops.mechanic.world; -import net.momirealms.customcrops.api.mechanic.world.ChunkPos; +import net.momirealms.customcrops.api.mechanic.world.BlockPos; import net.momirealms.customcrops.api.mechanic.world.CustomCropsBlock; import net.momirealms.customcrops.api.mechanic.world.level.CustomCropsSection; @@ -27,14 +27,14 @@ import java.util.concurrent.ConcurrentHashMap; public class CSection implements CustomCropsSection { private final int sectionID; - private final ConcurrentHashMap blocks; + private final ConcurrentHashMap blocks; public CSection(int sectionID) { this.sectionID = sectionID; this.blocks = new ConcurrentHashMap<>(); } - public CSection(int sectionID, ConcurrentHashMap blocks) { + public CSection(int sectionID, ConcurrentHashMap blocks) { this.blocks = blocks; this.sectionID = sectionID; } @@ -45,17 +45,17 @@ public class CSection implements CustomCropsSection { } @Override - public CustomCropsBlock getBlockAt(ChunkPos pos) { + public CustomCropsBlock getBlockAt(BlockPos pos) { return blocks.get(pos); } @Override - public CustomCropsBlock removeBlockAt(ChunkPos pos) { + public CustomCropsBlock removeBlockAt(BlockPos pos) { return blocks.remove(pos); } @Override - public CustomCropsBlock addBlockAt(ChunkPos pos, CustomCropsBlock block) { + public CustomCropsBlock addBlockAt(BlockPos pos, CustomCropsBlock block) { return blocks.put(pos, block); } @@ -70,7 +70,7 @@ public class CSection implements CustomCropsSection { } @Override - public Map getBlockMap() { + public Map getBlockMap() { return blocks; } } diff --git a/plugin/src/main/java/net/momirealms/customcrops/mechanic/world/adaptor/BukkitWorldAdaptor.java b/plugin/src/main/java/net/momirealms/customcrops/mechanic/world/adaptor/BukkitWorldAdaptor.java index b5e1ee8..3f2200a 100644 --- a/plugin/src/main/java/net/momirealms/customcrops/mechanic/world/adaptor/BukkitWorldAdaptor.java +++ b/plugin/src/main/java/net/momirealms/customcrops/mechanic/world/adaptor/BukkitWorldAdaptor.java @@ -28,7 +28,6 @@ import com.google.gson.Gson; import net.momirealms.customcrops.api.CustomCropsPlugin; import net.momirealms.customcrops.api.manager.ConfigManager; import net.momirealms.customcrops.api.manager.WorldManager; -import net.momirealms.customcrops.api.mechanic.item.ItemType; import net.momirealms.customcrops.api.mechanic.world.*; import net.momirealms.customcrops.api.mechanic.world.level.CustomCropsChunk; import net.momirealms.customcrops.api.mechanic.world.level.CustomCropsWorld; @@ -241,21 +240,21 @@ public class BukkitWorldAdaptor extends AbstractWorldAdaptor { PriorityQueue queue = new PriorityQueue<>(Math.max(11, tasksSize)); for (int i = 0; i < tasksSize; i++) { int time = chunkData.readInt(); - ChunkPos pos = new ChunkPos(chunkData.readInt()); + BlockPos pos = new BlockPos(chunkData.readInt()); queue.add(new TickTask(time, pos)); } // read ticked blocks int tickedSize = chunkData.readInt(); - HashSet tickedSet = new HashSet<>(Math.max(11, tickedSize)); + HashSet tickedSet = new HashSet<>(Math.max(11, tickedSize)); for (int i = 0; i < tickedSize; i++) { - tickedSet.add(new ChunkPos(chunkData.readInt())); + tickedSet.add(new BlockPos(chunkData.readInt())); } // read block data ConcurrentHashMap sectionMap = new ConcurrentHashMap<>(); int sections = chunkData.readInt(); // read sections for (int i = 0; i < sections; i++) { - ConcurrentHashMap blockMap = new ConcurrentHashMap<>(); + ConcurrentHashMap blockMap = new ConcurrentHashMap<>(); int sectionID = chunkData.readInt(); byte[] sectionBytes = new byte[chunkData.readInt()]; chunkData.read(sectionBytes); @@ -271,32 +270,32 @@ public class BukkitWorldAdaptor extends AbstractWorldAdaptor { switch (type) { case "CROP" -> { for (int pos : (int[]) block.get("pos").getValue()) { - ChunkPos chunkPos = new ChunkPos(pos); - blockMap.put(chunkPos, new MemoryCrop(chunkPos.getLocation(world, coordinate), new CompoundMap(data))); + BlockPos blockPos = new BlockPos(pos); + blockMap.put(blockPos, new MemoryCrop(blockPos.getLocation(world, coordinate), new CompoundMap(data))); } } case "POT" -> { for (int pos : (int[]) block.get("pos").getValue()) { - ChunkPos chunkPos = new ChunkPos(pos); - blockMap.put(chunkPos, new MemoryPot(chunkPos.getLocation(world, coordinate), new CompoundMap(data))); + BlockPos blockPos = new BlockPos(pos); + blockMap.put(blockPos, new MemoryPot(blockPos.getLocation(world, coordinate), new CompoundMap(data))); } } case "SPRINKLER" -> { for (int pos : (int[]) block.get("pos").getValue()) { - ChunkPos chunkPos = new ChunkPos(pos); - blockMap.put(chunkPos, new MemorySprinkler(chunkPos.getLocation(world, coordinate), new CompoundMap(data))); + BlockPos blockPos = new BlockPos(pos); + blockMap.put(blockPos, new MemorySprinkler(blockPos.getLocation(world, coordinate), new CompoundMap(data))); } } case "SCARECROW" -> { for (int pos : (int[]) block.get("pos").getValue()) { - ChunkPos chunkPos = new ChunkPos(pos); - blockMap.put(chunkPos, new MemoryScarecrow(chunkPos.getLocation(world, coordinate), new CompoundMap(data))); + BlockPos blockPos = new BlockPos(pos); + blockMap.put(blockPos, new MemoryScarecrow(blockPos.getLocation(world, coordinate), new CompoundMap(data))); } } case "GREENHOUSE" -> { for (int pos : (int[]) block.get("pos").getValue()) { - ChunkPos chunkPos = new ChunkPos(pos); - blockMap.put(chunkPos, new MemoryGlass(chunkPos.getLocation(world, coordinate), new CompoundMap(data))); + BlockPos blockPos = new BlockPos(pos); + blockMap.put(blockPos, new MemoryGlass(blockPos.getLocation(world, coordinate), new CompoundMap(data))); } } } @@ -375,10 +374,10 @@ public class BukkitWorldAdaptor extends AbstractWorldAdaptor { ); } - private int[] tickedBlocksToArray(Set set) { + private int[] tickedBlocksToArray(Set set) { int[] ticked = new int[set.size()]; int i = 0; - for (ChunkPos pos : set) { + for (BlockPos pos : set) { ticked[i] = pos.getPosition(); i++; } @@ -401,11 +400,11 @@ public class BukkitWorldAdaptor extends AbstractWorldAdaptor { return new SerializableSection(section.getSectionID(), toCompoundTags(section.getBlockMap())); } - private List toCompoundTags(Map blocks) { + private List toCompoundTags(Map blocks) { ArrayList tags = new ArrayList<>(blocks.size()); Map> blockToPosMap = new HashMap<>(); - for (Map.Entry entry : blocks.entrySet()) { - ChunkPos coordinate = entry.getKey(); + for (Map.Entry entry : blocks.entrySet()) { + BlockPos coordinate = entry.getKey(); CustomCropsBlock block = entry.getValue(); List coordinates = blockToPosMap.computeIfAbsent(block, k -> new ArrayList<>()); coordinates.add(coordinate.getPosition()); diff --git a/plugin/src/main/java/net/momirealms/customcrops/mechanic/world/adaptor/SlimeWorldAdaptor.java b/plugin/src/main/java/net/momirealms/customcrops/mechanic/world/adaptor/SlimeWorldAdaptor.java index ad0e49f..f1476bd 100644 --- a/plugin/src/main/java/net/momirealms/customcrops/mechanic/world/adaptor/SlimeWorldAdaptor.java +++ b/plugin/src/main/java/net/momirealms/customcrops/mechanic/world/adaptor/SlimeWorldAdaptor.java @@ -24,7 +24,7 @@ import com.infernalsuite.aswm.api.world.SlimeWorld; import net.momirealms.customcrops.api.CustomCropsPlugin; import net.momirealms.customcrops.api.manager.WorldManager; import net.momirealms.customcrops.api.mechanic.world.ChunkCoordinate; -import net.momirealms.customcrops.api.mechanic.world.ChunkPos; +import net.momirealms.customcrops.api.mechanic.world.BlockPos; import net.momirealms.customcrops.api.mechanic.world.CustomCropsBlock; import net.momirealms.customcrops.api.mechanic.world.level.CustomCropsChunk; import net.momirealms.customcrops.api.mechanic.world.level.CustomCropsWorld; @@ -194,13 +194,13 @@ public class SlimeWorldAdaptor extends BukkitWorldAdaptor { PriorityQueue queue = new PriorityQueue<>(Math.max(11, queued.length / 2)); for (int i = 0, size = queued.length / 2; i < size; i++) { - ChunkPos pos = new ChunkPos(queued[2*i+1]); + BlockPos pos = new BlockPos(queued[2*i+1]); queue.add(new TickTask(queued[2*i], pos)); } - HashSet tickedSet = new HashSet<>(Math.max(11, ticked.length)); + HashSet tickedSet = new HashSet<>(Math.max(11, ticked.length)); for (int tick : ticked) { - tickedSet.add(new ChunkPos(tick)); + tickedSet.add(new BlockPos(tick)); } ConcurrentHashMap sectionMap = new ConcurrentHashMap<>(); @@ -208,7 +208,7 @@ public class SlimeWorldAdaptor extends BukkitWorldAdaptor { for (Map.Entry> entry : sectionCompoundMap.entrySet()) { if (entry.getValue() instanceof ListTag listTag) { int id = Integer.parseInt(entry.getKey()); - ConcurrentHashMap blockMap = new ConcurrentHashMap<>(); + ConcurrentHashMap blockMap = new ConcurrentHashMap<>(); ListTag blocks = (ListTag) listTag; for (CompoundTag blockTag : blocks.getValue()) { CompoundMap block = blockTag.getValue(); @@ -217,32 +217,32 @@ public class SlimeWorldAdaptor extends BukkitWorldAdaptor { switch (type) { case "CROP" -> { for (int pos : (int[]) block.get("pos").getValue()) { - ChunkPos chunkPos = new ChunkPos(pos); - blockMap.put(chunkPos, new MemoryCrop(chunkPos.getLocation(world, coordinate), new CompoundMap(data))); + BlockPos blockPos = new BlockPos(pos); + blockMap.put(blockPos, new MemoryCrop(blockPos.getLocation(world, coordinate), new CompoundMap(data))); } } case "POT" -> { for (int pos : (int[]) block.get("pos").getValue()) { - ChunkPos chunkPos = new ChunkPos(pos); - blockMap.put(chunkPos, new MemoryPot(chunkPos.getLocation(world, coordinate), new CompoundMap(data))); + BlockPos blockPos = new BlockPos(pos); + blockMap.put(blockPos, new MemoryPot(blockPos.getLocation(world, coordinate), new CompoundMap(data))); } } case "SPRINKLER" -> { for (int pos : (int[]) block.get("pos").getValue()) { - ChunkPos chunkPos = new ChunkPos(pos); - blockMap.put(chunkPos, new MemorySprinkler(chunkPos.getLocation(world, coordinate), new CompoundMap(data))); + BlockPos blockPos = new BlockPos(pos); + blockMap.put(blockPos, new MemorySprinkler(blockPos.getLocation(world, coordinate), new CompoundMap(data))); } } case "SCARECROW" -> { for (int pos : (int[]) block.get("pos").getValue()) { - ChunkPos chunkPos = new ChunkPos(pos); - blockMap.put(chunkPos, new MemoryScarecrow(chunkPos.getLocation(world, coordinate), new CompoundMap(data))); + BlockPos blockPos = new BlockPos(pos); + blockMap.put(blockPos, new MemoryScarecrow(blockPos.getLocation(world, coordinate), new CompoundMap(data))); } } case "GLASS" -> { for (int pos : (int[]) block.get("pos").getValue()) { - ChunkPos chunkPos = new ChunkPos(pos); - blockMap.put(chunkPos, new MemoryGlass(chunkPos.getLocation(world, coordinate), new CompoundMap(data))); + BlockPos blockPos = new BlockPos(pos); + blockMap.put(blockPos, new MemoryGlass(blockPos.getLocation(world, coordinate), new CompoundMap(data))); } } } diff --git a/plugin/src/main/java/net/momirealms/customcrops/scheduler/task/TickTask.java b/plugin/src/main/java/net/momirealms/customcrops/scheduler/task/TickTask.java index f319d36..273816e 100644 --- a/plugin/src/main/java/net/momirealms/customcrops/scheduler/task/TickTask.java +++ b/plugin/src/main/java/net/momirealms/customcrops/scheduler/task/TickTask.java @@ -17,24 +17,24 @@ package net.momirealms.customcrops.scheduler.task; -import net.momirealms.customcrops.api.mechanic.world.ChunkPos; +import net.momirealms.customcrops.api.mechanic.world.BlockPos; import org.jetbrains.annotations.NotNull; public class TickTask implements Comparable { private static int taskID; private final int time; - private final ChunkPos chunkPos; + private final BlockPos blockPos; private final int id; - public TickTask(int time, ChunkPos chunkPos) { + public TickTask(int time, BlockPos blockPos) { this.time = time; - this.chunkPos = chunkPos; + this.blockPos = blockPos; this.id = taskID++; } - public ChunkPos getChunkPos() { - return chunkPos; + public BlockPos getChunkPos() { + return blockPos; } public int getTime() {