Skip to content

Commit

Permalink
Merge pull request #886 from Gu-ZT/releases/1.20.1
Browse files Browse the repository at this point in the history
优化 SelectOne 显示
  • Loading branch information
Gu-ZT authored Jun 12, 2024
2 parents d07726d + 5d237e2 commit 42a904c
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,17 @@
import dev.dubhe.anvilcraft.api.recipe.AnvilRecipeManager;
import dev.dubhe.anvilcraft.init.ModBlocks;
import dev.dubhe.anvilcraft.integration.emi.recipe.AnvilProcessEmiRecipe;
import dev.dubhe.anvilcraft.integration.emi.stack.BlockStateEmiStack;
import dev.emi.emi.api.EmiEntrypoint;
import dev.emi.emi.api.EmiPlugin;
import dev.emi.emi.api.EmiRegistry;
import dev.emi.emi.api.stack.EmiStack;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import org.jetbrains.annotations.NotNull;

@EmiEntrypoint
public class AnvilCraftEmiPlugin implements EmiPlugin {
@Override
public void register(EmiRegistry registry) {
for (Block block : BuiltInRegistries.BLOCK) {
EmiStack stack = BlockStateEmiStack.of(block.defaultBlockState());
registry.addEmiStack(stack);
}
public void register(@NotNull EmiRegistry registry) {
AnvilRecipeCategory.ALL.forEach(registry::addCategory);

registry.addWorkstation(AnvilRecipeCategory.STAMPING, EmiStack.of(ModBlocks.STAMPING_PLATFORM.get()));
Expand Down Expand Up @@ -103,6 +97,7 @@ public void register(EmiRegistry registry) {
registry.addWorkstation(AnvilRecipeCategory.SQUEEZE, EmiStack.of(Blocks.CHIPPED_ANVIL));
registry.addWorkstation(AnvilRecipeCategory.SQUEEZE, EmiStack.of(Blocks.DAMAGED_ANVIL));

registry.addWorkstation(AnvilRecipeCategory.SUPER_HEATING, EmiStack.of(Blocks.CAULDRON));
registry.addWorkstation(AnvilRecipeCategory.SUPER_HEATING, EmiStack.of(ModBlocks.HEATER));
registry.addWorkstation(AnvilRecipeCategory.SUPER_HEATING, EmiStack.of(ModBlocks.ROYAL_ANVIL));
registry.addWorkstation(AnvilRecipeCategory.SUPER_HEATING, EmiStack.of(Blocks.ANVIL));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,29 @@ public EmiRecipeCategory getCategory() {
return this.recipe.getId();
}

@SuppressWarnings("UnstableApiUsage")
@Override
public List<EmiIngredient> getInputs() {
return this.inputs;
List<EmiIngredient> in = new ArrayList<>();
in.addAll(this.inputs);
in.addAll(this.inputBlocks);
for (EmiIngredient input : this.inputs) {
if (input instanceof ListEmiIngredient ingredients) in.addAll(ingredients.getEmiStacks());
}
return in;
}

@Override
public List<EmiStack> getOutputs() {
return this.outputs;
List<EmiStack> out = new ArrayList<>();
out.addAll(this.outputs);
out.addAll(this.outputBlocks);
for (EmiStack output : this.outputs) {
if (output instanceof SelectOneEmiStack stack) {
out.addAll(stack.getStacks());
}
}
return out;
}

@Override
Expand Down Expand Up @@ -227,13 +242,13 @@ protected void addOutputArrow(@NotNull WidgetHolder widgets, int x, int y) {
}

protected void addInputSlots(WidgetHolder widgets) {
List<Vec2> posLis = this.getSlotsPosList(inputs.size());
List<Vec2> posList = this.getSlotsPosList(inputs.size());
Vec2 size = this.getSlotsComposeSize(inputs.size());
int x = this.inputs.size() == 1 ? 40 : (int) ((26 - size.x / 2) + 10);
int y = (int) ((26 - size.y / 2) + 15);
Iterator<EmiIngredient> ingredientIterator = inputs.iterator();
for (Vec2 vec2 : posLis) {
addSlot(ingredientIterator.next(), (int) (x + vec2.x), (int) (y + vec2.y), widgets);
for (Vec2 vec2 : posList) {
this.addSlot(ingredientIterator.next(), (int) (x + vec2.x), (int) (y + vec2.y), widgets);
}
}

Expand All @@ -260,33 +275,42 @@ protected Vec2 getSlotsComposeSize(int length) {
}

protected void addSlot(@NotNull EmiIngredient ingredient, int x, int y, WidgetHolder widgets) {
if (ingredient instanceof SelectOneEmiStack) {
this.addSelectOneSlot(ingredient, x, y, widgets);
return;
}
if (ingredient.getChance() < 1) {
addChangeSlot(ingredient, x, y, widgets);
this.addChanceSlot(ingredient, x, y, widgets);
return;
}
addSimpleSlot(ingredient, x, y, widgets);
this.addSimpleSlot(ingredient, x, y, widgets);
}

protected void addSimpleSlot(EmiIngredient ingredient, int x, int y, @NotNull WidgetHolder widgets) {
widgets.add(new SlotWidget(ingredient, x, y)
.customBackground(EMI_GUI_TEXTURES, 0, 0, 18, 18));
}

protected void addChangeSlot(EmiIngredient ingredient, int x, int y, @NotNull WidgetHolder widgets) {
protected void addChanceSlot(EmiIngredient ingredient, int x, int y, @NotNull WidgetHolder widgets) {
widgets.add(new SlotWidget(ingredient, x, y).recipeContext(this)
.customBackground(EMI_GUI_TEXTURES, 19, 0, 18, 18));
}

protected void addSelectOneSlot(EmiIngredient ingredient, int x, int y, @NotNull WidgetHolder widgets) {
widgets.add(new SlotWidget(ingredient, x, y).recipeContext(this)
.customBackground(EMI_GUI_TEXTURES, 38, 0, 18, 18));
}

protected void addOutputs(WidgetHolder widgets) {
int outputSize = outputs.size();
List<Vec2> posLis = getSlotsPosList(outputSize);
Vec2 composeSize = getSlotsComposeSize(outputSize);
int outputSize = this.outputs.size();
List<Vec2> posList = this.getSlotsPosList(outputSize);
Vec2 composeSize = this.getSlotsComposeSize(outputSize);
int x = outputSize == 1 ? 190 : (int) ((26 - composeSize.x / 2) + 190);
int y = (int) ((26 - composeSize.y / 2) + 15);
Iterator<EmiStack> emiStackIterator = outputs.iterator();
for (Vec2 vec2 : posLis) {
for (Vec2 vec2 : posList) {
if (emiStackIterator.hasNext()) {
addSlot(emiStackIterator.next(), (int) (x + vec2.x), (int) (y + vec2.y), widgets);
this.addSlot(emiStackIterator.next(), (int) (x + vec2.x), (int) (y + vec2.y), widgets);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,30 @@
import dev.dubhe.anvilcraft.data.recipe.anvil.outcome.SpawnItem;
import dev.emi.emi.api.stack.EmiIngredient;
import dev.emi.emi.api.stack.EmiStack;
import dev.emi.emi.screen.tooltip.EmiTooltip;
import lombok.Getter;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

@Getter
public class SelectOneEmiStack extends EmiStack {
private static final Minecraft MINECRAFT = Minecraft.getInstance();
private final List<EmiIngredient> stacks = new ArrayList<>();
private final List<EmiStack> stacks = new ArrayList<>();
private float maxChance = 0;

private SelectOneEmiStack(@NotNull List<EmiIngredient> stacks) {
for (EmiIngredient stack : stacks) {
private SelectOneEmiStack(@NotNull List<EmiStack> stacks) {
for (EmiStack stack : stacks) {
this.stacks.add(stack.copy());
this.maxChance += stack.getChance();
}
}

Expand All @@ -36,6 +43,9 @@ private SelectOneEmiStack(@NotNull SelectOne selectOne) {
this.stacks.add(BlockStateEmiStack.of(block.getResult()).setChance((float) outcome.getChance()));
}
}
for (EmiStack stack : this.stacks) {
this.maxChance += stack.getChance();
}
}

public static @NotNull SelectOneEmiStack of(@NotNull SelectOne selectOne) {
Expand All @@ -47,11 +57,16 @@ public SelectOneEmiStack copy() {
return new SelectOneEmiStack(this.stacks);
}

private @Nullable EmiIngredient get() {
if (MINECRAFT.level == null || this.stacks.isEmpty()) return null;
int index = (int) Math.floor((MINECRAFT.level.getGameTime() % (this.stacks.size() * 20)) / 20.0);
return this.stacks.get(index);
}

@Override
public void render(GuiGraphics draw, int x, int y, float delta, int flags) {
if (MINECRAFT.level == null || this.stacks.isEmpty()) return;
int index = (int) Math.floor((MINECRAFT.level.getGameTime() % (this.stacks.size() * 20)) / 20.0);
this.stacks.get(index).render(draw, x, y, delta, flags);
EmiIngredient ingredient = this.get();
if (ingredient != null) ingredient.render(draw, x, y, delta, flags);
}

@Override
Expand All @@ -64,16 +79,49 @@ public CompoundTag getNbt() {
return new CompoundTag();
}

@Override
public float getChance() {
EmiIngredient ingredient = this.get();
if (ingredient != null) return ingredient.getChance();
return super.getChance();
}

@Override
public long getAmount() {
EmiIngredient ingredient = this.get();
if (ingredient != null) return ingredient.getAmount();
return super.getAmount();
}

@Override
public Object getKey() {
return "SelectOne$" + this.stacks.hashCode();
}

@Override
public boolean isEqual(EmiStack stack) {
for (EmiStack stack1 : this.stacks) {
if (stack1.isEqual(stack)) return true;
}
return super.isEqual(stack);
}

@Override
public ResourceLocation getId() {
return AnvilCraft.of("select_one_" + this.stacks.hashCode());
}

@Override
public List<ClientTooltipComponent> getTooltip() {
List<ClientTooltipComponent> list;
EmiIngredient ingredient = this.get();
if (ingredient != null) {
list = ingredient.getTooltip();
list.add(EmiTooltip.chance("produce", ingredient.getChance() / this.maxChance));
} else list = List.of();
return list;
}

@Override
public List<Component> getTooltipText() {
return List.of();
Expand Down

0 comments on commit 42a904c

Please sign in to comment.