aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/recipes/VillagerTradeRecipe.java
blob: 530e8e32a724add76aa72f7078f97bd1f0842070 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package io.github.moulberry.notenoughupdates.recipes;

import com.google.common.collect.Sets;
import com.google.gson.JsonObject;
import com.mojang.authlib.GameProfile;
import io.github.moulberry.notenoughupdates.NEUManager;
import io.github.moulberry.notenoughupdates.NotEnoughUpdates;
import io.github.moulberry.notenoughupdates.miscgui.GuiItemRecipe;
import io.github.moulberry.notenoughupdates.util.SBInfo;
import io.github.moulberry.notenoughupdates.util.Utils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.AbstractClientPlayer;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.inventory.GuiInventory;
import net.minecraft.client.network.NetworkPlayerInfo;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.passive.EntityVillager;
import net.minecraft.util.ResourceLocation;

import java.util.*;

public class VillagerTradeRecipe implements NeuRecipe {

    public static final int COST_SLOT_X = 51;
    public static final int COST_SLOT_Y = 34;
    public static final int RESULT_SLOT_Y = 35;
    public static final int RESULT_SLOT_X = 124;

    private static class Holder { // This holder object exists to defer initialization to first access
        private static final GameProfile DREAM_PROFILE = new GameProfile(UUID.fromString("ec70bcaf-702f-4bb8-b48d-276fa52a780c"), "Dream");
        private static final EntityLivingBase DEMO_DREAM = new AbstractClientPlayer(null, DREAM_PROFILE) {
            @Override
            protected NetworkPlayerInfo getPlayerInfo() {
                return new NetworkPlayerInfo(DREAM_PROFILE) {
                    @Override
                    public ResourceLocation getLocationSkin() {
                        return new ResourceLocation("notenoughupdates", "dreamskin.png");
                    }
                };
            }
        };
        private static final EntityLivingBase DEMO_VILLAGER = new EntityVillager(null);

        private static boolean isAprilFirst() {
            Calendar cal = Calendar.getInstance();
            return cal.get(Calendar.DAY_OF_MONTH) == 1 && cal.get(Calendar.MONTH) == Calendar.APRIL;
        }

        private static final EntityLivingBase DEMO_ENTITY = isAprilFirst() ? DEMO_DREAM : DEMO_VILLAGER;

    }

    private final static ResourceLocation BACKGROUND = new ResourceLocation("notenoughupdates", "textures/gui/villager_recipe.png");

    private final Ingredient result;
    private final Ingredient cost;
    private final int minCost, maxCost;

    public VillagerTradeRecipe(Ingredient result, Ingredient cost, int minCost, int maxCost) {
        this.result = result;
        this.cost = cost;
        this.minCost = minCost;
        this.maxCost = maxCost;
    }

    public VillagerTradeRecipe(Ingredient result, Ingredient cost) {
        this(result, cost, -1, -1);
    }

    public boolean hasVariableCost() {
        return minCost != -1 && maxCost != -1;
    }

    @Override
    public Set<Ingredient> getIngredients() {
        return Sets.newHashSet(cost);
    }

    @Override
    public Set<Ingredient> getOutputs() {
        return Sets.newHashSet(result);
    }

    @Override
    public List<RecipeSlot> getSlots() {
        return Arrays.asList(
                new RecipeSlot(COST_SLOT_X, COST_SLOT_Y, cost.getItemStack()),
                new RecipeSlot(RESULT_SLOT_X, RESULT_SLOT_Y, result.getItemStack())
        );
    }

    @Override
    public boolean shouldUseForCraftCost() {
        return false;
    }

    @Override
    public boolean isAvailable() {
        return SBInfo.getInstance().getCurrentMode() == SBInfo.Gamemode.STRANDED || NotEnoughUpdates.INSTANCE.config.hidden.dev;
    }

    @Override
    public void drawExtraInfo(GuiItemRecipe gui, int mouseX, int mouseY) {
        if (hasVariableCost()) {
            FontRenderer fontRenderer = Minecraft.getMinecraft().fontRendererObj;
            Utils.drawStringCenteredScaledMaxWidth(
                    minCost + " - " + maxCost, fontRenderer,
                    gui.guiLeft + 50, gui.guiTop + 60, false, 75, 0xff00ff);

        }
    }

    @Override
    public void drawExtraBackground(GuiItemRecipe gui, int mouseX, int mouseY) {
        GuiInventory.drawEntityOnScreen(gui.guiLeft + 90, gui.guiTop + 75, 30, gui.guiLeft - mouseX + 80, gui.guiTop + 60 - mouseY, Holder.DEMO_ENTITY);
    }

    @Override
    public JsonObject serialize() {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("type", "trade");
        jsonObject.addProperty("result", result.serialize());
        jsonObject.addProperty("cost", cost.getInternalItemId());
        if (minCost > 0)
            jsonObject.addProperty("min", minCost);
        if (maxCost > 0)
            jsonObject.addProperty("max", maxCost);
        return jsonObject;
    }

    @Override
    public ResourceLocation getBackground() {
        return BACKGROUND;
    }

    public static VillagerTradeRecipe parseStaticRecipe(NEUManager manager, JsonObject recipe) {
        return new VillagerTradeRecipe(
                new Ingredient(manager, recipe.get("result").getAsString()),
                new Ingredient(manager, recipe.get("cost").getAsString()),
                recipe.has("min") ? recipe.get("min").getAsInt() : -1,
                recipe.has("max") ? recipe.get("max").getAsInt() : -1
        );
    }

}