Jump to content

(Closed) 1.12.2 | How to get a tile entity to detect EntityLivingBase in a 5 block radius


Recommended Posts

Posted (edited)

As of current, I'm trying to get a block I'm working on to detect players within a 5 block radius of it, so it may apply certain effects. I am actually fairly new with Java, and I have a history of misinterpreting what people say, but I usually figure it out in the end. Often asking ends up pointless, since I figure it out before someone actually says something that works, but I feel like I might as well. Below is the code for the tile entity and the block:

 

Block:

Spoiler

package com.camellias.temporalmod.blocks;

import java.util.List;
import java.util.Random;

import javax.annotation.Nullable;

import com.camellias.temporalmod.init.ModBlocks;
import com.camellias.temporalmod.init.ModItems;
import com.camellias.temporalmod.tileentities.TileEntityTimeAnomaly;
import com.camellias.temporalmod.util.IHasModel;

import ca.weblite.objc.Client;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.MobEffects;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityBeacon;
import net.minecraft.util.EnumBlockRenderType;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.client.event.ClientChatEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class BlockTimeAnomaly extends Block implements IHasModel
{

	public BlockTimeAnomaly(Material materialIn, String name) 
	{
		super(materialIn);
		
		this.setUnlocalizedName(name);
		this.setRegistryName(name);
		
		ModBlocks.BLOCKS.add(this);
		ModItems.ITEMS.add(new ItemBlock(this).setRegistryName(this.getUnlocalizedName()));
	}
	
	@SideOnly(Side.CLIENT)
	public void randomDisplayTick(IBlockState stateIn, World worldIn, BlockPos pos, Random rand)
	{
		for (int i = 0; i < 4; ++i)
        {
            double d0 = (double)((float)pos.getX() + rand.nextFloat());
            double d1 = (double)((float)pos.getY() + rand.nextFloat());
            double d2 = (double)((float)pos.getZ() + rand.nextFloat());
            double d3 = ((double)rand.nextFloat() - 0.5D) * 0.5D;
            double d4 = ((double)rand.nextFloat() - 0.5D) * 0.5D;
            double d5 = ((double)rand.nextFloat() - 0.5D) * 0.5D;
            int j = rand.nextInt(2) * 2 - 1;

            if (worldIn.getBlockState(pos.west()).getBlock() != this && worldIn.getBlockState(pos.east()).getBlock() != this)
            {
                d0 = (double)pos.getX() + 0.5D + 0.25D * (double)j;
                d3 = (double)(rand.nextFloat() * 2.0F * (float)j);
            }
            else
            {
                d2 = (double)pos.getZ() + 0.5D + 0.25D * (double)j;
                d5 = (double)(rand.nextFloat() * 2.0F * (float)j);
            }

            worldIn.spawnParticle(EnumParticleTypes.END_ROD, d0, d1, d2, d3, d4, d5);
        }
	}
	
	public TileEntity createNewTileEntity(World worldIn, int meta)
    {
        return new TileEntityTimeAnomaly();
    }
	
	public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
    {
        super.onBlockPlacedBy(worldIn, pos, state, placer, stack);

        if (stack.hasDisplayName())
        {
            TileEntity tileentity = worldIn.getTileEntity(pos);

            if (tileentity instanceof TileEntityTimeAnomaly)
            {
                ((TileEntityTimeAnomaly)tileentity).setName(stack.getDisplayName());
            }
        }
    }
	
	public boolean isOpaqueCube(IBlockState state)
    {
        return false;
    }

    public boolean isFullCube(IBlockState state)
    {
        return false;
    }
    
    public EnumBlockRenderType getRenderType(IBlockState state)
    {
        return EnumBlockRenderType.INVISIBLE;
    }
	
	@Override
	public void registerModels() 
	{
		
	}
}

 

 

Tile Entity:

Spoiler

package com.camellias.temporalmod.tileentities;

import java.util.List;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.MobEffects;
import net.minecraft.potion.PotionEffect;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.AxisAlignedBB;

public class TileEntityTimeAnomaly extends TileEntity implements ITickable
{

	public void setName(String displayName) 
	{
		
	}
	
	private void addEffectsToPlayers()
    {
        if (!this.world.isRemote)
        {
            double d0 = (double)(10 + 10);
            int i = 20;

            int j = 1;
            int k = this.pos.getX();
            int l = this.pos.getY();
            int i1 = this.pos.getZ();
            AxisAlignedBB axisalignedbb = (new AxisAlignedBB((double)k, (double)l, (double)i1, (double)(k + 1), (double)(l + 1), (double)(i1 + 1))).grow(d0).expand(0.0D, (double)this.world.getHeight(), 0.0D);
            List<EntityPlayer> list = this.world.<EntityPlayer>getEntitiesWithinAABB(EntityPlayer.class, axisalignedbb);

            for (EntityPlayer entityplayer : list)
            {
                entityplayer.addPotionEffect(new PotionEffect(MobEffects.SLOWNESS, j, i, true, true));
                entityplayer.addPotionEffect(new PotionEffect(MobEffects.HUNGER, j, i, true, true));
                entityplayer.addPotionEffect(new PotionEffect(MobEffects.WITHER, j, i, true, true));
            }
        }
    }

	@Override
	public void update() 
	{
		if (this.world.getTotalWorldTime() % 80L == 0L)
        {
            this.updateAnom();
        }
	}

	private void updateAnom() 
	{
		if (this.world != null)
        {
            this.addEffectsToPlayers();
        }
	}
}

 

 

Edited by Daring Do
Issue resolved, no thanks to here

Gne9eza.png

 

"Be patient with me, because I'm an absolute moron half the time."

Posted

Ok, what's the problem?

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted
1 minute ago, Draco18s said:

Ok, what's the problem?

The problem is in the title. I don't know how to make it check for a player, or if it is, it's not applying the affects. If you're going to recommend I look at the beacon code, I've already done so. It's how I've learned to mod. If you're going to recommend a tutorial, it better be a damn good one, because all that exist right now are "here's how to make the most basics of basics".

 

I apologize for being short right now, and that I'm taking it out on you, but there's a reason I usually don't ask here.

Gne9eza.png

 

"Be patient with me, because I'm an absolute moron half the time."

Posted

Do you want to detect players or EntityLivingBase? Your post and your title disagree.

Second, what about your code doesn't work? Have you tried the debugger?

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted
1 hour ago, Draco18s said:

Do you want to detect players or EntityLivingBase? Your post and your title disagree.

Second, what about your code doesn't work? Have you tried the debugger?

Either works, because both affect players. As for what doesn't work, as far as I can tell, nothing fails to function. The issue is it's not trying to detect anything to place the potion effects onto, from what I can tell. Or maybe it is, and I'm just failing to spot it, despite going over it tens of times already. 

Gne9eza.png

 

"Be patient with me, because I'm an absolute moron half the time."

Posted
58 minutes ago, Daring Do said:

Have you tried the debugger?

 

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted
1 hour ago, diesieben07 said:

Your block does not declare that it has a tile entity. Your createNewTileEntity method is not called by anyone, it does not override anything and you don't call it, hence it does precisely nothing.

Use @Override if you intend to override something.

And there's me feeling like an idiot. I'm not too familiar with tile entities, and assumed it was called correctly. But nope. Thanks. I'll see if that fixes it ^^

Gne9eza.png

 

"Be patient with me, because I'm an absolute moron half the time."

Posted
2 hours ago, diesieben07 said:

Your block does not declare that it has a tile entity. Your createNewTileEntity method is not called by anyone, it does not override anything and you don't call it, hence it does precisely nothing.

Use @Override if you intend to override something.

Okay, it's overridden now, and apparently I had to specify that in IHasModel, but it still fails to apply the effects. Not even sure if it is recognizing that I have onBlockPlacedBy, to set the tile entity in the world, or if it isn't adding any kind of detection radius.

Gne9eza.png

 

"Be patient with me, because I'm an absolute moron half the time."

Posted (edited)
3 minutes ago, diesieben07 said:

You really don't know what you are doing, right? If you have an error on @Override, the fix is never to just apply one of the eclipse quickfixes (I know that's what you did!). In fact, "programming by eclipse quickfix" is a terrible habit and will almost never get you what you want. Though, in my defense, the eclipse fixes have worked before in the past.

Oh. And yes, I admit, I know next to nothing when it comes to tile entities. I've never had luck with them in the past, but unfortunately, these blocks require to be tile entities, unless I want to deal with other types of fuckery, or a lot of lag issues.

Edited by Daring Do

Gne9eza.png

 

"Be patient with me, because I'm an absolute moron half the time."

Posted
8 minutes ago, diesieben07 said:

This has nothing to do with tile entities. Knowing how method overrides work is basic Java. If you do not know basic Java, you cannot make a mod.

I know how @Override works. My issue is with the tile entity either not existing, or not functioning. I've made mods before, in the past. They weren't any more complex than this one. If they were 1.12, I'd likely have reviewed them to see what I was messing up on.

Gne9eza.png

 

"Be patient with me, because I'm an absolute moron half the time."

Posted
Just now, diesieben07 said:

Obviously not.

 

Seriously, it is not hard. You need to override two obvious methods from the block class.

Being onBlockPlacedBy and createNewTileEntity. But I went beyond that, when I update the mod in the future, and went ahead and applied it to everything, so I can figure out if something breaks. I haven't gotten adding the annotation to things down as a habit yet.

Gne9eza.png

 

"Be patient with me, because I'm an absolute moron half the time."

Posted (edited)
6 minutes ago, diesieben07 said:

Neither of those have anything to do with tile entities.

onBlockPlacedBy gets called when a block gets placed down (duh) and createNewTileEntity does not exist.

public TileEntity createNewTileEntity(World worldIn, int meta)
    {
        return new TileEntityTimeAnomaly();
    }

It's right there. And in the current code, it's overridden. Believe it or not, even if I am not acquainted with the terminology used for programming, due to never actually taking classes, but rather figuring things out for myself, through trial, error, and looking over other bits of code, I do know what I'm doing, for the most part.

Edited by Daring Do

Gne9eza.png

 

"Be patient with me, because I'm an absolute moron half the time."

Posted
15 minutes ago, diesieben07 said:

This shows you have no idea what an IDE is. Do not override methods by hand... Your IDE exists for a reason.

And I do things the long way because it helps me keep track of what I do.

Gne9eza.png

 

"Be patient with me, because I'm an absolute moron half the time."

Posted
Just now, diesieben07 said:

It's right there, doing nothing. What makes you think this method does anything?

Hey, guess who showed up online? Zabi. And he's actually helped now. Yes, it was something in my tile entity class, not my block class.

Gne9eza.png

 

"Be patient with me, because I'm an absolute moron half the time."

Posted
31 minutes ago, diesieben07 said:

I guarantee you that no, it was not. As far as the game is concerned, the block you posted does not have a tile entity.

What I needed was this:

List<EntityPlayer> list =getWorld().getEntitiesWithinAABB(EntityPlayer.class,new AxisAlignedBB(getPos()).grow(5, 3, 5));

Gne9eza.png

 

"Be patient with me, because I'm an absolute moron half the time."

Posted
1 minute ago, diesieben07 said:

This will not magically make your block have a tile entity.

It's ok, you do not want to admit that I was right. Have fun modding.

I had also forgotten to register the tile entity, but it does work now. I didn't need to do anything in the block class. Maybe some method you use requires it, and the way I go about it doesn't. 

Gne9eza.png

 

"Be patient with me, because I'm an absolute moron half the time."

Posted
28 minutes ago, diesieben07 said:

Having a tile entity definitely requires the hasTileEntity and createTileEntity methods in your block class. No amount of bending over backwards will get you around it.

I have both of those. Look at the code. You missed the second one at first, so you likely missed the first one. 

Gne9eza.png

 

"Be patient with me, because I'm an absolute moron half the time."

Posted
5 minutes ago, diesieben07 said:

If you have them, then you posted the wrong code. The block class in your first post contains a method createNewTileEntity, which (as I told you) does nothing. There is no hasTileEntity or createTileEntity anywhere in sight.

Oh. I might've accidentally removed them when I edited out the commented out code of another method for checking for players. My apologies ^^;

Gne9eza.png

 

"Be patient with me, because I'm an absolute moron half the time."

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.