Jump to content

Recommended Posts

Posted

the problem is in the line i showed you, or something somewhere else blocks it, because other lines of code right before this execute properly.

 

and for the testing i used detector with 30 damage taken, coil 3, rod 3, display 3 and battery 3

 

package vms.archeology.items;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Nullable;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.IItemPropertyGetter;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.util.Constants.NBT;
import vms.archeology.Reference;
import vms.archeology.blocks.ArcheologyDirt;
import vms.archeology.handlers.EnumHandler;
import vms.archeology.init.ModBlocks;
import vms.archeology.util.Utils;

public class Detector extends Item{


private List<BlockPos> modifiers = new ArrayList();
private BlockPos scanBlock;


public Detector(String unlocalizedName){
	this.setUnlocalizedName(unlocalizedName);
	this.setRegistryName(new ResourceLocation(Reference.MODID, unlocalizedName));
	this.setMaxDamage(200000);

	this.addPropertyOverride(new ResourceLocation("found"), new IItemPropertyGetter(){
		@Override
		public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) {
			NBTTagCompound nbt = stack.getTagCompound();
			if(nbt != null){
				if(nbt.hasKey("found")){
					if (nbt.getBoolean("found")){
						return 1.0F;
					}
				}
			}
			return 0;
		}
	});
	this.addPropertyOverride(new ResourceLocation("coil"), new IItemPropertyGetter(){
		@Override
		public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) {
			NBTTagCompound nbt = stack.getTagCompound();
			if(nbt != null){
			if (nbt.hasKey("coil"))
				return ((float)nbt.getInteger("coil"))/10;
			}
			return 0;
		}
	});
	this.addPropertyOverride(new ResourceLocation("battery"), new IItemPropertyGetter(){
		@Override
		public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) {
			NBTTagCompound nbt = stack.getTagCompound();
			if(nbt != null){
			if (nbt.hasKey("battery"))
				return ((float)nbt.getInteger("battery"))/10;
			}
			return 0;
		}
	});
	this.addPropertyOverride(new ResourceLocation("rod"), new IItemPropertyGetter(){
		@Override
		public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) {
			NBTTagCompound nbt = stack.getTagCompound();
			if(nbt != null){
			if (nbt.hasKey("rod"))
				return ((float)nbt.getInteger("rod"))/10;
			}
			return 0;
		}
	});
	this.addPropertyOverride(new ResourceLocation("display"), new IItemPropertyGetter(){
		@Override
		public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) {
			NBTTagCompound nbt = stack.getTagCompound();
			if(nbt != null){
			if (nbt.hasKey("display"))
				return ((float)nbt.getInteger("display"))/10;
			}
			return 0;
		}
	});
	this.addPropertyOverride(new ResourceLocation("distance"), new IItemPropertyGetter(){
		@Override
		public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) {
			NBTTagCompound nbt = stack.getTagCompound();
			if(nbt != null){
			if (nbt.hasKey("distance_squared"))
				return ((float)nbt.getDouble("distance_squared"))/10000;
			}
			return 0;
		}
	});
	this.addPropertyOverride(new ResourceLocation("type"), new IItemPropertyGetter(){
		@Override
		public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) {
			NBTTagCompound nbt = stack.getTagCompound();
			if(nbt != null){
			if (nbt.hasKey("type"))
				return (((float)nbt.getInteger("type")) + 1)/10;
			}
			return 0;
		}
	});
}

@Override
public void onCreated(ItemStack stack, World worldIn, EntityPlayer playerIn) {
	if(stack.getTagCompound() != null || !(stack.hasTagCompound())){
		NBTTagCompound nbt = new NBTTagCompound();
		nbt.setInteger("coil", 3);
		nbt.setInteger("battery", 3);
		nbt.setInteger("rod", 3);
		nbt.setInteger("display", 3);

		nbt.setBoolean("found", false);
		nbt.setInteger("type", -1);
		nbt.setDouble("distance_squared", 0.0);
		nbt.setInteger("direction", 0);
		stack.setTagCompound(nbt);
	}
	super.onCreated(stack, worldIn, playerIn);
}

@Override
public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
	ItemStack stack = player.getHeldItem(hand);
	NBTTagCompound nbt = stack.getTagCompound();
	if (nbt != null){
		nbt.setBoolean("found", false);
		stack.setTagCompound(nbt);
	}
	int x = 0;
	int y = 0;
	int z = 0;
	modifiers.clear();
	if(nbt != null){
		if(nbt.hasKey("coil") && nbt.hasKey("battery")){
			int batteryMulti = 0;
			int coilMulti = 0;
			Utils.getLogger().info("Damage: " + stack.getItemDamage());
			if (stack.getItemDamage() < 199500){					
				switch(nbt.getInteger("coil")){
				case 1:
					generateScan(0, 1, 2);
					generateScan(-1, 1, 1);
					Scan(pos, worldIn, false, stack);
					coilMulti = 1;
					break;
				case 2:
					generateScan(0, 2, 3);
					generateScan(-1, 1, 2);
					generateScan(-2, 0, 0);
					Scan(pos, worldIn, false, stack);
					coilMulti = 2;
					break;
				case 3:
					generateScan(0, 3, 4);
					generateScan(-1, 2, 3);
					generateScan(-2, 1, 2);
					Scan(pos, worldIn, false, stack);
					coilMulti = 4;
					break;
				case 4:
					generateScan(2, 1, 2);
					generateScan(1, 2, 3);
					generateScan(0, 2, 3);
					generateScan(-1, 2, 3);
					generateScan(-2, 1, 2);
					Scan(pos, worldIn, false, stack);
					coilMulti = 5;
					break;
				case 5:
					generateScan(0, 2, 3);
					generateScan(-1, 1, 2);
					generateScan(-2, 0, 0);
					Scan(pos, worldIn, true, stack);
					coilMulti = 4;
					break;
				case 6:
					generateScan(0, 1, 2);
					generateScan(-1, 1, 2);
					generateScan(-2, 1, 2);
					generateScan(-3, 1, 1);
					generateScan(-4, 1, 1);
					generateScan(-5, 0, 0);
					Scan(pos, worldIn, false, stack);
					coilMulti = 4;
					break;
				}
				Utils.getLogger().info("Coil multi: " + coilMulti);
				switch(nbt.getInteger("battery")){
				case 1:
					batteryMulti = 100;
					break;
				case 2:
					batteryMulti = 25;
					break;
				case 3:
					batteryMulti = 10;
					break;
				case 4:
					batteryMulti = 4;
					break;
				case 5:
					batteryMulti = 8;
					break;
				case 6:
					batteryMulti = 34;
					break;
				case 7:
					batteryMulti = 18;
					break;						
				}
				Utils.getLogger().info("Battery multi: " + batteryMulti);

				Utils.getLogger().info("Total multi: " + batteryMulti * coilMulti);

				stack.damageItem(batteryMulti * coilMulti, player);

			} else {
				stack.damageItem(199999 - stack.getItemDamage(), player);
			}
		}
	}



	return super.onItemUse(player, worldIn, pos, hand, facing, hitX, hitY, hitZ);
}

public void generateScan(int y, int radius, int max){
	for(int x = 0 - radius; x <= radius; x++){
		for(int z = 0 - radius; z <= radius; z++){
			if(Math.abs(x) + Math.abs(z) <= max)
				modifiers.add(new BlockPos(x, y, z));
		}
	}
}

public void Scan(BlockPos pos, World world, Boolean ultraSound, ItemStack stack){
	NBTTagCompound nbt = stack.getTagCompound();
	for (int i = 0; i < modifiers.size(); i++) {
		scanBlock = new BlockPos(pos.getX() + modifiers.get(i).getX(), pos.getY() + modifiers.get(i).getY(), pos.getZ() + modifiers.get(i).getZ());
		if (world.getBlockState(scanBlock).getBlock().equals(ModBlocks.archeology_dirt)){	
			if(ultraSound && ArcheologyDirt.getIntFromState(world.getBlockState(scanBlock)) != 0 && ArcheologyDirt.getIntFromState(world.getBlockState(scanBlock)) != 14){
				nbt.setBoolean("found", true);
				nbt.setDouble("distance_squared", scanBlock.distanceSqToCenter(pos.getX(), pos.getY(), pos.getZ()));
				nbt.setInteger("type", EnumHandler.Types.getTypeId(ArcheologyDirt.getTypeFromMeta(ArcheologyDirt.getIntFromState((world.getBlockState(scanBlock))))));
				nbt.setInteger("direction", getDirection(modifiers.get(i)));
				stack.setTagCompound(nbt);

			} else if(ArcheologyDirt.getMetalFromMeta(ArcheologyDirt.getIntFromState(world.getBlockState(scanBlock)))){
				nbt.setBoolean("found", true);
				nbt.setDouble("distance_squared", scanBlock.distanceSqToCenter(pos.getX(), pos.getY(), pos.getZ()));
				nbt.setInteger("type", EnumHandler.Types.getTypeId(ArcheologyDirt.getTypeFromMeta(ArcheologyDirt.getIntFromState((world.getBlockState(scanBlock))))));
				nbt.setInteger("direction", getDirection(modifiers.get(i)));
				stack.setTagCompound(nbt);
			}
		}
	}
}

@Override
public void addInformation(ItemStack stack, EntityPlayer playerIn, List<String> tooltip, boolean advanced) {
	NBTTagCompound nbt = stack.getTagCompound();
	if(nbt != null && !(nbt.hasNoTags())){
		if (nbt.hasKey("coil"))
			tooltip.add("Coil: " + String.valueOf(nbt.getInteger("coil")));
		else
			tooltip.add("Needs coil");
		if (nbt.hasKey("battery"))
			tooltip.add("Battery: " + String.valueOf(nbt.getInteger("battery")));
		else
			tooltip.add("Needs battery");
		if (nbt.hasKey("display"))
			tooltip.add("Display: " + String.valueOf(nbt.getInteger("display")));
		else
			tooltip.add("Needs display");
		if (nbt.hasKey("rod"))
			tooltip.add("Rod: " + String.valueOf(nbt.getInteger("rod")));
		else
			tooltip.add("Needs rod");

		if (nbt.hasKey("found"))
			tooltip.add("Found something: " + String.valueOf(nbt.getBoolean("found")));		
	}
	super.addInformation(stack, playerIn, tooltip, advanced);

}

public int getDirection(BlockPos pos){
	int x = pos.getX();
	int z = pos.getZ();
	if(x == 0){
		if (z == 0)
			return 0;
		if (z > 0)
			return 1;
		if (z < 0);
			return 5;
	}
	float d = Math.abs((float)z)/((float)x);
	if (d > 2.4){
		if (z > 0)
			return 1;
		return 5;						
	}
	if (d < 0.4){
		if (x > 0)
			return 3;
		return 7;	
	}
	if (x > 0){
		if (z > 0)
			return 2;
		return 4;	
	}
	if (z > 0)
		return 8;
	return 6;	

}

@Override
public boolean shouldCauseReequipAnimation(ItemStack oldStack, ItemStack newStack, boolean slotChanged) {
	if (oldStack.getItem().equals(newStack.getItem())){
		//return false;
	}
	return super.shouldCauseReequipAnimation(oldStack, newStack, slotChanged);
}


}

Posted
Utils.getLogger().info("Battery multi: " + batteryMulti);
Utils.getLogger().info("Total multi: " + batteryMulti * coilMulti);

What are the outputs of these? I suspect the call to ItemStack#damageItem is working, just with a value of 0.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Posted
3 minutes ago, Villfuk02 said:

When i put two detectors in crafting table or anvil, they combine and repair. I don't want that to be possible, anybody knows how?

 

Call Item#setNoRepair to stop an Item from being repaired.

 

 

  • Like 1

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted
4 minutes ago, Villfuk02 said:

how do i thank you in this new version of the forum?

 

You can like posts by clicking the "Like this" button in the bottom right of the post. I think that's the only thanking mechanism available now.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted
14 minutes ago, Villfuk02 said:

Thanks for everything, now i'd like to make the item use capabilities instead of nbt to store the values.
Where do i start?

 

Start by reading the documentation.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted (edited)

But there are capabilities for storing items, fluids and energy, but i need to just store some ints bools doubles and some fields

 

so what?

Edited by Villfuk02

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

    • Im trying to build my mod using shade since i use the luaj library however i keep getting this error Reason: Task ':reobfJar' uses this output of task ':shadowJar' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed. So i try adding reobfJar.dependsOn shadowJar  Could not get unknown property 'reobfJar' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler. my gradle file plugins { id 'eclipse' id 'idea' id 'maven-publish' id 'net.minecraftforge.gradle' version '[6.0,6.2)' id 'com.github.johnrengelman.shadow' version '7.1.2' id 'org.spongepowered.mixin' version '0.7.+' } apply plugin: 'net.minecraftforge.gradle' apply plugin: 'org.spongepowered.mixin' apply plugin: 'com.github.johnrengelman.shadow' version = mod_version group = mod_group_id base { archivesName = mod_id } // Mojang ships Java 17 to end users in 1.18+, so your mod should target Java 17. java.toolchain.languageVersion = JavaLanguageVersion.of(17) //jarJar.enable() println "Java: ${System.getProperty 'java.version'}, JVM: ${System.getProperty 'java.vm.version'} (${System.getProperty 'java.vendor'}), Arch: ${System.getProperty 'os.arch'}" minecraft { mappings channel: mapping_channel, version: mapping_version copyIdeResources = true runs { configureEach { workingDirectory project.file('run') property 'forge.logging.markers', 'REGISTRIES' property 'forge.logging.console.level', 'debug' arg "-mixin.config=derp.mixin.json" mods { "${mod_id}" { source sourceSets.main } } } client { // Comma-separated list of namespaces to load gametests from. Empty = all namespaces. property 'forge.enabledGameTestNamespaces', mod_id } server { property 'forge.enabledGameTestNamespaces', mod_id args '--nogui' } gameTestServer { property 'forge.enabledGameTestNamespaces', mod_id } data { workingDirectory project.file('run-data') args '--mod', mod_id, '--all', '--output', file('src/generated/resources/'), '--existing', file('src/main/resources/') } } } sourceSets.main.resources { srcDir 'src/generated/resources' } repositories { flatDir { dirs './libs' } maven { url = "https://jitpack.io" } } configurations { shade implementation.extendsFrom shade } dependencies { minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}" implementation 'org.luaj:luaj-jse-3.0.2' implementation fg.deobf("com.github.Virtuoel:Pehkui:${pehkui_version}") annotationProcessor 'org.spongepowered:mixin:0.8.5:processor' minecraftLibrary 'luaj:luaj-jse:3.0.2' shade 'luaj:luaj-jse:3.0.2' } // Example for how to get properties into the manifest for reading at runtime. tasks.named('jar', Jar).configure { manifest { attributes([ 'Specification-Title' : mod_id, 'Specification-Vendor' : mod_authors, 'Specification-Version' : '1', // We are version 1 of ourselves 'Implementation-Title' : project.name, 'Implementation-Version' : project.jar.archiveVersion, 'Implementation-Vendor' : mod_authors, 'Implementation-Timestamp': new Date().format("yyyy-MM-dd'T'HH:mm:ssZ"), "TweakClass" : "org.spongepowered.asm.launch.MixinTweaker", "TweakOrder" : 0, "MixinConfigs" : "derp.mixin.json" ]) } rename 'mixin.refmap.json', 'derp.mixin-refmap.json' } shadowJar { archiveClassifier = '' configurations = [project.configurations.shade] finalizedBy 'reobfShadowJar' } assemble.dependsOn shadowJar reobf { re shadowJar {} } publishing { publications { mavenJava(MavenPublication) { artifact jar } } repositories { maven { url "file://${project.projectDir}/mcmodsrepo" } } }  
    • All versions of Minecraft Forge suddenly black screen even without mods (tried reinstalling original Minecraft, Java, updating drivers doesn't work)
    • When i join minecraft all ok, when i join world all working fine, but when i open indentity menu, i get this The game crashed whilst unexpected error Error: java.lang.NullPointerException: Cannot invoke "top.ribs.scguns.common.Gun$Projectile.getDamage()" because "this.projectile" is null crash report here https://paste.ee/p/0vKaf
  • Topics

×
×
  • Create New...

Important Information

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