Jump to content

Server-Client sync package notify


zedblade

Recommended Posts

Hello everyone,

 

I made a mod than replace a block with another own block, by right-clicking on it, with the follow function:

 

 

 

 

var1World.setBlockWithNotify(x, y, z, mymod.myblockID);

 

 

 

 

This work fine in SSP but in SMP the block seems replaced but when I reload the chunk or I right-click the block, this return back to the previous block. as if nothing had happened.

 

Now i know than i need to send to the server a custom package to notify the changes, and i've made the ServerPacketHandler class:

 

 

 

package mymod;

 

import cpw.mods.fml.common.network.IPacketHandler;

import cpw.mods.fml.common.network.Player;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.network.INetworkManager;

import net.minecraft.item.ItemStack;

import net.minecraft.network.packet.Packet250CustomPayload;

 

public class ServerPacketHandler implements IPacketHandler

{

    public void onPacketData(INetworkManager var1, Packet250CustomPayload var2, Player var3)

    {

        if (var2.channel.equals("MyMod"))

        {

            this.handlePacket(var2, (EntityPlayer)var3);

        }

    }

 

    private void handlePacket(Packet250CustomPayload var1, EntityPlayer var2)

    {

        String var3 = (new String(var1.data)).trim();

       

        if (var2.getCurrentEquippedItem() != null)

        {

            ItemStack var4 = var2.getCurrentEquippedItem();

        }

    }

}

 

 

 

And i initialized it in main mod class declaration network section:

 

 

 

@NetworkMod(

clientSideRequired = true,

serverSideRequired = false,

channels = {"MyMod"},

packetHandler = ServerPacketHandler.class

)

 

 

 

Now i dont know what i must put into ServerPacketHandler or into right-click event to notify the replaced block to the server.

 

Plese any suggestions ...

Link to comment
Share on other sites

Yes, i tried but unfortunately the problem happen also in SSP (client side).  :-\

 

Also I tried using this:

 

MinecraftServer mcServer =  MinecraftServer.getServer();

mcServer.worldServerForDimension(player.dimension).setBlockWithNotify(X, Y, Z, mymod.mymod.blockID);

or

mcServer.worldServerForDimension(player.dimension).setBlock(X, Y, Z, mymod.mymod.blockID);

 

but mc give me "java.lang.NullPointerException". I dont know why ...

Link to comment
Share on other sites

As you wish:

 

 

 

 

package mymod;

 

import java.util.Random;

 

import net.minecraft.block.Block;

//import net.minecraft.client.Minecraft;

import net.minecraft.creativetab.CreativeTabs;

import net.minecraft.entity.EntityLiving;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.block.material.Material;

import net.minecraft.world.World;

 

public class mymod_block extends Block

{

    public mymod_block(int blockID)

    {

        super(blockID, Material.iron);

        this.setCreativeTab(CreativeTabs.tabBlock);

        setHardness(20.0f);

        setResistance(20.0f);

        setStepSound(soundMetalFootstep);

        setBlockName("My Block");

        setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.2F, 1.0F);

    }

 

    @Override

    public String getTextureFile()

    {

        return CommonProxy.ITEMS;

    }

    @Override

    public int getBlockTextureFromSide(int side)

    {

        switch (side)

        {

            case 0:

                return 1;

 

            case 1:

                return 3;

 

            case 2:

                return 1;

 

            case 3:

                return 1;

 

            case 4:

                return 1;

 

            case 5:

                return 1;

        }

 

        return blockIndexInTexture;

    }

    @Override

    public boolean isOpaqueCube()

    {

        return false;

    }

    @Override

    public boolean renderAsNormalBlock()

    {

        return false;

    }

 

    @Override

    public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int i, float f, float g, float t)

    {

        if (player.getCurrentEquippedItem() == null)

        {

        if (world.isRemote)

            {

        player.addChatMessage("My Block inactive");

            }

            return true;

        }

        else

        {

            if (player.getCurrentEquippedItem().getItemName().equals("item.My Block"))

            {

                return false;

            }

            else

            {

            if (world.isRemote)

                {

            player.addChatMessage("My Block inactive!!");

                }

                return true;

            }

        }

    }

   

    @Override

    public void onBlockPlacedBy(World par1World, int par2, int par3, int par4, EntityLiving par5EntityLiving)

    {

if (par1World.isRemote)

{

if (par5EntityLiving instanceof EntityPlayer)

{

EntityPlayer thePlayer = (EntityPlayer) par5EntityLiving;

thePlayer.addChatMessage("My Block created!");

}

}

    }

 

    @Override

    public void randomDisplayTick(World var1, int var2, int var3, int var4, Random var5)

    {

    if (var1.isRemote)

          {

            if (mymod.ReplaceBlock)

          {

        var1.setBlockWithNotify(var2, var3, var4, mymod.myblockID);

       

        mymod.ReplaceBlock = false;

          }

        }

        super.randomDisplayTick(var1, var2, var3, var4, var5);

    }

   

}

 

 

 

Link to comment
Share on other sites

oh god, i put it in randomDisplayTick because previously it was in GUIscreen button (cliente side only too).

 

In fact the boolean mymod.ReplaceBlock = true when pressed OK button from a GUI ...

 

Now I really dont know where i can put that code. :'(

 

any suggestion?

Link to comment
Share on other sites

seems cant run updatetick function:

 

 

 

package mymod;

 

import java.util.Random;

 

import net.minecraft.block.Block;

//import net.minecraft.client.Minecraft;

import net.minecraft.creativetab.CreativeTabs;

import net.minecraft.entity.EntityLiving;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.block.material.Material;

import net.minecraft.world.World;

 

public class mymod_block extends Block

    public mymod_block(int blockID)

    {

        super(blockID, Material.iron);

        this.setCreativeTab(CreativeTabs.tabBlock);

        setHardness(20.0f);

        setResistance(20.0f);

        setStepSound(soundMetalFootstep);

        setBlockName("My Block");

        setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.2F, 1.0F);

        this.setTickRandomly(true);

        //this.setRequiresSelfNotify(); (tried with and without this)

    }

 

    @Override

    public String getTextureFile()

    {

        return CommonProxy.ITEMS;

    }

    @Override

    public int getBlockTextureFromSide(int side)

    {

        switch (side)

        {

            case 0:

                return 1;

 

            case 1:

                return 3;

 

            case 2:

                return 1;

 

            case 3:

                return 1;

 

            case 4:

                return 1;

 

            case 5:

                return 1;

        }

 

        return blockIndexInTexture;

    }

    @Override

    public boolean isOpaqueCube()

    {

        return false;

    }

    @Override

    public boolean renderAsNormalBlock()

    {

        return false;

    }

 

    @Override

    public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int i, float f, float g, float t)

    {

        if (player.getCurrentEquippedItem() == null)

        {

          if (world.isRemote)

            {

              player.addChatMessage("My Block inactive");

            }

            return true;

        }

        else

        {

            if (player.getCurrentEquippedItem().getItemName().equals("item.My Block"))

            {

                return false;

            }

            else

            {

              if (world.isRemote)

                {

                  player.addChatMessage("My Block inactive!!");

                }

                return true;

            }

        }

    }

   

    @Override

    public void onBlockPlacedBy(World par1World, int par2, int par3, int par4, EntityLiving par5EntityLiving)

    {

      if (par1World.isRemote)

      {

        if (par5EntityLiving instanceof EntityPlayer)

        {

            EntityPlayer thePlayer = (EntityPlayer) par5EntityLiving;

            thePlayer.addChatMessage("My Block created!");

        }

      }

    }

 

    public void updateTick(World par1World, int par2, int par3, int par4, Random par5Random)

    {

        super.updateTick(par1World, par2, par3, par4, par5Random);

 

      if (par1World.isRemote)

          {

            if (mymod.ReplaceBlock)

            {

            par1World.setBlockWithNotify(par2, par3, par4, mymod.myblockID);

           

            mymod.ReplaceBlock = false;

            }

          }

    }

   

}

 

 

 

also I added setRequiresSelfNotify as in wheat (you mean BlockCorp, right?) but wont work.

Link to comment
Share on other sites

if (player.getCurrentEquippedItem().getItemName().equals("item.My Block"))

it's not best practice to use strings to identify items, use IDs.

 

         if (par1World.isRemote)
          {
            if (mymod.ReplaceBlock)
            {
               par1World.setBlockWithNotify(par2, par3, par4, mymod.myblockID);
               
               mymod.ReplaceBlock = false;
            }
          }

so, your block-replacing code is supposed to run only on client? also take a look at mymod.ReplaceBlock if it does really what you wanted. if it's only set here then it means that only once after mod initialization (if its default value is true) one block changes ~ only 1 block after launching minecraft will be affected.

 

 

mnn.getNativeLang() != English

If I helped you please click on the "thank you" button.

Link to comment
Share on other sites

so, your block-replacing code is supposed to run only on client? also take a look at mymod.ReplaceBlock if it does really what you wanted. if it's only set here then it means that only once after mod initialization (if its default value is true) one block changes ~ only 1 block after launching minecraft will be affected.

 

No, the block-replacing code should run both client & server. the mymod.ReplaceBlock = true everytime the player press OK  on a GUI mask ;)

Link to comment
Share on other sites

You don't change blocks on the client. Only change them on the server, it will then tell the client. That how it works ;) If you want a button to trigger a block change, you need to send a custom packet (There is a tutorial on the wiki) that tells the server when the button is pressed and then make the block change. Only on the server.

 

Yes, but i cant found a specific tutorial to do it. (If you have one can you show me?)

on wiki i found very roughly guide line to handle the packet.

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.