Jump to content

[1.7.10] Entity mountable not working (problem with synchronization)


SCAREX

Recommended Posts

Recently, I had to create a new block for sitting but I had problem with synchronization : the player was correctly placed for the server but not for the client (I'm playing in single player).

I posted this problem on the MinecraftForgeFrance forum but no one could find an answer to my problem (http://www.minecraftforgefrance.fr/showthread.php?tid=2225).

 

My classes :

package fr.scarex.hilium.block;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.world.World;
import fr.scarex.hilium.entity.EntitySit;
import fr.scarex.hilium.tileentity.TileEntitySit;

/**
* @author SCAREX
* 
*/
public class BlockSit extends Block
{
protected BlockSit(Material material) {
	super(material);
}

@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ) {
	if (this.canSit(world, x, y, z, player, side, hitX, hitY, hitZ)) {
		Entity entity = world.getEntityByID(((TileEntitySit) world.getTileEntity(x, y, z)).getEntityId());
		if (entity != null) ((EntitySit) entity).interactFirst(player);
		return true;
	}
	return false;
}

public boolean canSit(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ) {
	return true;
}

@Override
public void onBlockAdded(World world, int x, int y, int z) {
	EntitySit e = new EntitySit(world, x + 0.5D, y + 0.5D, z + 0.5D, this.getOffsetY(world, x, y, z));
	world.spawnEntityInWorld(e);
	((TileEntitySit) world.getTileEntity(x, y, z)).setEntityId(e.getEntityId());
}

public float getOffsetY(World world, int x, int y, int z) {
	return 0.5F;
}
}

 

package fr.scarex.hilium.tileentity;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;

/**
* @author SCAREX
* 
*/
public class TileEntitySit extends TileEntity
{
protected int entityId;

public int getEntityId() {
	return this.entityId;
}

public void setEntityId(int id) {
	this.entityId = id;
}

@Override
public void readFromNBT(NBTTagCompound comp) {
	super.readFromNBT(comp);
	this.entityId = comp.getInteger("entityId");
}

@Override
public void writeToNBT(NBTTagCompound comp) {
	super.writeToNBT(comp);
	comp.setInteger("entityId", this.entityId);
}

@Override
public Packet getDescriptionPacket() {
	NBTTagCompound comp = new NBTTagCompound();
	this.writeToNBT(comp);
	return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, 0, comp);
}

@Override
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) {
	this.readFromNBT(pkt.func_148857_g());
}
}

 

package fr.scarex.hilium.entity;

import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import fr.scarex.hilium.Hilium;
import fr.scarex.hilium.block.BlockSit;

/**
* @author SCAREX
* 
*/
public class EntitySit extends Entity
{
protected float offsetY;

public EntitySit(World world) {
	super(world);
	this.noClip = true;
	this.preventEntitySpawning = true;
	this.setSize(0.0F, 0.0F);
	this.offsetY = 0.0F;
}

public EntitySit(World world, double x, double y, double z, float offsetY) {
	this(world);
	this.setPosition(x, y, z);
	this.offsetY = offsetY;
}

@Override
protected void entityInit() {}

@Override
protected void readEntityFromNBT(NBTTagCompound comp) {}

@Override
protected void writeEntityToNBT(NBTTagCompound comp) {}

@Override
public void onUpdate() {
	super.onUpdate();
	if (!(this.worldObj.getBlock(MathHelper.floor_double(this.posX), MathHelper.floor_double(this.posY), MathHelper.floor_double(this.posZ)) instanceof BlockSit)) this.kill();
}

@Override
public boolean interactFirst(EntityPlayer p) {
	if (this.riddenByEntity != null) return true;
	if (!this.worldObj.isRemote) p.mountEntity(this);
	return true;
}

@Override
public double getMountedYOffset() {
	return this.offsetY;
}
}

 

When I click the block the first time, it works, but after the game show the message "Press shift to dismount" but I can move around, and when I throw an Item on the floor, it is threw beside the block, which indicates that this is a problem with synchronization.

Link to comment
Share on other sites

I found out that the method onBlockAdded is fired only on the server, And when I spawn the entity, the client doesn't know that the entity is actually there : I will put the spawn of the mob in another method to see if there is a difference.

Link to comment
Share on other sites

Now, I can sit on thee block but the mounted Y offset is correct only for the server :

 

package fr.scarex.hilium.block;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.world.World;
import fr.scarex.hilium.Hilium;
import fr.scarex.hilium.entity.EntitySit;
import fr.scarex.hilium.tileentity.TileEntitySit;

/**
* @author SCAREX
* 
*/
public class BlockSit extends Block
{
protected BlockSit(Material material) {
	super(material);
}

@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ) {
	if (this.canSit(world, x, y, z, player, side, hitX, hitY, hitZ)) {
		EntitySit entity = new EntitySit(world, x + 0.5D, y + 0.5D, z + 0.5D, this.getOffsetY(world, x, y, z));
		world.spawnEntityInWorld(entity);
		entity.interactFirst(player);
		return true;
	}
	return false;
}

public boolean canSit(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ) {
	return true;
}

public double getOffsetY(World world, int x, int y, int z) {
	return 0.5D;
}
}

 

package fr.scarex.hilium.block;

import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import cpw.mods.fml.common.registry.GameRegistry;
import fr.scarex.hilium.Hilium;
import fr.scarex.hilium.client.ClientProxy;
import fr.scarex.hilium.tileentity.TileEntitySit;
import fr.scarex.hilium.tileentity.TileEntityUseless;

/**
* @author SCAREX
* 
*/
public class BlockToilet extends BlockSit
{
public static final String NAME = "toilet";

protected BlockToilet() {
	super(Material.glass);
	this.setHardness(0.5F);
	this.setResistance(5.0F);

	this.setStepSound(this.soundTypeGlass);
	this.setCreativeTab(CreativeTabs.tabDecorations);

	this.setBlockName(Hilium.MODID + "_" + NAME);
	this.setBlockTextureName(Hilium.MODID + ":" + NAME);

	this.register();
}

private void register() {
	GameRegistry.registerBlock(this, NAME);
	GameRegistry.registerTileEntity(TileEntitySit.class, Hilium.MODID + ":" + NAME);
}

@Override
public boolean renderAsNormalBlock() {
	return false;
}

@Override
public int getRenderType() {
	return ClientProxy.RENDER_ID;
}

@Override
public boolean isOpaqueCube() {
	return false;
}

@Override
public boolean hasTileEntity(int metadata) {
	return true;
}

@Override
public TileEntity createTileEntity(World world, int metadata) {
	return new TileEntitySit();
}

@Override
public void setBlockBoundsForItemRender() {
	this.setBlockBounds(0.05F, 0.0F, 0.05F, 0.95F, 0.88F, 0.95F);
}

@Override
public void registerBlockIcons(IIconRegister register) {
	this.blockIcon = register.registerIcon("glass");
}

@Override
public boolean canPlaceBlockAt(World world, int x, int y, int z) {
	return super.canPlaceBlockAt(world, x, y, z) && World.doesBlockHaveSolidTopSurface(world, x, y - 1, z);
}

@Override
public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entity, ItemStack stack) {
	int direction = MathHelper.floor_double((double) (entity.rotationYaw * 4.0F / 360.0F) + 2.5D) & 3;
	world.setBlockMetadataWithNotify(x, y, z, direction == 3 ? 0 : direction + 1, 2);
}

@Override
public double getOffsetY(World world, int x, int y, int z) {
	return 0.3D;
}
}

 

package fr.scarex.hilium.entity;

import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import fr.scarex.hilium.Hilium;
import fr.scarex.hilium.block.BlockSit;

/**
* @author SCAREX
* 
*/
public class EntitySit extends Entity
{
protected double offsetY;

public EntitySit(World world) {
	super(world);
	this.noClip = true;
	this.preventEntitySpawning = true;
	this.setSize(0.0F, 0.0F);
	this.offsetY = 0.0F;
}

public EntitySit(World world, double x, double y, double z, double offsetY) {
	this(world);
	this.setPosition(x, y, z);
	this.offsetY = offsetY;
}

@Override
protected void entityInit() {}

@Override
protected void readEntityFromNBT(NBTTagCompound comp) {}

@Override
protected void writeEntityToNBT(NBTTagCompound comp) {}

@Override
public void onEntityUpdate() {
	if (this.riddenByEntity == null || this.riddenByEntity.isDead) this.setDead();
	super.onEntityUpdate();
}

@Override
public boolean interactFirst(EntityPlayer p) {
	if (this.riddenByEntity != null) return true;
	if (!this.worldObj.isRemote) p.mountEntity(this);
	return true;
}

@Override
public double getMountedYOffset() {
	return this.offsetY;
}
}

Link to comment
Share on other sites

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.