Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit 04cbc4c

Browse files
authored
Add forge energy (#180)
* Add forge energy Pretty much exact from YarnForge, with formatting changes * Fix license * Update fabric.mod.json * Move energy to patchwork-capabilities * Remove patchwork-energy
1 parent ea0ad1b commit 04cbc4c

File tree

3 files changed

+232
-0
lines changed

3 files changed

+232
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.minecraftforge.energy;
21+
22+
import net.minecraftforge.common.capabilities.Capability;
23+
import net.minecraftforge.common.capabilities.Capability.IStorage;
24+
import net.minecraftforge.common.capabilities.CapabilityInject;
25+
import net.minecraftforge.common.capabilities.CapabilityManager;
26+
27+
import net.minecraft.nbt.IntTag;
28+
import net.minecraft.nbt.Tag;
29+
import net.minecraft.util.math.Direction;
30+
31+
public class CapabilityEnergy {
32+
@CapabilityInject(IEnergyStorage.class)
33+
public static Capability<IEnergyStorage> ENERGY = null;
34+
35+
public static void register() {
36+
CapabilityManager.INSTANCE.register(IEnergyStorage.class, new IStorage<IEnergyStorage>() {
37+
@Override
38+
public Tag writeNBT(Capability<IEnergyStorage> capability, IEnergyStorage instance, Direction side) {
39+
return new IntTag(instance.getEnergyStored());
40+
}
41+
42+
@Override
43+
public void readNBT(Capability<IEnergyStorage> capability, IEnergyStorage instance, Direction side, Tag nbt) {
44+
if (!(instance instanceof EnergyStorage)) {
45+
throw new IllegalArgumentException("Can not deserialize to an instance that isn't the default implementation");
46+
}
47+
48+
((EnergyStorage) instance).energy = ((IntTag) nbt).getInt();
49+
}
50+
},
51+
() -> new EnergyStorage(1000)
52+
);
53+
}
54+
}
55+
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.minecraftforge.energy;
21+
22+
/**
23+
* <p>Reference implementation of {@link IEnergyStorage}. Use/extend this or implement your own.</p>
24+
*
25+
* <p>Derived from the Redstone Flux power system designed by King Lemming and originally utilized in Thermal Expansion and related mods.
26+
* Created with consent and permission of King Lemming and Team CoFH. Released with permission under LGPL 2.1 when bundled with Forge.</p>
27+
*/
28+
public class EnergyStorage implements IEnergyStorage {
29+
protected int energy;
30+
protected int capacity;
31+
protected int maxReceive;
32+
protected int maxExtract;
33+
34+
public EnergyStorage(int capacity) {
35+
this(capacity, capacity, capacity, 0);
36+
}
37+
38+
public EnergyStorage(int capacity, int maxTransfer) {
39+
this(capacity, maxTransfer, maxTransfer, 0);
40+
}
41+
42+
public EnergyStorage(int capacity, int maxReceive, int maxExtract) {
43+
this(capacity, maxReceive, maxExtract, 0);
44+
}
45+
46+
public EnergyStorage(int capacity, int maxReceive, int maxExtract, int energy) {
47+
this.capacity = capacity;
48+
this.maxReceive = maxReceive;
49+
this.maxExtract = maxExtract;
50+
this.energy = Math.max(0, Math.min(capacity, energy));
51+
}
52+
53+
@Override
54+
public int receiveEnergy(int maxReceive, boolean simulate) {
55+
if (!canReceive()) {
56+
return 0;
57+
}
58+
59+
int energyReceived = Math.min(capacity - energy, Math.min(this.maxReceive, maxReceive));
60+
61+
if (!simulate) {
62+
energy += energyReceived;
63+
}
64+
65+
return energyReceived;
66+
}
67+
68+
@Override
69+
public int extractEnergy(int maxExtract, boolean simulate) {
70+
if (!canExtract()) {
71+
return 0;
72+
}
73+
74+
int energyExtracted = Math.min(energy, Math.min(this.maxExtract, maxExtract));
75+
76+
if (!simulate) {
77+
energy -= energyExtracted;
78+
}
79+
80+
return energyExtracted;
81+
}
82+
83+
@Override
84+
public int getEnergyStored() {
85+
return energy;
86+
}
87+
88+
@Override
89+
public int getMaxEnergyStored() {
90+
return capacity;
91+
}
92+
93+
@Override
94+
public boolean canExtract() {
95+
return this.maxExtract > 0;
96+
}
97+
98+
@Override
99+
public boolean canReceive() {
100+
return this.maxReceive > 0;
101+
}
102+
}
103+
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.minecraftforge.energy;
21+
22+
/**
23+
* <p>An energy storage is the unit of interaction with Energy inventories.</p>
24+
*
25+
* <p>A reference implementation can be found at {@link EnergyStorage}.</p>
26+
*
27+
* <p>Derived from the Redstone Flux power system designed by King Lemming and originally utilized in Thermal Expansion and related mods.
28+
* Created with consent and permission of King Lemming and Team CoFH. Released with permission under LGPL 2.1 when bundled with Forge.</p>
29+
*/
30+
public interface IEnergyStorage {
31+
/**
32+
* Adds energy to the storage. Returns quantity of energy that was accepted.
33+
*
34+
* @param maxReceive
35+
* Maximum amount of energy to be inserted.
36+
* @param simulate
37+
* If TRUE, the insertion will only be simulated.
38+
* @return Amount of energy that was (or would have been, if simulated) accepted by the storage.
39+
*/
40+
int receiveEnergy(int maxReceive, boolean simulate);
41+
42+
/**
43+
* Removes energy from the storage. Returns quantity of energy that was removed.
44+
*
45+
* @param maxExtract
46+
* Maximum amount of energy to be extracted.
47+
* @param simulate
48+
* If TRUE, the extraction will only be simulated.
49+
* @return Amount of energy that was (or would have been, if simulated) extracted from the storage.
50+
*/
51+
int extractEnergy(int maxExtract, boolean simulate);
52+
53+
/**
54+
* Returns the amount of energy currently stored.
55+
*/
56+
int getEnergyStored();
57+
58+
/**
59+
* Returns the maximum amount of energy that can be stored.
60+
*/
61+
int getMaxEnergyStored();
62+
63+
/**
64+
* Returns if this storage can have energy extracted.
65+
* If this is false, then any calls to extractEnergy will return 0.
66+
*/
67+
boolean canExtract();
68+
69+
/**
70+
* Used to determine if this storage can receive energy.
71+
* If this is false, then any calls to receiveEnergy will return 0.
72+
*/
73+
boolean canReceive();
74+
}

0 commit comments

Comments
 (0)