Move idsu manger to use capability's
This commit is contained in:
parent
14e30481e5
commit
f90bb45351
6 changed files with 112 additions and 174 deletions
|
@ -112,6 +112,8 @@ public class Core {
|
|||
compatModule.preInit(event);
|
||||
}
|
||||
|
||||
IDSUManager.init();
|
||||
|
||||
//Ore Dictionary
|
||||
OreDict.init();
|
||||
proxy.preInit(event);
|
||||
|
@ -154,10 +156,7 @@ public class Core {
|
|||
|
||||
// Multiblock events
|
||||
MinecraftForge.EVENT_BUS.register(new MultiblockEventHandler());
|
||||
// IDSU manager
|
||||
IDSUManager.INSTANCE = new IDSUManager();
|
||||
// Event busses
|
||||
MinecraftForge.EVENT_BUS.register(IDSUManager.INSTANCE);
|
||||
MinecraftForge.EVENT_BUS.register(new MultiblockServerTickHandler());
|
||||
MinecraftForge.EVENT_BUS.register(new TRTickHandler());
|
||||
//MinecraftForge.EVENT_BUS.register(worldGen.retroGen);
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
package techreborn.tiles.idsu;
|
||||
|
||||
import net.minecraft.nbt.NBTBase;
|
||||
import net.minecraft.nbt.NBTTagDouble;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public class CapabilityIDSUStorage implements Capability.IStorage<IDataIDSU> {
|
||||
@Nullable
|
||||
@Override
|
||||
public NBTBase writeNBT(Capability<IDataIDSU> capability, IDataIDSU instance, EnumFacing side) {
|
||||
return new NBTTagDouble(instance.getStoredPower());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readNBT(Capability<IDataIDSU> capability, IDataIDSU instance, EnumFacing side, NBTBase nbt) {
|
||||
instance.setStoredPower(((NBTTagDouble)nbt).getDouble());
|
||||
}
|
||||
}
|
19
src/main/java/techreborn/tiles/idsu/IDSUEnergyStore.java
Normal file
19
src/main/java/techreborn/tiles/idsu/IDSUEnergyStore.java
Normal file
|
@ -0,0 +1,19 @@
|
|||
package techreborn.tiles.idsu;
|
||||
|
||||
/**
|
||||
* Created by modmuss50 on 13/06/2017.
|
||||
*/
|
||||
public class IDSUEnergyStore implements IDataIDSU {
|
||||
|
||||
double power;
|
||||
|
||||
@Override
|
||||
public double getStoredPower() {
|
||||
return power;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStoredPower(double storedPower) {
|
||||
power = storedPower;
|
||||
}
|
||||
}
|
|
@ -24,199 +24,86 @@
|
|||
|
||||
package techreborn.tiles.idsu;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.storage.ISaveHandler;
|
||||
import net.minecraftforge.event.world.WorldEvent;
|
||||
import net.minecraftforge.fml.common.eventhandler.EventPriority;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
import net.minecraftforge.common.capabilities.CapabilityInject;
|
||||
import net.minecraftforge.common.capabilities.CapabilityManager;
|
||||
import net.minecraftforge.common.capabilities.ICapabilityProvider;
|
||||
import net.minecraftforge.event.AttachCapabilitiesEvent;
|
||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.HashMap;
|
||||
import java.util.TreeMap;
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
public class IDSUManager {
|
||||
|
||||
public static final String savename = "idsu.json";
|
||||
public static IDSUManager INSTANCE;
|
||||
public HashMap<World, IDSUWorldSaveData> worldData = new HashMap<>();
|
||||
|
||||
@SubscribeEvent(priority = EventPriority.HIGHEST)
|
||||
public void worldSave(WorldEvent.Save event) {
|
||||
if (event.getWorld() != null && event.getWorld().getSaveHandler() != null
|
||||
&& event.getWorld().getSaveHandler().getWorldDirectory() != null) {
|
||||
if (worldData.containsKey(event.getWorld())) {
|
||||
worldData.get(event.getWorld()).save();
|
||||
}
|
||||
}
|
||||
@CapabilityInject(IDataIDSU.class)
|
||||
public static Capability<IDataIDSU> CAPABILITY_IDSU = null;
|
||||
|
||||
public static void init(){
|
||||
CapabilityManager.INSTANCE.register(IDataIDSU.class, new CapabilityIDSUStorage(), new Factory());
|
||||
MinecraftForge.EVENT_BUS.register(IDSUManager.class);
|
||||
}
|
||||
|
||||
@SubscribeEvent(priority = EventPriority.HIGHEST)
|
||||
public void worldLoad(WorldEvent.Load event) {
|
||||
if (event.getWorld() != null && event.getWorld().getSaveHandler() != null
|
||||
&& event.getWorld().getSaveHandler().getWorldDirectory() != null) {
|
||||
if (worldData.containsKey(event.getWorld())) {
|
||||
worldData.get(event.getWorld()).load();
|
||||
} else {
|
||||
IDSUWorldSaveData worldSaveData = new IDSUWorldSaveData(event.getWorld());
|
||||
worldData.put(event.getWorld(), worldSaveData);
|
||||
worldSaveData.load();
|
||||
}
|
||||
}
|
||||
@SubscribeEvent
|
||||
public static void Attach(AttachCapabilitiesEvent<World> event){
|
||||
event.addCapability(new ResourceLocation("techreborn","idsuManager"), new CapProvider(event.getObject()));
|
||||
}
|
||||
|
||||
@SubscribeEvent(priority = EventPriority.LOWEST)
|
||||
public void worldClosed(WorldEvent.Unload event) {
|
||||
if (event.getWorld() != null && event.getWorld().getSaveHandler() != null
|
||||
&& event.getWorld().getSaveHandler().getWorldDirectory() != null) {
|
||||
if (worldData.containsKey(event.getWorld())) {
|
||||
worldData.get(event.getWorld()).save();
|
||||
}
|
||||
}
|
||||
// this clears the data ready for a new world
|
||||
worldData.clear();
|
||||
}
|
||||
private static class CapProvider implements ICapabilityProvider {
|
||||
|
||||
public IDSUValueSaveData getSaveDataForWorld(World world, String channel) {
|
||||
if (worldData.containsKey(world)) {
|
||||
return worldData.get(world).getSaves(channel);
|
||||
} else {
|
||||
IDSUWorldSaveData worldSaveData = new IDSUWorldSaveData(world);
|
||||
worldData.put(world, worldSaveData);
|
||||
worldSaveData.load();
|
||||
return worldSaveData.getSaves(channel);
|
||||
}
|
||||
}
|
||||
World world;
|
||||
IDSUEnergyStore store;
|
||||
|
||||
public IDSUWorldSaveData getWorldDataFormWorld(World world) {
|
||||
if (worldData.containsKey(world)) {
|
||||
return worldData.get(world);
|
||||
} else {
|
||||
IDSUWorldSaveData worldSaveData = new IDSUWorldSaveData(world);
|
||||
worldData.put(world, worldSaveData);
|
||||
worldSaveData.load();
|
||||
return worldSaveData;
|
||||
}
|
||||
}
|
||||
|
||||
public void loadFromString(String json, World world) {
|
||||
if (json.equals("EMPTY")) {
|
||||
return;
|
||||
}
|
||||
IDSUWorldSaveData worldSaveData;
|
||||
if (worldData.containsKey(world)) {
|
||||
worldSaveData = worldData.get(world);
|
||||
} else {
|
||||
worldSaveData = new IDSUWorldSaveData(world);
|
||||
worldData.put(world, worldSaveData);
|
||||
}
|
||||
Gson gson = new Gson();
|
||||
Type typeOfHashMap = new TypeToken<TreeMap<Integer, IDSUValueSaveData>>() {
|
||||
}.getType();
|
||||
worldSaveData.idsuValues.clear();
|
||||
worldSaveData.idsuValues = gson.fromJson(json, typeOfHashMap);
|
||||
}
|
||||
|
||||
public class IDSUWorldSaveData {
|
||||
|
||||
public TreeMap<String, IDSUValueSaveData> idsuValues = new TreeMap<>();
|
||||
|
||||
public World world;
|
||||
|
||||
ISaveHandler saveHandler;
|
||||
|
||||
File folder;
|
||||
|
||||
File file;
|
||||
|
||||
public IDSUWorldSaveData(World world) {
|
||||
public CapProvider(World world) {
|
||||
this.world = world;
|
||||
this.saveHandler = world.getSaveHandler();
|
||||
folder = new File(saveHandler.getWorldDirectory(), "idsuData");
|
||||
file = new File(folder, savename);
|
||||
this.store = new IDSUEnergyStore();
|
||||
}
|
||||
|
||||
public IDSUValueSaveData getSaves(String udid) {
|
||||
if (udid == null) {
|
||||
return null;
|
||||
}
|
||||
if (idsuValues.containsKey(udid)) {
|
||||
return idsuValues.get(udid);
|
||||
} else {
|
||||
IDSUValueSaveData data = new IDSUValueSaveData();
|
||||
idsuValues.put(udid, data);
|
||||
return data;
|
||||
@Override
|
||||
public boolean hasCapability(
|
||||
@Nonnull
|
||||
Capability<?> capability,
|
||||
@Nullable
|
||||
EnumFacing facing) {
|
||||
if(capability == CAPABILITY_IDSU){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void load() {
|
||||
if (!file.exists()) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Gson gson = new Gson();
|
||||
BufferedReader reader = new BufferedReader(new FileReader(file));
|
||||
Type typeOfHashMap = new TypeToken<TreeMap<String, IDSUValueSaveData>>() {
|
||||
}.getType();
|
||||
idsuValues.clear();
|
||||
idsuValues = gson.fromJson(reader, typeOfHashMap);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
@Nullable
|
||||
@Override
|
||||
public <T> T getCapability(
|
||||
@Nonnull
|
||||
Capability<T> capability,
|
||||
@Nullable
|
||||
EnumFacing facing) {
|
||||
if(capability == CAPABILITY_IDSU){
|
||||
return (T) store;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void save() {
|
||||
if (idsuValues.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
if (!file.exists()) {
|
||||
if (!folder.exists()) {
|
||||
folder.mkdirs();
|
||||
}
|
||||
try {
|
||||
file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
String json = gson.toJson(idsuValues);
|
||||
try {
|
||||
FileWriter writer = new FileWriter(file);
|
||||
writer.write(json);
|
||||
writer.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class IDSUValueSaveData {
|
||||
private static class Factory implements Callable<IDataIDSU> {
|
||||
|
||||
public double storedPower = 0;
|
||||
|
||||
public IDSUValueSaveData(double storedPower) {
|
||||
this.storedPower = storedPower;
|
||||
}
|
||||
|
||||
public IDSUValueSaveData() {
|
||||
}
|
||||
|
||||
public double getStoredPower() {
|
||||
return storedPower;
|
||||
}
|
||||
|
||||
public void setStoredPower(double storedPower) {
|
||||
this.storedPower = storedPower;
|
||||
}
|
||||
|
||||
public void addEnergy(double storedPower) {
|
||||
this.storedPower += storedPower;
|
||||
@Override
|
||||
public IDataIDSU call() throws Exception {
|
||||
return new IDSUEnergyStore();
|
||||
}
|
||||
}
|
||||
|
||||
public static IDataIDSU getData(World world){
|
||||
if(world.hasCapability(CAPABILITY_IDSU, null)){
|
||||
return world.getCapability(CAPABILITY_IDSU, null);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
12
src/main/java/techreborn/tiles/idsu/IDataIDSU.java
Normal file
12
src/main/java/techreborn/tiles/idsu/IDataIDSU.java
Normal file
|
@ -0,0 +1,12 @@
|
|||
package techreborn.tiles.idsu;
|
||||
|
||||
/**
|
||||
* Created by modmuss50 on 13/06/2017.
|
||||
*/
|
||||
public interface IDataIDSU {
|
||||
|
||||
public double getStoredPower();
|
||||
|
||||
public void setStoredPower(double storedPower);
|
||||
|
||||
}
|
|
@ -58,7 +58,7 @@ public class TileIDSU extends TileEnergyStorage implements IContainerProvider {
|
|||
if (ownerUdid == null || ownerUdid.isEmpty()) {
|
||||
return 0.0;
|
||||
}
|
||||
return IDSUManager.INSTANCE.getSaveDataForWorld(world, ownerUdid).storedPower;
|
||||
return IDSUManager.getData(world).getStoredPower();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -66,7 +66,7 @@ public class TileIDSU extends TileEnergyStorage implements IContainerProvider {
|
|||
if (ownerUdid == null || ownerUdid.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
IDSUManager.INSTANCE.getSaveDataForWorld(world, ownerUdid).storedPower = energy;
|
||||
IDSUManager.getData(world).setStoredPower(energy);
|
||||
}
|
||||
|
||||
public void readFromNBT(NBTTagCompound nbttagcompound) {
|
||||
|
|
Loading…
Add table
Reference in a new issue