Skip to content

Commit

Permalink
Avoid using chunk interface
Browse files Browse the repository at this point in the history
Seems like it loads a chunk whenever it's called. Seems unnecessary
  • Loading branch information
Thorinwasher committed Feb 4, 2024
1 parent 7a10e26 commit af2be3f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.sgrewritten.stargate.api.network.portal.PortalPosition;
import org.sgrewritten.stargate.api.network.portal.PositionType;
import org.sgrewritten.stargate.api.network.portal.RealPortal;
import org.sgrewritten.stargate.api.network.portal.StargateChunk;
import org.sgrewritten.stargate.exception.UnimplementedFlagException;
import org.sgrewritten.stargate.exception.name.InvalidNameException;
import org.sgrewritten.stargate.exception.name.NameLengthException;
Expand Down Expand Up @@ -261,5 +262,5 @@ public interface RegistryAPI {
* @param chunk <p>The chunk to retrieve portals from</p>
* @return <p>The portals in the chunk</p>
*/
@NotNull Set<RealPortal> getPortalsInChunk(Chunk chunk);
@NotNull Set<RealPortal> getPortalsInChunk(StargateChunk chunk);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.sgrewritten.stargate.api.network.portal;

import com.bergerkiller.bukkit.common.bases.IntVector2;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.World;

public class StargateChunk {

private final int x;
private final int z;
private final String worldName;

public StargateChunk(Chunk chunk){
this(chunk.getX(),chunk.getZ(), chunk.getWorld());
}

public StargateChunk(int x, int z, World world){
this(x,z,world.getName());
}

public StargateChunk(int x, int z, String worldName){
this.x = x;
this.z = z;
this.worldName = worldName;
}

public StargateChunk(IntVector2 coordinate, World world){
this(coordinate.x, coordinate.z, world);
}

public Chunk getChunk(){
return Bukkit.getWorld(worldName).getChunkAt(x,z);
}

@Override
public boolean equals(Object other){
if(other == this){
return true;
}
if(!(other instanceof StargateChunk otherChunk)){
return false;
}
return otherChunk.x == this.x && otherChunk.z == this.z && otherChunk.worldName.equals(this.worldName);
}

@Override
public int hashCode(){
int result = 18;
result = result * 27 + x;
result = result * 27 + z;
result = result * 27 + worldName.hashCode();
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import com.bergerkiller.bukkit.common.bases.IntVector2;
import com.bergerkiller.bukkit.common.events.MultiBlockChangeEvent;
import org.bukkit.Chunk;
import org.bukkit.World;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.sgrewritten.stargate.api.network.NetworkManager;
import org.sgrewritten.stargate.api.network.RegistryAPI;
import org.sgrewritten.stargate.api.network.portal.RealPortal;
import org.sgrewritten.stargate.api.network.portal.StargateChunk;
import org.sgrewritten.stargate.util.portal.PortalHelper;

import java.util.HashSet;
Expand All @@ -20,18 +20,18 @@ public class BKCommonLibListener implements Listener {
private final RegistryAPI registry;
private final NetworkManager networkManager;

public BKCommonLibListener(RegistryAPI registryAPI, NetworkManager networkManager){
public BKCommonLibListener(RegistryAPI registryAPI, NetworkManager networkManager) {
this.registry = registryAPI;
this.networkManager = networkManager;
}

@EventHandler(priority = EventPriority.MONITOR)
void onMultiBlockChangeEvent(MultiBlockChangeEvent event){
void onMultiBlockChangeEvent(MultiBlockChangeEvent event) {
World world = event.getWorld();
Set<IntVector2> coordinates = event.getChunkCoordinates();
Set<IntVector2> coordinates = event.getChunkCoordinates();
Set<RealPortal> portalsToValidate = new HashSet<>();
for(IntVector2 coordinate : coordinates){
Chunk chunk = coordinate.toChunk(world);
for (IntVector2 coordinate : coordinates) {
StargateChunk chunk = new StargateChunk(coordinate, world);
portalsToValidate.addAll(registry.getPortalsInChunk(chunk));
}
portalsToValidate.forEach(portal -> PortalHelper.portalValidityCheck(portal, networkManager));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.sgrewritten.stargate.api.network.portal.PortalPosition;
import org.sgrewritten.stargate.api.network.portal.PositionType;
import org.sgrewritten.stargate.api.network.portal.RealPortal;
import org.sgrewritten.stargate.api.network.portal.StargateChunk;
import org.sgrewritten.stargate.exception.UnimplementedFlagException;
import org.sgrewritten.stargate.exception.database.StorageReadException;
import org.sgrewritten.stargate.exception.database.StorageWriteException;
Expand Down Expand Up @@ -56,7 +57,7 @@ public class StargateRegistry implements RegistryAPI {
private final Map<GateStructureType, Map<BlockLocation, RealPortal>> portalFromStructureTypeMap = new EnumMap<>(GateStructureType.class);
private final Map<BlockLocation, PortalPosition> portalPositionMap = new HashMap<>();
private final Map<String, Map<BlockLocation, PortalPosition>> portalPositionPluginNameMap = new HashMap<>();
private final Map<Chunk, Set<RealPortal>> chunkPortalMap = new HashMap<>();
private final Map<StargateChunk, Set<RealPortal>> chunkPortalMap = new HashMap<>();

/**
* Instantiates a new Stargate registry
Expand Down Expand Up @@ -90,11 +91,11 @@ public void unregisterPortal(Portal portal) {
Stargate.log(Level.FINEST, "Unregistering portal position on location " + location.toString());
this.removePortalPosition(location);
}
Set<Chunk> chunks = getPortalChunks(realPortal);
Set<StargateChunk> chunks = getPortalChunks(realPortal);
chunks.forEach(chunk -> this.unregisterPortalChunk(chunk, realPortal));
}

private void unregisterPortalChunk(Chunk chunk, RealPortal realPortal) {
private void unregisterPortalChunk(StargateChunk chunk, RealPortal realPortal) {
Set<RealPortal> portals = chunkPortalMap.get(chunk);
if (portals == null) {
return;
Expand All @@ -120,11 +121,11 @@ public void registerPortal(RealPortal portal) {
Location location = gate.getLocation(portalPosition.getRelativePositionLocation());
this.registerPortalPosition(portalPosition, location, portal);
}
Set<Chunk> chunks = getPortalChunks(portal);
Set<StargateChunk> chunks = getPortalChunks(portal);
chunks.forEach(chunk -> registerPortalChunk(chunk, portal));
}

private void registerPortalChunk(Chunk chunk, RealPortal portal) {
private void registerPortalChunk(StargateChunk chunk, RealPortal portal) {
chunkPortalMap.putIfAbsent(chunk, new HashSet<>());
chunkPortalMap.get(chunk).add(portal);
}
Expand Down Expand Up @@ -368,15 +369,15 @@ public void renameNetwork(String newId, String oldId, StorageType storageType) t
}

@Override
public @NotNull Set<RealPortal> getPortalsInChunk(Chunk chunk) {
public @NotNull Set<RealPortal> getPortalsInChunk(StargateChunk chunk) {
Set<RealPortal> output = chunkPortalMap.get(chunk);
if (output == null) {
return new HashSet<>();
}
return output;
}

private @NotNull Set<Chunk> getPortalChunks(RealPortal portal) {
private @NotNull Set<StargateChunk> getPortalChunks(RealPortal portal) {
GateAPI gate = portal.getGate();
BoundingBox boundingBox = gate.getFormat().getBoundingBox();
BlockVector corner1 = new BlockVector(boundingBox.getMaxX(), boundingBox.getMaxY(), boundingBox.getMaxZ());
Expand All @@ -391,10 +392,10 @@ public void renameNetwork(String newId, String oldId, StorageType storageType) t
int xMod = corner1Chunk.getX() < corner2Chunk.getX() ? 1 : -1;
int zMod = corner1Chunk.getZ() < corner2Chunk.getZ() ? 1 : -1;

Set<Chunk> chunks = new HashSet<>();
Set<StargateChunk> chunks = new HashSet<>();
for (int x = corner1Chunk.getX(); !shouldStop(corner2Chunk.getX(), x, xMod); x += xMod) {
for (int z = corner1Chunk.getZ(); !shouldStop(corner2Chunk.getZ(), z, zMod); z += zMod) {
chunks.add(world.getChunkAt(x, z));
chunks.add(new StargateChunk(x, z, world));
}
}
return chunks;
Expand Down

0 comments on commit af2be3f

Please sign in to comment.