Java Error: The constructor C17PacketCustomPayload(String, byte[]) is undefined - java

Why I am getting this error?
The constructor C17PacketCustomPayload(String, byte[]) is undefined
Java Code:
package pw.cinque.ping;
import java.awt.Color;
import net.minecraft.client.Minecraft;
import net.minecraft.network.Packet;
import net.minecraft.network.PacketBuffer;
import net.minecraft.network.play.client.C17PacketCustomPayload;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.gameevent.InputEvent;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import java.awt.*;
import java.nio.ByteBuffer;
import org.lwjgl.input.Keyboard;
#Mod(modid = Packets.MODID, version = Packets.VERSION)
public class Packets
{
public static final String MODID = "Lower ur ping!";
public static final String VERSION = "1.0";
private static final Minecraft mc = Minecraft.getMinecraft();
private boolean textGui;
private final int textGuiKey = Keyboard.KEY_P;
private boolean reachToogle;
private final int reachKey = Keyboard.KEY_L;
#EventHandler
public void init(FMLInitializationEvent event)
{
FMLCommonHandler.instance().bus().register(this);
MinecraftForge.EVENT_BUS.register(this);
System.out.println("Intialized Reach Mod by Shiny");
}
#SubscribeEvent
public void onRender(TickEvent.RenderTickEvent e) {
if(textGui)
mc.fontRendererObj.drawStringWithShadow("Shiny", 2, 2, Color.BLACK.hashCode());
}
#SubscribeEvent
public void onKeyInput(InputEvent.KeyInputEvent e) {
if(Keyboard.isKeyDown(textGuiKey)) {
textGui = !textGui;
return;
}
else if(Keyboard.isKeyDown(reachKey)) {
reachToogle = !reachToogle;
}
Packet spoofedReachPacket = manipulateReachPacket(spoofReachValue(4.2));
mc.thePlayer.sendQueue.addToSendQueue(spoofedReachPacket);
}
private Packet manipulateReachPacket(byte[] spoofedReachValue) {
return new C17PacketCustomPayload("reach", spoofedReachValue);
}
private byte[] spoofReachValue(double reachValue) {
byte[] buffer = new byte[8];
ByteBuffer.wrap(buffer).putDouble(reachValue`enter code here`);
return buffer;
}
}

I'm guessing you're getting your error here:
return new C17PacketCustomPayload("reach", spoofedReachValue);
It's saying that the constructor is undefined which means the class C17PacketCustomPayload does not have a constructor that takes a string and/or byte[] as arguments. If you created this class yourself then you need to add a method that looks like this
public C17PacketCustomPayload(String string, byte[] bytes){
//add behavior here
}
If you didn't create this class but are merely importing, then you need to look up just what parameters this constructor takes and fix it accordingly.

Why I am getting this error?
The answer is evident from these reverse engineered javadocs.
Constructors:
C17PacketCustomPayload()
C17PacketCustomPayload(java.lang.String p1, PacketBuffer p1)
In your code, the second parameter to the constructor call needs to be a PacketBuffer object, not a byte[].

Related

getItemMeta is always returning null in my spigot plugin

Hi fellow coders I have been trying to make a custom sword and put it into a shop gui but when I try to change the swords meta using ItemStack.getitemmeta() and then i try to set the display name or something it says the sword meta is null. Also I am changing My actual name in irl to my name and the server name to server name
Error:
Caused by: java.lang.NullPointerException: Cannot invoke "org.bukkit.inventory.meta.ItemMeta.setDisplayName(String)" because "weaponMeta" is null
Blunt Knife.java
package me.myname.servername.items.weapons;
import me.myname.servername.Utils.Weapon;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
public class BluntKnife extends Weapon
{
public void BluntKnife()
{
tradable = true;
damage = 12;
rarity = "COMMON";
name = "Blunt Knife";
ArrayList<String> lore = new ArrayList<String>();
lore.add("Damage: +12");
lore.add("");
lore.add("Can be upgraded with /upgrade for 500 coins!");
lore.add("");
lore.add("⚔ COMMON SWORD ⚔");
item = new ItemStack(Material.AIR, 1);
}
}
MakeItem.java
package me.myname.servername.Utils;
import org.bukkit.Bukkit;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
public interface MakeItem
{
public default ItemStack makeItemFromWeapon(Weapon wpn)
{
ItemStack weapon = wpn.item;
ItemMeta weaponMeta = weapon.getItemMeta();
weaponMeta.setDisplayName(wpn.name);
weaponMeta.setLore(wpn.lore);
weapon.setItemMeta(weaponMeta);
return weapon;
}
}
Weapon.java
package me.myname.servername.Utils;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
public class Weapon
{
protected boolean tradable = true;
protected int damage = 5;
protected String rarity = null;
protected String name = null;
protected ArrayList<String> lore = new ArrayList<>();
protected ItemStack item = new ItemStack(Material.AIR, 1);
}
ShopCommand.java
package me.myname.servername.commands;
import me.myname.servername.Utils.MakeItem;
import me.myname.servername.Utils.Weapon;
import me.myname.servername.items.weapons.BluntKnife;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
public class ShopCommand implements CommandExecutor, MakeItem {
#Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
{
if(sender instanceof Player){
Player player = (Player) sender;
Inventory shopinv = Bukkit.createInventory(player, 9, ChatColor.AQUA + "Shop");
BluntKnife item1 = new BluntKnife();
ItemStack[] menu_items = {makeItemFromWeapon(item1)};
shopinv.setContents(menu_items);
player.openInventory(shopinv);
}
return true;
}
}
Of public interface MakeItem.
An interface in Java is always public and static
and
All fields (variables) must be static constants
and
All methods are an abstract signature only , no body code
You must "implement" the coded methods inside a class that uses the implement keyword and the interface name.
In BluntKnife.java I did the constructor wrong.
So I changed it from public void BluntKnife() too public BluntKnife()

parse C++ snippet using ANTLR and getting compiler errors

I am following a tutorial and trying to parse some simple code snippet using ANTLR but I cannot do so, I get a lot of compile time errors that I cannot resolve, I am posting a screenshot of the errors that I am getting in my IDE.
package cplusplusparser;
import java.util.ArrayList;
import java.util.List;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import org.antlr.v4.runtime.tree.TerminalNode;
import cplusplusparser.CPP14Parser.AbstractdeclaratorContext;
import cplusplusparser.CPP14Parser.TranslationunitContext;
import org.antlr.v4.*;
import org.antlr.*;
import org.antlr.runtime.tree.ParseTree;
public class UppercaseMethodListener extends CPP14BaseListener {
private List<String> errors = new ArrayList<>();
// ... getter for errors
#Override
public void enterMethodDeclarator(AbstractdeclaratorContext ctx) {
TerminalNode node = ctx.Identifier();
String methodName = node.getText();
if (Character.isUpperCase(methodName.charAt(0))) {
String error = String.format("Method %s is uppercased!", methodName);
errors.add(error);
}
}
// ... getter for errors
static String javaClassContent = "public class SampleClass { void DoSomething(){} }";
static CPP14Lexer java8Lexer = new CPP14Lexer(CharStreams.fromString(javaClassContent));
static CommonTokenStream tokens = new CommonTokenStream(java8Lexer);
static CPP14Parser parser = new CPP14Parser(tokens);
static TranslationunitContext tree = parser.translationunit();
static ParseTreeWalker walker = new ParseTreeWalker();
static UppercaseMethodListener listener= new UppercaseMethodListener();
public static void main (String []args ) {
walker.walk(listener, tree);
assertThat(listener.getErrors().size(), is(1));
assertThat(listener.getErrors().get(0),
is("Method DoSomething is uppercased!"));
}
}

Minecraft modding Java exception in mod item class

This is my code, I have errors on line 16 and 17, i don't know where im going wrong, this is in the main ModItems class and i have been using this video as a guide If you need the rest of my class files i have uploaded the current copy of my classes here
The error on both lines is The constructor Item(Item, Item) is undefined
package TheStraying11.QuarkyPower.init;
import TheStraying11.QuarkyPower.Reference;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.item.Item;
import net.minecraft.item.ItemSoup;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.fml.common.registry.GameRegistry;
import TheStraying11.QuarkyPower.items.QuarkUp;
import TheStraying11.QuarkyPower.items.QuarkDown;
public class ModItems {
public static Item QuarkUp;
public static Item QuarkDown;
public static void init() {
QuarkUp = new Item(QuarkUp, QuarkUp);
QuarkDown = new Item(QuarkDown, QuarkDown);
}
public static void register() {
registerItem(QuarkUp);
registerItem(QuarkDown);
}
public static void registerRenders() {
}
public static void registerItem(Item item) {
GameRegistry.register(item);
}
public static void registerRender(Item item) {
ModelLoader.setCustomModelResourceLocation(item, 0, new ModelResourceLocation(new ResourceLocation(Reference.MODID, item.getUnlocalizedName().substring(5)), "inventory"));
}
}
friend helped, changed
QuarkUp = new Item(QuarkUp, QuarkUp);
QuarkDown = new Item(QuarkDown, QuarkDown);
to
quark_Up = new itemQuarkUp();
quark_Down = new itemQuarkDown();
EDIT:
Completely started again as that video just made a mess imo, that i didn't understand, I guess ill get better soon haha, i just wish i could use Python instead

Item not working in minecraft mod - Minecraft Forge Mod Development [EDIT: FIXED]

I have started making a mod, it's not registering as an item. When i type /give Fidojj222 fcm:fuel_canister it should give me the item except it says it doesn't exist! I am using eclipse as my IDE I am suspecting it might be this warning when I compile it into a jar:
JAR export finished with warnings. See details for additional information.
Can not export external class folder at 'C:\Users\J.J\.gradle\caches\minecraft\net\minecraftforge\forge\1.8-11.14.3.1450\start'.
If that is the problem, then how can I fix it? If not here's my code:
CarsMod.java:
package com.fidojj222.carsmod;
import com.fidojj222.carsmod.init.CarsItems;
import com.fidojj222.carsmod.proxy.CommonProxy;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
#Mod(modid = Reference.MOD_ID, name = Reference.MOD_NAME, version = Reference.VERSION)
public class CarsMod {
#SidedProxy(clientSide = Reference.CLIENT_PROXY_CLASS, serverSide = Reference.SERVER_PROXY_CLASS)
public static CommonProxy proxy;
public void PreInit(FMLPreInitializationEvent event){
CarsItems.init();
CarsItems.register();
}
public void Init(FMLInitializationEvent event){
proxy.registerRenders();
}
public void PostInit(FMLPostInitializationEvent event){
}
}
Reference.java:
package com.fidojj222.carsmod;
public class Reference {
public static final String MOD_ID = "fcm";
public static final String MOD_NAME = "Fidojj222\'s Cars Mod";
public static final String VERSION = "1.0";
public static final String CLIENT_PROXY_CLASS = "com.fidojj222.carsmod.proxy.ClientProxy";
public static final String SERVER_PROXY_CLASS = "com.fidojj222.carsmod.proxy.CommonProxy";
}
CarsItems.java:
package com.fidojj222.carsmod.init;
import com.fidojj222.carsmod.Reference;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.item.Item;
import net.minecraftforge.fml.common.registry.GameRegistry;
public class CarsItems {
public static Item fuel_canister;
public static void init(){
fuel_canister = new Item().setUnlocalizedName("fuel_canister");
}
public static void register(){
GameRegistry.registerItem(fuel_canister, fuel_canister.getUnlocalizedName().substring(5));
}
public static void registerRenders(){
registerRender(fuel_canister);
}
public static void registerRender(Item item){
Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(item, 0, new ModelResourceLocation(Reference.MOD_ID + ":" + item.getUnlocalizedName().substring(5), "inventory"));
}
}
CommonProxy.java:
package com.fidojj222.carsmod.proxy;
public class CommonProxy {
public void registerRenders(){
}
}
ClientProxy.java:
package com.fidojj222.carsmod.proxy;
import com.fidojj222.carsmod.init.CarsItems;
public class ClientProxy extends CommonProxy {
#Override
public void registerRenders(){
CarsItems.registerRenders();
}
}
What do you mean by not showing? The item isn't found at all in the creative search menu, or it is an untextured (purple/black checkered) block?
If it is untextured, you need to make sure these 2 things are done:
Make sure you have this texture in place src/main/resources/assets/fcm/textures/items/fuel_canister.png it needs to be 16x16 pixels.
Create a fuel_canister.json file at src/main/resources/assets/fcm/models/item/fuel_canister.json This file defines how the image should be rendered ingame.
The contents of that file should be
{
"parent": "builtin/generated",
"textures":{
"layer0":"fcm:items/fuel_canister"
},
"display":{
"thirdperson":{
"rotation":[-90, 0, 0],
"translation":[0, 1, -3],
"scale":[0.55,0.55,0.55]
},
"firstperson":{
"rotation":[0,-135,25],
"translation":[0,4,2],
"scale":[1.7,1.7,1.7]
}
}
}

How do you serialize Guava's immutable collections using Protostuff?

I use protostuff-runtime to serialize object graphs. Some of these objects have reference to Guava immutable collections, such as ImmutableList and ImmutableSet. Protostuff is unable to deserialize these collections out of the box, because it tries to construct an instance and then "add" elements to it from the inputStream (which fails, since the collections are immutable).
Do you know of any library / protostuff plugin that does that out of the box? If not, is there a best practice to do this myself?
I've investigated, and found that protostuff has a concept of "delegate", that lets you take control of the serialization for specific types. It seems to be the answer to my problem, but I can't seem to get it working.
Here is what I have right now:
import com.dyuproject.protostuff.Schema;
import com.dyuproject.protostuff.runtime.RuntimeSchema;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;
/**
* This is the POJO I want to serialize. Note that the {#code strings} field refers to an {#link ImmutableList}.
*/
#Immutable
public class Foo {
public static final Schema<Foo> SCHEMA = RuntimeSchema.getSchema(Foo.class);
#Nonnull
private final ImmutableList<String> strings;
public Foo(ImmutableList<String> strings) {
this.strings = Preconditions.checkNotNull(strings);
}
#Nonnull
public ImmutableList<String> getStrings() {
return strings;
}
#Override
public boolean equals(Object obj) {
if (obj instanceof Foo) {
Foo that = (Foo) obj;
return this.strings.equals(that.strings);
}
return false;
}
#Override
public int hashCode() {
return strings.hashCode();
}
}
import com.dyuproject.protostuff.*;
import com.dyuproject.protostuff.runtime.Delegate;
import com.dyuproject.protostuff.runtime.RuntimeSchema;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import java.io.IOException;
import java.util.ArrayList;
public class ImmutableListDelegate implements Delegate<ImmutableList<?>> {
private static final Schema<ArrayList> LIST_SCHEMA = RuntimeSchema.getSchema(ArrayList.class);
#Override
public WireFormat.FieldType getFieldType() {
return WireFormat.FieldType.MESSAGE;
}
#Override
public ImmutableList<?> readFrom(Input input) throws IOException {
ArrayList<?> list = LIST_SCHEMA.newMessage();
input.mergeObject(list, LIST_SCHEMA);
return ImmutableList.copyOf(list);
}
#Override
public void writeTo(Output output, int number, ImmutableList<?> value, boolean repeated) throws IOException {
ArrayList<?> list = Lists.newArrayList(value);
output.writeObject(number, list, LIST_SCHEMA, repeated);
LIST_SCHEMA.writeTo(output, list);
}
#Override
public void transfer(Pipe pipe, Input input, Output output, int number, boolean repeated) throws IOException {
throw new UnsupportedOperationException("TODO");
}
#Override
public Class<?> typeClass() {
return ImmutableList.class;
}
}
import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtostuffIOUtil;
import com.dyuproject.protostuff.runtime.DefaultIdStrategy;
import com.dyuproject.protostuff.runtime.RuntimeEnv;
import com.google.common.collect.ImmutableList;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class ImmutableListDelegateTest {
#Before
public void before() {
// registers the delegate
if (RuntimeEnv.ID_STRATEGY instanceof DefaultIdStrategy) {
((DefaultIdStrategy) RuntimeEnv.ID_STRATEGY).registerDelegate(new ImmutableListDelegate());
}
}
#Test
public void testDelegate() throws IOException {
Foo foo = new Foo(ImmutableList.of("foo"));
Assert.assertEquals(foo, serializeThenDeserialize(foo));
}
private Foo serializeThenDeserialize(Foo fooToSerialize) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ProtostuffIOUtil.writeDelimitedTo(out, fooToSerialize, Foo.SCHEMA, buffer());
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Foo fooDeserialized = Foo.SCHEMA.newMessage();
ProtostuffIOUtil.mergeDelimitedFrom(in, fooDeserialized, Foo.SCHEMA, buffer());
return fooDeserialized;
}
private LinkedBuffer buffer() {
return LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
}
}
The test fails with the following exception, which seems to mean that my delegate only deserializes null values:
java.lang.NullPointerException
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:191)
at com.google.common.collect.SingletonImmutableList.<init>(SingletonImmutableList.java:40)
at com.google.common.collect.ImmutableList.asImmutableList(ImmutableList.java:305)
at com.google.common.collect.ImmutableList.copyFromCollection(ImmutableList.java:314)
at com.google.common.collect.ImmutableList.copyOf(ImmutableList.java:253)
at test.ImmutableListDelegate.readFrom(ImmutableListDelegate.java:25)
at test.ImmutableListDelegate.readFrom(ImmutableListDelegate.java:12)
at com.dyuproject.protostuff.runtime.RuntimeUnsafeFieldFactory$19$1.mergeFrom(RuntimeUnsafeFieldFactory.java:1111)
at com.dyuproject.protostuff.runtime.MappedSchema.mergeFrom(MappedSchema.java:188)
at com.dyuproject.protostuff.IOUtil.mergeDelimitedFrom(IOUtil.java:109)
at com.dyuproject.protostuff.ProtostuffIOUtil.mergeDelimitedFrom(ProtostuffIOUtil.java:151)
at test.ImmutableListDelegateTest.serializeThenDeserialize(ImmutableListDelegateTest.java:38)
at test.ImmutableListDelegateTest.testDelegate(ImmutableListDelegateTest.java:30)
Is this the right approach? What am I missing?
This is not a duplicate of the What is a Null Pointer Exception, and how do I fix it? question, which makes no sense. The fact that I mentioned that an NPE is thrown when trying to use a Protostuff delegate to de-serialize immutable collections, doesn't mean that this duplicates the "What is a NPE?" question in any way, shape, or form.
Everything looks fine and the
java.lang.NullPointerException
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:191)
at com.google.common.collect.SingletonImmutableList.<init>(SingletonImmutableList.java:40)
says that you're trying to put null into an ImmutableList, which is forbidden. To be sure, inspect you list just before the failing line. Make sure your input json doesn't look like [null].

Categories