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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I was just trying to play my modded world when i randomly got this crash for no reason. I sorted through like every mod and eventually I realized it was LLibrary but I can't seem to find a solution to fix the crashing. I can't lose the world that I have that uses this mod please help me. Here's the report: https://pastebin.com/0D00B79i If anyone has a solution please let me know.  
    • 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑   Daftar Slot Ligawin88 adalah bocoran slot rekomendasi gacor dari Ligawin88 yang bisa anda temukan di SLOT Ligawin88. Situs SLOT Ligawin88 hari ini yang kami bagikan di sini adalah yang terbaik dan bersiaplah untuk mengalami sensasi tak terlupakan dalam permainan slot online. Temukan game SLOT Ligawin88 terbaik dengan 100 pilihan provider ternama yang dipercaya akan memberikan kepuasan dan kemenangan hari ini untuk meraih x500. RTP SLOT Ligawin88 merupakan SLOT Ligawin88 hari ini yang telah menjadi pilihan utama bagi pemain judi online di seluruh Indonesia. Setiap harinya jutaan pemain memasuki dunia maya untuk memperoleh hiburan seru dan kemenangan besar dalam bermain slot dengan adanya bocoran RTP SLOT Ligawin88. Tidak ada yang lebih menyenangkan daripada mengungguli mesin slot dan meraih jackpot x500 yang menggiurkan di situs SLOT Ligawin88 hari ini yang telah disediakan SLOT Ligawin88. Menangkan jackpot besar x500 rajanya maxwin dari segala slot dan raih kemenangan spektakuler di situs Ligawin88 terbaik 2024 adalah tempat yang menyediakan mesin slot dengan peluang kemenangan lebih tinggi daripada situs slot lainnya. Bagi anda yang mencari pengalaman judi slot paling seru dan mendebarkan, situs bo SLOT Ligawin88 terbaik 2024 adalah pilihan yang tepat. Jelajahi dunia slot online melalui situs SLOT Ligawin88 di link SLOT Ligawin88.
    • 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑   Daftar Slot Asusslot adalah bocoran slot rekomendasi gacor dari Asusslot yang bisa anda temukan di SLOT Asusslot. Situs SLOT Asusslot hari ini yang kami bagikan di sini adalah yang terbaik dan bersiaplah untuk mengalami sensasi tak terlupakan dalam permainan slot online. Temukan game SLOT Asusslot terbaik dengan 100 pilihan provider ternama yang dipercaya akan memberikan kepuasan dan kemenangan hari ini untuk meraih x500. RTP SLOT Asusslot merupakan SLOT Asusslot hari ini yang telah menjadi pilihan utama bagi pemain judi online di seluruh Indonesia. Setiap harinya jutaan pemain memasuki dunia maya untuk memperoleh hiburan seru dan kemenangan besar dalam bermain slot dengan adanya bocoran RTP SLOT Asusslot. Tidak ada yang lebih menyenangkan daripada mengungguli mesin slot dan meraih jackpot x500 yang menggiurkan di situs SLOT Asusslot hari ini yang telah disediakan SLOT Asusslot. Menangkan jackpot besar x500 rajanya maxwin dari segala slot dan raih kemenangan spektakuler di situs Asusslot terbaik 2024 adalah tempat yang menyediakan mesin slot dengan peluang kemenangan lebih tinggi daripada situs slot lainnya. Bagi anda yang mencari pengalaman judi slot paling seru dan mendebarkan, situs bo SLOT Asusslot terbaik 2024 adalah pilihan yang tepat. Jelajahi dunia slot online melalui situs SLOT Asusslot di link SLOT Asusslot.
    • 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑 Daftar Slot Galeri555 adalah bocoran slot rekomendasi gacor dari Galeri555 yang bisa anda temukan di SLOT Galeri555. Situs SLOT Galeri555 hari ini yang kami bagikan di sini adalah yang terbaik dan bersiaplah untuk mengalami sensasi tak terlupakan dalam permainan slot online. Temukan game SLOT Galeri555 terbaik dengan 100 pilihan provider ternama yang dipercaya akan memberikan kepuasan dan kemenangan hari ini untuk meraih x500. RTP SLOT Galeri555 merupakan SLOT Galeri555 hari ini yang telah menjadi pilihan utama bagi pemain judi online di seluruh Indonesia. Setiap harinya jutaan pemain memasuki dunia maya untuk memperoleh hiburan seru dan kemenangan besar dalam bermain slot dengan adanya bocoran RTP SLOT Galeri555. Tidak ada yang lebih menyenangkan daripada mengungguli mesin slot dan meraih jackpot x500 yang menggiurkan di situs SLOT Galeri555 hari ini yang telah disediakan SLOT Galeri555. Menangkan jackpot besar x500 rajanya maxwin dari segala slot dan raih kemenangan spektakuler di situs Galeri555 terbaik 2024 adalah tempat yang menyediakan mesin slot dengan peluang kemenangan lebih tinggi daripada situs slot lainnya. Bagi anda yang mencari pengalaman judi slot paling seru dan mendebarkan, situs bo SLOT Galeri555 terbaik 2024 adalah pilihan yang tepat. Jelajahi dunia slot online melalui situs SLOT Galeri555 di link SLOT Galeri555.
    • 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑 Daftar Slot Kocok303 adalah bocoran slot rekomendasi gacor dari Kocok303 yang bisa anda temukan di SLOT Kocok303. Situs SLOT Kocok303 hari ini yang kami bagikan di sini adalah yang terbaik dan bersiaplah untuk mengalami sensasi tak terlupakan dalam permainan slot online. Temukan game SLOT Kocok303 terbaik dengan 100 pilihan provider ternama yang dipercaya akan memberikan kepuasan dan kemenangan hari ini untuk meraih x500. RTP SLOT Kocok303 merupakan SLOT Kocok303 hari ini yang telah menjadi pilihan utama bagi pemain judi online di seluruh Indonesia. Setiap harinya jutaan pemain memasuki dunia maya untuk memperoleh hiburan seru dan kemenangan besar dalam bermain slot dengan adanya bocoran RTP SLOT Kocok303. Tidak ada yang lebih menyenangkan daripada mengungguli mesin slot dan meraih jackpot x500 yang menggiurkan di situs SLOT Kocok303 hari ini yang telah disediakan SLOT Kocok303. Menangkan jackpot besar x500 rajanya maxwin dari segala slot dan raih kemenangan spektakuler di situs Kocok303 terbaik 2024 adalah tempat yang menyediakan mesin slot dengan peluang kemenangan lebih tinggi daripada situs slot lainnya. Bagi anda yang mencari pengalaman judi slot paling seru dan mendebarkan, situs bo SLOT Kocok303 terbaik 2024 adalah pilihan yang tepat. Jelajahi dunia slot online melalui situs SLOT Kocok303 di link SLOT Kocok303.
  • Topics

×
×
  • Create New...

Important Information

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