Added frequency framework, should make creating frequency-based teleportation networks a breeze

This commit is contained in:
Aidan C. Brady 2015-03-09 23:55:32 -05:00
parent 6ebf226db0
commit 605532adab
3 changed files with 259 additions and 1 deletions

View file

@ -5,8 +5,8 @@ import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
import mekanism.common.frequency.FrequencyManager;
import mekanism.common.multiblock.MultiblockManager;
import net.minecraft.world.ChunkCoordIntPair;
import net.minecraft.world.World;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
@ -73,6 +73,11 @@ public class CommonWorldTickHandler
{
MultiblockManager.load(world);
}
if(!FrequencyManager.loaded)
{
FrequencyManager.load(world);
}
}
}

View file

@ -0,0 +1,58 @@
package mekanism.common.frequency;
import io.netty.buffer.ByteBuf;
import java.util.ArrayList;
import mekanism.common.PacketHandler;
import net.minecraft.nbt.NBTTagCompound;
public class Frequency
{
public String name;
public String owner;
public Frequency(String n, String o)
{
name = n;
owner = o;
}
public void write(NBTTagCompound nbtTags)
{
nbtTags.setString("name", name);
nbtTags.setString("owner", owner);
}
protected void read(NBTTagCompound nbtTags)
{
name = nbtTags.getString("name");
owner = nbtTags.getString("owner");
}
public void write(ArrayList data)
{
data.add(name);
data.add(owner);
}
protected void read(ByteBuf dataStream)
{
name = PacketHandler.readString(dataStream);
owner = PacketHandler.readString(dataStream);
}
@Override
public int hashCode()
{
int code = 1;
code = 31 * code + name.hashCode();
return code;
}
@Override
public boolean equals(Object obj)
{
return obj instanceof Frequency && ((Frequency)obj).name.equals(name);
}
}

View file

@ -0,0 +1,195 @@
package mekanism.common.frequency;
import io.netty.buffer.ByteBuf;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.World;
import net.minecraft.world.WorldSavedData;
import net.minecraftforge.common.util.Constants.NBT;
public class FrequencyManager
{
public static boolean loaded;
private static Set<FrequencyManager> managers = new HashSet<FrequencyManager>();
private Set<Frequency> frequencies = new HashSet<Frequency>();
private FrequencyDataHandler dataHandler;
private String owner;
private Class<? extends Frequency> frequencyClass;
public FrequencyManager(Class c)
{
frequencyClass = c;
managers.add(this);
}
public FrequencyManager(Class c, String s)
{
this(c);
owner = s;
}
public static void load(World world)
{
loaded = true;
for(FrequencyManager manager : managers)
{
manager.createOrLoad(world);
}
}
public void createOrLoad(World world)
{
if(dataHandler == null)
{
dataHandler = (FrequencyDataHandler)world.perWorldStorage.loadData(FrequencyDataHandler.class, getName());
if(dataHandler == null)
{
dataHandler = new FrequencyDataHandler(getName());
dataHandler.setManager(this);
world.perWorldStorage.setData(getName(), dataHandler);
}
else {
dataHandler.setManager(this);
dataHandler.syncManager();
}
}
}
public Set<Frequency> getFrequencies()
{
return frequencies;
}
public void addFrequency(Frequency freq)
{
frequencies.add(freq);
}
public boolean containsFrequency(Frequency freq)
{
return frequencies.contains(freq);
}
public void writeFrequencies(ArrayList data)
{
data.add(frequencies.size());
for(Frequency freq : frequencies)
{
freq.write(data);
}
}
public Set<Frequency> readFrequencies(ByteBuf dataStream)
{
Set<Frequency> ret = new HashSet<Frequency>();
int size = dataStream.readInt();
try {
for(int i = 0; i < size; i++)
{
Frequency freq = frequencyClass.newInstance();
freq.read(dataStream);
ret.add(freq);
}
} catch(Exception e) {
e.printStackTrace();
}
return ret;
}
public String getName()
{
return owner != null ? owner + "FrequencyHandler" : "FrequencyHandler";
}
public static class FrequencyDataHandler extends WorldSavedData
{
public FrequencyManager manager;
public Set<Frequency> loadedFrequencies;
public FrequencyDataHandler(String tagName)
{
super(tagName);
}
public void setManager(FrequencyManager m)
{
manager = m;
}
public void syncManager()
{
if(loadedFrequencies != null)
{
manager.frequencies = loadedFrequencies;
}
}
@Override
public void readFromNBT(NBTTagCompound nbtTags)
{
try {
String frequencyClass = nbtTags.getString("frequencyClass");
if(nbtTags.hasKey("owner"))
{
manager.owner = nbtTags.getString("owner");
}
NBTTagList list = nbtTags.getTagList("invList", NBT.TAG_COMPOUND);
loadedFrequencies = new HashSet<Frequency>();
for(int i = 0; i < list.tagCount(); i++)
{
NBTTagCompound compound = list.getCompoundTagAt(i);
Frequency freq = (Frequency)Class.forName(frequencyClass).newInstance();
freq.read(compound);
loadedFrequencies.add(freq);
}
} catch(Exception e) {
e.printStackTrace();
}
}
@Override
public void writeToNBT(NBTTagCompound nbtTags)
{
nbtTags.setString("frequencyClass", manager.frequencyClass.getName());
if(manager.owner != null)
{
nbtTags.setString("owner", manager.owner);
}
NBTTagList list = new NBTTagList();
for(Frequency freq : manager.getFrequencies())
{
NBTTagCompound compound = new NBTTagCompound();
freq.write(compound);
list.appendTag(compound);
}
nbtTags.setTag("freqList", list);
}
}
}