Jump to content
  • Home
  • Files
  • Docs
Topics
  • All Content

  • This Topic
  • This Forum

  • Advanced Search
  • Existing user? Sign In  

    Sign In



    • Not recommended on shared computers


    • Forgot your password?

  • Sign Up
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [1.7.10] Entity mountable not working (problem with synchronization)
Currently Supported: 1.16.X (Latest) and 1.15.X (LTS)
Sign in to follow this  
Followers 1
SCAREX

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

By SCAREX, June 22, 2015 in Modder Support

  • Reply to this topic
  • Start new topic

Recommended Posts

SCAREX    2

SCAREX

SCAREX    2

  • Tree Puncher
  • SCAREX
  • Members
  • 2
  • 28 posts
Posted June 22, 2015

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.

  • Quote

Share this post


Link to post
Share on other sites

EverythingGames    20

EverythingGames

EverythingGames    20

  • Diamond Finder
  • EverythingGames
  • Forge Modder
  • 20
  • 388 posts
Posted June 23, 2015

Hi

Have you synced your data correctly? When placing the player, sync the data before placing to ensure a pinpoint placement on the server and the client. Maybe the XYZ values are not being updated for the player.

  • Quote

Development of Plugins [2012 - 2014] Development of Mods [2012 - Current]

Share this post


Link to post
Share on other sites

SCAREX    2

SCAREX

SCAREX    2

  • Tree Puncher
  • SCAREX
  • Members
  • 2
  • 28 posts
Posted June 23, 2015

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.

  • Quote

Share this post


Link to post
Share on other sites

SCAREX    2

SCAREX

SCAREX    2

  • Tree Puncher
  • SCAREX
  • Members
  • 2
  • 28 posts
Posted June 23, 2015

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;
}
}

  • Quote

Share this post


Link to post
Share on other sites

SCAREX    2

SCAREX

SCAREX    2

  • Tree Puncher
  • SCAREX
  • Members
  • 2
  • 28 posts
Posted June 23, 2015

I can get it to work by implementing IEntityAdditionalSpawnData, but it takes a bit of time to update the y offset :

 

iA5xl.jpg

 

After a bit of time :

iA5zS.jpg

  • Quote

Share this post


Link to post
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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

    • Insert image from URL
×
  • Desktop
  • Tablet
  • Phone
Sign in to follow this  
Followers 1
Go To Topic Listing



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • HDMI_Steckdose
      Forge error Forge won't open

      By HDMI_Steckdose · Posted 2 minutes ago

      When i try to start forge on my server i get this error
    • Extrupt
      Forge won't fully download

      By Extrupt · Posted 2 minutes ago

      AVG free
    • diesieben07
      Forge won't fully download

      By diesieben07 · Posted 5 minutes ago

      Are you using any antivirus? If so, which?
    • Draco18s
      [1.16.x] Custom block as a shop

      By Draco18s · Posted 7 minutes ago

      I didn't have that version downloaded so looked at the most current version I had.
    • ThisIsNotOriginal
      When I try to run my mod after I just added Deferred Registries it crashes

      By ThisIsNotOriginal · Posted 12 minutes ago

      i have a repo ready but do i need to show my assets files as well?
  • Topics

    • HDMI_Steckdose
      0
      Forge error Forge won't open

      By HDMI_Steckdose
      Started 2 minutes ago

    • Extrupt
      3
      Forge won't fully download

      By Extrupt
      Started 27 minutes ago

    • Retsal
      7
      [1.16.x] Custom block as a shop

      By Retsal
      Started Yesterday at 01:04 PM

    • ThisIsNotOriginal
      15
      When I try to run my mod after I just added Deferred Registries it crashes

      By ThisIsNotOriginal
      Started 15 hours ago

    • troublemaker_47
      6
      Teleport player in same direction as where he looks

      By troublemaker_47
      Started 1 hour ago

  • Who's Online (See full list)

    • Luis_ST
    • Tavi007
    • Extrupt
    • diesieben07
    • Draco18s
    • ThisIsNotOriginal
    • dresacraft
    • hugo.talento@gmail.com
    • HDMI_Steckdose
    • Katie328
    • Gorp5
    • GenElectrovise
    • troublemaker_47
    • RaySanketsu
    • J_F1Nl3Y_
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [1.7.10] Entity mountable not working (problem with synchronization)
  • Theme

Copyright © 2019 ForgeDevelopment LLC · Ads by Longitude Ads LLC Powered by Invision Community