Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/main/java/net/kaupenjoe/tutorialmod/thirst/PlayerThirst.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package net.kaupenjoe.tutorialmod.thirst; | ||
|
||
import net.minecraft.nbt.CompoundTag; | ||
|
||
public class PlayerThirst { | ||
private int thirst; | ||
private final int MIN_THIRST = 0; | ||
private final int MAX_THIRST = 10; | ||
|
||
public int getThirst() { | ||
return thirst; | ||
} | ||
|
||
public void addThirst(int add) { | ||
this.thirst = Math.min(thirst + add, MAX_THIRST); | ||
} | ||
|
||
public void subThirst(int sub) { | ||
this.thirst = Math.max(thirst - sub, MIN_THIRST); | ||
} | ||
|
||
public void copyFrom(PlayerThirst source) { | ||
this.thirst = source.thirst; | ||
} | ||
|
||
public void saveNBTData(CompoundTag nbt) { | ||
nbt.putInt("thirst", thirst); | ||
} | ||
|
||
public void loadNBTData(CompoundTag nbt) { | ||
thirst = nbt.getInt("thirst"); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/net/kaupenjoe/tutorialmod/thirst/PlayerThirstProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package net.kaupenjoe.tutorialmod.thirst; | ||
|
||
import net.minecraft.core.Direction; | ||
import net.minecraft.nbt.CompoundTag; | ||
import net.minecraftforge.common.capabilities.Capability; | ||
import net.minecraftforge.common.capabilities.CapabilityManager; | ||
import net.minecraftforge.common.capabilities.CapabilityToken; | ||
import net.minecraftforge.common.capabilities.ICapabilityProvider; | ||
import net.minecraftforge.common.util.INBTSerializable; | ||
import net.minecraftforge.common.util.LazyOptional; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public class PlayerThirstProvider implements ICapabilityProvider, INBTSerializable<CompoundTag> { | ||
public static Capability<PlayerThirst> PLAYER_THIRST = CapabilityManager.get(new CapabilityToken<PlayerThirst>() { }); | ||
|
||
private PlayerThirst thirst = null; | ||
private final LazyOptional<PlayerThirst> optional = LazyOptional.of(this::createPlayerThirst); | ||
|
||
private PlayerThirst createPlayerThirst() { | ||
if(this.thirst == null) { | ||
this.thirst = new PlayerThirst(); | ||
} | ||
|
||
return this.thirst; | ||
} | ||
|
||
@Override | ||
public @NotNull <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) { | ||
if(cap == PLAYER_THIRST) { | ||
return optional.cast(); | ||
} | ||
|
||
return LazyOptional.empty(); | ||
} | ||
|
||
@Override | ||
public CompoundTag serializeNBT() { | ||
CompoundTag nbt = new CompoundTag(); | ||
createPlayerThirst().saveNBTData(nbt); | ||
return nbt; | ||
} | ||
|
||
@Override | ||
public void deserializeNBT(CompoundTag nbt) { | ||
createPlayerThirst().loadNBTData(nbt); | ||
} | ||
} |