First pass on ISDU rewrite, uses RC's new DataAttachment system

This commit is contained in:
modmuss50 2019-08-13 15:53:50 +01:00
parent 5e33977d6a
commit 35bb2df90e
5 changed files with 97 additions and 116 deletions

View file

@ -35,6 +35,8 @@ import org.apache.logging.log4j.Logger;
import reborncore.common.config.Configuration;
import reborncore.common.recipes.RecipeCrafter;
import reborncore.common.util.Torus;
import reborncore.common.world.DataAttachment;
import techreborn.blockentity.storage.idsu.IDSUManager;
import techreborn.client.GuiHandler;
import techreborn.config.TechRebornConfig;
import techreborn.events.ModRegistry;
@ -44,6 +46,8 @@ import techreborn.packets.ServerboundPackets;
import techreborn.utils.BehaviorDispenseScrapbox;
import techreborn.world.WorldGenerator;
import java.util.function.Supplier;
public class TechReborn implements ModInitializer {
public static final String MOD_ID = "techreborn";
@ -83,6 +87,8 @@ public class TechReborn implements ModInitializer {
Torus.genSizeMap(TechRebornConfig.fusionControlComputerMaxCoilSize);
DataAttachment.REGISTRY.register(IDSUManager.class, IDSUManager::new);
LOGGER.info("TechReborn setup done!");
}

View file

@ -24,18 +24,79 @@
package techreborn.blockentity.storage.idsu;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.World;
import org.checkerframework.checker.nullness.qual.NonNull;
import reborncore.common.util.NBTSerializable;
import reborncore.common.world.DataAttachment;
import reborncore.common.world.DataAttachmentProvider;
public class IDSUManager {
public static IDataIDSU getData(World world) {
throw new UnsupportedOperationException("needs rewriting anyway");
// IDSUSaveManger instance = (IDSUSaveManger) storage.getOrLoadData(IDSUSaveManger.class, TechReborn.MOD_ID + "_IDSU");
//
// if (instance == null) {
// instance = new IDSUSaveManger(TechReborn.MOD_ID + "_IDSU");
// storage.setData(TechReborn.MOD_ID + "_IDSU", instance);
// }
// return instance;
import java.util.HashMap;
public class IDSUManager implements DataAttachment {
@NonNull
public static IDSUPlayer getPlayer(World world, String uuid){
return get(world).getPlayer(uuid);
}
public static IDSUManager get(World world){
return DataAttachmentProvider.get(world, IDSUManager.class);
}
private final HashMap<String, IDSUPlayer> playerHashMap = new HashMap<>();
@NonNull
public IDSUPlayer getPlayer(String uuid){
return playerHashMap.computeIfAbsent(uuid, s -> new IDSUPlayer());
}
@Override
public void read(@NonNull CompoundTag tag) {
for(String uuid : tag.getKeys()){
playerHashMap.put(uuid, new IDSUPlayer(tag.getCompound(uuid)));
}
}
@NonNull
@Override
public CompoundTag write() {
CompoundTag tag = new CompoundTag();
playerHashMap.forEach((uuid, player) -> tag.put(uuid, player.write()));
return tag;
}
public static class IDSUPlayer implements NBTSerializable {
private double energy;
private IDSUPlayer() {
}
private IDSUPlayer(CompoundTag compoundTag){
read(compoundTag);
}
@NonNull
@Override
public CompoundTag write() {
CompoundTag tag = new CompoundTag();
tag.putDouble("energy", energy);
return tag;
}
@Override
public void read(@NonNull CompoundTag tag) {
energy = tag.getDouble("energy");
}
public double getEnergy() {
return energy;
}
public void setEnergy(double energy) {
this.energy = energy;
}
}
}

View file

@ -1,66 +0,0 @@
/*
* This file is part of TechReborn, licensed under the MIT License (MIT).
*
* Copyright (c) 2018 TechReborn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package techreborn.blockentity.storage.idsu;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.PersistentState;
import techreborn.TechReborn;
/**
* Created by modmuss50 on 13/06/2017.
*/
public class IDSUSaveManger extends PersistentState implements IDataIDSU {
public IDSUSaveManger(String name) {
super(TechReborn.MOD_ID + "_IDSU");
}
@Override
public void fromTag(CompoundTag nbt) {
power = nbt.getDouble("power");
}
@Override
public CompoundTag toTag(CompoundTag compound) {
compound.putDouble("power", power);
return compound;
}
@Override
public boolean isDirty() {
return true;
}
double power;
@Override
public double getStoredPower() {
return power;
}
@Override
public void setStoredPower(double storedPower) {
power = storedPower;
}
}

View file

@ -1,36 +0,0 @@
/*
* This file is part of TechReborn, licensed under the MIT License (MIT).
*
* Copyright (c) 2018 TechReborn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package techreborn.blockentity.storage.idsu;
/**
* Created by modmuss50 on 13/06/2017.
*/
public interface IDataIDSU {
public double getStoredPower();
public void setStoredPower(double storedPower);
}

View file

@ -40,6 +40,9 @@ public class InterdimensionalSUBlockEntity extends EnergyStorageBlockEntity impl
public String ownerUdid;
//This is the energy value that is synced to the client
private double clientEnergy;
public InterdimensionalSUBlockEntity() {
super(TRBlockEntities.INTERDIMENSIONAL_SU, "IDSU", 2, TRContent.Machine.INTERDIMENSIONAL_SU.block, EnumPowerTier.EXTREME, TechRebornConfig.idsuMaxInput, TechRebornConfig.idsuMaxOutput, TechRebornConfig.idsuMaxEnergy);
}
@ -49,7 +52,10 @@ public class InterdimensionalSUBlockEntity extends EnergyStorageBlockEntity impl
if (ownerUdid == null || ownerUdid.isEmpty()) {
return 0.0;
}
return IDSUManager.getData(world).getStoredPower();
if(world.isClient){
return clientEnergy;
}
return IDSUManager.getPlayer(world, ownerUdid).getEnergy();
}
@Override
@ -57,7 +63,11 @@ public class InterdimensionalSUBlockEntity extends EnergyStorageBlockEntity impl
if (ownerUdid == null || ownerUdid.isEmpty()) {
return;
}
IDSUManager.getData(world).setStoredPower(energy);
if(world.isClient){
clientEnergy = energy;
} else {
IDSUManager.getPlayer(world, ownerUdid).setEnergy(energy);
}
}
@Override
@ -65,7 +75,10 @@ public class InterdimensionalSUBlockEntity extends EnergyStorageBlockEntity impl
if (ownerUdid == null || ownerUdid.isEmpty()) {
return 0.0;
}
double energy = IDSUManager.getData(world).getStoredPower();
if(world.isClient){
throw new UnsupportedOperationException("cannot set energy on the client!");
}
double energy = IDSUManager.getPlayer(world, ownerUdid).getEnergy();
if (extract > energy) {
extract = energy;
}
@ -80,7 +93,10 @@ public class InterdimensionalSUBlockEntity extends EnergyStorageBlockEntity impl
if (ownerUdid == null || ownerUdid.isEmpty()) {
return false;
}
return input <= IDSUManager.getData(world).getStoredPower();
if(world.isClient){
throw new UnsupportedOperationException("cannot set energy on the client!");
}
return input <= IDSUManager.getPlayer(world, ownerUdid).getEnergy();
}
@Override