Jump to content

[Solved]Help with packets


vroominator

Recommended Posts

Time for what's probably a noob question!

I've never worked with multiplayer modding before, and have no idea how I'm supposed to get packets working. I noticed there's a packets and channels section in the wiki page for updating to 1.3, but there's nothing in there. Any idea when that's going to be done?

In the mean time, I'm trying to set up a gui that handles a custom crafting thing for my mod. When a button is pushed in the gui, the container looks at what's in the two core slots and writes some NBT stuff to the itemstack in the wand slot. How should I make this work? Should the button send off a packet to the server, where all the crafting stuff is done, and then send a packet back with the finished product? if so, how would I go about doing this with what I currently have?

 

If any of my code is needed, here:

(Just a warning, I'm a messy coder, so prepare your eyeholes)

 

GuiWandCrafting.java

 

 

package net.minecraft.src.sorcery;

import org.lwjgl.opengl.GL11;

import net.minecraft.src.*;

public class GuiWandCrafting extends GuiContainer
{
public GuiButtonWandCrafting Button;

    public GuiWandCrafting(InventoryPlayer player, World world, int i, int j, int k)
    {
        super(new ContainerWandCrafting(player, world, i, j, k));
    }
    
    public void initGui()
    {
    	super.initGui();
    	controlList.clear();
    	controlList.add(Button = new GuiButtonWandCrafting(1, width / 2 - 10, height / 2 - 41, true));
    }

    protected void drawGuiContainerForegroundLayer()
    {
        this.fontRenderer.drawString(StatCollector.translateToLocal("container.wandcrafting"), 40, 4, 0);
        this.fontRenderer.drawString(StatCollector.translateToLocal("container.inventory"), 8, this.ySize - 96 + 2, 0);
    }

    protected void drawGuiContainerBackgroundLayer(float i, int j, int k)
    {
        int background = this.mc.renderEngine.getTexture("/sorcery/guis/wandcrafting.png");
        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.mc.renderEngine.bindTexture(background);
        int x = (this.width - this.xSize) / 2;
        int y = (this.height - this.ySize) / 2;
        this.drawTexturedModalRect(x, y, 0, 0, this.xSize, this.ySize);
    }
    
    protected void actionPerformed(GuiButton button)
    {
        if(button instanceof GuiButtonWandCrafting && this.Button.canUse)
        {
        	ContainerWandCrafting.craftWand();
        }
    }
}

 

 

 

ContainerWandCrafting.java

 

 

package net.minecraft.src.sorcery;

import net.minecraft.src.*;

public class ContainerWandCrafting extends Container
{
    public InventoryWandCrafting craftSlots = new InventoryWandCrafting(this);
    private World worldObj;
    private int posX;
    private int posY;
    private int posZ;
    public static Slot wandSlot;
    public static Slot crystalSlot;
    public static Slot coreSlot1;
    public static Slot coreSlot2;
    public boolean canRun;

    public ContainerWandCrafting(InventoryPlayer player, World world, int x, int y, int z)
    {
        this.worldObj = world;
        this.posX = x;
        this.posY = y;
        this.posZ = z;
        
        //Core Containers
        coreSlot1 = this.addSlotToContainer(new Slot(this.craftSlots, 0,  44, 17));
        coreSlot2 = this.addSlotToContainer(new Slot(this.craftSlots, 1, 116, 17));
        
        //Crystal Container
        crystalSlot = this.addSlotToContainer(new Slot(this.craftSlots, 2, 80, 56));
        
        //Wand Container
        wandSlot = this.addSlotToContainer(new Slot(this.craftSlots, 3, 80, 17));   
        
        for (int i = 0; i < 3; ++i)
        {
            for (int j = 0; j < 9; ++j)
            {
                this.addSlotToContainer(new Slot(player, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
            }
        }

        for (int i = 0; i < 9; ++i)
        {
            this.addSlotToContainer(new Slot(player, i, 8 + i * 18, 142));
        }
    }
    
    public static void craftWand()
    {
    	EnumWandMods[] wandMods = new EnumWandMods[3];
    	ItemStack wand = wandSlot.getStack();
    	ItemStack crystal = crystalSlot.getStack();
    	ItemStack core1 = coreSlot1.getStack();
    	ItemStack core2 = coreSlot2.getStack();
    	
    	if(wandSlot.getHasStack() && crystalSlot.getHasStack())
    	{
    		if(wand.itemID == (new ItemStack(Sorcery.wand)).itemID && crystal.itemID == (new ItemStack(Sorcery.battery, 1, 0)).itemID && crystal.getItemDamage() == 0)
    		{
    			System.out.println("There's a " + wand + " in the wand slot and a " + crystal + " in the crystal slot.");
    			System.out.println("Core 1: " + core1 + ". Core 2: " + core2 + ".");
    			System.out.println("There are " + EnumWandMods.values().length + " Wand Modifiers;");
    			int j = 0;
    			
    			for(EnumWandMods mods : EnumWandMods.values())
    			{
        			System.out.println(EnumWandMods.values()[j].item);
    				if(coreSlot1.getHasStack())
    				{
    					if(core1.itemID == EnumWandMods.values()[j].item.itemID)
    					{
    						wandMods[0] = SorceryHelper.getInstance().addWandMod(EnumWandMods.values()[j].item);
    						coreSlot1.decrStackSize(1);
    						System.out.println(wandMods[0]);
    					}
    				}
    				
    				if(coreSlot2.getHasStack())
    				{
    					if(core2.itemID == EnumWandMods.values()[j].item.itemID)
    					{
    						wandMods[1] = SorceryHelper.getInstance().addWandMod(EnumWandMods.values()[j].item);
    	        	    	coreSlot2.decrStackSize(1);
    						System.out.println(wandMods[1]);
    					}
    				}
    				j = j+1;
    			}	
    			
    			crystalSlot.decrStackSize(1);
    			
    			wand.setTagCompound(new NBTTagCompound());
    			wand.stackTagCompound.setTag("mods", new NBTTagList("mods"));
    			NBTTagList list = (NBTTagList)wand.stackTagCompound.getTag("mods");
    			
    			for(int i = 0; i > wandMods.length; i++)
    			{
    				NBTTagCompound tag = new NBTTagCompound();
    				tag.setInteger("ID", wandMods[i].modID);
    				tag.setBoolean("Positive", wandMods[i].pos);
    				list.appendTag(tag);
    			}
    		}
    	}
    }

    public void onCraftGuiClosed(EntityPlayer player)
    {
        super.onCraftGuiClosed(player);

        if (!this.worldObj.isRemote)
        {
            for (int var2 = 0; var2 < 4; ++var2)
            {
                ItemStack var3 = this.craftSlots.getStackInSlotOnClosing(var2);

                if (var3 != null)
                {
                    player.dropPlayerItem(var3);
                }
            }
        }
    }

    public boolean canInteractWith(EntityPlayer player)
    {
        return this.worldObj.getBlockId(this.posX, this.posY, this.posZ) != Sorcery.machine.blockID ? false : player.getDistanceSq((double)this.posX + 0.5D, (double)this.posY + 0.5D, (double)this.posZ + 0.5D) <= 64.0D;
    }
}

 

 

 

The addWandMod method from SorceryHelper

 

 

public EnumWandMods addWandMod(ItemStack item)
{
	int i = 0;
	for(EnumWandMods mods: EnumWandMods.values())
	{
		if(EnumWandMods.values()[i].item.itemID == item.itemID)
		{
			return EnumWandMods.values()[i];
		}
		i++;
	}
return EnumWandMods.empty;
}

 

 

 

Tell me if there's any more information you need.

 

Thanks

 

EDIT: Changed the question a bit.

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

    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
    • It is an issue with quark - update it to this build: https://www.curseforge.com/minecraft/mc-mods/quark/files/3642325
    • Remove Instant Massive Structures Mod from your server     Add new crash-reports with sites like https://paste.ee/  
  • Topics

×
×
  • Create New...

Important Information

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