Add recipe gui

This commit is contained in:
Marcel Konrad
2018-06-28 01:06:32 +02:00
parent 307aafe010
commit 70c7419795
6 changed files with 266 additions and 0 deletions

View File

@@ -0,0 +1,97 @@
package exopandora.worldhandler.builder.impl;
import javax.annotation.Nullable;
import exopandora.worldhandler.builder.CommandBuilder;
import exopandora.worldhandler.builder.Syntax;
import exopandora.worldhandler.builder.impl.BuilderAdvancement.EnumActionType;
import exopandora.worldhandler.builder.types.Type;
import exopandora.worldhandler.helper.EnumHelper;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class BuilderRecipe extends CommandBuilder
{
public BuilderRecipe()
{
this(null, null, null);
}
public BuilderRecipe(EnumMode mode, String player, ResourceLocation recipe)
{
this.setMode(mode);
this.setPlayer(player);
this.setRecipe(recipe);
}
public void setMode(EnumMode mode)
{
this.setNode(0, mode != null ? mode.toString() : null);
}
@Nullable
public EnumMode getMode()
{
return EnumHelper.valueOf(EnumMode.class, this.getNodeAsString(0));
}
public void setPlayer(String player)
{
this.setNode(1, player);
}
@Nullable
public String getPlayer()
{
return this.getNodeAsString(1);
}
public void setRecipe(ResourceLocation recipe)
{
this.setNode(2, recipe);
}
@Nullable
public ResourceLocation getRecipe()
{
return this.getNodeAsResourceLocation(2);
}
public BuilderRecipe getBuilderForMode(EnumMode mode)
{
return new BuilderRecipe(mode, this.getPlayer(), this.getRecipe());
}
@Override
public String getCommandName()
{
return "recipe";
}
@Override
public Syntax getSyntax()
{
Syntax syntax = new Syntax();
syntax.addRequired("give|take", Type.STRING);
syntax.addOptional("player", Type.STRING);
syntax.addOptional("recipe", Type.RESOURCE_LOCATION);
return syntax;
}
@SideOnly(Side.CLIENT)
public static enum EnumMode
{
GIVE,
TAKE;
@Override
public String toString()
{
return this.name().toLowerCase();
}
}
}

View File

@@ -26,6 +26,7 @@ public class Contents
public static final Content WORLD_INFO;
public static final Content GAMERULES;
public static final Content RECIPES;
public static final Content PLAYER;
public static final Content EXPERIENCE;
@@ -59,6 +60,7 @@ public class Contents
WORLD_INFO = Contents.getRegisteredContainer("world");
GAMERULES = Contents.getRegisteredContainer("gamerules");
RECIPES = Contents.getRegisteredContainer("recipes");
PLAYER = Contents.getRegisteredContainer("player");
EXPERIENCE = Contents.getRegisteredContainer("experience");

View File

@@ -0,0 +1,158 @@
package exopandora.worldhandler.gui.content.impl;
import java.util.ArrayList;
import java.util.List;
import exopandora.worldhandler.WorldHandler;
import exopandora.worldhandler.builder.ICommandBuilder;
import exopandora.worldhandler.builder.impl.BuilderRecipe;
import exopandora.worldhandler.builder.impl.BuilderRecipe.EnumMode;
import exopandora.worldhandler.builder.types.Type;
import exopandora.worldhandler.gui.button.EnumTooltip;
import exopandora.worldhandler.gui.button.GuiButtonWorldHandler;
import exopandora.worldhandler.gui.category.Categories;
import exopandora.worldhandler.gui.category.Category;
import exopandora.worldhandler.gui.container.Container;
import exopandora.worldhandler.gui.content.Content;
import exopandora.worldhandler.gui.content.Contents;
import exopandora.worldhandler.gui.content.element.impl.ElementPageList;
import exopandora.worldhandler.gui.content.element.logic.ILogicPageList;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.resources.I18n;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.item.crafting.IRecipe;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class ContentRecipes extends Content
{
private final BuilderRecipe builderRecipe = new BuilderRecipe();
@Override
public ICommandBuilder getCommandBuilder()
{
return this.builderRecipe;
}
@Override
public void initGui(Container container, int x, int y)
{
List<IRecipe> recipes = new ArrayList<IRecipe>();
for(IRecipe recipe : CraftingManager.REGISTRY)
{
if(!recipe.isDynamic())
{
recipes.add(recipe);
}
}
ElementPageList<IRecipe, String> list = new ElementPageList<IRecipe, String>(x, y, recipes, null, 114, 20, 3, this, new int[] {6, 7, 8}, new ILogicPageList<IRecipe, String>()
{
@Override
public String translate(IRecipe key)
{
if(!key.getRecipeOutput().equals(ItemStack.EMPTY))
{
return key.getRecipeOutput().getDisplayName();
}
return key.getRegistryName().toString();
}
@Override
public void onClick(IRecipe clicked)
{
builderRecipe.setRecipe(clicked.getRegistryName());
}
@Override
public String getRegistryName(IRecipe key)
{
return key.getRegistryName().toString();
}
@Override
public void onRegister(int id, int x, int y, int width, int height, String display, String registry, boolean enabled, IRecipe value, Container container)
{
GuiButtonWorldHandler button;
container.add(button = new GuiButtonWorldHandler(id, x, y, width, height, display, value.getRegistryName().toString(), EnumTooltip.TOP_RIGHT));
button.enabled = enabled;
}
@Override
public IRecipe convert(String object)
{
return CraftingManager.REGISTRY.getObject(Type.parseResourceLocation(object));
}
@Override
public String getId()
{
return "recipe";
}
});
container.add(list);
}
@Override
public void initButtons(Container container, int x, int y)
{
container.add(new GuiButtonWorldHandler(0, x, y + 96, 114, 20, I18n.format("gui.worldhandler.generic.back")));
container.add(new GuiButtonWorldHandler(1, x + 118, y + 96, 114, 20, I18n.format("gui.worldhandler.generic.backToGame")));
container.add(new GuiButtonWorldHandler(2, x + 118, y + 24, 114, 20, I18n.format("gui.worldhandler.recipes.give")));
container.add(new GuiButtonWorldHandler(3, x + 118, y + 48, 114, 20, I18n.format("gui.worldhandler.recipes.take")));
}
@Override
public void actionPerformed(Container container, GuiButton button)
{
switch(button.id)
{
case 2:
WorldHandler.sendCommand(this.builderRecipe.getBuilderForMode(EnumMode.GIVE));
container.initButtons();
break;
case 3:
WorldHandler.sendCommand(this.builderRecipe.getBuilderForMode(EnumMode.TAKE));
container.initButtons();
break;
default:
break;
}
}
@Override
public Category getCategory()
{
return Categories.WORLD;
}
@Override
public String getTitle()
{
return "Recipes";
}
@Override
public String getTabTitle()
{
return "Recipes";
}
@Override
public Content getActiveContent()
{
return Contents.RECIPES;
}
@Override
public void onPlayerNameChanged(String username)
{
this.builderRecipe.setPlayer(username);
}
}