From cb0dec8b832e64cb0ad452553db812d4eee74b9d Mon Sep 17 00:00:00 2001 From: Tobias Ottenweller Date: Wed, 12 Jun 2013 21:58:01 +0200 Subject: [PATCH] More distant portal blocks now get send to players. --- src/de/craftinc/gates/GatesManager.java | 71 +++++++++++++++---- .../gates/util/GateBlockChangeSender.java | 3 +- src/de/craftinc/gates/util/SimpleChunk.java | 9 +++ 3 files changed, 66 insertions(+), 17 deletions(-) diff --git a/src/de/craftinc/gates/GatesManager.java b/src/de/craftinc/gates/GatesManager.java index 9d43ae4..9683cb1 100644 --- a/src/de/craftinc/gates/GatesManager.java +++ b/src/de/craftinc/gates/GatesManager.java @@ -40,6 +40,8 @@ public class GatesManager private static final String gatesPath = "gates"; // path to gates inside the yaml file private static final String storageVersionPath = "version"; private static final int storageVersion = 1; + + private static final int chunkRadius = 4; // TODO: move search radius into a config file / get value from config class private Map gatesById; private Map> gatesByChunk; @@ -119,9 +121,9 @@ public class GatesManager // migration int fileStorageVersion = gatesConfig.getInt(storageVersionPath); - if (fileStorageVersion > storageVersion) { - throw new RuntimeException("Unsupported storage version detected! Make sure you have the latest version of Craft Inc. Gates installed."); - } + if (fileStorageVersion > storageVersion) { + throw new RuntimeException("Unsupported storage version detected! Make sure you have the latest version of Craft Inc. Gates installed."); + } if (fileStorageVersion < storageVersion) { Plugin.log("Outdated storage version detected. Performing data migration..."); @@ -142,12 +144,24 @@ public class GatesManager private void fillGatesByChunk() { - HashSet chunksUsedByGates = new HashSet(); + HashSet chunksUsedByGates = new HashSet(); for (Gate g : gates) { if (g.getLocation() != null) { - chunksUsedByGates.add(g.getLocation().getChunk()); + + Chunk c = g.getLocation().getChunk(); + + int x = c.getX(); + int z = c.getZ(); + + for (int i = x-chunkRadius; i < x+chunkRadius; i++) { + + for (int j = z-chunkRadius; j < z+chunkRadius; j++) { + + chunksUsedByGates.add(new SimpleChunk(i, j, c.getWorld())); + } + } } } @@ -208,9 +222,24 @@ public class GatesManager private void removeGateFromChunk(Gate g, Location l) { if (l != null) { - SimpleChunk sc = new SimpleChunk(l.getChunk()); - Set gatesInChunk = gatesByChunk.get(sc); - gatesInChunk.remove(g); + + Chunk c = l.getChunk(); + int x = c.getX(); + int z = c.getZ(); + + for (int i = x-chunkRadius; i < x+chunkRadius; i++) { + + for (int j = z-chunkRadius; j < z+chunkRadius; j++) { + + SimpleChunk sc = new SimpleChunk(i, j, c.getWorld()); + Set gatesInChunk = gatesByChunk.get(sc); + + if (gatesInChunk != null) { + gatesInChunk.remove(g); + } + + } + } } } @@ -220,15 +249,27 @@ public class GatesManager Location gateLocation = g.getLocation(); if (gateLocation != null) { - SimpleChunk c = new SimpleChunk(gateLocation.getChunk()); - Set gatesForC = gatesByChunk.get(c); - if (gatesForC == null) { - gatesForC = new HashSet(); // NOTE: not optimizing size here - gatesByChunk.put(c, gatesForC); + Chunk c = g.getLocation().getChunk(); + int x = c.getX(); + int z = c.getZ(); + + for (int i = x-chunkRadius; i < x+chunkRadius; i++) { + + for (int j = z-chunkRadius; j < z+chunkRadius; j++) { + + SimpleChunk sc = new SimpleChunk(i, j, c.getWorld()); + + Set gatesForC = gatesByChunk.get(sc); + + if (gatesForC == null) { + gatesForC = new HashSet(); // NOTE: not optimizing size here + gatesByChunk.put(sc, gatesForC); + } + + gatesForC.add(g); + } } - - gatesForC.add(g); } } diff --git a/src/de/craftinc/gates/util/GateBlockChangeSender.java b/src/de/craftinc/gates/util/GateBlockChangeSender.java index a5c1417..1dc1dbd 100644 --- a/src/de/craftinc/gates/util/GateBlockChangeSender.java +++ b/src/de/craftinc/gates/util/GateBlockChangeSender.java @@ -24,13 +24,12 @@ import org.bukkit.Material; import org.bukkit.entity.Player; import java.util.ArrayList; -import java.util.List; import java.util.Set; public class GateBlockChangeSender { - protected static final int searchRadius = 16; // TODO: move search radius into a config file / get value from config class + protected static final int searchRadius = 64; // TODO: move search radius into a config file / get value from config class public static void updateGateBlocks(final Player player) diff --git a/src/de/craftinc/gates/util/SimpleChunk.java b/src/de/craftinc/gates/util/SimpleChunk.java index b19555f..da3f1de 100644 --- a/src/de/craftinc/gates/util/SimpleChunk.java +++ b/src/de/craftinc/gates/util/SimpleChunk.java @@ -17,6 +17,7 @@ package de.craftinc.gates.util; import org.bukkit.Chunk; +import org.bukkit.World; public class SimpleChunk { @@ -31,6 +32,14 @@ public class SimpleChunk this.world = c.getWorld().getName(); } + + public SimpleChunk(int x, int z, World w) + { + this.x = x; + this.z = z; + this.world = w.getName(); + } + @Override public boolean equals(Object o)