Skip to content

Commit 286cab6

Browse files
committed
more churn from fixing SonarLint and SonarQube issues
1 parent 204232b commit 286cab6

File tree

10 files changed

+120
-184
lines changed

10 files changed

+120
-184
lines changed

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

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,21 @@
3232
public enum PluginLoader {
3333

3434
INSTANCE;
35-
35+
3636
private class PluginData {
3737
public final String modId;
3838
public final String resourcePath;
3939
public final IOreSpawnPlugin plugin;
40-
40+
4141
public PluginData(String modId, String resourcePath, IOreSpawnPlugin plugin) {
4242
this.modId = modId;
4343
this.resourcePath = resourcePath;
4444
this.plugin = plugin;
4545
}
4646
}
47-
47+
4848
private List<PluginData> dataStore = new ArrayList<>();
49-
49+
5050
private String getAnnotationItem(String item, final ASMData asmData) {
5151
if (asmData.getAnnotationInfo().get(item) != null) {
5252
return asmData.getAnnotationInfo().get(item).toString();
@@ -73,7 +73,7 @@ public void load(FMLPreInitializationEvent event) {
7373
}
7474
}
7575
}
76-
76+
7777
public void register() {
7878
dataStore.forEach( pd -> { scanResources(pd); pd.plugin.register(OreSpawn.API); });
7979
}
@@ -85,7 +85,7 @@ public void scanResources(PluginData pd) {
8585
// nothing to load!
8686
return;
8787
}
88-
88+
8989
URI uri;
9090
try {
9191
uri = resURL.toURI();
@@ -97,6 +97,7 @@ public void scanResources(PluginData pd) {
9797

9898
Path myPath = null;
9999
FileSystem fileSystem = null;
100+
String tName = null;
100101
try {
101102
if (uri.getScheme().equals("jar")) {
102103
fileSystem = FileSystems.newFileSystem(uri, Collections.<String, Object>emptyMap());
@@ -105,7 +106,7 @@ public void scanResources(PluginData pd) {
105106
myPath = Paths.get(uri);
106107
}
107108

108-
if( Files.notExists(myPath) ) {
109+
if( !myPath.toFile().exists() ) {
109110
return;
110111
}
111112

@@ -115,25 +116,25 @@ public void scanResources(PluginData pd) {
115116
String name = p.getFileName().toString();
116117
if( "json".equals(FilenameUtils.getExtension(name)) ) {
117118
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);
119+
Path target = Paths.get(".","orespawn","os3",String.format("%s.json", pd.modId));
120+
tName = String.format("%s.json", pd.modId);
121+
if( target.toFile().exists() ) {
122+
// the file we were going to copy out to already exists!
123+
walk.close();
124+
return;
131125
}
126+
reader = Files.newInputStream(p);
127+
FileUtils.copyInputStreamToFile(reader, target.toFile());
128+
IOUtils.closeQuietly(reader);
132129
}
133130
}
134131
walk.close();
135132
} 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());
133+
String resName = (new ResourceLocation(pd.modId,
134+
String.format("%s/%s", pd.resourcePath,
135+
FilenameUtils.getBaseName(uri.getPath())))).toString();
136+
CrashReport report = CrashReport.makeCrashReport(exc,
137+
String.format("Failed in copying out config %s to %s", resName, tName));
137138
report.getCategory().addCrashSection("OreSpawn Version", Constants.VERSION);
138139
OreSpawn.LOGGER.info(report.getCompleteReport());
139140
} finally {

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

Lines changed: 41 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
import com.google.gson.*;
44
import com.mcmoddev.orespawn.OreSpawn;
5+
import com.mcmoddev.orespawn.api.os3.BiomeBuilder;
6+
import com.mcmoddev.orespawn.api.os3.DimensionBuilder;
7+
import com.mcmoddev.orespawn.api.os3.FeatureBuilder;
8+
import com.mcmoddev.orespawn.api.os3.OreBuilder;
9+
import com.mcmoddev.orespawn.api.os3.SpawnBuilder;
10+
import com.mcmoddev.orespawn.data.ReplacementsRegistry;
511
import com.mcmoddev.orespawn.util.StateUtil;
612
import net.minecraft.block.state.IBlockState;
713
import net.minecraft.command.CommandBase;
@@ -14,16 +20,14 @@
1420
import net.minecraft.server.MinecraftServer;
1521
import net.minecraft.util.EnumHand;
1622
import net.minecraft.util.text.TextComponentString;
17-
import org.apache.commons.io.Charsets;
18-
import org.apache.commons.io.FileUtils;
19-
import org.apache.commons.lang3.StringEscapeUtils;
2023

21-
import java.io.File;
22-
import java.io.IOException;
24+
import java.util.ArrayList;
25+
import java.util.List;
2326

2427
public class AddOreCommand extends CommandBase {
25-
private static final String dim = "dimension";
26-
private static final String all = "all";
28+
private static final String BLOCK = "block";
29+
private static final String STATE2 = "state";
30+
private static final String ALL = "all";
2731

2832
@Override
2933
public String getName() {
@@ -51,77 +55,59 @@ public void execute(MinecraftServer server, ICommandSender sender, String[] args
5155
} else if (args.length != 2) {
5256
throw new CommandException(this.getUsage(sender));
5357
}
54-
55-
File file = new File(".", "orespawn" + File.separator + args[0] + ".json");
56-
JsonParser parser = new JsonParser();
58+
59+
String file = args[0];
5760
@SuppressWarnings("deprecation")
5861
IBlockState state = ((ItemBlock) stack.getItem()).getBlock().getStateFromMeta(stack.getItemDamage());
5962

60-
if (!file.exists()) {
61-
throw new CommandException("That file doesn't exist" + (args[0].endsWith(".json") ? " (don't add .json)" : ""));
62-
}
63-
6463
int dimension = OreSpawn.API.dimensionWildcard();
6564

6665
try {
67-
if (!args[1].equals(all)) {
66+
if (!args[1].equals(ALL)) {
6867
dimension = Integer.parseInt(args[1]);
6968
}
7069
} catch (NumberFormatException e) {
7170
throw new CommandException(args[1] + " isn't a valid dimension");
7271
}
7372

7473
JsonObject ore = new JsonObject();
75-
ore.addProperty("block", state.getBlock().getRegistryName().toString());
76-
ore.addProperty("state", StateUtil.serializeState(state));
74+
ore.addProperty(BLOCK, state.getBlock().getRegistryName().toString());
75+
ore.addProperty(STATE2, StateUtil.serializeState(state));
7776
ore.addProperty("size", 25);
7877
ore.addProperty("variation", 12);
7978
ore.addProperty("frequency", 20);
8079
ore.addProperty("min_height", 0);
8180
ore.addProperty("max_height", 128);
8281

83-
try {
84-
JsonArray json = parser.parse(FileUtils.readFileToString(file)).getAsJsonArray();
85-
86-
for (JsonElement element : json) {
87-
JsonObject object = element.getAsJsonObject();
88-
89-
if (object.has(dim) ? dimension == object.get(dim).getAsInt() : dimension == OreSpawn.API.dimensionWildcard()) {
90-
object.get("ores").getAsJsonArray().add(ore);
91-
this.saveFile(json, file);
92-
93-
return;
94-
}
95-
}
96-
97-
JsonObject object = new JsonObject();
98-
99-
if (dimension != OreSpawn.API.dimensionWildcard()) {
100-
object.addProperty(dim, dimension);
101-
}
102-
103-
JsonArray array = new JsonArray();
104-
array.add(ore);
105-
object.add("ores", array);
106-
107-
this.saveFile(json, file);
108-
} catch (IOException e) {
109-
throw new CommandException("Something went wrong - "+e.getMessage());
110-
}
82+
this.putFile(file, ore, dimension);
11183

11284
player.sendStatusMessage(new TextComponentString("Added " + state.getBlock().getRegistryName().toString() + " to the json"), true);
11385
}
11486

115-
private void saveFile(JsonArray array, File file) throws CommandException {
116-
Gson gson = new GsonBuilder().setPrettyPrinting().create();
117-
String json = gson.toJson(array);
118-
119-
try {
120-
FileUtils.writeStringToFile(file, StringEscapeUtils.unescapeJson(json), Charsets.UTF_8);
121-
} catch (IOException e) {
122-
throw new CommandException("Something went wrong - "+e.getMessage());
123-
}
124-
}
87+
private void putFile(String file, JsonObject ore, int id) {
88+
DimensionBuilder db = OreSpawn.API.getLogic(file).newDimensionBuilder(id);
89+
SpawnBuilder sb = db.newSpawnBuilder(null);
90+
OreBuilder ob = sb.newOreBuilder();
91+
String b = ore.get(BLOCK).getAsString();
92+
ore.remove(BLOCK);
93+
String s = ore.get(STATE2).getAsString();
94+
ore.remove(STATE2);
95+
if( "normal".equals(s) ) {
96+
ob.setOre(b);
97+
} else {
98+
ob.setOre(b, s);
99+
}
100+
FeatureBuilder fb = sb.newFeatureBuilder("default");
101+
fb.setGenerator("default").setDefaultParameters().setParameters(ore);
102+
BiomeBuilder bb = sb.newBiomeBuilder();
103+
IBlockState rep = ReplacementsRegistry.getDimensionDefault(id);
104+
List<IBlockState> rl = new ArrayList<>();
105+
rl.add(rep);
106+
sb.create(bb, fb, rl, ob);
107+
db.create(sb);
108+
OreSpawn.API.getLogic(file).create(db);
109+
OreSpawn.API.registerLogic(OreSpawn.API.getLogic(file));
110+
}
125111

126112
@Override
127113
public int compareTo(ICommand command) {

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,17 @@
2525
import net.minecraft.crash.CrashReport;
2626

2727
public class FeatureRegistry {
28+
private static final String ORE_SPAWN_VERSION = "OreSpawn Version";
2829
private Map<String, IFeature> features;
2930
private Map<IFeature, String> featuresInverse;
30-
private static final String def = "default";
31+
private static final String DEF = "default";
3132

3233
public FeatureRegistry() {
3334
features = new HashMap<>();
3435
featuresInverse = new HashMap<>();
3536
IFeature defaultGen = new DefaultFeatureGenerator();
36-
features.put(def, defaultGen);
37-
featuresInverse.put(defaultGen, def);
37+
features.put(DEF, defaultGen);
38+
featuresInverse.put(defaultGen, DEF);
3839
}
3940

4041
public Map<String, IFeature> getFeatures() {
@@ -45,15 +46,15 @@ public String getFeatureName(IFeature feature) {
4546
if( this.hasFeature(feature) ) {
4647
return this.featuresInverse.get(feature);
4748
} else {
48-
return def;
49+
return DEF;
4950
}
5051
}
5152

5253
public IFeature getFeature(String name) {
5354
if( this.hasFeature(name) ) {
5455
return this.features.get(name);
5556
} else {
56-
return this.features.get(def);
57+
return this.features.get(DEF);
5758
}
5859
}
5960

@@ -75,12 +76,9 @@ public void addFeature(JsonObject entry) {
7576

7677
public void addFeature(String name, String className) {
7778
IFeature feature = getInstance(className);
78-
if( feature != null ) {
79-
// the feature might already be registered
80-
if( !features.containsKey(name) ) {
79+
if( feature != null && !features.containsKey(name) ) {
8180
features.put(name, feature);
8281
featuresInverse.put(feature, name);
83-
}
8482
}
8583
}
8684

@@ -94,7 +92,7 @@ private IFeature getInstance(String className) {
9492
feature = (IFeature)featureCons.newInstance();
9593
} catch(Exception e) {
9694
CrashReport report = CrashReport.makeCrashReport(e, "Failed to load and instantiate an instance of the feature generator named "+className+" that was specified as a feature generator");
97-
report.getCategory().addCrashSection("OreSpawn Version", Constants.VERSION);
95+
report.getCategory().addCrashSection(ORE_SPAWN_VERSION, Constants.VERSION);
9896
OreSpawn.LOGGER.info(report.getCompleteReport());
9997
return null;
10098
}
@@ -109,7 +107,7 @@ public void loadFeaturesFile(File file) {
109107
rawJson = FileUtils.readFileToString(file);
110108
} catch(IOException e) {
111109
CrashReport report = CrashReport.makeCrashReport(e, "Failed reading config " + file.getName());
112-
report.getCategory().addCrashSection("OreSpawn Version", Constants.VERSION);
110+
report.getCategory().addCrashSection(ORE_SPAWN_VERSION, Constants.VERSION);
113111
OreSpawn.LOGGER.info(report.getCompleteReport());
114112
return;
115113
}
@@ -140,7 +138,7 @@ public void writeFeatures(File file) {
140138
FileUtils.writeStringToFile(file, StringEscapeUtils.unescapeJson(json), Charsets.UTF_8);
141139
} catch (IOException e) {
142140
CrashReport report = CrashReport.makeCrashReport(e, "Failed writing config " + file.getName());
143-
report.getCategory().addCrashSection("OreSpawn Version", Constants.VERSION);
141+
report.getCategory().addCrashSection(ORE_SPAWN_VERSION, Constants.VERSION);
144142
OreSpawn.LOGGER.info(report.getCompleteReport());
145143
return;
146144
}

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

Lines changed: 0 additions & 41 deletions
This file was deleted.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public class VanillaOres implements IOreSpawnPlugin {
1010

1111
@Override
1212
public void register(OS3API apiInterface) {
13+
// nothing for us to do - all of our ores are in the
14+
// jar and the code handles that
1315
}
1416

1517
}

0 commit comments

Comments
 (0)