Jump to content

mclich

Members
  • Posts

    68
  • Joined

  • Last visited

Everything posted by mclich

  1. I have an item that displays my own player animation while holding right click, but if there is another item at the same time in off hand, it doesn't attach to it on use. I tried to rotate item model using IForgeBakedModel#handlePerspective, but it didn't work. Is there any way to achieve that? There is an example: https://imgur.com/a/kBYEOJ8
  2. So i have an item that changes player arm model positions and rotations when holding right click on it (like bow and trident). The problem is player arms became completely immovable, but item still have "bob effect" (when item moves slightly from side to side in player hand in third person view) and i want to disable it. I tried to search in vanillas code, but i didn't even figure out where this effect is applying. Is there any way to disable it?
  3. Still need help with this problem. Any thoughts?
  4. What method should i use in that case? I tried PlayerRenderer#render, but it throws an "net.minecraft.crash.ReportedException: Rendering entity in world". How do i fix it?
  5. I have an item that activates while holding right-click button. I want it to show my own third-person player animation while it is active. Since vanilla animations are specified in UseAction class, which is enum, i can't create my own instance of it. What i did is subscribed to RenderPlayerEvent.Pre and changed player position values there, but it didn't work at all. Any ideas how i can implement this? My code is below: @EventBusSubscriber(modid=ElderNorseGods.MOD_ID, bus=EventBusSubscriber.Bus.FORGE, value=Dist.CLIENT) public class HealingStaffItem extends Item implements IVanishable { public static final String ID="healing_staff"; public HealingStaffItem(Rarity rarity, int durability) { super(new Item.Properties().rarity(rarity).durability(durability).tab(ENGTabs.COMBAT)); } @Override public int getUseDuration(ItemStack itemStack) { return 72000; } @Override public UseAction getUseAnimation(ItemStack itemStack) { return UseAction.BOW; } @Override public boolean isValidRepairItem(ItemStack itemStack, ItemStack repairStack) { return false; } @Override public ActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) { player.startUsingItem(hand); return ActionResult.consume(player.getItemInHand(hand)); } @SubscribeEvent public static void renderArmsPoses(RenderPlayerEvent.Pre event) { if(event.getPlayer().getUseItem().getItem()==ENGItems.HEALING_STAFF.get()) { PlayerModel<AbstractClientPlayerEntity> model=event.getRenderer().getModel(); model.rightArm.xRot=model.rightArm.xRot*0.5F-(float)Math.PI; model.rightArm.yRot=0.0F; model.leftArm.xRot=model.leftArm.xRot*0.5F-(float)Math.PI; model.leftArm.yRot=0.0F; } } }
  6. My god, i'm an idiot... I changed registration class like this: @EventBusSubscriber(modid=ElderNorseGods.MOD_ID, bus=EventBusSubscriber.Bus.MOD) public abstract class ENGCapabilities { @SubscribeEvent public static void registerCapabilities(final FMLCommonSetupEvent event) { CapabilityManager.INSTANCE.register(IManaHandler.class, new ManaStorage(), ManaHandler::new); } @EventBusSubscriber(modid=ElderNorseGods.MOD_ID, bus=EventBusSubscriber.Bus.FORGE) private static class AttachCapabilities { @SubscribeEvent public static void attachCapabilities(final AttachCapabilitiesEvent<Entity> event) { if(!(event.getObject() instanceof PlayerEntity)) return; ManaProvider provider=new ManaProvider(); event.addCapability(ManaCapability.LOCATION, provider); event.addListener(provider::invalidate); } } } and it works now! Thank you for your help!
  7. @EventBusSubscriber(modid=ElderNorseGods.MOD_ID, bus=EventBusSubscriber.Bus.FORGE) public abstract class ENGCapabilities { @SubscribeEvent public static void registerCapabilities(final FMLCommonSetupEvent event) { CapabilityManager.INSTANCE.register(IManaHandler.class, new ManaStorage(), ManaHandler::new); } @SubscribeEvent public static void attachCapabilities(final AttachCapabilitiesEvent<Entity> event) { if(!(event.getObject() instanceof PlayerEntity)) return; ManaProvider provider=new ManaProvider(); event.addCapability(ManaCapability.LOCATION, provider); event.addListener(provider::invalidate); } }
  8. If i understood correctly, something is broken here. When i check for capability instance being null: @Override public CompoundNBT serializeNBT() { if(ManaCapability.CAP_INSTANCE==null) return new CompoundNBT(); else return (CompoundNBT)ManaCapability.CAP_INSTANCE.writeNBT(this.mana, null); } it always returns true, so serializeNBT() always returns new CompoundNBT(), i don't know why
  9. At the moment i have only one item that change values after use. There is code: public class ManaTinctureItem extends Item { public static final String ID="mana_tincture"; public ManaTinctureItem() { super(new Item.Properties().food(new Food.Builder().nutrition(2).saturationMod(0.6F).alwaysEat().build()).stacksTo(1).rarity(Rarity.RARE).tab(ENGTabs.FOOD)); } @Override public ItemStack finishUsingItem(ItemStack itemStack, World world, LivingEntity entity) { itemStack=entity.eat(world, itemStack); if(!world.isClientSide()&&entity instanceof ServerPlayerEntity) { ServerPlayerEntity player=(ServerPlayerEntity)entity; player.getCapability(ManaCapability.CAP_INSTANCE).ifPresent ( mana-> { if(!mana.getStatus()) { mana.setStatus(true); mana.setMana(20F); mana.update(player); } } ); if(player.gameMode.isSurvival()) { if(itemStack.getCount()==0&&!player.inventory.contains(new ItemStack(Items.BOWL))) { player.inventory.removeItem(itemStack); player.inventory.add(player.inventory.selected, new ItemStack(Items.BOWL)); } else player.inventory.add(new ItemStack(Items.BOWL)); } } return itemStack; } @Override public boolean isFoil(ItemStack itemStack) { return true; } }
  10. Capability class: public class ManaCapability { public static final ResourceLocation LOCATION=new ResourceLocation(ElderNorseGods.MOD_ID, "mana"); @CapabilityInject(IManaHandler.class) public static Capability<IManaHandler> CAP_INSTANCE=null; public static class ManaStorage implements IStorage<IManaHandler> { @Override public INBT writeNBT(Capability<IManaHandler> cap, IManaHandler manaHandler, Direction side) { CompoundNBT tag=new CompoundNBT(); tag.putFloat("Mana", manaHandler.getMana()); tag.putBoolean("Status", manaHandler.getStatus()); return tag; } @Override public void readNBT(Capability<IManaHandler> cap, IManaHandler manaHandler, Direction side, INBT nbt) { manaHandler.setMana(((CompoundNBT)nbt).getFloat("Mana")); manaHandler.setStatus(((CompoundNBT)nbt).getBoolean("Status")); } } public static class ManaProvider implements ICapabilitySerializable<CompoundNBT> { private final ManaHandler mana=new ManaHandler(); private final LazyOptional<IManaHandler> manaOptional=LazyOptional.of(()->this.mana); public void invalidate() { this.manaOptional.invalidate(); } @Override public CompoundNBT serializeNBT() { if(ManaCapability.CAP_INSTANCE==null) return new CompoundNBT(); else return (CompoundNBT)ManaCapability.CAP_INSTANCE.writeNBT(this.mana, null); } @Override public void deserializeNBT(CompoundNBT nbt) { if(ManaCapability.CAP_INSTANCE!=null) ManaCapability.CAP_INSTANCE.readNBT(this.mana, null, nbt); } @Override public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) { return cap==ManaCapability.CAP_INSTANCE?this.manaOptional.cast():LazyOptional.empty(); } } } Capability handler class: public class ManaHandler implements IManaHandler { private float mana; private boolean active; public ManaHandler() { this.mana=0F; this.active=false; } @Override public void setMana(float amount) { if(amount<0F) this.mana=0F; else if(amount>20F) this.mana=20F; else this.mana=amount; } @Override public float getMana() { return this.mana; } @Override public void setStatus(boolean value) { this.active=value; } @Override public boolean getStatus() { return this.active; } @Override public void update(ServerPlayerEntity player) { NetworkHandler.sendToPlayer(player, new ManaDataPacket(this.getMana(), this.getStatus())); } } Capability event handler class: @EventBusSubscriber(modid=ElderNorseGods.MOD_ID, bus=EventBusSubscriber.Bus.FORGE) public abstract class ManaEventHandler { private static void sendUpdates(ServerPlayerEntity player) { if(!player.getCommandSenderWorld().isClientSide()) { player.getCapability(ManaCapability.CAP_INSTANCE).ifPresent(mana->mana.update(player)); } } @SubscribeEvent public static void onPlayerChangedDimension(PlayerChangedDimensionEvent event) { ManaEventHandler.sendUpdates((ServerPlayerEntity)event.getPlayer()); } @SubscribeEvent public static void onPlayerRespawn(PlayerRespawnEvent event) { ManaEventHandler.sendUpdates((ServerPlayerEntity)event.getPlayer()); } @SubscribeEvent public static void onPlayerLoggedIn(PlayerLoggedInEvent event) { ManaEventHandler.sendUpdates((ServerPlayerEntity)event.getPlayer()); } @SubscribeEvent public static void onPlayerClone(PlayerEvent.Clone event) { if(!event.isWasDeath()) return; IManaHandler oldMana=event.getOriginal().getCapability(ManaCapability.CAP_INSTANCE).orElse(null); IManaHandler newMana=event.getPlayer().getCapability(ManaCapability.CAP_INSTANCE).orElse(null); if(oldMana!=null&&newMana!=null) { newMana.setMana(oldMana.getMana()); newMana.setStatus(oldMana.getStatus()); } } } Packet class: public class ManaDataPacket { private float value; private boolean status; public ManaDataPacket(float value, boolean status) { this.value=value; this.status=status; } public static void encode(ManaDataPacket packet, PacketBuffer buffer) { buffer.writeFloat(packet.value); buffer.writeBoolean(packet.status); } public static ManaDataPacket decode(PacketBuffer buffer) { return new ManaDataPacket(buffer.readFloat(), buffer.readBoolean()); } public static void handle(ManaDataPacket packet, Supplier<NetworkEvent.Context> ctx) { ctx.get().enqueueWork(()->DistExecutor.unsafeRunWhenOn(Dist.CLIENT, ()->()->PacketHandler.handlePacket(packet, ctx))); ctx.get().setPacketHandled(true); } private static class PacketHandler { private static void handlePacket(ManaDataPacket packet, Supplier<NetworkEvent.Context> ctx) { Minecraft mc=Minecraft.getInstance(); mc.player.getCapability(ManaCapability.CAP_INSTANCE).ifPresent ( mana-> { mana.setMana(packet.value); mana.setStatus(packet.status); } ); } } }
  11. So i made my custom capability and attach it to all players. I also made serialization and synchronization using my own packet (at least i think i do) to allow client (player) always access updated capability data after some changes. In my capability event handler class i've subscribed to PlayerLoggedInEvent and send updated data through my packet. The problem is the data just doesn't save after player leaves the world and sets to default values after player joins the world again. Any ideas how to fix that?
  12. Okay, i've just solved it. In case someone has the same problem all you need to do is check in your render method that the getType() equals ElementType.ALL. In my case i just replaced one if statement: for if(!mc.gameMode.getPlayerMode().isSurvival()||event.getType()!=ElementType.ALL) return;
  13. I render it above the food bar, but it still covres it
  14. The code i posted above is also for 1.16 version, and the problem is my custom bar covers my food bar in 1.16
  15. Okay, thanks. Btw, how can i do this in 1.16.5 (I make my mod for both 1.17.1 and 1.16.5 at the same time)?
  16. So i added a capability system to my mod and it works fine, but now i have a problem with rendering. This code of mine makes render of mana bar (it should be right above vanilla food bar): private static final ResourceLocation ICONS=new ResourceLocation(ElderNorseGods.MOD_ID+":textures/gui/bar/mana.png"); @SubscribeEvent(priority=EventPriority.LOWEST) public static void renderManaBar(RenderGameOverlayEvent.Post event) { Minecraft mc=Minecraft.getInstance(); if(!mc.gameMode.getPlayerMode().isSurvival()||event.getType()==ElementType.ALL) return; LazyOptional<IManaHandler> manaHandler=mc.player.getCapability(ManaHandlerCapability.MANA_HANDLER_CAPABILITY); RenderSystem.enableBlend(); mc.getTextureManager().bind(ManaBar.ICONS); int x=mc.getWindow().getGuiScaledWidth()/2+10; int y=mc.getWindow().getGuiScaledHeight()-49; mc.gui.blit(event.getMatrixStack(), x, y, 0, 0, 54, 9); RenderSystem.disableBlend(); } Everything works as it should works, but for some reason after binding and bliting my texture vanilla food bar just disappears. I have no idea what is going on here and why it's just gone. Btw, my texture size is 256x256. I tried to use other (static ones) blit methods because i want to use smaller version of my texture, but they didn't work. I assume that my texture is just too big and covers food bar (RenderSystem.enableBlend seems not working too), but it doesn't cover hotbar and xp bar. Any ideas what is wrong here?
  17. So should i use AttachCapabilitiesEvent<Entity> or create my own capability?
  18. So, i want to add a mana bar and show it near health and food bars. I know that i need to render it using RenderGameOverlayEvent.Post, but what i don't know, is how do i add some custom data (like health or saturation) to all players at once (in my case it is mana data). I watched LivingEntity source code, and as i understand, i need to define a new EntityDataAccessor using entityData field. If so, how can i do this? I thought there are some player events that help creating custom data, but i didn't find any. Any help would be appreciated
×
×
  • Create New...

Important Information

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