Skip to content

Commit ab94109

Browse files
committed
some SonarQube issues fixed
1 parent 2ef8752 commit ab94109

File tree

8 files changed

+116
-69
lines changed

8 files changed

+116
-69
lines changed

src/main/java/com/mcmoddev/orespawn/OreSpawn.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import net.minecraftforge.fml.common.Mod.EventHandler;
3131
import net.minecraftforge.fml.common.Mod.Instance;
3232
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
33-
import net.minecraftforge.fml.common.event.FMLInterModComms;
3433
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
3534
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
3635
import net.minecraftforge.fml.common.event.FMLServerStartingEvent;
@@ -55,7 +54,11 @@ public class OreSpawn {
5554
public static final EventHandlers eventHandlers = new EventHandlers();
5655
public static final FeatureRegistry FEATURES = new FeatureRegistry();
5756
private String os1ConfigPath;
58-
public static final Map<Integer, List<SpawnBuilder>> spawns = new HashMap<>();
57+
protected static final Map<Integer, List<SpawnBuilder>> spawns = new HashMap<>();
58+
59+
public static Map<Integer, List<SpawnBuilder>> getSpawns() {
60+
return spawns;
61+
}
5962

6063
@EventHandler
6164
public void preInit(FMLPreInitializationEvent ev) {

src/main/java/com/mcmoddev/orespawn/api/IFeature.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
import com.google.gson.JsonObject;
66

77
import net.minecraft.block.state.IBlockState;
8+
import net.minecraft.util.math.ChunkPos;
89
import net.minecraft.world.World;
910
import net.minecraft.world.chunk.IChunkGenerator;
1011
import net.minecraft.world.chunk.IChunkProvider;
1112

1213
public interface IFeature {
13-
void generate(Random random, int chunkX, int chunkZ, World world, IChunkGenerator chunkGenerator,
14+
void generate(ChunkPos pos, World world, IChunkGenerator chunkGenerator,
1415
IChunkProvider chunkProvider, JsonObject parameters, IBlockState block, IBlockState blockReplace );
1516

17+
void setRandom(Random rand);
18+
1619
JsonObject getDefaultParameters();
1720
}

src/main/java/com/mcmoddev/orespawn/api/plugin/PluginLoader.java

Lines changed: 60 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@
2121
import org.apache.commons.io.FileUtils;
2222

2323
import com.mcmoddev.orespawn.api.plugin.IOreSpawnPlugin;
24+
import com.mcmoddev.orespawn.data.Constants;
2425
import com.mcmoddev.orespawn.OreSpawn;
2526

27+
import net.minecraft.crash.CrashReport;
28+
import net.minecraft.util.ResourceLocation;
2629
import net.minecraftforge.fml.common.discovery.ASMDataTable.ASMData;
2730
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
2831

@@ -72,58 +75,71 @@ public void load(FMLPreInitializationEvent event) {
7275
}
7376

7477
public void register() {
75-
dataStore.forEach( pd -> {
76-
try {
77-
scanResources(pd);
78-
} catch (IOException | URISyntaxException e) {
79-
OreSpawn.LOGGER.error("Houston, we have a problem: mod {} apparently registered a path for files and there was an issue.", pd.modId, e);
80-
}
81-
pd.plugin.register(OreSpawn.API);
82-
});
78+
dataStore.forEach( pd -> { scanResources(pd); pd.plugin.register(OreSpawn.API); });
8379
}
8480

85-
public void scanResources(PluginData pd) throws IOException, URISyntaxException {
81+
public void scanResources(PluginData pd) {
8682
String base = String.format("assets/%s/%s", pd.modId, pd.resourcePath);
8783
URL resURL = getClass().getClassLoader().getResource(base);
8884
if( resURL == null ) {
8985
// nothing to load!
9086
return;
9187
}
92-
URI uri = resURL.toURI();
93-
Path myPath;
94-
if (uri.getScheme().equals("jar")) {
95-
FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.<String, Object>emptyMap());
96-
myPath = fileSystem.getPath(base);
97-
} else {
98-
myPath = Paths.get(uri);
99-
}
100-
101-
if( Files.notExists(myPath) ) {
102-
return;
103-
}
88+
89+
URI uri;
90+
try {
91+
uri = resURL.toURI();
92+
} catch (URISyntaxException ex) {
93+
CrashReport report = CrashReport.makeCrashReport(ex, String.format("Failed to get URI for %s", (new ResourceLocation(pd.modId,pd.resourcePath)).toString()));
94+
report.getCategory().addCrashSection("OreSpawn Version", Constants.VERSION);
95+
return;
96+
}
97+
98+
Path myPath = null;
99+
FileSystem fileSystem = null;
100+
try {
101+
if (uri.getScheme().equals("jar")) {
102+
fileSystem = FileSystems.newFileSystem(uri, Collections.<String, Object>emptyMap());
103+
myPath = fileSystem.getPath(base);
104+
} else {
105+
myPath = Paths.get(uri);
106+
}
104107

105-
Stream<Path> walk = Files.walk(myPath, 1);
106-
for (Iterator<Path> it = walk.iterator(); it.hasNext();){
107-
Path p = it.next();
108-
String name = p.getFileName().toString();
109-
if( "json".equals(FilenameUtils.getExtension(name)) ) {
110-
InputStream reader = null;
111-
try {
112-
Path target = Paths.get(".","orespawn","os3",String.format("%s.json", pd.modId));
113-
if( Files.exists(target) ) {
114-
// the file we were going to copy out to already exists!
115-
return;
116-
}
117-
reader = Files.newInputStream(p);
118-
FileUtils.copyInputStreamToFile(reader, target.toFile());
119-
} catch (IOException e) {
120-
OreSpawn.LOGGER.error("Error creating a Buffered Reader to load Json from {} for mod {}",
121-
p.toString(), pd.modId, e);
122-
} finally {
123-
IOUtils.closeQuietly(reader);
124-
}
125-
}
126-
}
127-
walk.close();
108+
if( Files.notExists(myPath) ) {
109+
return;
110+
}
111+
112+
Stream<Path> walk = Files.walk(myPath, 1);
113+
for (Iterator<Path> it = walk.iterator(); it.hasNext();){
114+
Path p = it.next();
115+
String name = p.getFileName().toString();
116+
if( "json".equals(FilenameUtils.getExtension(name)) ) {
117+
InputStream reader = null;
118+
try {
119+
Path target = Paths.get(".","orespawn","os3",String.format("%s.json", pd.modId));
120+
if( Files.exists(target) ) {
121+
// the file we were going to copy out to already exists!
122+
return;
123+
}
124+
reader = Files.newInputStream(p);
125+
FileUtils.copyInputStreamToFile(reader, target.toFile());
126+
} catch (IOException e) {
127+
OreSpawn.LOGGER.error("Error creating a Buffered Reader to load Json from {} for mod {}",
128+
p.toString(), pd.modId, e);
129+
} finally {
130+
IOUtils.closeQuietly(reader);
131+
}
132+
}
133+
}
134+
walk.close();
135+
} catch( IOException exc ) {
136+
CrashReport report = CrashReport.makeCrashReport(exc, String.format("Failed in copying out config %s to %s", (new ResourceLocation(pd.modId,String.format("%s/%s", pd.resourcePath, FilenameUtils.getExtension(uri.getPath()))))).toString());
137+
report.getCategory().addCrashSection("OreSpawn Version", Constants.VERSION);
138+
OreSpawn.LOGGER.info(report.getCompleteReport());
139+
} finally {
140+
if( fileSystem != null ) {
141+
IOUtils.closeQuietly(fileSystem);
142+
}
143+
}
128144
}
129145
}

src/main/java/com/mcmoddev/orespawn/commands/ClearChunkCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public void execute(MinecraftServer server, ICommandSender sender, String[] args
3939
for (int z = chunkPos.getZStart(); z <= chunkPos.getZEnd(); z++) {
4040
BlockPos pos = new BlockPos(x, y, z);
4141
Block block = player.world.getBlockState(pos).getBlock();
42-
43-
if (OreSpawnWorldGen.SPAWN_BLOCKS.contains(block)) {
42+
43+
if (OreSpawnWorldGen.getSpawnBlocks().contains(block)) {
4444
player.world.setBlockToAir(pos);
4545
}
4646
}

src/main/java/com/mcmoddev/orespawn/data/FeatureRegistry.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,10 @@ public void writeFeatures(File file) {
139139
try {
140140
FileUtils.writeStringToFile(file, StringEscapeUtils.unescapeJson(json), Charsets.UTF_8);
141141
} catch (IOException e) {
142-
e.printStackTrace();
142+
CrashReport report = CrashReport.makeCrashReport(e, "Failed writing config " + file.getName());
143+
report.getCategory().addCrashSection("OreSpawn Version", Constants.VERSION);
144+
OreSpawn.LOGGER.info(report.getCompleteReport());
145+
return;
143146
}
144147
}
145148
}

src/main/java/com/mcmoddev/orespawn/impl/features/DefaultFeatureGenerator.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
import com.mcmoddev.orespawn.data.ReplacementsRegistry;
1616

1717
import net.minecraft.block.state.IBlockState;
18+
import net.minecraft.client.Minecraft;
1819
import net.minecraft.init.Blocks;
1920
import net.minecraft.util.math.BlockPos;
21+
import net.minecraft.util.math.ChunkPos;
2022
import net.minecraft.util.math.Vec3i;
2123
import net.minecraft.world.World;
2224
import net.minecraft.world.chunk.IChunkGenerator;
@@ -27,14 +29,22 @@ public class DefaultFeatureGenerator implements IFeature {
2729
private static final int maxCacheSize = 1024;
2830
/** overflow cache so that ores that spawn at edge of chunk can
2931
* appear in the neighboring chunk without triggering a chunk-load */
30-
private static final Map<Integer3D,Map<BlockPos,IBlockState>> overflowCache = new HashMap<>(maxCacheSize);
31-
private static final Deque<Integer3D> cacheOrder = new LinkedList<>();
32+
private static final Map<Vec3i,Map<BlockPos,IBlockState>> overflowCache = new HashMap<>(maxCacheSize);
33+
private static final Deque<Vec3i> cacheOrder = new LinkedList<>();
34+
private Random random;
35+
36+
public DefaultFeatureGenerator() {
37+
this.random = new Random();
38+
}
39+
3240

3341
@Override
34-
public void generate(Random random, int chunkX, int chunkZ, World world, IChunkGenerator chunkGenerator,
42+
public void generate(ChunkPos pos, World world, IChunkGenerator chunkGenerator,
3543
IChunkProvider chunkProvider, JsonObject parameters, IBlockState block, IBlockState replaceBlock ) {
3644
// First, load cached blocks for neighboring chunk ore spawns
37-
Integer3D chunkCoord = new Integer3D(chunkX, chunkZ, world.provider.getDimension());
45+
int chunkX = pos.x;
46+
int chunkZ = pos.z;
47+
Vec3i chunkCoord = new Vec3i(chunkX, chunkZ, world.provider.getDimension());
3848
Map<BlockPos,IBlockState> cache = retrieveCache(chunkCoord);
3949
for(Entry<BlockPos,IBlockState> ent : cache.entrySet()){
4050
spawn(cache.get(ent.getKey()),world,ent.getKey(),world.provider.getDimension(),false,replaceBlock);
@@ -202,11 +212,11 @@ private static void spawn(IBlockState b, World w, BlockPos coord, int dimension,
202212

203213

204214
protected static void cacheOverflowBlock(IBlockState bs, BlockPos coord, int dimension){
205-
Integer3D chunkCoord = new Integer3D(coord.getX() >> 4, coord.getY() >> 4, dimension);
215+
Vec3i chunkCoord = new Vec3i(coord.getX() >> 4, coord.getY() >> 4, dimension);
206216
if(overflowCache.containsKey(chunkCoord)){
207217
cacheOrder.addLast(chunkCoord);
208218
if(cacheOrder.size() > maxCacheSize){
209-
Integer3D drop = cacheOrder.removeFirst();
219+
Vec3i drop = cacheOrder.removeFirst();
210220
overflowCache.get(drop).clear();
211221
overflowCache.remove(drop);
212222
}
@@ -216,7 +226,7 @@ protected static void cacheOverflowBlock(IBlockState bs, BlockPos coord, int dim
216226
cache.put(coord, bs);
217227
}
218228

219-
protected static Map<BlockPos,IBlockState> retrieveCache(Integer3D chunkCoord ){
229+
protected static Map<BlockPos,IBlockState> retrieveCache(Vec3i chunkCoord ){
220230
if(overflowCache.containsKey(chunkCoord)){
221231
Map<BlockPos,IBlockState> cache =overflowCache.get(chunkCoord);
222232
cacheOrder.remove(chunkCoord);
@@ -238,4 +248,10 @@ public JsonObject getDefaultParameters() {
238248
return defParams;
239249
}
240250

251+
252+
@Override
253+
public void setRandom(Random rand) {
254+
this.random = rand;
255+
}
256+
241257
}

src/main/java/com/mcmoddev/orespawn/impl/os3/OS3APIImpl.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.ArrayList;
44
import java.util.HashMap;
5+
import java.util.List;
56
import java.util.Map;
67
import java.util.Random;
78
import java.util.Map.Entry;
@@ -12,6 +13,7 @@
1213
import com.mcmoddev.orespawn.api.os3.BuilderLogic;
1314
import com.mcmoddev.orespawn.api.os3.DimensionBuilder;
1415
import com.mcmoddev.orespawn.api.os3.OS3API;
16+
import com.mcmoddev.orespawn.api.os3.SpawnBuilder;
1517
import com.mcmoddev.orespawn.data.ReplacementsRegistry;
1618
import com.mcmoddev.orespawn.worldgen.OreSpawnWorldGen;
1719

@@ -21,7 +23,6 @@
2123

2224
public class OS3APIImpl implements OS3API {
2325
private final Map<String, BuilderLogic> logic;
24-
private OreSpawnWorldGen worldGenerator;
2526

2627
public OS3APIImpl() {
2728
this.logic = new HashMap<>();
@@ -85,25 +86,25 @@ public ImmutableMap<String, BuilderLogic> getSpawns() {
8586

8687
@Override
8788
public void registerSpawns() {
89+
Map<Integer,List<SpawnBuilder>> spawns = OreSpawn.getSpawns();
8890
// build a proper tracking of data for the spawner
8991
for( Entry<String, BuilderLogic> ent : logic.entrySet() ) {
9092
for( Entry<Integer, DimensionBuilder> dL : ent.getValue().getAllDimensions().entrySet()) {
91-
if( OreSpawn.spawns.containsKey(dL.getKey()) ) {
92-
OreSpawn.spawns.get(dL.getKey()).addAll(dL.getValue().getAllSpawns());
93+
if( spawns.containsKey(dL.getKey()) ) {
94+
spawns.get(dL.getKey()).addAll(dL.getValue().getAllSpawns());
9395
} else {
94-
OreSpawn.spawns.put(dL.getKey(), new ArrayList<>());
95-
OreSpawn.spawns.get(dL.getKey()).addAll(dL.getValue().getAllSpawns());
96+
spawns.put(dL.getKey(), new ArrayList<>());
97+
spawns.get(dL.getKey()).addAll(dL.getValue().getAllSpawns());
9698
}
9799
}
98100
OreSpawn.LOGGER.info("Registered spawn logic for mod {}", ent.getKey());
99101
}
100102

101103
Random random = new Random();
102104

103-
worldGenerator = new OreSpawnWorldGen(OreSpawn.spawns, random.nextLong());
104-
//if (!Config.getBoolean(Constants.RETROGEN_KEY)) {
105+
OreSpawnWorldGen worldGenerator = new OreSpawnWorldGen(spawns, random.nextLong());
106+
105107
GameRegistry.registerWorldGenerator(worldGenerator, 100);
106-
//}
107108

108109
}
109110

src/main/java/com/mcmoddev/orespawn/worldgen/OreSpawnWorldGen.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.Random;
88
import java.util.stream.Collectors;
99

10+
import com.google.common.collect.ImmutableList;
1011
import com.mcmoddev.orespawn.OreSpawn;
1112
import com.mcmoddev.orespawn.api.IFeature;
1213
import com.mcmoddev.orespawn.api.os3.SpawnBuilder;
@@ -17,6 +18,7 @@
1718
import net.minecraft.init.Blocks;
1819
import net.minecraft.item.ItemBlock;
1920
import net.minecraft.util.math.BlockPos;
21+
import net.minecraft.util.math.ChunkPos;
2022
import net.minecraft.world.World;
2123
import net.minecraft.world.biome.Biome;
2224
import net.minecraft.world.chunk.IChunkGenerator;
@@ -27,7 +29,7 @@
2729
public class OreSpawnWorldGen implements IWorldGenerator {
2830

2931
private final Map<Integer, List<SpawnBuilder>> dimensions;
30-
public static final List<Block> SPAWN_BLOCKS = new ArrayList<>();
32+
protected static final List<Block> SPAWN_BLOCKS = new ArrayList<>();
3133

3234
@SuppressWarnings("unused") private final long nextL;
3335

@@ -42,6 +44,10 @@ public OreSpawnWorldGen(Map<Integer, List<SpawnBuilder>> allDimensions, long nex
4244
}
4345
}
4446

47+
public static ImmutableList<Block> getSpawnBlocks() {
48+
return ImmutableList.<Block>copyOf(SPAWN_BLOCKS);
49+
}
50+
4551
@Override
4652
public void generate(Random random, int chunkX, int chunkZ, World world, IChunkGenerator chunkGenerator,
4753
IChunkProvider chunkProvider) {
@@ -56,8 +62,6 @@ public void generate(Random random, int chunkX, int chunkZ, World world, IChunkG
5662

5763
entries = this.dimensions.get(OreSpawn.API.dimensionWildcard());
5864
if( entries == null ) {
59-
// got complaints about this
60-
// OreSpawn.LOGGER.fatal("no spawn entries for dimension "+thisDim+" or for all dimensions");
6165
return;
6266
}
6367
} else if( thisDim != -1 && thisDim != 1
@@ -73,7 +77,8 @@ public void generate(Random random, int chunkX, int chunkZ, World world, IChunkG
7377
if( replacement == null ) {
7478
replacement = ReplacementsRegistry.getDimensionDefault(thisDim);
7579
}
76-
currentFeatureGen.generate(random, chunkX, chunkZ, world, chunkGenerator, chunkProvider, sE.getFeatureGen().getParameters(), sE.getOres().get(0).getOre(), replacement);
80+
currentFeatureGen.setRandom(random);
81+
currentFeatureGen.generate(new ChunkPos(chunkX, chunkZ), world, chunkGenerator, chunkProvider, sE.getFeatureGen().getParameters(), sE.getOres().get(0).getOre(), replacement);
7782
}
7883
}
7984
}

0 commit comments

Comments
 (0)