Skip to content

Commit

Permalink
Removed call to getTargetWorld from three event listeners.
Browse files Browse the repository at this point in the history
This makes the code compatible with changes introduced in API 6
  • Loading branch information
gravityfox committed Jun 5, 2017
1 parent 2ab82d8 commit ccbc575
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 75 deletions.
2 changes: 1 addition & 1 deletion FoxCore
114 changes: 77 additions & 37 deletions src/main/java/net/foxdenstudio/sponge/foxguard/plugin/FGManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.spongepowered.api.Sponge;
import org.spongepowered.api.util.GuavaCollectors;
import org.spongepowered.api.util.Tristate;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;

import java.util.*;
Expand Down Expand Up @@ -92,6 +93,17 @@ public static FGManager getInstance() {
return instance;
}

public static boolean isNameValid(String name) {
if (name.matches("^.*[ :.=;\"\'\\\\/{}()\\[\\]<>#@|?*].*$")) return false;
for (String s : FGStorageManager.FS_ILLEGAL_NAMES) {
if (name.equalsIgnoreCase(s)) return false;
}
for (String s : ILLEGAL_NAMES) {
if (name.equalsIgnoreCase(s)) return false;
}
return true;
}

public boolean isRegistered(IHandler handler) {
return handlers.contains(handler);
}
Expand Down Expand Up @@ -236,32 +248,38 @@ public Set<IRegion> getRegionsAtPos(World world, Vector3d position, boolean incl
.collect(Collectors.toSet());
}

public Set<IRegion> getRegionsAtMultiPosI(World world, Iterable<Vector3i> positions) {
return getRegionsAtMultiPosI(world, positions, false);
public Set<IRegion> getRegionsAtMultiLocI(Iterable<Location<World>> locations) {
return getRegionsAtMultiLocI(locations, false);
}

public Set<IRegion> getRegionsAtMultiPosI(World world, Iterable<Vector3i> positions, boolean includeDisabled) {
public Set<IRegion> getRegionsAtMultiLocI(Iterable<Location<World>> locations, boolean includeDisabled) {
Set<IRegion> set = new HashSet<>();
SetMultimap<Vector3i, Vector3i> chunkPosMap = HashMultimap.create();
for (Vector3i pos : positions) {
SetMultimap<Chunk, Vector3i> chunkPosMap = HashMultimap.create();
for (Location<World> loc : locations) {
Vector3i pos = loc.getBlockPosition();
chunkPosMap.put(
new Vector3i(
pos.getX() >> 4,
pos.getY() >> 4,
pos.getZ() >> 4
), pos
new Chunk(
new Vector3i(
pos.getX() >> 4,
pos.getY() >> 4,
pos.getZ() >> 4
),
loc.getExtent()
),
loc.getBlockPosition()
);
}
for (Map.Entry<Vector3i, Collection<Vector3i>> entry : chunkPosMap.asMap().entrySet()) {
RegionCache.ChunkData data = this.regionCache.getData(world, entry.getKey());
for (Map.Entry<Chunk, Collection<Vector3i>> entry : chunkPosMap.asMap().entrySet()) {
Chunk chunk = entry.getKey();
RegionCache.ChunkData data = this.regionCache.getData(chunk.world, chunk.chunk);
Set<IRegion> candidates = new HashSet<>(data.getRegions(includeDisabled));
candidates.removeAll(set);
for (Vector3i pos : entry.getValue()) {
if (candidates.isEmpty()) break;
Iterator<IRegion> regionIterator = candidates.iterator();
do {
IRegion region = regionIterator.next();
if (region.contains(pos, world)) {
if (region.contains(pos, chunk.world)) {
set.add(region);
regionIterator.remove();
}
Expand All @@ -272,32 +290,38 @@ public Set<IRegion> getRegionsAtMultiPosI(World world, Iterable<Vector3i> positi
return set;
}

public Set<IRegion> getRegionsAtMultiPosD(World world, Iterable<Vector3d> positions) {
return getRegionsAtMultiPosD(world, positions, false);
public Set<IRegion> getRegionsAtMultiLocD(Iterable<Location<World>> locations) {
return getRegionsAtMultiLocD(locations, false);
}

public Set<IRegion> getRegionsAtMultiPosD(World world, Iterable<Vector3d> positions, boolean includeDisabled) {
public Set<IRegion> getRegionsAtMultiLocD(Iterable<Location<World>> locations, boolean includeDisabled) {
Set<IRegion> set = new HashSet<>();
SetMultimap<Vector3i, Vector3d> chunkPosMap = HashMultimap.create();
for (Vector3d pos : positions) {
SetMultimap<Chunk, Vector3d> chunkPosMap = HashMultimap.create();
for (Location<World> loc : locations) {
Vector3i pos = loc.getBlockPosition();
chunkPosMap.put(
new Vector3i(
GenericMath.floor(pos.getX()) >> 4,
GenericMath.floor(pos.getY()) >> 4,
GenericMath.floor(pos.getZ()) >> 4)
, pos
new Chunk(
new Vector3i(
pos.getX() >> 4,
pos.getY() >> 4,
pos.getZ() >> 4
),
loc.getExtent()
),
loc.getPosition()
);
}
for (Map.Entry<Vector3i, Collection<Vector3d>> entry : chunkPosMap.asMap().entrySet()) {
RegionCache.ChunkData data = this.regionCache.getData(world, entry.getKey());
for (Map.Entry<Chunk, Collection<Vector3d>> entry : chunkPosMap.asMap().entrySet()) {
Chunk chunk = entry.getKey();
RegionCache.ChunkData data = this.regionCache.getData(chunk.world, chunk.chunk);
Set<IRegion> candidates = new HashSet<>(data.getRegions(includeDisabled));
candidates.removeAll(set);
for (Vector3d pos : entry.getValue()) {
if (candidates.isEmpty()) break;
Iterator<IRegion> regionIterator = candidates.iterator();
do {
IRegion region = regionIterator.next();
if (region.contains(pos, world)) {
if (region.contains(pos, chunk.world)) {
set.add(region);
regionIterator.remove();
}
Expand Down Expand Up @@ -477,22 +501,38 @@ public GlobalHandler getGlobalHandler() {
return globalHandler;
}

public static boolean isNameValid(String name) {
if (name.matches("^.*[ :.=;\"\'\\\\/{}()\\[\\]<>#@|?*].*$")) return false;
for (String s : FGStorageManager.FS_ILLEGAL_NAMES) {
if (name.equalsIgnoreCase(s)) return false;
}
for (String s : ILLEGAL_NAMES) {
if (name.equalsIgnoreCase(s)) return false;
}
return true;
}

public void markDirty(IRegion region, RegionCache.DirtyType type) {
regionCache.markDirty(region, type);
}

public void clearRegionCache() {
this.regionCache.clearCaches();
}

private static class Chunk {
Vector3i chunk;
World world;

public Chunk(Vector3i chunk, World world) {
this.chunk = chunk;
this.world = world;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Chunk chunk1 = (Chunk) o;

return chunk.equals(chunk1.chunk) && world.equals(chunk1.world);
}

@Override
public int hashCode() {
int result = chunk.hashCode();
result = 31 * result + world.hashCode();
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.chat.ChatTypes;
import org.spongepowered.api.util.Tristate;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;

import java.util.*;
Expand Down Expand Up @@ -94,22 +95,22 @@ public void handle(ChangeBlockEvent event) throws Exception {
//FoxGuardMain.instance().getLogger().info(player.getName());

List<IHandler> handlerList;
World world = event.getTargetWorld();

List<Transaction<BlockSnapshot>> transactions = event.getTransactions();
Set<IHandler> handlerSet = new HashSet<>();
if (transactions.size() == 1) {
Vector3i pos = transactions.get(0).getFinal().getLocation().get().getBlockPosition();
Location<World> loc = transactions.get(0).getOriginal().getLocation().get();
Vector3i pos = loc.getBlockPosition();
World world = loc.getExtent();
FGManager.getInstance().getRegionsInChunkAtPos(world, pos).stream()
.filter(region -> region.contains(pos, world))
.forEach(region -> region.getHandlers().stream()
.filter(IFGObject::isEnabled)
.forEach(handlerSet::add));
} else {
FGManager.getInstance().getRegionsAtMultiPosI(
world,
FGManager.getInstance().getRegionsAtMultiLocI(
transactions.stream()
.map(trans -> trans.getFinal().getLocation().get().getBlockPosition())
.map(trans -> trans.getFinal().getLocation().get())
.collect(Collectors.toList())
).forEach(region -> region.getHandlers().stream()
.filter(IFGObject::isEnabled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public void handle(ExplosionEvent event) throws Exception {
} else user = null;
}

World world = event.getTargetWorld();
FlagBitSet flags = FLAG_SET.clone();

Set<IHandler> handlerSet = new HashSet<>();
Expand All @@ -91,10 +90,9 @@ public void handle(ExplosionEvent event) throws Exception {
flags.set(CHANGE);

ExplosionEvent.Post postEvent = (ExplosionEvent.Post) event;
FGManager.getInstance().getRegionsAtMultiPosI(
world,
FGManager.getInstance().getRegionsAtMultiLocI(
postEvent.getTransactions().stream()
.map(trans -> trans.getFinal().getLocation().get().getBlockPosition())
.map(trans -> trans.getFinal().getLocation().get())
.collect(Collectors.toList())
).forEach(region -> region.getHandlers().stream()
.filter(IFGObject::isEnabled)
Expand All @@ -103,18 +101,15 @@ public void handle(ExplosionEvent event) throws Exception {
flags.set(DETONATE);

ExplosionEvent.Detonate detonateEvent = ((ExplosionEvent.Detonate) event);
FGManager.getInstance().getRegionsAtMultiPosI(
world,
detonateEvent.getAffectedLocations().stream()
.map(Location::getBlockPosition)
.collect(Collectors.toList())
).forEach(region -> region.getHandlers().stream()
.filter(IFGObject::isEnabled)
.forEach(handlerSet::add));
FGManager.getInstance().getRegionsAtMultiLocI(detonateEvent.getAffectedLocations())
.forEach(region -> region.getHandlers().stream()
.filter(IFGObject::isEnabled)
.forEach(handlerSet::add));
} else if (event instanceof ExplosionEvent.Pre) {
flags.set(PRE);

Vector3d pos = event.getExplosion().getLocation().getPosition();
Location<World> loc = event.getExplosion().getLocation();
Vector3d pos = loc.getPosition();
World world = loc.getExtent();
FGManager.getInstance().getRegionsInChunkAtPos(world, pos).stream()
.filter(region -> region.contains(pos, world))
.forEach(region -> region.getHandlers().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.chat.ChatTypes;
import org.spongepowered.api.util.Tristate;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;

import java.util.ArrayList;
Expand Down Expand Up @@ -107,10 +108,11 @@ public void handle(SpawnEntityEvent event) throws Exception {
}

List<IHandler> handlerList = new ArrayList<>();
World world = event.getTargetWorld();

for (Entity entity : event.getEntities()) {
Vector3d pos = entity.getLocation().getPosition();
Location<World> loc = entity.getLocation();
Vector3d pos = loc.getPosition();
World world = loc.getExtent();
FGManager.getInstance().getRegionsInChunkAtPos(world, pos).stream()
.filter(region -> region.contains(pos, world))
.forEach(region -> region.getHandlers().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,14 @@

package net.foxdenstudio.sponge.foxguard.plugin.region.world;

import com.flowpowered.math.vector.Vector3i;
import net.foxdenstudio.sponge.foxcore.plugin.util.IWorldBounded;
import net.foxdenstudio.sponge.foxguard.plugin.region.IRegion;
import org.spongepowered.api.world.World;

public interface IWorldRegion extends IRegion, IWorldBounded {

@Override
World getWorld();

void setWorld(World world);

@Override
default boolean contains(int x, int y, int z, World world) {
return world == getWorld() && contains(x, y, z);
}

@Override
default boolean contains(double x, double y, double z, World world) {
return world == getWorld() && contains(x, y, z);
}

@Override
default boolean isInChunk(Vector3i chunk, World world) {
return world == getWorld() && isInChunk(chunk);
}
}

0 comments on commit ccbc575

Please sign in to comment.