Jump to content

[1.16.5 mcp] display my capability mana in gui


auriny

Recommended Posts

I made mana through caps, it works, but I don’t know how to visually display it with a custom sprite. i made this code using minecraft and divinerpg sources as references but this code doesn't works. 
what wrong with it?

public class EtherRenderer {

    Minecraft mc = Minecraft.getInstance();

    @SubscribeEvent
    public void onRender(RenderGameOverlayEvent.Post event) {
        if (event.getType() == RenderGameOverlayEvent.ElementType.ALL) {
            onTickRender(event);
        }
    }

    private int getPercentage() {
        assert mc.player != null;
        Ether ether = mc.player.getCapability(EtherCapability.CAPABILITY_ETHER, null).orElse(Ether.createADefaultInstance());
        float result = ether.getEther() / ether.getMaxEther() * 100;
        return (int) MathHelper.clamp(Math.floor(result), 0, 100);
    }

    @SubscribeEvent
    @SuppressWarnings("deprecation")
    private void onTickRender(RenderGameOverlayEvent.Post event) {
        if (mc.currentScreen == null) {
            GlStateManager.color4f(1.0F, 1.0F, 1.0F, 1.0F);
            IngameGui gui = mc.ingameGUI;
            int x = this.mc.getMainWindow().getScaledWidth() - 111;
            int y = this.mc.getMainWindow().getScaledHeight() - 18;
            gui.blit(event.getMatrixStack(), x, y, 0, 9, 100, 9);
            gui.blit(event.getMatrixStack(), x, y, 0, 9, getPercentage(), 18);
            this.mc.getTextureManager().bindTexture(new ResourceLocation(CoSRPG.MOD_ID, "textures/gui/ether_bar.png"));
        }
    }
}

 

Link to comment
Share on other sites

2 minutes ago, diesieben07 said:
  • Currently you are first drawing, then binding the texture. Obviously this is the wrong way around.
  • Why?

i don't know what to put in here lol. i tried null but it gives out npe, this thing worked with consume. is there other variants?

Edited by auriny
Link to comment
Share on other sites

2 minutes ago, diesieben07 said:

Currently you are first drawing, then binding the texture. Obviously this is the wrong way around.

i.e. should i move ```this.mc.getTextureManager().bindTexture(new ResourceLocation(CoSRPG.MOD_ID, "textures/gui/ether_bar.png"));``` up?

Link to comment
Share on other sites

6 minutes ago, diesieben07 said:

Do you expect your capability to ever not be there? If so, what you want might be the correct choice. But you need to think about it. Usually people always attach their capability, but still use orElse instead of orElseThrow (which would indicate absence as an error).

hmm ok thanks

6 minutes ago, diesieben07 said:

Correct.

going to try

Link to comment
Share on other sites

17 minutes ago, diesieben07 said:

Correct.

public class EtherRenderer {

    Minecraft mc = Minecraft.getInstance();

    @SubscribeEvent
    public void onRender(RenderGameOverlayEvent.Post event) {
        if (event.getType() == RenderGameOverlayEvent.ElementType.ALL) {
            onTickRender(event);
        }
    }

    private int getPercentage() {
        assert mc.player != null;
        Ether ether = mc.player.getCapability(EtherCapability.CAPABILITY_ETHER, null).orElse(Ether.createADefaultInstance());
        float result = ether.getEther() / ether.getMaxEther() * 100;
        return (int) MathHelper.clamp(Math.floor(result), 0, 100);
    }

    @SuppressWarnings("deprecation")
    private void onTickRender(RenderGameOverlayEvent.Post event) {
        this.mc.getTextureManager().bindTexture(new ResourceLocation(CoSRPG.MOD_ID, "textures/gui/ether_bar.png"));
        if (mc.currentScreen == null) {
            GlStateManager.color4f(1.0F, 1.0F, 1.0F, 1.0F);
            IngameGui gui = mc.ingameGUI;
            int x = this.mc.getMainWindow().getScaledWidth() - 111;
            int y = this.mc.getMainWindow().getScaledHeight() - 18;
            gui.blit(event.getMatrixStack(), x, y, 0, 9, 100, 9);
            gui.blit(event.getMatrixStack(), x, y, 0, 9, getPercentage(), 18);
        }
    }
}

still doesn't work... did i do something wrong?
a bar is displayed, but it does not change depending on the ether (mana)

Link to comment
Share on other sites

3 minutes ago, diesieben07 said:

This is basic Java programming knowledge and outside the scope of this forum.

are you talking about the one that activates on shift + f9? i thought you were talking about some kind of debugger in the game x)

Link to comment
Share on other sites

1 hour ago, diesieben07 said:

Is your code executing

yes

 

1 hour ago, diesieben07 said:

Are your position calculations correct

yes, to make sure of this on 100%, i even changed the height of the second blit and i had two bars on the same level, not counting the height.

 

that's right

Link to comment
Share on other sites

3 minutes ago, diesieben07 said:

So by this you mean you changed your code and the blit started rendering something?

i changed my code and blit started rendering two bars, and all of those bars was full (i have two sprites: full and empty, and sprites was full)
and these bars still doesn't changed

Link to comment
Share on other sites

21 minutes ago, diesieben07 said:

Do these methods return the correct value? Check with the debugger.

hmm no, ether is always equals 200, but this code working perfectly and if i make a certain number of right clicks, the whole ether will end
and in both cases its returning 200
 

    public ActionResult<ItemStack> onItemRightClick(World world, PlayerEntity player, Hand hand) {
        Ether ether = player.getCapability(EtherCapability.CAPABILITY_ETHER, null).orElse(Ether.createADefaultInstance());
        ItemStack itemstack = player.getHeldItem(hand);
        if (!world.isRemote() && ether.getEther() >= 25) { // <- here is breakpoint
            world.playSound(null, player.getPosX(), player.getPosY(), player.getPosZ(),
                    SoundEvents.BLOCK_LAVA_EXTINGUISH, SoundCategory.PLAYERS, 1, 1);
            ether.consume(player, 25);
            player.getCooldownTracker().setCooldown(this, 20);
            return ActionResult.resultFail(itemstack);
        }
        return ActionResult.resultSuccess(itemstack);
    }


ether = {Ether@18499} 
 tickDelay = 4
 max = 200.0
 ether = 200.0

Link to comment
Share on other sites

3 minutes ago, Luis_ST said:

did you sync your Capability to the client?

since rendering is client side only but it seems so you set the value only server side

seems like no, i can show all caps code, but how to do it?

Link to comment
Share on other sites

7 minutes ago, Luis_ST said:

you need a custom Network packet which you send when the capability data changes on server, and in PlayerLoggedInEvent, PlayerEvent.PlayerRespawnEvent and PlayerEvent.PlayerChangedDimensionEvent

like... this?
i has that event before you said, it was used for ether regeneration

 

@Mod.EventBusSubscriber(modid = CoSRPG.MOD_ID, bus = Mod.EventBusSubscriber.Bus.FORGE)
public class TickEvent {
    public static int tick;
    @SubscribeEvent
    public void tickServer(TickEvent.ServerTickEvent evt) {
        if(evt.phase == TickEvent.Phase.END) {
            tick ++;
            if(tick > 100000) tick = 0;
        }
    }

    @SubscribeEvent
    public static void playerTick(TickEvent.PlayerTickEvent event){
        if(event.phase == TickEvent.Phase.START){
            Ether ether = event.player.getCapability(EtherCapability.CAPABILITY_ETHER, null).orElse(Ether.createADefaultInstance());
            ether.regen(event.player);
        }
    }

    @SubscribeEvent
    public void onPlayerRespawn(PlayerEvent.PlayerRespawnEvent event) {
        Ether ether = event.getPlayer().getCapability(EtherCapability.CAPABILITY_ETHER, null).orElse(Ether.createADefaultInstance());
        ether.fill(event.getPlayer(), ether.getEther());
    }

    @SubscribeEvent
    public void onChangeDimension(PlayerEvent.PlayerChangedDimensionEvent event) {
        Ether ether = event.getPlayer().getCapability(EtherCapability.CAPABILITY_ETHER, null).orElse(Ether.createADefaultInstance());
        ether.fill(event.getPlayer(), ether.getMaxEther()-ether.getEther());
    }

    @SubscribeEvent
    public void onLoggedIn(PlayerEvent.PlayerLoggedInEvent event) {
        Ether ether = event.getPlayer().getCapability(EtherCapability.CAPABILITY_ETHER, null).orElse(Ether.createADefaultInstance());
        ether.fill(event.getPlayer(), ether.getEther());
    }
}

 

Link to comment
Share on other sites

4 minutes ago, auriny said:

like... this?
i has that event before you said, it was used for ether regeneration

 

@Mod.EventBusSubscriber(modid = CoSRPG.MOD_ID, bus = Mod.EventBusSubscriber.Bus.FORGE)
public class TickEvent {
    public static int tick;
    @SubscribeEvent
    public void tickServer(TickEvent.ServerTickEvent evt) {
        if(evt.phase == TickEvent.Phase.END) {
            tick ++;
            if(tick > 100000) tick = 0;
        }
    }

    @SubscribeEvent
    public static void playerTick(TickEvent.PlayerTickEvent event){
        if(event.phase == TickEvent.Phase.START){
            Ether ether = event.player.getCapability(EtherCapability.CAPABILITY_ETHER, null).orElse(Ether.createADefaultInstance());
            ether.regen(event.player);
        }
    }

    @SubscribeEvent
    public void onPlayerRespawn(PlayerEvent.PlayerRespawnEvent event) {
        Ether ether = event.getPlayer().getCapability(EtherCapability.CAPABILITY_ETHER, null).orElse(Ether.createADefaultInstance());
        ether.fill(event.getPlayer(), ether.getEther());
    }

    @SubscribeEvent
    public void onChangeDimension(PlayerEvent.PlayerChangedDimensionEvent event) {
        Ether ether = event.getPlayer().getCapability(EtherCapability.CAPABILITY_ETHER, null).orElse(Ether.createADefaultInstance());
        ether.fill(event.getPlayer(), ether.getMaxEther()-ether.getEther());
    }

    @SubscribeEvent
    public void onLoggedIn(PlayerEvent.PlayerLoggedInEvent event) {
        Ether ether = event.getPlayer().getCapability(EtherCapability.CAPABILITY_ETHER, null).orElse(Ether.createADefaultInstance());
        ether.fill(event.getPlayer(), ether.getEther());
    }
}

 

well, this didn't work

Link to comment
Share on other sites

hah, and this value is out of sync with the test item when i use it. ether is regens, but if i try to use an item and if i completely deplete all the ether, it will still be equal to what i put + time for regen

 

should i send all capability code?

Edited by auriny
Link to comment
Share on other sites

1 hour ago, loordgek said:

if you use @Mod.EventBusSubscriber your event methods must be static

ok thx

 

1 hour ago, Luis_ST said:

did you read the link? the code you use has nothing to do with Networking

this code is located in PacketEther class, that registries in NetworkRegistry class

    private static int packetId = 0;
    public static SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel(new ResourceLocation(CoSRPG.MOD_ID,
            "main"), () -> "1.0", s -> true, s -> true);

    private static int nextID() {
        return packetId++;
    }

    public static void init() {
        INSTANCE.registerMessage(nextID(), PacketEtherBar.class, PacketEtherBar::toBytes, PacketEtherBar::new, PacketEtherBar::handle);
    }

 

1 hour ago, Luis_ST said:

Edit: show the Ether class (the implementation of your Capability interface)

public class Ether {
    private int tickDelay = 4;
    private float max = 100;
    private float ether;

    public Ether() {
        this(200);
    }
    
    public Ether(float chargeLevel) {
        ether = chargeLevel;
    }

    public float getEther() {
        return ether;
    }

    public void consume(PlayerEntity player, float points) {
        set(getEther() - points);
        sendPacket(player);
    }

    public void fill(PlayerEntity player, float points) {
        float a = getEther();
        set(a + points);

        if (a != getEther())
            sendPacket(player);
    }

    public void regen(PlayerEntity player) {
        if (player.world.getGameTime() % tickDelay == 0)
            fill(player, 1);
    }

    public void set(float points) {
        ether = MathHelper.clamp(points, 0, getMaxEther());
    }


    public float getMaxEther() {
        return max;
    }

    public void setMaxEther(float max) {
        this.max = max;
    }

    public int getRegenDelay() {
        return tickDelay;
    }

    public void setRegenDelay(int delay) {
        tickDelay = delay;
    }

    private void sendPacket(PlayerEntity player) {
        if (!(player instanceof FakePlayer) && player instanceof ServerPlayerEntity)
            NetworkingRegistry.INSTANCE.sendToServer(new PacketEtherBar(this));
    }


    public static class EtherStorage implements Capability.IStorage<Ether> { // implementation of Capability
        @Override
        public INBT writeNBT(Capability<Ether> capability, Ether instance, Direction side) {
            return FloatNBT.valueOf(instance.ether);
        }

        @Override
        public void readNBT(Capability<Ether> capability, Ether instance, Direction side, INBT nbt) {
            float ether = 0;
            if (nbt.getType() == FloatNBT.TYPE) {
                ether = ((FloatNBT) nbt).getFloat();
            }
            instance.set(ether);
        }
    }

    public static Ether createADefaultInstance() {
        return new Ether();
    }
}

 

Link to comment
Share on other sites

45 minutes ago, Luis_ST said:

you need to send a Packeet to the client

like this?  NetworkingRegistry.INSTANCE.send(PacketDistributor.ALL.noArg(), new PacketEtherBar(this));

Edited by auriny
Link to comment
Share on other sites

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.



×
×
  • Create New...

Important Information

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