Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

mclich

Members
  • Posts

    59
  • Joined

  • Last visited

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

mclich's Achievements

Stone Miner

Stone Miner (3/8)

0

Reputation

  1. 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!
  2. @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); } }
  3. 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
  4. 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; } }
  5. 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); } ); } } }
  6. 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?
  7. 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;
  8. I render it above the food bar, but it still covres it
  9. 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
  10. 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)?
  11. 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?
  12. So should i use AttachCapabilitiesEvent<Entity> or create my own capability?
×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.