Last active
September 21, 2019 20:00
-
-
Save matyklug18/a12baa9980a6b5467be0439d40f14b88 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@SubscribeEvent | |
static void onWorldLoad(WorldEvent.Load e) { | |
Random rand = new Random(); | |
WorldDataTable table = WorldDataTable.get(e.getWorld(), true); | |
if(!table.didgen) { | |
table.list.add( | |
new OreData( | |
rand.nextInt(256), | |
rand.nextInt(256), | |
rand.nextInt(256), | |
(((float) rand.nextInt(5000)) / 1000f), | |
rand.nextInt(20), | |
rand.nextInt(50), | |
rand.nextInt(5), | |
rand.nextInt(20), | |
rand.nextInt(100000))); | |
table.didgen = true; | |
table.markDirty(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package matyk.aqarium2.oreData; | |
import org.json.simple.JSONArray; | |
import org.json.simple.JSONObject; | |
import org.json.simple.JSONValue; | |
import org.json.simple.parser.JSONParser; | |
import org.json.simple.parser.ParseException; | |
public class OreData { | |
public int red, green, blue; | |
public float hardness; | |
public int damage, miningSpeed, miningLevel, armor, durability; | |
public OreData() { | |
} | |
public OreData(int red, int green, int blue, float hardness, int damage, int miningSpeed, int miningLevel, int armor, int durability) { | |
this.red = red; | |
this.green = green; | |
this.blue = blue; | |
this.hardness = hardness; | |
this.damage = damage; | |
this.miningSpeed = miningSpeed; | |
this.miningLevel = miningLevel; | |
this.armor = armor; | |
this.durability = durability; | |
} | |
@Override | |
public String toString() { | |
JSONObject obj = new JSONObject(); | |
JSONArray colors = new JSONArray(); | |
colors.add(red); | |
colors.add(green); | |
colors.add(blue); | |
JSONArray stats = new JSONArray(); | |
stats.add(hardness); | |
stats.add(damage); | |
stats.add(miningSpeed); | |
stats.add(miningLevel); | |
stats.add(armor); | |
stats.add(durability); | |
obj.put("colors", colors); | |
obj.put("stats", stats); | |
return obj.toJSONString(); | |
} | |
public OreData readFromJson(String json) { | |
JSONParser parser = new JSONParser(); | |
JSONObject obj = null; | |
try { | |
obj = (JSONObject) parser.parse(json); | |
} catch (ParseException e) { | |
e.printStackTrace(); | |
} | |
JSONArray colors = (JSONArray) obj.get("colors"); | |
JSONArray stats = (JSONArray) obj.get("stats"); | |
this.red = (int) colors.get(0); | |
this.green = (int) colors.get(1); | |
this.blue = (int) colors.get(2); | |
this.hardness = (float) stats.get(0); | |
this.damage = (int) stats.get(1); | |
this.miningSpeed = (int) stats.get(2); | |
this.miningLevel = (int) stats.get(3); | |
this.armor = (int) stats.get(4); | |
this.durability = (int) stats.get(5); | |
return this; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package matyk.aqarium2.worldData; | |
import java.util.ArrayList; | |
import matyk.aqarium2.main.Main; | |
import matyk.aqarium2.oreData.OreData; | |
import net.minecraft.nbt.NBTTagCompound; | |
import net.minecraft.world.World; | |
import net.minecraft.world.storage.MapStorage; | |
import net.minecraft.world.storage.WorldSavedData; | |
public class WorldDataTable extends WorldSavedData { | |
public boolean didgen; | |
private static final String DATA_NAME = Main.MODID + "_TableData"; | |
public ArrayList<OreData> list = new ArrayList<OreData>(); | |
// Required constructors | |
public WorldDataTable() { | |
super(DATA_NAME); | |
} | |
public WorldDataTable(String s) { | |
super(s); | |
} | |
@Override | |
public void readFromNBT(NBTTagCompound cmpd) { | |
didgen = cmpd.getBoolean("didGen"); | |
int size = cmpd.getInteger("dataSize"); | |
for(int i = 0; i < size; i++) { | |
list.add(new OreData().readFromJson(cmpd.getString("data_" + i))); | |
} | |
markDirty(); | |
} | |
@Override | |
public NBTTagCompound writeToNBT(NBTTagCompound cmpd) { | |
for(int i = 0; i < list.size(); i ++) | |
cmpd.setString("data_" + i, list.get(i).toString()); | |
cmpd.setInteger("dataSize", list.size()); | |
cmpd.setBoolean("didGen", didgen); | |
return cmpd; | |
} | |
public static WorldDataTable get(World world, boolean IS_GLOBAL) { | |
MapStorage storage = IS_GLOBAL ? world.getMapStorage() : world.getPerWorldStorage(); | |
WorldDataTable instance = (WorldDataTable) storage.getOrLoadData(WorldDataTable.class, DATA_NAME); | |
if (instance == null) { | |
instance = new WorldDataTable(); | |
storage.setData(DATA_NAME, instance); | |
} | |
if(instance.didgen != true) { | |
Random rand = new Random(); | |
instance.list.add( | |
new OreData( | |
rand.nextInt(256), | |
rand.nextInt(256), | |
rand.nextInt(256), | |
(((float) rand.nextInt(5000)) / 1000f), | |
rand.nextInt(20), | |
rand.nextInt(50), | |
rand.nextInt(5), | |
rand.nextInt(20), | |
rand.nextInt(100000))); | |
instance.didgen = true; | |
instance.markDirty(); | |
} | |
return instance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment