Advertisement
AssortedBrunoz

Main file

Dec 23rd, 2024
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.16 KB | None | 0 0
  1. package net.orion.stressedout;
  2.  
  3. import com.mojang.brigadier.CommandDispatcher;
  4. import net.minecraft.commands.CommandSourceStack;
  5. import net.minecraft.world.damagesource.DamageSource;
  6. import net.minecraft.world.entity.LivingEntity;
  7. import net.minecraft.world.entity.player.Player;
  8. import net.minecraftforge.common.MinecraftForge;
  9. import net.minecraftforge.event.TickEvent;
  10. import net.minecraftforge.event.entity.living.LivingDamageEvent;
  11. import net.minecraftforge.event.entity.player.PlayerEvent;
  12. import net.minecraftforge.event.server.ServerStartingEvent;
  13. import net.minecraftforge.eventbus.api.IEventBus;
  14. import net.minecraftforge.eventbus.api.SubscribeEvent;
  15. import net.minecraftforge.fml.common.Mod;
  16. import net.orion.stressedout.commands.GetStressCommand;
  17. import net.orion.stressedout.commands.SetStressCommand;
  18.  
  19. @Mod(StressedOutMod.MODID)
  20. public class StressedOutMod
  21. {
  22.     public static final String MODID = "stressedout";
  23.     public static final String STRESSNBTKEY = MODID + ":stress";
  24.     public static final String TIMERNBTKEY = MODID + ":timer";
  25.  
  26.     public StressedOutMod()
  27.     {
  28.         IEventBus forgeEventBus = MinecraftForge.EVENT_BUS;
  29.         //Script listeners
  30.         forgeEventBus.addListener(this::onServerStarting);
  31.         forgeEventBus.addListener(this::onPlayerClone);
  32.         forgeEventBus.addListener(this::onPlayerRespawn);
  33.         forgeEventBus.addListener(this::onPlayerDamage);
  34.         forgeEventBus.addListener(this::onPlayerTick);
  35.     }
  36.  
  37.     @SubscribeEvent
  38.     public void onServerStarting(ServerStartingEvent event)
  39.     {
  40.         //Register commands on server start
  41.         CommandDispatcher<CommandSourceStack> dispatcher = event.getServer().getCommands().getDispatcher();
  42.         GetStressCommand.register(dispatcher);
  43.         SetStressCommand.register(dispatcher);
  44.     }
  45.  
  46.     @SubscribeEvent
  47.     public void onPlayerDamage(LivingDamageEvent event) {
  48.         LivingEntity entity = event.getEntity();
  49.  
  50.         if (entity instanceof Player player) {
  51.  
  52.             DamageSource damageSource = event.getSource();
  53.             int stressValue = Math.max(0,  100 * (entity.getMaxHealth() == 0 ? 0 : ((int)event.getAmount() / (int)entity.getMaxHealth())));
  54.             if (damageSource.getEntity() instanceof Player)
  55.             {
  56.                 stressValue *= 1.5;
  57.                 System.out.println(entity.getDisplayName().getString() + " damaged");
  58.             }
  59.  
  60.             System.out.println("gained " + stressValue);
  61.             event.getEntity().getPersistentData().putInt(STRESSNBTKEY,event.getEntity().getPersistentData().getInt(STRESSNBTKEY) + stressValue);
  62.         }
  63.     }
  64.  
  65.     @SubscribeEvent
  66.     public void onPlayerTick(TickEvent.PlayerTickEvent event)
  67.     {
  68.         if (event.side.isClient()) {return;}
  69.  
  70.         int currentStress = event.player.getPersistentData().getInt(STRESSNBTKEY);
  71.         int currentTimer = event.player.getPersistentData().getInt(TIMERNBTKEY);
  72.  
  73.         if (currentTimer == 0) {
  74.  
  75.             if (currentStress > 0) {
  76.                 event.player.getPersistentData().putInt(STRESSNBTKEY, currentStress - 1);
  77.             }
  78.  
  79.             event.player.getPersistentData().putInt(TIMERNBTKEY,  40 );
  80.         }else{event.player.getPersistentData().putInt(TIMERNBTKEY, currentTimer - 1);}
  81.     }
  82.  
  83.     @SubscribeEvent
  84.     public void onPlayerClone(PlayerEvent.Clone event)
  85.     {
  86.         //Only fires when cloning so we do not need to worry about colliding with onPlayerRespawn
  87.         //Pass over Original's data to clone's
  88.         event.getEntity().getPersistentData().putInt(STRESSNBTKEY,
  89.                 event.getOriginal().getPersistentData().getInt(STRESSNBTKEY));
  90.     }
  91.  
  92.     @SubscribeEvent
  93.     public void onPlayerRespawn(PlayerEvent.PlayerRespawnEvent event)
  94.     {
  95.         // Check if player already has stress to avoid colliding with onPlayerClone
  96.         if (!event.getEntity().getPersistentData().contains(STRESSNBTKEY)){
  97.             //Add stress NBT if player has none
  98.             event.getEntity().getPersistentData().putInt(STRESSNBTKEY, 0);
  99.         }
  100.         //Add timer, resets on death 400 Ticks = 1 second
  101.         event.getEntity().getPersistentData().putInt(TIMERNBTKEY, 40);
  102.     }
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement