mirror of
https://github.com/GTNewHorizons/Botania.git
synced 2026-11-17 02:45:13 +00:00
Merge pull request #1750 from blacksmithgu/swapper-concurrency
Add preemptive bugfix - Swappers now threadsafe
This commit is contained in:
commit
b7c0c12e5b
2 files changed
+47
-11
No files matched your search
@@ -11,11 +11,13 @@
|
||||
package vazkii.botania.common.item;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.client.renderer.texture.IIconRegister;
|
||||
@@ -41,7 +43,11 @@ import cpw.mods.fml.relauncher.SideOnly;
|
||||
|
||||
public class ItemGrassSeeds extends ItemMod implements IFloatingFlowerVariant {
|
||||
|
||||
private static Map<Integer, List<BlockSwapper>> blockSwappers = new HashMap();
|
||||
/**
|
||||
* Represents a map of dimension IDs to a set of all block swappers
|
||||
* active in that dimension.
|
||||
*/
|
||||
private static Map<Integer, Set<BlockSwapper>> blockSwappers = new ConcurrentHashMap<Integer, Set<BlockSwapper>>();
|
||||
|
||||
private static final IslandType[] ISLAND_TYPES = {
|
||||
IslandType.GRASS, IslandType.PODZOL, IslandType.MYCEL,
|
||||
@@ -169,25 +175,39 @@ public class ItemGrassSeeds extends ItemMod implements IFloatingFlowerVariant {
|
||||
if(event.phase == Phase.END) {
|
||||
int dim = event.world.provider.dimensionId;
|
||||
if(blockSwappers.containsKey(dim)) {
|
||||
List<BlockSwapper> swappers = blockSwappers.get(dim);
|
||||
Set<BlockSwapper> swappers = blockSwappers.get(dim);
|
||||
|
||||
Iterator<BlockSwapper> iter = swappers.listIterator();
|
||||
Iterator<BlockSwapper> iter = swappers.iterator();
|
||||
|
||||
while(iter.hasNext()) {
|
||||
BlockSwapper next = iter.next();
|
||||
if(!next.tick())
|
||||
if(next == null || !next.tick())
|
||||
iter.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a grass seed block swapper to the world at the provided positiona
|
||||
* and with the provided meta (which designates the type of the grass
|
||||
* being spread).
|
||||
* @param world The world the swapper will be in.
|
||||
* @param x The x-position of the swapper.
|
||||
* @param y The y-position of the swapper.
|
||||
* @param z The z-position of the swapper.
|
||||
* @param meta The meta value representing the type of block being swapped.
|
||||
* @return The created block swapper.
|
||||
*/
|
||||
private static BlockSwapper addBlockSwapper(World world, int x, int y, int z, int meta) {
|
||||
BlockSwapper swapper = swapperFromMeta(world, x, y, z, meta);
|
||||
|
||||
// If a set for the dimension doesn't exist, create it.
|
||||
int dim = world.provider.dimensionId;
|
||||
if(!blockSwappers.containsKey(dim))
|
||||
blockSwappers.put(dim, new ArrayList());
|
||||
blockSwappers.put(dim, Collections.newSetFromMap(new ConcurrentHashMap<BlockSwapper, Boolean>()));
|
||||
|
||||
// Add the block swapper
|
||||
blockSwappers.get(dim).add(swapper);
|
||||
|
||||
return swapper;
|
||||
|
||||
+21
-5
@@ -11,13 +11,14 @@
|
||||
package vazkii.botania.common.item.equipment.tool.terrasteel;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.PriorityQueue;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.client.renderer.texture.IIconRegister;
|
||||
@@ -70,7 +71,7 @@ public class ItemTerraAxe extends ItemManasteelAxe implements ISequentialBreaker
|
||||
*/
|
||||
private static final int MANA_PER_DAMAGE = 100;
|
||||
|
||||
private static Map<Integer, List<BlockSwapper>> blockSwappers = new HashMap<Integer, List<BlockSwapper>>();
|
||||
private static Map<Integer, Set<BlockSwapper>> blockSwappers = new ConcurrentHashMap<Integer, Set<BlockSwapper>>();
|
||||
|
||||
IIcon iconOn, iconOff;
|
||||
|
||||
@@ -134,27 +135,42 @@ public class ItemTerraAxe extends ItemManasteelAxe implements ISequentialBreaker
|
||||
if(event.phase == Phase.END) {
|
||||
int dim = event.world.provider.dimensionId;
|
||||
if(blockSwappers.containsKey(dim)) {
|
||||
List<BlockSwapper> swappers = blockSwappers.get(dim);
|
||||
Set<BlockSwapper> swappers = blockSwappers.get(dim);
|
||||
|
||||
// Iterate through all of our swappers, removing any
|
||||
// which no longer need to tick.
|
||||
Iterator<BlockSwapper> swapper = swappers.iterator();
|
||||
while(swapper.hasNext()) {
|
||||
BlockSwapper next = swapper.next();
|
||||
if(!next.tick())
|
||||
|
||||
// If a null sneaks in or the swapper is done, remove it
|
||||
if(next == null || !next.tick())
|
||||
swapper.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new block swapper to the provided world as the provided player.
|
||||
* @param world The world to add the swapper to.
|
||||
* @param player The player who is responsible for this swapper.
|
||||
* @param stack The Terra Truncator which caused this block swapper.
|
||||
* @param origCoords The original coordinates the swapper should start at.
|
||||
* @param steps The range of the block swapper, in blocks.
|
||||
* @param leaves If true, will treat leaves specially (see the BlockSwapper
|
||||
* documentation).
|
||||
* @return The created block swapper.
|
||||
*/
|
||||
private static BlockSwapper addBlockSwapper(World world, EntityPlayer player, ItemStack stack, ChunkCoordinates origCoords, int steps, boolean leaves) {
|
||||
BlockSwapper swapper = new BlockSwapper(world, player, stack, origCoords, steps, leaves);
|
||||
|
||||
// If the mapping for this dimension doesn't exist, create it.
|
||||
int dim = world.provider.dimensionId;
|
||||
if(!blockSwappers.containsKey(dim))
|
||||
blockSwappers.put(dim, new ArrayList<BlockSwapper>());
|
||||
blockSwappers.put(dim, Collections.newSetFromMap(new ConcurrentHashMap<BlockSwapper, Boolean>()));
|
||||
|
||||
// Add the swapper
|
||||
blockSwappers.get(dim).add(swapper);
|
||||
|
||||
return swapper;
|
||||
|
||||
Reference in new issue
Block a user