Jump to content

Recommended Posts

Posted

I'm having some trouble with my mod. what I'm trying to do is make it so that you have the ability to kill everything within 100 blocks that's not the issue though, the issue is after you die the effect sticks around in the world and follows you around so every 15 seconds you die can anyone help with this, it is in 1.18. here's my code

package com.projectmushroom.lavapotions.client;

import java.util.Random;
import java.util.UUID;

import com.projectmushroom.lavapotions.effect.LavaEffects;
import com.projectmushroom.lavapotions.effect.Thanatos;
import com.projectmushroom.lavapotions.init.BlockInit;
import com.projectmushroom.lavapotions.init.ItemInit;

import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.FlowerBlock;
import net.minecraft.world.level.block.GrassBlock;
import net.minecraft.world.level.block.LeavesBlock;
import net.minecraft.world.level.block.RotatedPillarBlock;
import net.minecraft.world.level.block.TallFlowerBlock;
import net.minecraft.world.level.block.TallGrassBlock;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.entity.living.PotionEvent.PotionAddedEvent;
import net.minecraftforge.event.entity.living.PotionEvent.PotionExpiryEvent;
import net.minecraftforge.event.entity.living.PotionEvent.PotionRemoveEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;

public class ThanatosStartEvent
{
    boolean dead = false;
    
    Random rnd = new Random();
    
    AttributeModifier thanatos = new AttributeModifier(UUID.fromString("b48ce840-2916-4c28-acab-f6693bf3cf58"),
            "thanatos", 5.0D, AttributeModifier.Operation.MULTIPLY_TOTAL);
    AttributeModifier thanatos_speed = new AttributeModifier(UUID.fromString("04ceb1a3-516f-4489-940f-24c8da10462c"),
            "thanatos_speed", -0.03D, AttributeModifier.Operation.ADDITION);
    int timer = 0;
    LivingEntity entity = null;
    
    @SubscribeEvent
    public void onThanatosStart (PotionAddedEvent event)
    {
        
        if(event.getPotionEffect().getEffect().equals(LavaEffects.THANATOS.get()))
        {
            entity = event.getEntityLiving();
            if(!event.getEntityLiving().getAttribute(Attributes.MAX_HEALTH).hasModifier(thanatos))
            {
                event.getEntityLiving().getAttribute(Attributes.MAX_HEALTH).addTransientModifier(thanatos);
                event.getEntityLiving().heal(100);
            }
            if(!event.getEntityLiving().getAttribute(Attributes.MOVEMENT_SPEED).hasModifier(thanatos_speed))
            {
                event.getEntityLiving().getAttribute(Attributes.MOVEMENT_SPEED).addTransientModifier(thanatos_speed);
            }
            if(event.getEntityLiving() instanceof Player)
            {
                Player player = ((Player)event.getEntityLiving());
                player.getInventory().add(new ItemStack(ItemInit.SCYTHE.get()));
                dead = false;
            }
        }
    }    
    
    @SubscribeEvent
    public void thanatosTick(TickEvent event)
    {
        if(entity != null)
        {
            timer += 1;
            if(!entity.hasEffect(LavaEffects.THANATOS.get()))
            {
                entity = null;
                dead = true;
            }
            if(timer >= 2400)
            {
                thanatosKill(entity);
                timer = 0;
            }
        }
    }
    
    public void thanatosKill(LivingEntity entity)
    {
        if(entity.hasEffect(LavaEffects.THANATOS.get()) && !dead)
        {
            int limit = 0 ;
            if(!entity.getAttribute(Attributes.MAX_HEALTH).hasModifier(thanatos))
            {
                entity.getAttribute(Attributes.MAX_HEALTH).addPermanentModifier(thanatos);
                entity.heal(100);
            }
            if(entity.getLevel() instanceof ServerLevel)
            {
                ServerLevel level = (ServerLevel) entity.getLevel();
                for(int i = -20; i <= 20; i+= 1)
                {
                    
                    for(int x = -20; x <= 5; x+= 1)
                    {
                        for(int z = -20; z <= 20; z+= 1)
                        {
                            Block block = level.getBlockState(new BlockPos(entity.getBlockX() - i, entity.getBlockY() - x, entity.getBlockZ() - z)).getBlock();
                            
                            if(block instanceof GrassBlock)
                            {
                                if (rnd.nextInt(2) == 0)
                                {
                                    level.setBlock(new BlockPos(entity.getBlockX() - i, entity.getBlockY() - x, entity.getBlockZ() - z), Blocks.COARSE_DIRT.defaultBlockState(), Block.UPDATE_ALL);
                                    System.out.println("there is a grass block at " + i + x + z);
                                } else {
                                    level.setBlock(new BlockPos(entity.getBlockX() - i, entity.getBlockY() - x, entity.getBlockZ() - z), Blocks.ROOTED_DIRT.defaultBlockState(), Block.UPDATE_ALL);
                                    System.out.println("there is a grass block at " + i + x + z);
                                }
                            }
                            if(block instanceof LeavesBlock || block instanceof TallGrassBlock)
                            {
                                level.setBlock(new BlockPos(entity.getBlockX() - i, entity.getBlockY() - x, entity.getBlockZ() - z), Blocks.AIR.defaultBlockState(), Block.UPDATE_ALL);
                                System.out.println("there is a leaf or tall grass block at " + i + x + z);
                            }
                            if(block instanceof FlowerBlock || block instanceof TallFlowerBlock )
                            {
                                level.setBlock(new BlockPos(entity.getBlockX() - i, entity.getBlockY() - x, entity.getBlockZ() - z), Blocks.WITHER_ROSE.defaultBlockState(), Block.UPDATE_ALL);
                                System.out.println("there is a flower at " + i + x + z);
                            }
                            if(block instanceof RotatedPillarBlock )
                            {
                                RotatedPillarBlock wood = ((RotatedPillarBlock)block);
                                if(wood == Blocks.OAK_LOG)
                                {
                                    level.setBlock(new BlockPos(entity.getBlockX() - i, entity.getBlockY() - x, entity.getBlockZ() - z), BlockInit.DEAD_OAK_LOG.get().defaultBlockState(),
                                    Block.UPDATE_ALL);
                                    System.out.println("there is a peice of wood at " + i + x + z);
                                }
                                if(wood == Blocks.BIRCH_LOG)
                                {
                                    level.setBlock(new BlockPos(entity.getBlockX() - i, entity.getBlockY() - x, entity.getBlockZ() - z), Blocks.STRIPPED_BIRCH_LOG.defaultBlockState(),
                                    Block.UPDATE_ALL);
                                    System.out.println("there is a peice of wood at " + i + x + z);
                                }
                                if(wood == Blocks.ACACIA_LOG)
                                {
                                    level.setBlock(new BlockPos(entity.getBlockX() - i, entity.getBlockY() - x, entity.getBlockZ() - z), Blocks.STRIPPED_ACACIA_LOG.defaultBlockState(),
                                    Block.UPDATE_ALL);
                                    System.out.println("there is a peice of wood at " + i + x + z);
                                }
                                if(wood == Blocks.DARK_OAK_LOG)
                                {
                                    level.setBlock(new BlockPos(entity.getBlockX() - i, entity.getBlockY() - x, entity.getBlockZ() - z), Blocks.STRIPPED_DARK_OAK_LOG.defaultBlockState(),
                                    Block.UPDATE_ALL);
                                    System.out.println("there is a peice of wood at " + i + x + z);
                                }
                                if(wood == Blocks.JUNGLE_LOG)
                                {
                                    level.setBlock(new BlockPos(entity.getBlockX() - i, entity.getBlockY() - x, entity.getBlockZ() - z), Blocks.STRIPPED_JUNGLE_LOG.defaultBlockState(),
                                    Block.UPDATE_ALL);
                                    System.out.println("there is a peice of wood at " + i + x + z);
                                }
                                if(wood == Blocks.SPRUCE_LOG)
                                {
                                    level.setBlock(new BlockPos(entity.getBlockX() - i, entity.getBlockY() - x, entity.getBlockZ() - z), Blocks.STRIPPED_SPRUCE_LOG.defaultBlockState(),
                                    Block.UPDATE_ALL);
                                    System.out.println("there is a peice of wood at " + i + x + z);
                                }
                            }
                            
                            
                        }
                    }
                }
                
                for(int i = 0; i < 100; i+= 5)
                {
                
                    
                    for (Entity entities : level.getAllEntities())
                    {
                        if(!(entities == entity) && entities instanceof LivingEntity)
                        {
                            LivingEntity target = ((LivingEntity)entities);
                            
                            if((!(target.getBlockX() > (entity.getBlockX() + i)) && !(target.getBlockX() < (entity.getBlockX() - i)))
                                    && (!(target.getBlockY() > (entity.getBlockY() + i)) && !(target.getBlockY() < (entity.getBlockY() - i)))
                                    && (!(target.getBlockZ() > (entity.getBlockZ() + i)) && !(target.getBlockZ() < (entity.getBlockZ() - i)))
                                    && limit <= 200)
                            {
                                if(target.hasEffect(LavaEffects.INVINC.get()))
                                {
                                    target.removeEffect(LavaEffects.INVINC.get());
                                }
                                if(target.hasEffect(MobEffects.DAMAGE_RESISTANCE))
                                {
                                    target.removeEffect(MobEffects.DAMAGE_RESISTANCE);
                                }
                                target.hurt(new DamageSource("Devil"), 20000);
                                System.out.println(i + " Killed " + target);
                                limit += 1;
                                
                            }
                        }
                    }
                }
                System.out.println("total killed " + limit);
            }
        }
    }
    @SubscribeEvent
    public void onThanatosEnd (PotionRemoveEvent event)
    {
        
        if(event.getPotionEffect().getEffect().equals(LavaEffects.THANATOS.get()))
        {
            dead = true;
            if(event.getEntityLiving().getAttribute(Attributes.MAX_HEALTH).hasModifier(thanatos))
            {
                event.getEntityLiving().getAttribute(Attributes.MAX_HEALTH).removeModifier(thanatos);
            }
            if(event.getEntityLiving().getAttribute(Attributes.MOVEMENT_SPEED).hasModifier(thanatos_speed))
            {
                event.getEntityLiving().getAttribute(Attributes.MOVEMENT_SPEED).removeModifier(thanatos_speed);
            }
            if(event.getEntityLiving() instanceof Player)
            {
                Player player = ((Player)event.getEntityLiving());
                player.getInventory().removeItem(new ItemStack(ItemInit.SCYTHE.get()));
            }
        }
    }
//    @SubscribeEvent
//    public void onThanatosDeath (PotionExpiryEvent event)
//    {
//        LivingEntity target = ((LivingEntity)event.getEntityLiving());
//        if(target.hasEffect(LavaEffects.THANATOS.get()))
//        {
//            target.removeEffect(LavaEffects.THANATOS.get());
//        }
//    }
}

Posted (edited)

You are using TickEvent wrong.

https://github.com/MinecraftForge/MinecraftForge/blob/1.19.x/src/main/java/net/minecraftforge/event/TickEvent.java#L21

NOTE: how it has different subclasses for what is ticking. You are responding to every event on both the client and server (the side).

Also it has a "Phase" to say whether it is start or end of the tick. If you don't check that your code will run twice per tick.

 

Finally, using static fields to store shared data (your timer field) is also wrong.

You need to store the value against whatever you intend to tick, I would guess the player?

e.g. using a capability. https://forge.gemwire.uk/wiki/Capabilities,

but in your use case you can probably just check the player's mob effects?

 

That way when the player dies it will stop ticking and you can also handle more than one player ticking with your effect.

Edited by warjort

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.