Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.orion.stressedout;
- import com.mojang.brigadier.CommandDispatcher;
- import net.minecraft.commands.CommandSourceStack;
- import net.minecraft.world.damagesource.DamageSource;
- import net.minecraft.world.entity.LivingEntity;
- import net.minecraft.world.entity.player.Player;
- import net.minecraftforge.common.MinecraftForge;
- import net.minecraftforge.event.TickEvent;
- import net.minecraftforge.event.entity.living.LivingDamageEvent;
- import net.minecraftforge.event.entity.player.PlayerEvent;
- import net.minecraftforge.event.server.ServerStartingEvent;
- import net.minecraftforge.eventbus.api.IEventBus;
- import net.minecraftforge.eventbus.api.SubscribeEvent;
- import net.minecraftforge.fml.common.Mod;
- import net.orion.stressedout.commands.GetStressCommand;
- import net.orion.stressedout.commands.SetStressCommand;
- @Mod(StressedOutMod.MODID)
- public class StressedOutMod
- {
- public static final String MODID = "stressedout";
- public static final String STRESSNBTKEY = MODID + ":stress";
- public static final String TIMERNBTKEY = MODID + ":timer";
- public StressedOutMod()
- {
- IEventBus forgeEventBus = MinecraftForge.EVENT_BUS;
- //Script listeners
- forgeEventBus.addListener(this::onServerStarting);
- forgeEventBus.addListener(this::onPlayerClone);
- forgeEventBus.addListener(this::onPlayerRespawn);
- forgeEventBus.addListener(this::onPlayerDamage);
- forgeEventBus.addListener(this::onPlayerTick);
- }
- @SubscribeEvent
- public void onServerStarting(ServerStartingEvent event)
- {
- //Register commands on server start
- CommandDispatcher<CommandSourceStack> dispatcher = event.getServer().getCommands().getDispatcher();
- GetStressCommand.register(dispatcher);
- SetStressCommand.register(dispatcher);
- }
- @SubscribeEvent
- public void onPlayerDamage(LivingDamageEvent event) {
- LivingEntity entity = event.getEntity();
- if (entity instanceof Player player) {
- DamageSource damageSource = event.getSource();
- int stressValue = Math.max(0, 100 * (entity.getMaxHealth() == 0 ? 0 : ((int)event.getAmount() / (int)entity.getMaxHealth())));
- if (damageSource.getEntity() instanceof Player)
- {
- stressValue *= 1.5;
- System.out.println(entity.getDisplayName().getString() + " damaged");
- }
- System.out.println("gained " + stressValue);
- event.getEntity().getPersistentData().putInt(STRESSNBTKEY,event.getEntity().getPersistentData().getInt(STRESSNBTKEY) + stressValue);
- }
- }
- @SubscribeEvent
- public void onPlayerTick(TickEvent.PlayerTickEvent event)
- {
- if (event.side.isClient()) {return;}
- int currentStress = event.player.getPersistentData().getInt(STRESSNBTKEY);
- int currentTimer = event.player.getPersistentData().getInt(TIMERNBTKEY);
- if (currentTimer == 0) {
- if (currentStress > 0) {
- event.player.getPersistentData().putInt(STRESSNBTKEY, currentStress - 1);
- }
- event.player.getPersistentData().putInt(TIMERNBTKEY, 40 );
- }else{event.player.getPersistentData().putInt(TIMERNBTKEY, currentTimer - 1);}
- }
- @SubscribeEvent
- public void onPlayerClone(PlayerEvent.Clone event)
- {
- //Only fires when cloning so we do not need to worry about colliding with onPlayerRespawn
- //Pass over Original's data to clone's
- event.getEntity().getPersistentData().putInt(STRESSNBTKEY,
- event.getOriginal().getPersistentData().getInt(STRESSNBTKEY));
- }
- @SubscribeEvent
- public void onPlayerRespawn(PlayerEvent.PlayerRespawnEvent event)
- {
- // Check if player already has stress to avoid colliding with onPlayerClone
- if (!event.getEntity().getPersistentData().contains(STRESSNBTKEY)){
- //Add stress NBT if player has none
- event.getEntity().getPersistentData().putInt(STRESSNBTKEY, 0);
- }
- //Add timer, resets on death 400 Ticks = 1 second
- event.getEntity().getPersistentData().putInt(TIMERNBTKEY, 40);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement