
link1213
Members-
Posts
20 -
Joined
-
Last visited
Everything posted by link1213
-
Shouldn't every player have the capability regardless by doing @SubscribeEvent public static void onAttachCapabilities(AttachCapabilitiesEvent<Entity> event) { if (event.getObject() instanceof PlayerEntity) { event.addCapability(new ResourceLocation(FunWithDrugs.MOD_ID, "drug_use"), new DrugUseProvider()); } } So if somehow the player doesn't have the capability, wouldn't the orElse method give it to them? I wouldn't want there to be a situation where the player does not have the capability
-
So I think I have figured it out, i tested it with a couple events and while It is having no issue getting the data, it seems to have a problem checking it. @SubscribeEvent public static void onPlayerLogsIn(PlayerLoggedInEvent event) { PlayerEntity player = event.getPlayer(); LazyOptional<IDrugUse> drugUse = player.getCapability(DrugUseProvider.DRUG_USE_CAPABILITY, null); IDrugUse pDrugUse = drugUse.orElse(new DrugUse()); player.sendMessage(new StringTextComponent("Your drug use variable is " + (pDrugUse.getDrugUse())), null); } This does display the correct number @SubscribeEvent public void onPlayerDamage(LivingDamageEvent event) { if(event.getEntityLiving() instanceof PlayerEntity) { PlayerEntity player = (PlayerEntity) event.getEntityLiving(); LazyOptional<IDrugUse> drugUse = player.getCapability(DrugUseProvider.DRUG_USE_CAPABILITY, null); IDrugUse pDrugUse = drugUse.orElse(new DrugUse()); if (pDrugUse.getDrugUse() == 5) { player.setHealth(0); } } } This does nothing however. Am I accessing the data incorrectly?
-
So then if i am understanding this correctly, LazyOptional is holding a value, and in this case that value is an instance of my DrugUse capability. So when I am calling the getCapability function, I am getting the LazyOptional that contains that players DrugUse Capability? So I would somehow need to get that info, from the LazyOptional if I wanted to check it for anything?
-
I am grateful for your help so sorry if that last statement sounded rude, I just don't get why you assume that me having trouble = me not knowing java
-
I do know the basics. I have taken 3 college courses on java. What I do not understand is what a LazyOptional is which is why I am so confused. I know exactly how inheritance and casting works, but since I dont get what a LazyOptional is, I do not know if it works in this situation. Hence why I asked the question if that Is what I am to do. I also asked that question as that is the suggestion that Eclipse gave me to solve it so it wasnt a question that I asked out of nowhere.
-
Ah It complied earlier but i just checked and it didn't this time so I must have accidentally changed something before I posted the code here. So how would I want to go about this then? Would I just want to cast the LazyOptional to an IDrugUse? Or is the issue a lot more complex?
-
So I have been trying to make a capability and attach it to the player. This capability would monitor how much they are using the "drugs", which are just custom potions, in my mod and have negative consequences if they use them a lot. I have been following the documentation and guides but I still haven't managed to add the capability properly and I can't figure out why. package com.mitymods.funwithdrugs.capabilities; public interface IDrugUse { public void remove(float points); public void add(float points); public void setDrugUse(float points); public float getDrugUse(); } package com.mitymods.funwithdrugs.capabilities; public class DrugUse implements IDrugUse { private float drug_use; public DrugUse() { this.drug_use = 5; } @Override public void remove(float points) { // TODO Auto-generated method stub this.drug_use -= points; } @Override public void add(float points) { // TODO Auto-generated method stub this.drug_use += points; } @Override public void setDrugUse(float points) { // TODO Auto-generated method stub this.drug_use = points; } @Override public float getDrugUse() { // TODO Auto-generated method stub return this.drug_use; } } package com.mitymods.funwithdrugs.capabilities; import net.minecraft.nbt.INBT; import net.minecraft.util.Direction; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.CapabilityInject; import net.minecraftforge.common.capabilities.ICapabilitySerializable; import net.minecraftforge.common.util.LazyOptional; public class DrugUseProvider implements ICapabilitySerializable<INBT> { @CapabilityInject(IDrugUse.class) public static final Capability<IDrugUse> DRUG_USE_CAPABILITY = null; private LazyOptional<IDrugUse> instance = LazyOptional.of(DRUG_USE_CAPABILITY::getDefaultInstance); @Override public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) { return cap == DRUG_USE_CAPABILITY ? instance.cast() : LazyOptional.empty(); } @Override public INBT serializeNBT() { // @formatter:off return DRUG_USE_CAPABILITY.getStorage().writeNBT(DRUG_USE_CAPABILITY, this.instance.orElseThrow(() -> new IllegalArgumentException("LazyOptional must not be empty!")), null); // @formatter:on } @Override public void deserializeNBT(INBT nbt) { // @formatter:off DRUG_USE_CAPABILITY.getStorage().readNBT(DRUG_USE_CAPABILITY, this.instance.orElseThrow(() -> new IllegalArgumentException("LazyOptional must not be empty!")), null, nbt); // @formatter:on } } package com.mitymods.funwithdrugs.capabilities; import net.minecraft.nbt.CompoundNBT; import net.minecraft.nbt.INBT; import net.minecraft.util.Direction; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.Capability.IStorage; public class DrugUseStorage implements Capability.IStorage<IDrugUse> { @Override public INBT writeNBT(Capability<IDrugUse> capability, IDrugUse instance, Direction side) { // return an NBT tag CompoundNBT tag = new CompoundNBT(); tag.putFloat("drug_use", instance.getDrugUse()); return tag; } @Override public void readNBT(Capability<IDrugUse> capability, IDrugUse instance, Direction side, INBT nbt) { // load from the NBT tag CompoundNBT tag = (CompoundNBT) nbt; instance.setDrugUse(tag.getFloat("drug_use")); } } package com.mitymods.funwithdrugs.events; import com.mitymods.funwithdrugs.FunWithDrugs; import com.mitymods.funwithdrugs.capabilities.DrugUseProvider; import com.mitymods.funwithdrugs.capabilities.IDrugUse; import net.minecraft.entity.Entity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.util.ResourceLocation; import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.event.AttachCapabilitiesEvent; import net.minecraftforge.event.entity.living.LivingFallEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus; @Mod.EventBusSubscriber(modid = FunWithDrugs.MOD_ID, bus = Bus.FORGE) public class AddDrugUse { @SubscribeEvent public static void onAttachCapabilities(AttachCapabilitiesEvent<Entity> event) { if (event.getObject() instanceof PlayerEntity) { event.addCapability(new ResourceLocation(FunWithDrugs.MOD_ID, "drug_use"), new DrugUseProvider()); } } @SubscribeEvent public void onPlayerFalls(LivingFallEvent event) { if(event.getEntityLiving() instanceof PlayerEntity) { PlayerEntity player = (PlayerEntity) event.getEntityLiving(); IDrugUse drugUse = player.getCapability(DrugUseProvider.DRUG_USE_CAPABILITY, null); if (drugUse.getDrugUse() == 5) { player.setHealth(0); } } } } And a bit of code in my main mod file FMLJavaModLoadingContext.get().getModEventBus().addListener(this::onCommonSetup); private void onCommonSetup(FMLCommonSetupEvent event) { CapabilityManager.INSTANCE.register(IDrugUse.class, new DrugUseStorage(), DrugUse::new); } The purpose of this second event is to test if the player has the capability and if it equals the default value, however this code doesn't even work for some reason that I can not figure out. I am very stumped on what the issue is. And I DO know java, I just am having a hard time understanding this specific thing. I know my code is probably pretty messy, I do intend to clean it up once I can figure out what I'm doing wrong
-
oh! I'll take another look. Thank you both so much!
-
I'm not sure this is quite what I'm looking for. This seems to be about storing energy, fluid, etc. What I want is just a variable that would go up over time as you take more potions, and go back down through various means.
-
is it possible to add a variable to the player so that I can keep track of how often they are taking my custom potions, and cause negative effects if they do it too often. Kind of like how in thaumcraft you can go mad and have negative effects if you study certain things.
-
Oh i see what you mean, thank you that worked!
-
So I tried that by doing living.setMotion(living.getLookVec().getX(), 0, living.getLookVec().getZ()); but now the player just cant move their y position at all. As in they cant jump and there is no gravity
-
No, this is for a custom potion effect. I want the player to be able to move where ever they are looking with a significant speed boost. I just want the player to still need to manually jump so they cant fly around. The idea is that they get a speed boost far greater than that of a regular speed potion, but the drawback is that its harder to control
-
See that's what I have been trying, I just don't understand how to ignore the y living.setMotion(living.getLookVec()); this is what I have but obviously that will include the y. How do I tell it to ignore that?
-
Is it possible to make the player move towards where ever they are looking at a fairly fast speed, while ignoring the y vector? Basically i want them to still need to jump and stuff but still be constantly moving forward
-
I'm trying to figure out how to make a custom crafting table but I can't find any help online and the vanilla code is just confusing me. Does anyone know where to look to help send me in the right direction?
-
thank you so much, I think that will do exactly what I want
-
how do i check if an entityLiving has a specific potion effect?
-
[1.16.3] Custom Villager Point of Interest and professions not working
link1213 replied to link1213's topic in Modder Support
I have checked the log, everything is indeed being registered so I can safely say that that is not the issue -
I am trying to create a custom villager profession "drug_dealer" but I can't get any villagers to become this profession. I believe that I set up and registered the profession and point of interest correctly so I really can't figure out what the case of the issue is This is my class that set up the point of interest public class PointsOfInterestInit { public static DeferredRegister<PointOfInterestType> POI_TYPES = DeferredRegister.create(ForgeRegistries.POI_TYPES, FunWithDrugs.MOD_ID); public static Set<BlockState> getAllStates(Block block) { return ImmutableSet.copyOf(block.getStateContainer().getValidStates()); } public static final RegistryObject<PointOfInterestType> DRUG_DEALER_TYPE = POI_TYPES.register("drug_dealer_type", () -> new PointOfInterestType("drug_dealer_type", getAllStates(Blocks.GOLD_BLOCK), 1, 1)); } I used the gold block as a testing block since my own mod blocks don't work either this is my class that sets up the villager profession public class VillagerInit { public static DeferredRegister<VillagerProfession> PROFESSIONS = DeferredRegister.create(ForgeRegistries.PROFESSIONS, FunWithDrugs.MOD_ID); public static final RegistryObject<VillagerProfession> DRUG_DEALER = PROFESSIONS.register("drug_dealer", () -> new VillagerProfession("drug_dealer", PointsOfInterestInit.DRUG_DEALER_TYPE.get(), ImmutableSet.of(), ImmutableSet.of(), SoundEvents.ENTITY_VILLAGER_WORK_BUTCHER)); } I don't see what the issue is with the way i set it up but still no villagers will become this profession