Jump to content

[1.14.4] SimpleChannel.registerMessage not working


EnderUnknown

Recommended Posts

I am trying to register a packet to send block contents data from the server to the client. The SimpleChannel isn't letting me register my packet.

PacketHandler class

package com.corruption.network;

import com.corruption.network.packets.PacketPedestal;

import net.minecraft.client.Minecraft;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.network.NetworkRegistry;
import net.minecraftforge.fml.network.PacketDistributor;
import net.minecraftforge.fml.network.simple.SimpleChannel;

public final class PacketHandler {

	private static final String PROTICOL_VERSION = "1";
    public static final SimpleChannel CHANNEL = NetworkRegistry.newSimpleChannel(
    		new ResourceLocation("corruption","main")
    		,() -> PROTICOL_VERSION
    		, PROTICOL_VERSION::equals
    		, PROTICOL_VERSION::equals);
    
    public static void register() {
    	int id = 0;
    	
    	CHANNEL.registerMessage(id++, PacketPedestal.class, PacketPedestal::encode, PacketPedestal::new, PacketPedestal::handle);
    }
}

Packet class

package com.corruption.network.packets;

import com.google.common.base.Supplier;

import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.fml.network.NetworkEvent;

public class PacketPedestal {
	private final ItemStack item;
	
	public PacketPedestal(ItemStack stack) {
		this.item = stack;
	}
	public static void encode(PacketPedestal msg, PacketBuffer buf) {
		buf.writeItemStack(msg.item);
	}
	public static PacketPedestal decode(PacketBuffer buf) {
		return new PacketPedestal(buf.readItemStack());
	}
	public static void handle(PacketPedestal msg, Supplier<NetworkEvent.Context> context) {
		
	}
}

Thanks! 

Link to comment
Share on other sites

Quote
  •  Please clarify on "isn't letting me".

I get an error message on PacketPedestal::encode that says:

The type PacketPedestal does not define encode(MSG, PacketBuffer) that is applicable here

and the same for PacketPedestal::handle. The PacketPedestal::decode (previously PacketPedestal::new)  has a different error:

The type of decode(PacketBuffer) from the type PacketPedestal is PacketPedestal, this is incompatible with the descriptor's return type: MSG

I have been following the latest Forge Documentation for Packets which I know is for 1.13 but it is the closest that I could find.

Edited by EnderUnknown
Link to comment
Share on other sites

Okay. I changed the PacketPedastal::new to PacketPedastal::decode but it still won't regard that as a MSG. MSG doesn't appear to be a class/interface. How would I go about making the signatures match up or am I missing something big here? Should I use the actual methods instead of just references? 

Link to comment
Share on other sites

class MyPacket {
    private final int data;
    MyPacket(PacketBuffer buf) {
        this.data = buf.readInt();
    }

    MyPacket(int data) {
        this.data = data;
    }

    void encode(PacketBuffer buf) {
        buf.writeInt(data);
    }

    void handle(Supplier<NetworkEvent.Context> context) {
    }
}

channel.registerMessage(0, MyPacket.class, MyPacket::encode, MyPacket::new, MyPacket::handle);

I found this on a 1.14.2 post for a similar issue. I tried inserting the code and I received different errors. All three of the method references gave the same error with a different method that isn't being defined:

The type PacketPedestal does not define encode(MSG, PacketBuffer) that is applicable here

Link to comment
Share on other sites

I reloaded and refreshed all of the gradlew stuff and updated forge and I still received the same errors. Would there be an alternative to using a packet? All I need to do is let my TileEntityRenderer know what item is in the PedestalTileEntity, but since renders are handled on the Client side and the client thinks the block is empty nothing happens. I will link my TileEntity and Render classes below. The block calls the method addItemToInventory when activated.

package com.corruption.tileentity;

import com.corruption.Corruption;

import net.minecraft.entity.item.ItemEntity;
import net.minecraft.entity.item.ItemFrameEntity;
import net.minecraft.inventory.ItemStackHelper;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.datasync.DataParameter;
import net.minecraft.network.datasync.DataSerializers;
import net.minecraft.network.datasync.EntityDataManager;
import net.minecraft.tileentity.ITickableTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.NonNullList;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

public class PedestalTileEntity extends TileEntity implements ITickableTileEntity{

	protected NonNullList<ItemStack> inv = NonNullList.withSize(1, ItemStack.EMPTY);
	private static final DataParameter<ItemStack> ITEM = EntityDataManager.createKey(ItemFrameEntity.class, DataSerializers.ITEMSTACK);
	protected double currentRotation = 0;
	protected double prevRotation = 0;
	
	public PedestalTileEntity() {
		super(TileEntityRegistry.PEDESTAL);
	}

	
	@Override
	public void tick() {
		this.prevRotation = this.currentRotation;
        this.currentRotation = (this.currentRotation + (double)(50.0F / ((float)20 + 200.0F))) % 360.0D;
        Corruption.LOGGER.info(inv.get(0));
	}
	
	
	public CompoundNBT write(CompoundNBT compound) {	
		super.write(compound);
		ItemStackHelper.saveAllItems(compound, inv);
		return compound;
	}
	public void read(CompoundNBT compound) {
		super.read(compound);
		this.inv = NonNullList.withSize(1, ItemStack.EMPTY);
		ItemStackHelper.loadAllItems(compound, inv);
	}
	
	
	public boolean addItemToInventory(Item item, CompoundNBT itemNbt) {
		if(inv.get(0).isEmpty() && item!=null) {
			inv.clear();
			ItemStack i = new ItemStack(item,1,itemNbt);
			i.setTag(itemNbt);
			inv = NonNullList.withSize(1,i);
			return true;
		}
		else {
			dropItems();
			return false;
		}
	}
	
	
	
	public void dropItems() {
		if(!inv.get(0).isEmpty()) {
			ItemEntity i = new ItemEntity(this.world,(double)this.pos.getX()+0.5D,(double)this.pos.getY()+1.0D, (double)this.pos.getZ()+0.5D,inv.get(0));
			this.world.addEntity(i);
			this.inv.clear();
			this.inv = NonNullList.withSize(1, ItemStack.EMPTY);
		}
	}
	
	public ItemStack getContainedItem() {
		return inv.get(0);
		
	}
	
	@OnlyIn(Dist.CLIENT)
	public double getCurrentRotation() {
		return this.currentRotation;
	}
	@OnlyIn(Dist.CLIENT)
	public double getPrevRotation() {
		return this.prevRotation;
	}

}
package com.corruption.client.render;

import com.corruption.tileentity.PedestalTileEntity;
import com.mojang.blaze3d.platform.GlStateManager;

import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ItemRenderer;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.renderer.model.ItemCameraTransforms;
import net.minecraft.client.renderer.tileentity.TileEntityRenderer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.MathHelper;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

@OnlyIn(Dist.CLIENT)
public class PedestalRender extends TileEntityRenderer<PedestalTileEntity>{
	
	private  ItemRenderer itemRenderer;
	
	
	@Override
	public void render(PedestalTileEntity tileEntityIn, double x, double y, double z, float partialTicks,
			int destroyStage) {
		if(itemRenderer == null)
			itemRenderer = Minecraft.getInstance().getItemRenderer();
		ItemStack stack = tileEntityIn.GetContainedItem();
		if(!stack.isEmpty()) {
			RenderHelper.enableStandardItemLighting();
			GlStateManager.pushMatrix();
			
			GlStateManager.translated(x + 0.5, y + 1.5, z + 0.5);
			GlStateManager.scalef(0.5f, 0.5f, 0.5f);
			GlStateManager.rotatef((float)MathHelper.lerp((double)partialTicks, tileEntityIn.GetPrevRotation(), tileEntityIn.GetCurrentRotation()) * 10.0F, 0.0F, 1.0F, 0.0F);
			
			GlStateManager.enableLighting();
			this.itemRenderer.renderItem(stack, ItemCameraTransforms.TransformType.FIXED);
			
			GlStateManager.popMatrix();

		}
		super.render(tileEntityIn, x, y, z, partialTicks, destroyStage);
	}
}

Any help would be appreciated. Thanks! 

Edited by EnderUnknown
Link to comment
Share on other sites

  • 2 years later...

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.