Jump to content

Recommended Posts

Posted

Hi all,

Today I pose an interesting problem. I've written up some code for a gun and on SSP it works flawlessly. But for some reason, when using it in SMP the onItemRightClick and onUpdate methods don't go threw and the item doesn't work. Any ideas?

 

 

package blfngl.fallout.gun;

import java.util.List;
import java.util.Random;

import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import blfngl.fallout.Fallout;
import blfngl.fallout.entity.EntityBullet;
import blfngl.fallout.handler.PerkHandler;

public class BaseGun extends Item
{
private int damage;
private double reloadtick;
private double reloadmax;
private double clipid;
private double firetick;
private double firemax;
private String firesound;
private String reloadsound;
public int count = 0;
public int clipSize;
public Item ammoType;
private int critChance;
private int critDamage;
public int rounds;
public int gunHealth;
public int cnd;
public String name;
private int tempDam;
public int calcCND;

Random rand = new Random();

public boolean isFull3D()
{
	return true;
}

public BaseGun(int var1, int var2, int var3, double var4, double var5, String var6, String var7, Item var8, int var9)
{
	super(var1);
	damage = var2;
	firemax = var5*10;
	firetick = firemax;
	reloadmax = 50;
	reloadtick = var4;
	firesound = var6;
	reloadsound = var7;
	setMaxStackSize(1);
	setMaxDamage(var9);
	clipSize = var3;
	ammoType = var8;
	critChance = 5;
	critDamage = 4;
	cnd = var9;
	tempDam = damage;
}

public ItemStack onItemRightClick(ItemStack var1, World var2, EntityPlayer var3)
{
	if (!var2.isRemote && rounds > 0)
	{
		if (firetick == firemax && firemax != 0)
		{
			if (PerkHandler.grunt)
			{
				if (itemID == Fallout.pistol9mm.itemID){damage += damage * .25;}
				if (itemID == Fallout.pistol45Auto.itemID ){damage += damage * .25;}
				if (itemID == Fallout.smg45Auto.itemID){damage += damage * .25;}
				if (itemID == Fallout.rifleService.itemID ){damage += damage * .25;}
				if (itemID == Fallout.rifleMarksmanCarbine.itemID){damage += damage * .25;}
				if (itemID == Fallout.rifleAllAmerican.itemID){damage += damage * .25;}
				if (itemID == Fallout.rifleAssaultCarbine.itemID){damage += damage * .25;}
				if (itemID == Fallout.rifleLMG.itemID){damage += damage * .25;}
				if (itemID == Fallout.rifleBozar.itemID){damage += damage * .25;}
			}

			if (PerkHandler.cowboy)
			{
				if (itemID == Fallout.rifleLeverAction.itemID){damage += damage * .25;}
				if (itemID == Fallout.pistol357.itemID){damage += damage * .25;}
				if (itemID == Fallout.pistol44.itemID){damage += damage * .25;}
				if (itemID == Fallout.pistol32.itemID){damage += damage * .25;}
				if (itemID == Fallout.pistolBlackhawk.itemID){damage += damage * .25;}
				if (itemID == Fallout.pistolCallahans.itemID){damage += damage * .25;}
				if (itemID == Fallout.pistolPaulsons.itemID){damage += damage * .25;}
				if (itemID == Fallout.pistolScoped44.itemID){damage += damage * .25;}
				if (itemID == Fallout.pistolRangerSequoia.itemID){damage += damage * .25;}
				if (itemID == Fallout.pistolHuntingRevolver.itemID){damage += damage * .25;}
				if (itemID == Fallout.pistolLucky.itemID){damage += damage * .25;}
				if (itemID == Fallout.pistol556.itemID){damage += damage * .25;}
				if (itemID == Fallout.pistolThatGun.itemID){damage += damage * .25;}
				if (itemID == Fallout.rifleAbileneKid.itemID){damage += damage * .25;}
				if (itemID == Fallout.rifleBBGun.itemID){damage += damage * .25;}
				if (itemID == Fallout.rifleTrailCarbine.itemID){damage += damage * .25;}
				if (itemID == Fallout.rifleMedicineStick.itemID){damage += damage * .25;}
				if (itemID == Fallout.rifleBrushGun.itemID){damage += damage * .25;}
				if (itemID == Fallout.rifleCowboyRepeater.itemID){damage += damage * .25;}
				if (itemID == Fallout.rifleLongueCarabine.itemID){damage += damage * .25;}
			}

			if (PerkHandler.laserCommander && ammoType.itemID == Fallout.cellElectron.itemID){damage += damage * 0.15;}
			if (PerkHandler.laserCommander && ammoType.itemID == Fallout.cellMF.itemID){damage += damage * 0.15;}
			if (PerkHandler.laserCommander && ammoType.itemID == Fallout.cellEnergy.itemID){damage += damage * 0.15;}

			if (PerkHandler.bloodyMess){damage += .05 * damage;}

			if (rand.nextInt(100)+1 <= critChance)
			{
				var2.spawnEntityInWorld(new EntityBullet(var2, var3, damage + rand.nextInt(critDamage)+1, 1));
			}

			else {var2.spawnEntityInWorld(new EntityBullet(var2, var3, damage, 1));}

			var2.playSoundAtEntity(var3, firesound, 1.0F, 1.0F);
			var1.damageItem(1, var3);

			if (!var3.capabilities.isCreativeMode)
			{
				rounds-=1;
				if (ammoType.itemID == Fallout.a10mm.itemID){var3.inventory.addItemStackToInventory(new ItemStack(Fallout.case10mm));}
				if (ammoType.itemID == Fallout.a22LR.itemID){var3.inventory.addItemStackToInventory(new ItemStack(Fallout.case22LR));}
				if (ammoType.itemID == Fallout.a9mm.itemID){var3.inventory.addItemStackToInventory(new ItemStack(Fallout.case9mm));}
				if (ammoType.itemID == Fallout.a357.itemID){var3.inventory.addItemStackToInventory(new ItemStack(Fallout.case357));}
				if (ammoType.itemID == Fallout.a44.itemID){var3.inventory.addItemStackToInventory(new ItemStack(Fallout.case44));}
				if (ammoType.itemID == Fallout.a45Auto.itemID){var3.inventory.addItemStackToInventory(new ItemStack(Fallout.case45Auto));}
				if (ammoType.itemID == Fallout.a50MG.itemID){var3.inventory.addItemStackToInventory(new ItemStack(Fallout.case50MG));}
				if (ammoType.itemID == Fallout.aGovt.itemID){var3.inventory.addItemStackToInventory(new ItemStack(Fallout.caseGovt));}
				if (ammoType.itemID == Fallout.a556.itemID){var3.inventory.addItemStackToInventory(new ItemStack(Fallout.case556));}
				if (ammoType.itemID == Fallout.a5mm.itemID){var3.inventory.addItemStackToInventory(new ItemStack(Fallout.case5mm));}
				if (ammoType.itemID == Fallout.a127.itemID){var3.inventory.addItemStackToInventory(new ItemStack(Fallout.case127));}
				if (ammoType.itemID == Fallout.aGauge20.itemID){var3.inventory.addItemStackToInventory(new ItemStack(Fallout.hull20));}
				if (ammoType.itemID == Fallout.aGauge12.itemID){var3.inventory.addItemStackToInventory(new ItemStack(Fallout.hull12));}

				if (PerkHandler.vigilantRecycle)
				{
					if (ammoType.itemID == Fallout.cellMF.itemID && rand.nextInt(100) + 1 > 30){var3.inventory.addItemStackToInventory(new ItemStack(Fallout.cellMFD));}
					if (ammoType.itemID == Fallout.cellElectron.itemID && rand.nextInt(100) + 1 > 30){var3.inventory.addItemStackToInventory(new ItemStack(Fallout.cellECPD));}
					if (ammoType.itemID == Fallout.cellEnergy.itemID && rand.nextInt(100) + 1 > 30){var3.inventory.addItemStackToInventory(new ItemStack(Fallout.cellED));}
				}
				if (ammoType.itemID == Fallout.cellMF.itemID && rand.nextInt(100) + 1 > 65){var3.inventory.addItemStackToInventory(new ItemStack(Fallout.cellMFD));}
				if (ammoType.itemID == Fallout.cellElectron.itemID && rand.nextInt(100) + 1 > 65){var3.inventory.addItemStackToInventory(new ItemStack(Fallout.cellECPD));}
				if (ammoType.itemID == Fallout.cellEnergy.itemID && rand.nextInt(100) + 1 > 65){var3.inventory.addItemStackToInventory(new ItemStack(Fallout.cellED));}
			}

			firetick = 0;
		}
		else{/*++firetick;*/}
	}

	if (firetick == firemax && firemax != 0 && rounds > 0){}
	damage = tempDam;
	return var1;
}

public void func_94581_a(IconRegister iconRegister)
{
	itemIcon = iconRegister.registerIcon("blfngl" + ":" + this.getUnlocalizedName().substring(this.getUnlocalizedName().indexOf(".") + 1));
}

public void onCreated(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer){name = par3EntityPlayer.username;}

public void addInformation(ItemStack var1, EntityPlayer var2, List var3, boolean var4)
{
	gunHealth = var1.getItemDamage();
	var3.add("\u00A74DAM: " + (double)(tempDam / 2));// * calcCND);
	var3.add("\u00A79Clip size: " + rounds + "/" + clipSize + " Ammo Loaded");
	var3.add("\u00A72Ammo type: " + ammoType.getItemDisplayName(new ItemStack(ammoType)));
	var3.add("\u00A76CND: " + (cnd - gunHealth));

	if(name !=null){var3.add("\u00A7eCrafted by: " + name);}
	else{var3.add("\u00A7eNo Owner.");}
}

boolean reloading = false;

public void onUpdate(ItemStack par1ItemStack, World var2, Entity par3Entity, int par4, boolean par5) 
{
	EntityPlayer var3 = (EntityPlayer)par3Entity;

	if (!var2.isRemote && var3.inventory.hasItem(ammoType.itemID) && rounds<clipSize && Fallout.isReloading && !reloading)
	{
		reloading=true;

		if (reloadtick >= reloadmax)
		{
			reloadtick = 0;
			count = 0;
			var2.playSoundAtEntity(var3, reloadsound, 1.0F, 1.0F);

			while (rounds<clipSize && var3.inventory.hasItem(ammoType.itemID))
			{
				var3.inventory.consumeInventoryItem(ammoType.itemID);
				rounds += 1;
			}
		}

		else{++reloadtick;}
		reloading=false;
	}

	if(reloadtick<reloadmax){reloadtick+=1;}
	if(firetick<firemax){firetick+=1;}

	//TODO get scopes to work on servers
	/**if (Fallout.isScoped)
	{
		ModLoader.getMinecraftInstance().gameSettings.fovSetting = -1.0F;
		MinecraftForgeClient.registerItemRenderer(Fallout.rifleBozar.itemID, new RenderBozarScoped());
		MinecraftForgeClient.registerItemRenderer(Fallout.rifleRatslayer.itemID, new RenderRatslayerScoped());
		MinecraftForgeClient.registerItemRenderer(Fallout.rifleChineseAssault.itemID, new RenderChineseAssaultRifleScoped());
		MinecraftForgeClient.registerItemRenderer(Fallout.pistol45Auto.itemID, new Render45AutoScoped());

	}

	else
	{
		ModLoader.getMinecraftInstance().gameSettings.fovSetting = 0.0F;
		MinecraftForgeClient.registerItemRenderer(Fallout.rifleBozar.itemID, new RenderBozar());
		MinecraftForgeClient.registerItemRenderer(Fallout.rifleRatslayer.itemID, new RenderRatslayer());
		MinecraftForgeClient.registerItemRenderer(Fallout.rifleChineseAssault.itemID, new RenderChineseAssaultRifle());
		MinecraftForgeClient.registerItemRenderer(Fallout.pistol45Auto.itemID, new Render45Auto());
	}**/
}
}

 

There isn't an error, it just doesn't work.

Posted

public ItemStack onItemRightClick(ItemStack var1, World var2, EntityPlayer var3)
{
	if (!var2.isRemote && rounds > 0)
	{

public void onUpdate(ItemStack par1ItemStack, World var2, Entity par3Entity, int par4, boolean par5) 
{
	EntityPlayer var3 = (EntityPlayer)par3Entity;

	if (!var2.isRemote && var3.inventory.hasItem(ammoType.itemID) && rounds<clipSize && Fallout.isReloading && !reloading)
	{

The !isRemote would be my first guess.

BEWARE OF GOD

---

Co-author of Pentachoron Labs' SBFP Tech.

Posted

public ItemStack onItemRightClick(ItemStack var1, World var2, EntityPlayer var3)
{
	if (!var2.isRemote && rounds > 0)
	{

public void onUpdate(ItemStack par1ItemStack, World var2, Entity par3Entity, int par4, boolean par5) 
{
	EntityPlayer var3 = (EntityPlayer)par3Entity;

	if (!var2.isRemote && var3.inventory.hasItem(ammoType.itemID) && rounds<clipSize && Fallout.isReloading && !reloading)
	{

The !isRemote would be my first guess.

I was testing it and apparently the rounds value isn't changing properly...any ideas?
Posted

public ItemStack onItemRightClick(ItemStack var1, World var2, EntityPlayer var3)
{
	if (!var2.isRemote && rounds > 0)
	{

public void onUpdate(ItemStack par1ItemStack, World var2, Entity par3Entity, int par4, boolean par5) 
{
	EntityPlayer var3 = (EntityPlayer)par3Entity;

	if (!var2.isRemote && var3.inventory.hasItem(ammoType.itemID) && rounds<clipSize && Fallout.isReloading && !reloading)
	{

The !isRemote would be my first guess.

I was testing it and apparently the rounds value isn't changing properly...any ideas?

In both client and server?

BEWARE OF GOD

---

Co-author of Pentachoron Labs' SBFP Tech.

Posted

public ItemStack onItemRightClick(ItemStack var1, World var2, EntityPlayer var3)
{
	if (!var2.isRemote && rounds > 0)
	{

public void onUpdate(ItemStack par1ItemStack, World var2, Entity par3Entity, int par4, boolean par5) 
{
	EntityPlayer var3 = (EntityPlayer)par3Entity;

	if (!var2.isRemote && var3.inventory.hasItem(ammoType.itemID) && rounds<clipSize && Fallout.isReloading && !reloading)
	{

The !isRemote would be my first guess.

I was testing it and apparently the rounds value isn't changing properly...any ideas?

In both client and server?

It's acting very strangely. When I remove the !var2.isRemote completely, when firing on a server the rounds variable never changes and on the client no audio plays. If the !var2.isRemote is there, then nothing happens on the server but the item works perfectly in SSP. If it's var2.isRemote instead, no audio is played on the client and the audio spazzes out on the server.

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

    • I know that this may be a basic question, but I am very new to modding. I am trying to have it so that I can create modified Vanilla loot tables that use a custom enchantment as a condition (i.e. enchantment present = item). However, I am having trouble trying to implement this; the LootItemRandomChanceWithEnchantedBonusCondition constructor needs a Holder<Enchantment> and I am unable to use the getOrThrow() method on the custom enchantment declared in my mod's enchantments class. Here is what I have so far in the GLM:   protected void start(HolderLookup.Provider registries) { HolderLookup.RegistryLookup<Enchantment> registrylookup = registries.lookupOrThrow(Registries.ENCHANTMENT); LootItemRandomChanceWithEnchantedBonusCondition lootItemRandomChanceWithEnchantedBonusCondition = new LootItemRandomChanceWithEnchantedBonusCondition(0.0f, LevelBasedValue.perLevel(0.07f), registrylookup.getOrThrow(*enchantment here*)); this.add("nebu_from_deepslate", new AddItemModifier(new LootItemCondition[]{ LootItemBlockStatePropertyCondition.hasBlockStateProperties(Blocks.DEEPSLATE).build(), LootItemRandomChanceCondition.randomChance(0.25f).build(), lootItemRandomChanceWithEnchantedBonusCondition }, OrichalcumItems.NEBU.get())); }   Inserting Enchantments.[vanilla enchantment here] actually works but trying to declare an enchantment from my custom enchantments class as [mod enchantment class].[custom enchantment] does not work even though they are both a ResourceKey and are registered in Registries.ENCHANTMENT. Basically, how would I go about making it so that a custom enchantment declared as a ResourceKey<Enchantment> of value ResourceKey.create(Registries.ENCHANTMENT, ResourceLocation.fromNamespaceAndPath([modid], [name])), declared in a seperate enchantments class, can be used in the LootItemRandomChanceWithEnchantedBonusCondition constructor as a Holder? I can't use getOrThrow() because there is no level or block entity/entity in the start() method and it is running as datagen. It's driving me nuts.
    • Hi here is an update. I was able to fix the code so my mod does not crash Minecraft. Please understand that I am new to modding but I honestly am having a hard time understanding how anyone can get this to work without having extensive programming and debugging experience as well as searching across the Internet, multiple gen AI bots (claude, grok, openai), and examining source code hidden in the gradle directory and in various github repositories. I guess I am wrong because clearly there are thousands of mods so maybe I am just a newbie. Ok, rant over, here is a step by step summary so others can save the 3 days it took me to figure this out.   1. First, I am using forge 54.1.0 and Minecraft 1.21.4 2. I am creating a mod to add a shotgun to Minecraft 3. After creating the mod and compiling it, I installed the .jar file to the proper directory in Minecraft and used 1.21.4-forge-54.1.0 4. The mod immediately crashed with the error: Caused by: java.lang.NullPointerException: Item id not set 5. Using the stack trace, I determined that the Exception was being thrown from the net.minecraft.world.item.Item.Properties class 6. It seems that there are no javadocs for this class, so I used IntelliJ which was able to provide a decompiled version of the class, which I then examined to see the source of the error. Side question: Are there javadocs? 7. This method, specifically, was the culprit: protected String effectiveDescriptionId() {      return this.descriptionId.get(Objects.requireNonNull(this.id, "Item id not set"));  } 8. Now my quest was to determine how to set this.id. Looking at the same source file, I determined there was another method:  public Item.Properties setId(ResourceKey<Item> pId) {             this.id = pId;             return this;   } 9. So now, I need to figure out how to call setId(). This required working backwards a bit. Starting from the constructor, I stubbed out the variable p which is of type Item.Properties public static final RegistryObject<Item> SHOTGUN = ITEMS.register("shotgun", () -> new ShotgunItem(p)); Rather than putting this all on one line, I split it up for readability like this: private static final Item.Properties p = new Item.Properties().useItemDescriptionPrefix().setId(rk); Here is was the missing function, setId(), which takes a type of ResourceKey<Item>. My next problem is that due to the apparent lack of documentation (I tried searching the docs on this site) I could not determine the full import path to ResourceKey. I did some random searching on the Internet and stumbled across a Github repository which gave two clues: import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; Then I created the rk variable like this: private static ResourceKey<Item> rk = ResourceKey.create(Registries.ITEM, ResourceLocation.parse("modid:shotgunmod")); And now putting it all together in order: private static ResourceKey<Item> rk = ResourceKey.create(Registries.ITEM, ResourceLocation.parse("modid:shotgunmod")); private static final Item.Properties p = new Item.Properties().useItemDescriptionPrefix().setId(rk); public static final RegistryObject<Item> SHOTGUN = ITEMS.register("shotgun", () -> new ShotgunItem(p)); This compiled and the mod no longer crashes. I still have more to do on it, but hopefully this will save someone hours. I welcome any feedback and if I missed some obvious modding resource or tutorial that has this information. If not, I might suggest we add it somewhere for people trying to write a mod that doesn't crash. Thank you !!!  
    • I will keep adding to this thread with more information in case anyone can help, or at the very least I can keep my troubleshooting organized. I decided to downgrade to 54.1.0 in the hopes that this would fix the issue but it didn't. At least now I am on a "recommended" version. The crash report did confirm my earlier post that the Exception is coming from effectiveDescriptionId(). I'll continue to see if I can find a way to set the ID manually.   Caused by: java.lang.NullPointerException: Item id not set         at java.base/java.util.Objects.requireNonNull(Objects.java:259) ~[?:?]         at TRANSFORMER/[email protected]/net.minecraft.world.item.Item$Properties.effectiveDescriptionId(Item.java:465) ~[forge-1.21.4-54.1.0-client.jar!/:?]         at TRANSFORMER/[email protected]/net.minecraft.world.item.Item.<init>(Item.java:111) ~[forge-1.21.4-54.1.0-client.jar!/:?]         at TRANSFORMER/[email protected]/com.example.shotgunmod.ShotgunItem.<init>(ShotgunItem.java:19) ~[shotgunmod-1.0.0.jar!/:1.0.0]         at TRANSFORMER/[email protected]/com.example.shotgunmod.ModItems.lambda$static$0(ModItems.java:15) ~[shotgunmod-1.0.0.jar!/:1.0.0]         at TRANSFORMER/[email protected]/net.minecraftforge.registries.DeferredRegister$EventDispatcher.lambda$handleEvent      
    • It just randomly stop working after a rebooted my dedicated server PLEASE HELP!   com.google.gson   Failed to start the minecraft server com.google.gson.JsonSyntaxException: Expected a com.google.gson.JsonObject but was com.google.gson.JsonPrimitive; at path $  
  • Topics

×
×
  • Create New...

Important Information

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