ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/booklet/page/PageTrials.java

100 lines
3.6 KiB
Java
Raw Normal View History

package de.ellpeck.actuallyadditions.booklet.page;
2017-02-18 00:54:58 +01:00
import de.ellpeck.actuallyadditions.api.booklet.internal.GuiBookletBase;
import de.ellpeck.actuallyadditions.common.ActuallyAdditions;
import de.ellpeck.actuallyadditions.common.data.PlayerData;
import de.ellpeck.actuallyadditions.common.data.PlayerData.PlayerSave;
import de.ellpeck.actuallyadditions.common.network.PacketHandlerHelper;
import de.ellpeck.actuallyadditions.common.util.StringUtil;
2017-02-18 00:54:58 +01:00
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
2019-05-02 09:10:29 +02:00
public class PageTrials extends BookletPage {
2017-02-18 00:54:58 +01:00
2017-04-01 19:40:39 +02:00
private final int buttonId;
2017-02-18 00:54:58 +01:00
@SideOnly(Side.CLIENT)
private GuiButton button;
2019-05-02 09:10:29 +02:00
public PageTrials(int localizationKey, boolean button, boolean text) {
2017-02-18 00:54:58 +01:00
super(localizationKey);
2019-05-02 09:10:29 +02:00
if (!text) {
2017-02-18 00:54:58 +01:00
this.setNoText();
}
2019-05-02 09:10:29 +02:00
if (button) {
2017-02-18 00:54:58 +01:00
this.buttonId = PageLinkButton.nextButtonId;
PageLinkButton.nextButtonId++;
2019-05-02 09:10:29 +02:00
} else {
2017-02-18 00:54:58 +01:00
this.buttonId = -1;
}
}
@Override
@SideOnly(Side.CLIENT)
2019-05-02 09:10:29 +02:00
public void initGui(GuiBookletBase gui, int startX, int startY) {
2017-02-18 00:54:58 +01:00
super.initGui(gui, startX, startY);
2019-05-02 09:10:29 +02:00
if (this.buttonId >= 0) {
this.button = new GuiButton(this.buttonId, startX + 125 / 2 - 50, startY + 120, 100, 20, "");
2017-02-18 00:54:58 +01:00
gui.getButtonList().add(this.button);
this.updateButton();
}
}
@Override
@SideOnly(Side.CLIENT)
2019-05-02 09:10:29 +02:00
public void drawScreenPre(GuiBookletBase gui, int startX, int startY, int mouseX, int mouseY, float partialTicks) {
2017-02-18 00:54:58 +01:00
super.drawScreenPre(gui, startX, startY, mouseX, mouseY, partialTicks);
2019-05-02 09:10:29 +02:00
PageTextOnly.renderTextToPage(gui, this, startX + 6, startY + 5);
2017-02-18 00:54:58 +01:00
}
@Override
@SideOnly(Side.CLIENT)
2019-05-02 09:10:29 +02:00
protected String getLocalizationKey() {
return "booklet." + ActuallyAdditions.MODID + ".trials." + this.chapter.getIdentifier() + ".text." + this.localizationKey;
2017-02-18 00:54:58 +01:00
}
@Override
@SideOnly(Side.CLIENT)
2019-05-02 09:10:29 +02:00
public void actionPerformed(GuiBookletBase gui, GuiButton button) {
if (this.buttonId >= 0 && button.id == this.buttonId) {
2017-02-18 00:54:58 +01:00
EntityPlayer player = Minecraft.getMinecraft().player;
PlayerSave data = PlayerData.getDataFromPlayer(player);
String id = this.chapter.getIdentifier();
boolean completed = data.completedTrials.contains(id);
2019-05-02 09:10:29 +02:00
if (completed) {
2017-02-18 00:54:58 +01:00
data.completedTrials.remove(id);
2019-05-02 09:10:29 +02:00
} else {
2017-02-18 00:54:58 +01:00
data.completedTrials.add(id);
}
this.updateButton();
PacketHandlerHelper.sendPlayerDataToServer(false, 2);
2019-05-02 09:10:29 +02:00
} else {
2017-02-18 00:54:58 +01:00
super.actionPerformed(gui, button);
}
}
@SideOnly(Side.CLIENT)
2019-05-02 09:10:29 +02:00
private void updateButton() {
if (this.buttonId >= 0 && this.button != null) {
2017-02-18 00:54:58 +01:00
EntityPlayer player = Minecraft.getMinecraft().player;
PlayerSave data = PlayerData.getDataFromPlayer(player);
boolean completed = data.completedTrials.contains(this.chapter.getIdentifier());
2019-05-02 09:10:29 +02:00
if (completed) {
this.button.displayString = TextFormatting.DARK_GREEN + StringUtil.localize("booklet." + ActuallyAdditions.MODID + ".trialFinishButton.completed.name");
} else {
this.button.displayString = TextFormatting.DARK_RED + StringUtil.localize("booklet." + ActuallyAdditions.MODID + ".trialFinishButton.uncompleted.name");
2017-02-18 00:54:58 +01:00
}
}
}
}