Skip to content

Commit

Permalink
player capability
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaupenjoe committed Jul 13, 2022
1 parent 4830d1d commit d82cd96
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/main/java/net/kaupenjoe/tutorialmod/event/ModEvents.java
Expand Up @@ -3,14 +3,25 @@
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import net.kaupenjoe.tutorialmod.TutorialMod;
import net.kaupenjoe.tutorialmod.item.ModItems;
import net.kaupenjoe.tutorialmod.thirst.PlayerThirst;
import net.kaupenjoe.tutorialmod.thirst.PlayerThirstProvider;
import net.kaupenjoe.tutorialmod.villager.ModVillagers;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.npc.VillagerProfession;
import net.minecraft.world.entity.npc.VillagerTrades;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.trading.MerchantOffer;
import net.minecraftforge.common.capabilities.RegisterCapabilitiesEvent;
import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.event.village.VillagerTradesEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.LogicalSide;
import net.minecraftforge.fml.common.Mod;

import java.util.List;
Expand Down Expand Up @@ -39,4 +50,41 @@ public static void addCustomTrades(VillagerTradesEvent event) {
stack,10,8,0.02F));
}
}

@SubscribeEvent
public static void onAttachCapabilitiesPlayer(AttachCapabilitiesEvent<Entity> event) {
if(event.getObject() instanceof Player) {
if(!event.getObject().getCapability(PlayerThirstProvider.PLAYER_THIRST).isPresent()) {
event.addCapability(new ResourceLocation(TutorialMod.MOD_ID, "properties"), new PlayerThirstProvider());
}
}
}

@SubscribeEvent
public static void onPlayerCloned(PlayerEvent.Clone event) {
if(event.isWasDeath()) {
event.getOriginal().getCapability(PlayerThirstProvider.PLAYER_THIRST).ifPresent(oldStore -> {
event.getOriginal().getCapability(PlayerThirstProvider.PLAYER_THIRST).ifPresent(newStore -> {
newStore.copyFrom(oldStore);
});
});
}
}

@SubscribeEvent
public static void onRegisterCapabilities(RegisterCapabilitiesEvent event) {
event.register(PlayerThirst.class);
}

@SubscribeEvent
public static void onPlayerTick(TickEvent.PlayerTickEvent event) {
if(event.side == LogicalSide.SERVER) {
event.player.getCapability(PlayerThirstProvider.PLAYER_THIRST).ifPresent(thirst -> {
if(thirst.getThirst() > 0 && event.player.getRandom().nextFloat() < 0.005f) { // Once Every 10 Seconds on Avg
thirst.subThirst(1);
event.player.sendSystemMessage(Component.literal("Subtracted Thirst"));
}
});
}
}
}
@@ -1,5 +1,6 @@
package net.kaupenjoe.tutorialmod.networking.packet;

import net.kaupenjoe.tutorialmod.thirst.PlayerThirstProvider;
import net.minecraft.ChatFormatting;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
Expand Down Expand Up @@ -45,12 +46,22 @@ public boolean handle(Supplier<NetworkEvent.Context> supplier) {
0.5F, level.random.nextFloat() * 0.1F + 0.9F);

// increase the water level / thirst level of player
player.getCapability(PlayerThirstProvider.PLAYER_THIRST).ifPresent(thirst -> {
thirst.addThirst(1);
player.sendSystemMessage(Component.literal("Current Thirst " + thirst.getThirst())
.withStyle(ChatFormatting.AQUA));
});

// Output the current thirst level

} else {
// Notify the player that there is no water around!
player.sendSystemMessage(Component.translatable(MESSAGE_NO_WATER).withStyle(ChatFormatting.RED));
// Output the current thirst level
player.getCapability(PlayerThirstProvider.PLAYER_THIRST).ifPresent(thirst -> {
player.sendSystemMessage(Component.literal("Current Thirst " + thirst.getThirst())
.withStyle(ChatFormatting.AQUA));
});
}
});
return true;
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/net/kaupenjoe/tutorialmod/thirst/PlayerThirst.java
@@ -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");
}
}
@@ -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);
}
}

0 comments on commit d82cd96

Please sign in to comment.