Jump to content

Recommended Posts

Posted

I have a GUI with some buttons in it, and when i click those buttons i want the gui to change appearance (basically some buttons will be replaced with others). How can i do so that the gui automatically refresh without closing it and reopening it to see the changes? Also i've noticed that the gui repaint itself when i resize the window, doing something like this
MgTIRet.png

How can i made so the gui doesn't do this every time i resize? This is my gui code, thanks in advance to everyone who will help me understand these problems :)

	package com.cf.gui;
	import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
	import org.lwjgl.opengl.GL11;
	import com.cf.CoreFaction;
import com.cf.network.GuiCoreAddExpansionServerMessage;
import com.cf.network.GuiCoreRemoveServerMessage;
import com.cf.network.GuiCoreSaveServerMessage;
import com.cf.utils.Settings;
import com.cf.utils.Utils;
	import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.init.MobEffects;
import net.minecraft.potion.Potion;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
	public class GuiCore extends GuiScreen {
    private static final ResourceLocation BG = new ResourceLocation(CoreFaction.MODID, "textures/gui/core.png");
	    private String territory;
    private String faction;
    private int coreLevel;
    private int expansions;
    private int primaryEffect;
    private int secondaryEffect;
    private int primaryLevel;
    private int secondaryLevel;
	    private GuiCore.UpgradeButton upgradeCoreButton;
    private GuiCore.ExpansionButton expansionButton;
    private GuiCore.RemoveButton removeButton;
	    private int guiLeft;
    private int guiTop;
    private int xSize = 256;
    private int ySize = 181;
    private ArrayList<AllyEffectButton> allyButtons = new ArrayList<AllyEffectButton>();
    private ArrayList<EnemyEffectButton> enemyButtons = new ArrayList<EnemyEffectButton>();
    private ArrayList<AllyActiveEffectButton> allyActiveButtons = new ArrayList<AllyActiveEffectButton>();
    private ArrayList<EnemyActiveEffectButton> enemyActiveButtons = new ArrayList<EnemyActiveEffectButton>();
    private ArrayList<Potion> allyEffects = new ArrayList<Potion>();
    private ArrayList<Potion> enemyEffects = new ArrayList<Potion>();
    private int maxCoreLevel = 10;
	    public GuiCore(String t, String f, int l, int e, int e1, int e2, int l1, int l2) {
        this.territory = t;
        this.faction = f;
        this.coreLevel = l;
        this.expansions = e;
        this.primaryEffect = e1;
        this.secondaryEffect = e2;
        this.primaryLevel = l1;
        this.secondaryLevel = l2;
    }
	    @Override
    public void initGui() {
        this.guiLeft = (this.width - xSize) / 2;
        this.guiTop = (this.height - ySize) / 2;
        this.upgradeCoreButton = new GuiCore.UpgradeButton(-1, this.guiLeft + 19, this.guiTop + 144);
        this.expansionButton = new GuiCore.ExpansionButton(-2, this.guiLeft + 116, this.guiTop + 144);
        this.removeButton = new GuiCore.RemoveButton(-3, this.guiLeft + 213, this.guiTop + 144);
	        this.buttonList.add(upgradeCoreButton);
        this.buttonList.add(expansionButton);
        this.buttonList.add(removeButton);
	        this.allyEffects.add(Potion.getPotionById(Potion.getIdFromPotion(MobEffects.JUMP_BOOST)));
        this.allyEffects.add(Potion.getPotionById(Potion.getIdFromPotion(MobEffects.SPEED)));
        this.allyEffects.add(Potion.getPotionById(Potion.getIdFromPotion(MobEffects.RESISTANCE)));
        this.allyEffects.add(Potion.getPotionById(Potion.getIdFromPotion(MobEffects.HASTE)));
        this.allyEffects.add(Potion.getPotionById(Potion.getIdFromPotion(MobEffects.ABSORPTION)));
	        this.enemyEffects.add(Potion.getPotionById(Potion.getIdFromPotion(MobEffects.HUNGER)));
        this.enemyEffects.add(Potion.getPotionById(Potion.getIdFromPotion(MobEffects.WEAKNESS)));
        this.enemyEffects.add(Potion.getPotionById(Potion.getIdFromPotion(MobEffects.SLOWNESS)));
        this.enemyEffects.add(Potion.getPotionById(Potion.getIdFromPotion(MobEffects.BLINDNESS)));
        this.enemyEffects.add(Potion.getPotionById(Potion.getIdFromPotion(MobEffects.MINING_FATIGUE)));
	        for (int i = 0; i < 5; i++) {
            this.allyButtons.add(
                    new AllyEffectButton(i, this.guiLeft + 11 + 5, this.guiTop + 20 + (24 * i), allyEffects.get(i), 0));
            this.allyActiveButtons.add(new AllyActiveEffectButton(i, this.guiLeft + 11 + 5, this.guiTop + 20 + (24 * i),
                    allyEffects.get(i), 0));
            this.enemyButtons.add(new EnemyEffectButton(i + 5, this.guiLeft + (this.xSize - 106 - 11 - 5),
                    this.guiTop + 20 + (24 * i), enemyEffects.get(i), 0));
            this.enemyActiveButtons.add(new EnemyActiveEffectButton(i + 5, this.guiLeft + (this.xSize - 106 - 11 - 5),
                    this.guiTop + 20 + (24 * i), enemyEffects.get(i), 0));
        }
	        this.buttonList.addAll(allyButtons);
        this.buttonList.addAll(enemyButtons);
    }
	    @Override
    public void updateScreen() {
        if (this.primaryLevel != -999) {
            for (int i = 0; i < allyEffects.size(); i++) {
                if (Potion.getIdFromPotion(allyEffects.get(i)) == this.primaryEffect) {
                    this.buttonList.remove(allyButtons.get(i));
                    this.buttonList.add(allyActiveButtons.get(i));
                }
            }
            for (int i = 0; i < enemyEffects.size(); i++) {
                if (Potion.getIdFromPotion(enemyEffects.get(i)) == this.primaryEffect) {
                    this.buttonList.remove(enemyEffects.get(i));
                    this.buttonList.add(enemyActiveButtons.get(i));
                }
            }
        }
        if (this.secondaryLevel != -999) {
            for (int i = 0; i < enemyEffects.size(); i++) {
                if (Potion.getIdFromPotion(enemyEffects.get(i)) == this.secondaryEffect) {
                    this.buttonList.remove(enemyButtons.get(i));
                    this.buttonList.add(enemyActiveButtons.get(i));
                }
            }
            for (int i = 0; i < enemyEffects.size(); i++) {
                if (Potion.getIdFromPotion(enemyEffects.get(i)) == this.secondaryEffect) {
                    this.buttonList.remove(enemyEffects.get(i));
                    this.buttonList.add(enemyActiveButtons.get(i));
                }
            }
        }
        super.updateScreen();
    }
	    @Override
    public void drawScreen(int mouseX, int mouseY, float partialTicks) {
        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
        GL11.glDisable(GL11.GL_LIGHTING);
        this.mc.getTextureManager().bindTexture(getBg());
        this.drawTexturedModalRect(this.guiLeft, this.guiTop, 0, 0, 256, 181);
        String terr = Utils.getTranslation("gui.core.territory", TextFormatting.GREEN);
        terr += ": " + territory;
        if (terr.length() >= 38) {
            if (coreLevel == maxCoreLevel) {
                this.fontRendererObj.drawString(terr.substring(0, 37) + "...", this.guiLeft + 19, this.guiTop + 10,
                        4210752);
                this.fontRendererObj.drawString(" | " + Utils.getTranslation("gui.core.level", TextFormatting.GOLD)
                        + ": " + String.valueOf(coreLevel), this.guiLeft + this.xSize - 64, this.guiTop + 10, 4210752);
            } else {
                this.fontRendererObj.drawString(terr.substring(0, 38) + "...", this.guiLeft + 19, this.guiTop + 10,
                        4210752);
                this.fontRendererObj.drawString(" | " + Utils.getTranslation("gui.core.level", TextFormatting.GOLD)
                        + ": " + String.valueOf(coreLevel), this.guiLeft + this.xSize - 58, this.guiTop + 10, 4210752);
            }
        } else {
            this.fontRendererObj.drawString(terr, this.guiLeft + 19, this.guiTop + 10, 4210752);
            this.fontRendererObj.drawString(
                    " | " + Utils.getTranslation("gui.core.level", TextFormatting.GOLD) + ": "
                            + String.valueOf(coreLevel),
                    coreLevel == maxCoreLevel ? this.guiLeft + this.xSize - 64 : this.guiLeft + this.xSize - 58,
                    this.guiTop + 10, 4210752);
        }
	        for (int i = 0; i < this.buttonList.size(); ++i) {
            ((GuiButton) this.buttonList.get(i)).drawButton(this.mc, mouseX, mouseY);
        }
	        for (int i = 0; i < this.buttonList.size(); ++i) {
            if (this.buttonList.get(i) instanceof AllyActiveEffectButton
                    || this.buttonList.get(i) instanceof AllyEffectButton)
                this.drawString(Minecraft.getMinecraft().fontRendererObj,
                        Utils.getTranslation(((HoverEffectButton) this.buttonList.get(i)).getEffect()) + " "
                                + (((HoverEffectButton) this.buttonList.get(i)).getTier() == 0 ? ""
                                        : (((HoverEffectButton) this.buttonList.get(i)).getTier() + 1)),
                        ((HoverEffectButton) this.buttonList.get(i)).xPosition + 25,
                        ((HoverEffectButton) this.buttonList.get(i)).yPosition + 7, 0xFFFFFF);
            else if (this.buttonList.get(i) instanceof EnemyActiveEffectButton
                    || this.buttonList.get(i) instanceof EnemyEffectButton)
                this.drawString(Minecraft.getMinecraft().fontRendererObj,
                        Utils.getTranslation(((HoverEffectButton) this.buttonList.get(i)).getEffect()) + " "
                                + (((HoverEffectButton) this.buttonList.get(i)).getTier() == 0 ? ""
                                        : (((HoverEffectButton) this.buttonList.get(i)).getTier() + 1)),
                        ((HoverEffectButton) this.buttonList.get(i)).xPosition + 5,
                        ((HoverEffectButton) this.buttonList.get(i)).yPosition + 7, 0xFFFFFF);
        }
	        this.mc.getTextureManager().bindTexture(getBg());
	        if (coreLevel < Settings.expansionLevel || expansions >= Settings.maxPurchasableExpansions)
            this.expansionButton.setEnabled(false);
	        if (coreLevel == maxCoreLevel)
            this.upgradeCoreButton.setEnabled(false);
	        if (this.coreLevel < 2) {
            this.drawTexturedModalRect(this.allyButtons.get(0).xPosition + 3, this.allyButtons.get(0).yPosition + 2, 0,
                    192, 18, 18);
            this.drawTexturedModalRect(this.enemyButtons.get(0).xPosition + 84, this.enemyButtons.get(0).yPosition + 2,
                    0, 192, 18, 18);
            this.allyButtons.get(0).setEnabled(false);
            this.enemyButtons.get(0).setEnabled(false);
        }
        if (this.coreLevel < 4) {
            this.drawTexturedModalRect(this.allyButtons.get(1).xPosition + 3, this.allyButtons.get(1).yPosition + 2, 0,
                    192, 16, 18);
            this.drawTexturedModalRect(this.enemyButtons.get(1).xPosition + 84, this.enemyButtons.get(1).yPosition + 2,
                    0, 192, 16, 18);
            this.allyButtons.get(1).setEnabled(false);
            this.enemyButtons.get(1).setEnabled(false);
        }
        if (this.coreLevel < 6) {
            this.drawTexturedModalRect(this.allyButtons.get(2).xPosition + 3, this.allyButtons.get(2).yPosition + 2, 0,
                    192, 16, 18);
            this.drawTexturedModalRect(this.enemyButtons.get(2).xPosition + 84, this.enemyButtons.get(2).yPosition + 2,
                    0, 192, 16, 18);
            this.allyButtons.get(2).setEnabled(false);
            this.enemyButtons.get(2).setEnabled(false);
        }
        if (this.coreLevel <  {
            this.drawTexturedModalRect(this.allyButtons.get(3).xPosition + 3, this.allyButtons.get(3).yPosition + 2, 0,
                    192, 16, 18);
            this.drawTexturedModalRect(this.enemyButtons.get(3).xPosition + 84, this.enemyButtons.get(3).yPosition + 2,
                    0, 192, 16, 18);
            this.allyButtons.get(3).setEnabled(false);
            this.enemyButtons.get(3).setEnabled(false);
        }
        if (this.coreLevel < 10) {
            this.drawTexturedModalRect(this.allyButtons.get(4).xPosition + 3, this.allyButtons.get(4).yPosition + 2, 0,
                    192, 16, 18);
            this.drawTexturedModalRect(this.enemyButtons.get(4).xPosition + 84, this.enemyButtons.get(4).yPosition + 2,
                    0, 192, 16, 18);
            this.allyButtons.get(4).setEnabled(false);
            this.enemyButtons.get(4).setEnabled(false);
        }
	        for (GuiButton button : this.buttonList) {
            if (button.isMouseOver()) {
                if (button instanceof HoverEffectButton) {
                    this.zLevel = 100;
                    if (button.enabled) {
                        this.drawCreativeTabHoveringText(((HoverEffectButton) button).getHoverText(), mouseX, mouseY);
                    } else {
                        if (button.id == 0 || button.id == 5)
                            this.drawCreativeTabHoveringText(Utils.getTranslation("gui.core.unlock.2"), mouseX, mouseY);
                        else if (button.id == 1 || button.id == 6)
                            this.drawCreativeTabHoveringText(Utils.getTranslation("gui.core.unlock.4"), mouseX, mouseY);
                        else if (button.id == 2 || button.id == 7)
                            this.drawCreativeTabHoveringText(Utils.getTranslation("gui.core.unlock.6"), mouseX, mouseY);
                        else if (button.id == 3 || button.id == 
                            this.drawCreativeTabHoveringText(Utils.getTranslation("gui.core.unlock.8"), mouseX, mouseY);
                        else if (button.id == 4 || button.id == 9)
                            this.drawCreativeTabHoveringText(Utils.getTranslation("gui.core.unlock.10"), mouseX,
                                    mouseY);
                    }
                    this.zLevel = 0;
                } else {
                    if (button.id == -1) {
                        if (coreLevel < maxCoreLevel) {
                            int g = Settings.baseUpgradeCost * coreLevel;
                            String up = Utils.getTranslation("gui.core.upgrade", TextFormatting.GOLD) + " "
                                    + String.valueOf(coreLevel + 1);
                            String down = (g != 0
                                    ? String.valueOf(g) + Utils.getTranslation("gui.core.gemstones", TextFormatting.LIGHT_PURPLE)
                                    : "");
                            this.drawHoveringText(Arrays.<String>asList(up, down), mouseX, mouseY);
                        } else
                            this.drawCreativeTabHoveringText(
                                    Utils.getTranslation("gui.core.upgrade.max", TextFormatting.RED), mouseX, mouseY);
                    } else if (button.id == -2) {
                        if (coreLevel >= Settings.expansionLevel) {
                            if (this.expansions < Settings.maxPurchasableExpansions) {
                                int g = Settings.expansionBaseCost.getGold() * (expansions + 1);
                                int s = Settings.expansionBaseCost.getSilver() * (expansions + 1);
                                int c = Settings.expansionBaseCost.getCopper() * (expansions + 1);
                                String up = Utils.getTranslation("gui.core.expansion", TextFormatting.GREEN) + " ("
                                        + Utils.getTranslation("gui.core.purchased") + ": " + String.valueOf(expansions)
                                        + "/" + String.valueOf(Settings.maxPurchasableExpansions) + ")";
                                String down = (g != 0 ? String.valueOf(g)
                                        + Utils.getTranslation("gui.core.golds", TextFormatting.YELLOW) : "")
                                        + (s != 0
                                                ? String.valueOf(s)
                                                        + Utils.getTranslation("gui.core.silvers", TextFormatting.GRAY)
                                                : "")
                                        + (c != 0 ? String.valueOf(c)
                                                + Utils.getTranslation("gui.core.coppers", TextFormatting.DARK_RED)
                                                : "");
                                this.drawHoveringText(Arrays.<String>asList(up, down), mouseX, mouseY);
                            } else
                                this.drawCreativeTabHoveringText(
                                        Utils.getTranslation("gui.core.expansion.max", TextFormatting.RED), mouseX,
                                        mouseY);
                        } else
                            this.drawCreativeTabHoveringText(Utils.getTranslation("gui.core.expansion.locked") + " "
                                    + String.valueOf(Settings.expansionLevel), mouseX, mouseY);
                    } else if (button.id == -3) {
                        String up = Utils.getTranslation("gui.core.remove", TextFormatting.RED);
                        String down = Utils.getTranslation("gui.core.remove.back");
                        this.drawHoveringText(Arrays.<String>asList(up, down), mouseX, mouseY);
                    }
                }
            }
        }
    }
	    @Override
    protected void actionPerformed(GuiButton button) throws IOException {
        if (button.enabled) {
            int id = button.id;
            if (id == -1) {
                int g = Settings.baseUpgradeCost * coreLevel;
                if (Utils.canPayGemstones(this.mc.thePlayer, g) && coreLevel < 10) {
                    Utils.payGemstones(this.mc.thePlayer, g);
                    coreLevel++;
                    if (coreLevel >= maxCoreLevel / 2 && coreLevel < maxCoreLevel) {
                        primaryLevel = 2;
                        secondaryLevel = 2;
                    } else if (coreLevel == maxCoreLevel) {
                        primaryLevel = 3;
                        secondaryLevel = 3;
                    }
                    Utils.sendMessage(this.mc.thePlayer,
                            Utils.getTranslation("gui.core.upgrade.success", TextFormatting.GREEN));
                    CoreFaction.NETWORK.sendToServer(new GuiCoreSaveServerMessage(this.territory,
                            String.valueOf(this.coreLevel), String.valueOf(this.expansions), String.valueOf(this.primaryEffect),
                            String.valueOf(this.secondaryEffect), String.valueOf(this.primaryLevel),
                            String.valueOf(this.secondaryLevel)));
                } else
                    Utils.sendMessage(this.mc.thePlayer,
                            Utils.getTranslation("gui.core.upgrade.fail", TextFormatting.RED));
                this.mc.displayGuiScreen((GuiScreen)null);
            } else if (id == -2) {
                int g = Settings.expansionBaseCost.getGold() * expansions;
                int s = Settings.expansionBaseCost.getSilver() * expansions;
                int c = Settings.expansionBaseCost.getCopper() * expansions;
                if (Utils.canPay(this.mc.thePlayer, g, s, c) && coreLevel >= Settings.expansionLevel
                        && this.expansions < Settings.maxPurchasableExpansions) {
                    Utils.pay(this.mc.thePlayer, g, s, c);
                    CoreFaction.NETWORK.sendToServer(new GuiCoreAddExpansionServerMessage(territory));
                    expansions++;
                    Utils.sendMessage(this.mc.thePlayer,
                            Utils.getTranslation("gui.core.expansion.success", TextFormatting.GREEN));
                } else {
                    this.mc.displayGuiScreen((GuiScreen)null);
                    Utils.sendMessage(this.mc.thePlayer,
                            Utils.getTranslation("gui.core.expansion.fail", TextFormatting.RED));
                }
                
            } else if (id == -3) {
                CoreFaction.NETWORK.sendToServer(new GuiCoreRemoveServerMessage(territory, faction));
                Utils.sendMessage(this.mc.thePlayer,
                        Utils.getTranslation("gui.core.remove.success", TextFormatting.RED));
                this.mc.displayGuiScreen((GuiScreen) null);
                return;
            } else if (id == 0) {
                if(this.primaryEffect != -999 && this.primaryEffect != Potion.getIdFromPotion(allyEffects.get(0)) && coreLevel == maxCoreLevel) {
                    this.secondaryEffect = this.secondaryEffect == Potion.getIdFromPotion(allyEffects.get(0)) ? -999
                            : Potion.getIdFromPotion(allyEffects.get(0));
                } else {
                    this.primaryEffect = this.primaryEffect == Potion.getIdFromPotion(allyEffects.get(0)) ? -999
                            : Potion.getIdFromPotion(allyEffects.get(0));
                }
            } else if (id == 1) {
                if(this.primaryEffect != -999 && this.primaryEffect != Potion.getIdFromPotion(allyEffects.get(1)) && coreLevel == maxCoreLevel) {
                    this.secondaryEffect = this.secondaryEffect == Potion.getIdFromPotion(allyEffects.get(1)) ? -999
                            : Potion.getIdFromPotion(allyEffects.get(1));
                } else {
                    this.primaryEffect = this.primaryEffect == Potion.getIdFromPotion(allyEffects.get(1)) ? -999
                            : Potion.getIdFromPotion(allyEffects.get(1));
                }
            } else if (id == 2) {
                if(this.primaryEffect != -999 && this.primaryEffect != Potion.getIdFromPotion(allyEffects.get(2)) && coreLevel == maxCoreLevel) {
                    this.secondaryEffect = this.secondaryEffect == Potion.getIdFromPotion(allyEffects.get(2)) ? -999
                            : Potion.getIdFromPotion(allyEffects.get(2));
                } else {
                    this.primaryEffect = this.primaryEffect == Potion.getIdFromPotion(allyEffects.get(2)) ? -999
                            : Potion.getIdFromPotion(allyEffects.get(2));
                }
            } else if (id == 3) {
                if(this.primaryEffect != -999 && this.primaryEffect != Potion.getIdFromPotion(allyEffects.get(3)) && coreLevel == maxCoreLevel) {
                    this.secondaryEffect = this.secondaryEffect == Potion.getIdFromPotion(allyEffects.get(3)) ? -999
                            : Potion.getIdFromPotion(allyEffects.get(3));
                } else {
                    this.primaryEffect = this.primaryEffect == Potion.getIdFromPotion(allyEffects.get(3)) ? -999
                            : Potion.getIdFromPotion(allyEffects.get(3));
                }
            } else if (id == 4) {
                if(this.primaryEffect != -999 && this.primaryEffect != Potion.getIdFromPotion(allyEffects.get(4)) && coreLevel == maxCoreLevel) {
                    this.secondaryEffect = this.secondaryEffect == Potion.getIdFromPotion(allyEffects.get(4)) ? -999
                            : Potion.getIdFromPotion(allyEffects.get(4));
                } else {
                    this.primaryEffect = this.primaryEffect == Potion.getIdFromPotion(allyEffects.get(4)) ? -999
                            : Potion.getIdFromPotion(allyEffects.get(4));
                }
            } else if (id == 5) {
                if(this.primaryEffect != -999 && this.primaryEffect != Potion.getIdFromPotion(enemyEffects.get(0)) && coreLevel == maxCoreLevel) {
                    this.secondaryEffect = this.secondaryEffect == Potion.getIdFromPotion(enemyEffects.get(0)) ? -999
                            : Potion.getIdFromPotion(enemyEffects.get(0));
                } else {
                    this.primaryEffect = this.primaryEffect == Potion.getIdFromPotion(enemyEffects.get(0)) ? -999
                            : Potion.getIdFromPotion(enemyEffects.get(0));
                }
            } else if (id == 6) {
                if(this.primaryEffect != -999 && this.primaryEffect != Potion.getIdFromPotion(enemyEffects.get(1)) && coreLevel == maxCoreLevel) {
                    this.secondaryEffect = this.secondaryEffect == Potion.getIdFromPotion(enemyEffects.get(1)) ? -999
                            : Potion.getIdFromPotion(enemyEffects.get(1));
                } else {
                    this.primaryEffect = this.primaryEffect == Potion.getIdFromPotion(enemyEffects.get(1)) ? -999
                            : Potion.getIdFromPotion(enemyEffects.get(1));
                }
            } else if (id == 7) {
                if(this.primaryEffect != -999 && this.primaryEffect != Potion.getIdFromPotion(enemyEffects.get(2)) && coreLevel == maxCoreLevel) {
                    this.secondaryEffect = this.secondaryEffect == Potion.getIdFromPotion(enemyEffects.get(2)) ? -999
                            : Potion.getIdFromPotion(enemyEffects.get(2));
                } else {
                    this.primaryEffect = this.primaryEffect == Potion.getIdFromPotion(enemyEffects.get(2)) ? -999
                            : Potion.getIdFromPotion(enemyEffects.get(2));
                }
	            } else if (id ==  {
                if(this.primaryEffect != -999 && this.primaryEffect != Potion.getIdFromPotion(enemyEffects.get(3)) && coreLevel == maxCoreLevel) {
                    this.secondaryEffect = this.secondaryEffect == Potion.getIdFromPotion(enemyEffects.get(3)) ? -999
                            : Potion.getIdFromPotion(enemyEffects.get(3));
                } else {
                    this.primaryEffect = this.primaryEffect == Potion.getIdFromPotion(enemyEffects.get(3)) ? -999
                            : Potion.getIdFromPotion(enemyEffects.get(3));
                }
            } else if (id == 9) {
                if(this.primaryEffect != -999 && this.primaryEffect != Potion.getIdFromPotion(enemyEffects.get(4)) && coreLevel == maxCoreLevel) {
                    this.secondaryEffect = this.secondaryEffect == Potion.getIdFromPotion(enemyEffects.get(4)) ? -999
                            : Potion.getIdFromPotion(enemyEffects.get(4));
                } else {
                    this.primaryEffect = this.primaryEffect == Potion.getIdFromPotion(enemyEffects.get(4)) ? -999
                            : Potion.getIdFromPotion(enemyEffects.get(4));
                }
                
            }
            CoreFaction.NETWORK.sendToServer(new GuiCoreSaveServerMessage(this.territory,
                    String.valueOf(this.coreLevel), String.valueOf(this.expansions), String.valueOf(this.primaryEffect),
                    String.valueOf(this.secondaryEffect), String.valueOf(this.primaryLevel),
                    String.valueOf(this.secondaryLevel)));
        }
        super.actionPerformed(button);
    }
	    @Override
    public boolean doesGuiPauseGame() {
        return false;
    }
	    public static ResourceLocation getBg() {
        return BG;
    }
	    @SideOnly(Side.CLIENT)
    class UpgradeButton extends ActionButton {
        public UpgradeButton(int buttonId, int x, int y) {
            super(buttonId, x, y, GuiCore.getBg(), 0, 237);
        }
	        public void drawButtonForegroundLayer(int mouseX, int mouseY) {
            GuiCore.this.drawCreativeTabHoveringText(
                    Utils.getTranslation("gui.core.upgrade", TextFormatting.GOLD) + "\n" + "Test", mouseX, mouseY);
        }
    }
	    @SideOnly(Side.CLIENT)
    class ExpansionButton extends ActionButton {
        public ExpansionButton(int buttonId, int x, int y) {
            super(buttonId, x, y, GuiCore.getBg(), 0, 237);
        }
	        public void drawButtonForegroundLayer(int mouseX, int mouseY) {
            GuiCore.this.drawCreativeTabHoveringText(Utils.getTranslation("gui.core.expansion", TextFormatting.GREEN),
                    mouseX, mouseY);
        }
    }
	    @SideOnly(Side.CLIENT)
    class RemoveButton extends ActionButton {
        public RemoveButton(int buttonId, int x, int y) {
            super(buttonId, x, y, GuiCore.getBg(), 23, 210);
        }
	        public void drawButtonForegroundLayer(int mouseX, int mouseY) {
            GuiCore.this.drawCreativeTabHoveringText(Utils.getTranslation("gui.core.remove", TextFormatting.RED),
                    mouseX, mouseY);
        }
    }
}

Don't blame me if i always ask for your help. I just want to learn to be better :)

  • 3 weeks later...
Posted (edited)

Clear the relevant lists in your initGui method. By default vanilla clears the button list on gui's resize. If you setup something else in that method you need to clear it beforehand too or you end up with stuff duplicating in your lists.

To clarify:

On 6/23/2017 at 0:49 AM, JimiIT92 said:

this.buttonList.addAll(allyButtons);         this.buttonList.addAll(enemyButtons);

This is your culprit. The butonList is cleared before initGui is called, but your allyButtons and enemyButtons are not.

Edited by V0idWa1k3r

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

    • Version 1.19 - Forge 41.0.63 I want to create a wolf entity that I can ride, so far it seems to be working, but the problem is that when I get on the wolf, I can’t control it. I then discovered that the issue is that the server doesn’t detect that I’m riding the wolf, so I’m struggling with synchronization. However, it seems to not be working properly. As I understand it, the server receives the packet but doesn’t register it correctly. I’m a bit new to Java, and I’ll try to provide all the relevant code and prints *The comments and prints are translated by chatgpt since they were originally in Spanish* Thank you very much in advance No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. MountableWolfEntity package com.vals.valscraft.entity; import com.vals.valscraft.network.MountSyncPacket; import com.vals.valscraft.network.NetworkHandler; import net.minecraft.client.Minecraft; import net.minecraft.network.syncher.EntityDataAccessor; import net.minecraft.network.syncher.EntityDataSerializers; import net.minecraft.network.syncher.SynchedEntityData; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.Mob; import net.minecraft.world.entity.ai.attributes.AttributeSupplier; import net.minecraft.world.entity.ai.attributes.Attributes; import net.minecraft.world.entity.animal.Wolf; import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.Entity; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; import net.minecraft.world.level.Level; import net.minecraft.world.phys.Vec3; import net.minecraftforge.event.TickEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.network.PacketDistributor; public class MountableWolfEntity extends Wolf { private boolean hasSaddle; private static final EntityDataAccessor<Byte> DATA_ID_FLAGS = SynchedEntityData.defineId(MountableWolfEntity.class, EntityDataSerializers.BYTE); public MountableWolfEntity(EntityType<? extends Wolf> type, Level level) { super(type, level); this.hasSaddle = false; } @Override protected void defineSynchedData() { super.defineSynchedData(); this.entityData.define(DATA_ID_FLAGS, (byte)0); } public static AttributeSupplier.Builder createAttributes() { return Wolf.createAttributes() .add(Attributes.MAX_HEALTH, 20.0) .add(Attributes.MOVEMENT_SPEED, 0.3); } @Override public InteractionResult mobInteract(Player player, InteractionHand hand) { ItemStack itemstack = player.getItemInHand(hand); if (itemstack.getItem() == Items.SADDLE && !this.hasSaddle()) { if (!player.isCreative()) { itemstack.shrink(1); } this.setSaddle(true); return InteractionResult.SUCCESS; } else if (!level.isClientSide && this.hasSaddle()) { player.startRiding(this); MountSyncPacket packet = new MountSyncPacket(true); // 'true' means the player is mounted NetworkHandler.CHANNEL.sendToServer(packet); // Ensure the server handles the packet return InteractionResult.SUCCESS; } return InteractionResult.PASS; } @Override public void travel(Vec3 travelVector) { if (this.isVehicle() && this.getControllingPassenger() instanceof Player) { System.out.println("The wolf has a passenger."); System.out.println("The passenger is a player."); Player player = (Player) this.getControllingPassenger(); // Ensure the player is the controller this.setYRot(player.getYRot()); this.yRotO = this.getYRot(); this.setXRot(player.getXRot() * 0.5F); this.setRot(this.getYRot(), this.getXRot()); this.yBodyRot = this.getYRot(); this.yHeadRot = this.yBodyRot; float forward = player.zza; float strafe = player.xxa; if (forward <= 0.0F) { forward *= 0.25F; } this.flyingSpeed = this.getSpeed() * 0.1F; this.setSpeed((float) this.getAttributeValue(Attributes.MOVEMENT_SPEED) * 1.5F); this.setDeltaMovement(new Vec3(strafe, travelVector.y, forward).scale(this.getSpeed())); this.calculateEntityAnimation(this, false); } else { // The wolf does not have a passenger or the passenger is not a player System.out.println("No player is mounted, or the passenger is not a player."); super.travel(travelVector); } } public boolean hasSaddle() { return this.hasSaddle; } public void setSaddle(boolean hasSaddle) { this.hasSaddle = hasSaddle; } @Override protected void dropEquipment() { super.dropEquipment(); if (this.hasSaddle()) { this.spawnAtLocation(Items.SADDLE); this.setSaddle(false); } } @SubscribeEvent public static void onServerTick(TickEvent.ServerTickEvent event) { if (event.phase == TickEvent.Phase.START) { MinecraftServer server = net.minecraftforge.server.ServerLifecycleHooks.getCurrentServer(); if (server != null) { for (ServerPlayer player : server.getPlayerList().getPlayers()) { if (player.isPassenger() && player.getVehicle() instanceof MountableWolfEntity) { MountableWolfEntity wolf = (MountableWolfEntity) player.getVehicle(); System.out.println("Tick: " + player.getName().getString() + " is correctly mounted on " + wolf); } } } } } private boolean lastMountedState = false; @Override public void tick() { super.tick(); if (!this.level.isClientSide) { // Only on the server boolean isMounted = this.isVehicle() && this.getControllingPassenger() instanceof Player; // Only print if the state changed if (isMounted != lastMountedState) { if (isMounted) { Player player = (Player) this.getControllingPassenger(); // Verify the passenger is a player System.out.println("Server: Player " + player.getName().getString() + " is now mounted."); } else { System.out.println("Server: The wolf no longer has a passenger."); } lastMountedState = isMounted; } } } @Override public void addPassenger(Entity passenger) { super.addPassenger(passenger); if (passenger instanceof Player) { Player player = (Player) passenger; if (!this.level.isClientSide && player instanceof ServerPlayer) { // Send the packet to the server to indicate the player is mounted NetworkHandler.CHANNEL.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), new MountSyncPacket(true)); } } } @Override public void removePassenger(Entity passenger) { super.removePassenger(passenger); if (passenger instanceof Player) { Player player = (Player) passenger; if (!this.level.isClientSide && player instanceof ServerPlayer) { // Send the packet to the server to indicate the player is no longer mounted NetworkHandler.CHANNEL.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), new MountSyncPacket(false)); } } } @Override public boolean isControlledByLocalInstance() { Entity entity = this.getControllingPassenger(); return entity instanceof Player; } @Override public void positionRider(Entity passenger) { if (this.hasPassenger(passenger)) { double xOffset = Math.cos(Math.toRadians(this.getYRot() + 90)) * 0.4; double zOffset = Math.sin(Math.toRadians(this.getYRot() + 90)) * 0.4; passenger.setPos(this.getX() + xOffset, this.getY() + this.getPassengersRidingOffset() + passenger.getMyRidingOffset(), this.getZ() + zOffset); } } } MountSyncPacket package com.vals.valscraft.network; import com.vals.valscraft.entity.MountableWolfEntity; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.player.Player; import net.minecraftforge.network.NetworkEvent; import java.util.function.Supplier; public class MountSyncPacket { private final boolean isMounted; public MountSyncPacket(boolean isMounted) { this.isMounted = isMounted; } public void encode(FriendlyByteBuf buffer) { buffer.writeBoolean(isMounted); } public static MountSyncPacket decode(FriendlyByteBuf buffer) { return new MountSyncPacket(buffer.readBoolean()); } public void handle(NetworkEvent.Context context) { context.enqueueWork(() -> { ServerPlayer player = context.getSender(); // Get the player from the context if (player != null) { // Verifies if the player has dismounted if (!isMounted) { Entity vehicle = player.getVehicle(); if (vehicle instanceof MountableWolfEntity wolf) { // Logic to remove the player as a passenger wolf.removePassenger(player); System.out.println("Server: Player " + player.getName().getString() + " is no longer mounted."); } } } }); context.setPacketHandled(true); // Marks the packet as handled } } networkHandler package com.vals.valscraft.network; import com.vals.valscraft.valscraft; import net.minecraft.resources.ResourceLocation; import net.minecraftforge.network.NetworkRegistry; import net.minecraftforge.network.simple.SimpleChannel; import net.minecraftforge.network.NetworkEvent; import java.util.function.Supplier; public class NetworkHandler { private static final String PROTOCOL_VERSION = "1"; public static final SimpleChannel CHANNEL = NetworkRegistry.newSimpleChannel( new ResourceLocation(valscraft.MODID, "main"), () -> PROTOCOL_VERSION, PROTOCOL_VERSION::equals, PROTOCOL_VERSION::equals ); public static void init() { int packetId = 0; // Register the mount synchronization packet CHANNEL.registerMessage( packetId++, MountSyncPacket.class, MountSyncPacket::encode, MountSyncPacket::decode, (msg, context) -> msg.handle(context.get()) // Get the context with context.get() ); } }  
    • Do you use features of inventory profiles next (ipnext) or is there a change without it?
    • Remove rubidium - you are already using embeddium, which is a fork of rubidium
  • Topics

×
×
  • Create New...

Important Information

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