Skip to content

Commit

Permalink
Merge pull request #16 from vrglab/dev/1.19.2
Browse files Browse the repository at this point in the history
Dev/1.19.2
  • Loading branch information
vrglab authored Jun 19, 2024
2 parents f01ed1d + b63274f commit 1bedf53
Show file tree
Hide file tree
Showing 37 changed files with 1,505 additions and 328 deletions.
1 change: 1 addition & 0 deletions 1.19.2/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ architectury {
allprojects {
group = rootProject.maven_group
version = "$project.name-$rootProject.mod_version"

}

subprojects {
Expand Down
221 changes: 221 additions & 0 deletions 1.19.2/common/src/main/java/org/Vrglab/EnergySystem/EnergyStorage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
package org.Vrglab.EnergySystem;

import net.minecraft.block.entity.BlockEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World;
import org.Vrglab.Modloader.CreationHelpers.TypeTransformer;
import org.Vrglab.Modloader.Types.ICallBack;
import org.Vrglab.Modloader.Types.ICallBackVoidNoArg;
import org.Vrglab.Utils.VLModInfo;

import static org.Vrglab.EnergySystem.EnergyStorageUtils.hasExternalStorage;

public class EnergyStorage implements IEnergyContainer{

public static <T extends EnergyStorage> T createStorage(Class<T> clazz, long capacity) {
try {
return clazz.getConstructor(long.class).newInstance(capacity);
} catch (Exception e) {
throw new RuntimeException("Failed to create EnergyStorage instance", e);
}
}

public static <T extends EnergyStorage> T createStorage(Class<T> clazz, long capacity, long maxTransfer) {
try {
return clazz.getConstructor(long.class, long.class).newInstance(capacity, maxTransfer);
} catch (Exception e) {
throw new RuntimeException("Failed to create EnergyStorage instance", e);
}
}

public static <T extends EnergyStorage> T createStorage(Class<T> clazz, long capacity, long maxTransfer, long maxExtract) {
try {
return clazz.getConstructor(long.class, long.class, long.class).newInstance(capacity, maxTransfer, maxExtract);
} catch (Exception e) {
throw new RuntimeException("Failed to create EnergyStorage instance", e);
}
}

public static <T extends EnergyStorage> T createStorage(Class<T> clazz, long capacity, long maxTransfer, long maxExtract, long energy) {
try {
return clazz.getConstructor(long.class, long.class, long.class, long.class).newInstance(capacity, maxTransfer, maxExtract, energy);
} catch (Exception e) {
throw new RuntimeException("Failed to create EnergyStorage instance", e);
}
}

public static EnergyStorage createStorage(long capacity){
return createStorage(EnergyStorage.class, capacity);
}
public static EnergyStorage createStorage(long capacity, long maxTransfer){
return createStorage(EnergyStorage.class, capacity, maxTransfer);
}

public static EnergyStorage createStorage(long capacity, long maxTransfer, long maxExtract) {
return createStorage(EnergyStorage.class, capacity, maxTransfer, maxExtract);
}

public static EnergyStorage createStorage(long capacity, long maxTransfer, long maxExtract, long energy){
return createStorage(EnergyStorage.class, capacity, maxTransfer, maxExtract, energy);
}

public static IEnergyContainer getStorageInWorld(World world, BlockPos blockPos, Direction facing){
IEnergyContainer storage = null;
BlockEntity entity = world.getBlockEntity(blockPos.offset(facing));
if(containEnergyStorage(entity)) {
try {
if(entity != null && entity instanceof IEnergySupplier<?>) {
storage = ((IEnergySupplier<?>)entity).getEnergyStorage();
} else if((boolean)hasExternalStorage.accept(entity)){
if (EnergyStorageUtils.getCachedContainer(blockPos.offset(facing)) != null) {
try{
storage = EnergyStorageUtils.getCachedContainer(blockPos.offset(facing));
if(((BlockEntity)((EnergyStorage)storage).blockEntity).isRemoved()) {
EnergyStorageUtils.removeFromCache(blockPos.offset(facing));
throw new RuntimeException("Accessed Storage is removed");
}
} catch (Throwable t) {
storage = (EnergyStorage)EnergyStorageUtils.wrapExternalStorage.accept(world, blockPos, facing, entity);
EnergyStorageUtils.cacheEnergyContainer(blockPos.offset(facing), storage);
}
} else {
storage = (EnergyStorage)EnergyStorageUtils.wrapExternalStorage.accept(world, blockPos, facing, entity);
EnergyStorageUtils.cacheEnergyContainer(blockPos.offset(facing), storage);
}
}

} catch (Throwable t) {

}
}
return storage;
}

public static boolean containEnergyStorage(World world, BlockPos blockPos){
BlockEntity entity = world.getBlockEntity(blockPos);
return containEnergyStorage(entity);
}

public static boolean containEnergyStorage(BlockEntity entity){
if(entity != null && entity instanceof IEnergySupplier<?> || (boolean)hasExternalStorage.accept(entity)) {
try {
return true;
} catch (Throwable t) {

}
}
return false;
}


protected long energy;
protected long capacity;
protected long maxReceive;
protected long maxExtract;
public ICallBackVoidNoArg makeDirty;
protected Object actualEnergyInstance, blockEntity;

public EnergyStorage(long capacity) {
this(capacity, capacity, capacity, 0);
}

public EnergyStorage(long capacity, long maxTransfer) {
this(capacity, maxTransfer, maxTransfer, 0);
}

public EnergyStorage(long capacity, long maxReceive, long maxExtract) {
this(capacity, maxReceive, maxExtract, 0);
}

public EnergyStorage(long capacity, long maxReceive, long maxExtract, long energy) {
this.capacity = capacity;
this.maxReceive = maxReceive;
this.maxExtract = maxExtract;
this.energy = Math.max(0 , Math.min(capacity, energy));
this.actualEnergyInstance = EnergyStorageUtils.createStorageInstance.accept(capacity, maxReceive, maxExtract, energy, this, blockEntity);
}

public EnergyStorage(Object instance, long capacity, long maxReceive, long maxExtract, long energy) {
this(capacity, maxReceive, maxExtract, energy);
this.actualEnergyInstance = instance;
}

@Override
public long receiveEnergy(long maxReceive, boolean simulate) {
energy += Long.valueOf(EnergyStorageUtils.receiveEnergyInstance.accept(actualEnergyInstance, maxReceive, simulate).toString());
return energy;
}

@Override
public long extractEnergy(long maxExtract, boolean simulate) {
energy -= Long.valueOf(EnergyStorageUtils.extractEnergyInstance.accept(actualEnergyInstance, maxExtract, simulate).toString());
return energy;
}

/**
* Adds energy to the storage. Returns quantity of energy that was accepted.
*
* @param maxReceive Maximum amount of energy to be inserted.
* @return Amount of energy that was accepted by the storage.
*/
@Override
public long receiveEnergy(long maxReceive) {
return receiveEnergy(maxReceive, false);
}

/**
* Removes energy from the storage. Returns quantity of energy that was removed.
*
* @param maxExtract Maximum amount of energy to be extracted.
* @return Amount of energy that was (or would have been) extracted from the storage.
*/
@Override
public long extractEnergy(long maxExtract) {
return extractEnergy(maxExtract, false);
}

/**
* Set's the energy Storages block entity type
* <p> DO NOT USE {@link TypeTransformer#ObjectToType} ON THE GIVEN OBJECT</p>
* @param blockEntity the un-transformed object of the block entity
* @return The Energy storage reference
*/
public EnergyStorage setBlockEntityType(Object blockEntity) {
this.blockEntity = TypeTransformer.ObjectToType.accept(blockEntity);
return this;
}

public EnergyStorage setMakeDirtyFunction(ICallBackVoidNoArg makeDirty) {
this.makeDirty = makeDirty;
return this;
}

@Override
public long getEnergyStored() {
return energy;
}

@Override
public long getMaxEnergyStored() {
return capacity;
}

@Override
public boolean canExtract() {
return this.maxExtract > 0;
}

@Override
public boolean canReceive() {
return this.maxReceive > 0;
}

public boolean isEmpty() {
return (energy == 0);
}

public boolean atMaxCapacity(){
return (energy == capacity);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.Vrglab.EnergySystem;

import net.minecraft.block.entity.BlockEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World;
import org.Vrglab.Modloader.Types.ICallBack;
import org.Vrglab.Utils.VLModInfo;

import java.util.HashMap;
import java.util.Map;

public class EnergyStorageUtils {
public static ICallBack createStorageInstance, extractEnergyInstance, receiveEnergyInstance, wrapExternalStorage, hasExternalStorage;
private static Map<BlockPos, IEnergyContainer> CACHED_CONTAINER = new HashMap<>();

public static <T extends IEnergyContainer> T getCachedContainer(BlockPos pos){
return (T) CACHED_CONTAINER.get(pos);
}

public static <T extends IEnergyContainer> void cacheEnergyContainer(BlockPos pos, T container){
CACHED_CONTAINER.put(pos, container);
}

public static void removeFromCache(BlockPos pos) {
CACHED_CONTAINER.remove(pos);
}


public static boolean pushEnergyTo(BlockEntity self, World world, BlockPos blockPos, Direction dir, long amnt) {
if(EnergyStorage.containEnergyStorage(world, blockPos.offset(dir))) {
EnergyStorage storage = (EnergyStorage)EnergyStorage.getStorageInWorld(world, blockPos, dir);
EnergyStorage self_storage = ((IEnergySupplier)self).getEnergyStorage();
if(storage != null && (!self_storage.isEmpty() && !storage.atMaxCapacity())) {
storage.receiveEnergy(amnt);
self_storage.extractEnergy(amnt);
return true;
}
}
return false;
}

public static boolean pullEnergyFrom(BlockEntity self, World world, BlockPos blockPos, Direction dir, long amnt) {
if(EnergyStorage.containEnergyStorage(world, blockPos.offset(dir))) {
EnergyStorage storage = (EnergyStorage)EnergyStorage.getStorageInWorld(world, blockPos, dir);
EnergyStorage self_storage = ((IEnergySupplier)self).getEnergyStorage();
if(storage != null && (!self_storage.atMaxCapacity() && !storage.isEmpty())) {
storage.extractEnergy(amnt);
self_storage.receiveEnergy(amnt);
return true;
}
}
return false;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.Vrglab.EnergySystem;

import net.minecraft.nbt.NbtType;

public interface IEnergyContainer {
/**
* Adds energy to the storage. Returns quantity of energy that was accepted.
*
* @param maxReceive
* Maximum amount of energy to be inserted.
* @param simulate
* If TRUE, the insertion will only be simulated.
* @return Amount of energy that was (or would have been, if simulated) accepted by the storage.
*/
long receiveEnergy(long maxReceive, boolean simulate);

/**
* Removes energy from the storage. Returns quantity of energy that was removed.
*
* @param maxExtract
* Maximum amount of energy to be extracted.
* @param simulate
* If TRUE, the extraction will only be simulated.
* @return Amount of energy that was (or would have been, if simulated) extracted from the storage.
*/
long extractEnergy(long maxExtract, boolean simulate);

/**
* Adds energy to the storage. Returns quantity of energy that was accepted.
*
* @param maxReceive
* Maximum amount of energy to be inserted.
* @return Amount of energy that was accepted by the storage.
*/
long receiveEnergy(long maxReceive);

/**
* Removes energy from the storage. Returns quantity of energy that was removed.
*
* @param maxExtract
* Maximum amount of energy to be extracted.
* @return Amount of energy that was (or would have been) extracted from the storage.
*/
long extractEnergy(long maxExtract);

/**
* Returns the amount of energy currently stored.
*/
long getEnergyStored();

/**
* Returns the maximum amount of energy that can be stored.
*/
long getMaxEnergyStored();

/**
* Returns if this storage can have energy extracted.
* If this is false, then any calls to extractEnergy will return 0.
*/
boolean canExtract();

/**
* Used to determine if this storage can receive energy.
* If this is false, then any calls to receiveEnergy will return 0.
*/
boolean canReceive();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.Vrglab.EnergySystem;

public interface IEnergySupplier<T extends EnergyStorage> {
T getEnergyStorage();
}
Loading

0 comments on commit 1bedf53

Please sign in to comment.