Jump to content

Recommended Posts

Posted

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"));
        }
    }
}

 

Posted (edited)
  On 5/31/2022 at 12:18 PM, diesieben07 said:
  • Currently you are first drawing, then binding the texture. Obviously this is the wrong way around.
  • Why?

Expand  

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
Posted
  On 5/31/2022 at 12:18 PM, diesieben07 said:

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

Expand  

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

Posted
  On 5/31/2022 at 12:28 PM, 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).

Expand  

hmm ok thanks

  On 5/31/2022 at 12:28 PM, diesieben07 said:

Correct.

Expand  

going to try

Posted
  On 5/31/2022 at 12:28 PM, diesieben07 said:

Correct.

Expand  
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)

Posted
  On 5/31/2022 at 1:36 PM, diesieben07 said:

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

Expand  

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)

Posted
  On 5/31/2022 at 2:29 PM, diesieben07 said:

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

Expand  

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

Posted
  On 5/31/2022 at 2:42 PM, diesieben07 said:

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

Expand  

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

Posted
  On 5/31/2022 at 3:14 PM, 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

Expand  

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

Posted
  On 5/31/2022 at 3:46 PM, 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

Expand  

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());
    }
}

 

Posted
  On 5/31/2022 at 3:55 PM, 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());
    }
}

 

Expand  

well, this didn't work

Posted (edited)

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
Posted
  On 5/31/2022 at 4:16 PM, loordgek said:

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

Expand  

ok thx

 

  On 5/31/2022 at 4:50 PM, Luis_ST said:

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

Expand  

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);
    }

 

  On 5/31/2022 at 4:50 PM, Luis_ST said:

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

Expand  
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();
    }
}

 

Posted (edited)
  On 5/31/2022 at 6:17 PM, Luis_ST said:

you need to send a Packeet to the client

Expand  

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

Edited by auriny

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

    • This is what happened after I removed it https://gist.github.com/StrikeAttack7/58fc756babdef7219f4819658fb85e1b
    • Remove MC Dungeons Weapons (mcdw)
    • This is what happened after i added it https://gist.github.com/StrikeAttack7/782db25bf495a088e338ccd810454fb9
    • Greetings! If you clicked on this I can only assume you have at the very least a slight interest in using Minecraft as a vehicle for making nations, roleplaying, and worldbuilding, which is amazing to hear, thank you for your interest and I will do my best to explain this as much as I can here but I would like to start by advising you to join our discord, even if you have the slightest interest you can leave whenever you want and I can explain this whole thing a whole lot better. Our goal is to foster a creative and unified community and thrive on a minecraft server that was made for creating everything from nations to individual cultures. This recruitment post is for the city of Sajurgard (inside a larger nation) on the server of Stoneworks but fear not if you have ambitions to create your own nation/group simply join our discord and we will help you get set up. Our nation is all about making the experience as positive as possible for everyone! If you've never heard of something like this before that's perfect, come on down and we will get you started on your journey! If your a veteran of this type of thing we also welcome you and promise you will find the experience with us enjoyable. I would love to explain more about our lore, history and community and what we have going on here but it wouldn't fit here, so if this at all interests you join our discord and contact us! We would love to have you and are happy to answer all questions and promise to ensure you will enjoy your time with us. discord server: https://discord.gg/ez9XjNUJJj
    • Add the full crash-report or latest.log (logs-folder) with sites like https://mclo.gs/ and paste the link to it here
  • Topics

  • Who's Online (See full list)

    • There are no registered users currently online
×
×
  • Create New...

Important Information

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