Skip to content

Commit a7b566c

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents a5db689 + dc7a1bc commit a7b566c

39 files changed

Lines changed: 904 additions & 364 deletions

src/main/java/top/ctnstudio/futurefood/api/adapter/UnlimitedLinkStorage.java

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@
66
import net.minecraft.core.BlockPos;
77
import net.minecraft.core.HolderLookup.Provider;
88
import net.minecraft.nbt.CompoundTag;
9+
import net.minecraft.nbt.IntArrayTag;
910
import net.minecraft.nbt.ListTag;
1011
import net.minecraft.nbt.Tag;
1112
import net.minecraft.world.level.Level;
1213
import net.minecraft.world.level.block.state.BlockState;
1314
import net.neoforged.neoforge.energy.IEnergyStorage;
1415
import top.ctnstudio.futurefood.api.capability.IUnlimitedLinkStorage;
1516
import top.ctnstudio.futurefood.core.FutureFood;
17+
import top.ctnstudio.futurefood.util.ModUtil;
1618

1719
import javax.annotation.CheckForNull;
1820
import javax.annotation.Nonnull;
21+
import java.util.Collection;
1922
import java.util.Objects;
2023
import java.util.Queue;
2124
import java.util.Set;
@@ -77,7 +80,7 @@ public final boolean removeLink(BlockPos pos) {
7780

7881
@Override
7982
public boolean linkBlock(Level level, BlockPos pos) {
80-
if (level == null || level.isClientSide) {
83+
if (level == null) {
8184
linkFailure(pos);
8285
return false;
8386
}
@@ -106,20 +109,29 @@ public boolean isContainLink(BlockPos pos) {
106109
public CompoundTag serializeNBT(Provider provider) {
107110
CompoundTag nbt = new CompoundTag();
108111
ListTag tags = new ListTag();
109-
linkSet.forEach(
110-
(pos) -> {
111-
BlockState state = getLinkedBlock(pos);
112-
if (state == null) {
113-
return;
114-
}
115-
CompoundTag tag = new CompoundTag();
116-
tag.putIntArray("pos", new int[]{pos.getX(), pos.getY(), pos.getZ()});
112+
linkSet.forEach(pos -> {
113+
BlockState state = getLinkBlock(pos);
114+
if (state == null) {
115+
return;
117116
}
118-
);
117+
tags.add(new IntArrayTag(new int[]{pos.getX(), pos.getY(), pos.getZ()}));
118+
});
119119
nbt.put("linkList", tags);
120120
return nbt;
121121
}
122122

123+
@Override
124+
public void deserializeNBT(Provider provider, CompoundTag nbt) {
125+
Tag linkList = nbt.get("linkList");
126+
if (!(linkList instanceof ListTag tags) || tags.isEmpty()) {
127+
return;
128+
}
129+
linkSet.clear();
130+
linkSet.addAll(tags.stream().map(tag ->
131+
tag instanceof IntArrayTag intTags ? ModUtil.getBlockPos(intTags.getAsIntArray()) : null)
132+
.filter(Objects::nonNull).toList());
133+
}
134+
123135
/**
124136
* 获取一个链接的方块
125137
*
@@ -128,7 +140,7 @@ public CompoundTag serializeNBT(Provider provider) {
128140
*/
129141
@CheckForNull
130142
@Override
131-
public BlockState getLinkedBlock(BlockPos pos) {
143+
public BlockState getLinkBlock(BlockPos pos) {
132144
if (Objects.isNull(this.getLevel())) {
133145
return null;
134146
}
@@ -137,30 +149,6 @@ public BlockState getLinkedBlock(BlockPos pos) {
137149
return !blockState.isEmpty() ? blockState : null;
138150
}
139151

140-
@Override
141-
public void deserializeNBT(Provider provider, CompoundTag nbt) {
142-
Tag linkList = nbt.get("linkList");
143-
if (!(linkList instanceof ListTag tags)) {
144-
linkSet.clear();
145-
return;
146-
}
147-
if (tags.isEmpty()) {
148-
return;
149-
}
150-
tags.forEach(tag -> {
151-
if (!(tag instanceof CompoundTag compoundTag)) {
152-
return;
153-
}
154-
int[] posNbtArray = compoundTag.getIntArray("pos");
155-
BlockPos pos = new BlockPos(posNbtArray[0], posNbtArray[1], posNbtArray[2]);
156-
BlockState state = getLinkedBlock(pos);
157-
if (state == null) {
158-
return;
159-
}
160-
linkSet.add(pos);
161-
});
162-
}
163-
164152
@Override
165153
public final void addLinkCache(BlockPos pos) {
166154
this.cacheData.add(pos);
@@ -170,4 +158,23 @@ public final void addLinkCache(BlockPos pos) {
170158
public final Set<BlockPos> getLinkSet() {
171159
return ImmutableSet.copyOf(linkSet);
172160
}
161+
162+
@Override
163+
public void clear() {
164+
linkSet.clear();
165+
}
166+
167+
@Override
168+
public boolean setLink(Level level, BlockPos oldPos, BlockPos newPos) {
169+
if (level == null || isContainLink(newPos) || !removeLink(oldPos)) {
170+
return false;
171+
}
172+
return linkBlock(level, newPos);
173+
}
174+
175+
@Override
176+
public void setLinkList(Collection<BlockPos> linkList) {
177+
linkSet.clear();
178+
linkSet.addAll(linkList);
179+
}
173180
}

src/main/java/top/ctnstudio/futurefood/api/capability/IUnlimitedLinkStorage.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import javax.annotation.Nonnull;
1616
import javax.annotation.Nullable;
17+
import java.util.Collection;
1718
import java.util.Objects;
1819

1920
/**
@@ -122,7 +123,7 @@ static LinkedListMultimap<Direction, IEnergyStorage> getEnergyStorageAllCapabili
122123
* @param pos 要获取的链接方块位置
123124
* @return 链接的方块
124125
*/
125-
BlockState getLinkedBlock(BlockPos pos);
126+
BlockState getLinkBlock(BlockPos pos);
126127

127128
/**
128129
* 加载链接列表
@@ -134,6 +135,28 @@ static LinkedListMultimap<Direction, IEnergyStorage> getEnergyStorageAllCapabili
134135
*/
135136
void addLinkCache(BlockPos pos);
136137

138+
/**
139+
* 清空链接列表
140+
*/
141+
void clear();
142+
143+
/**
144+
* 修改链接
145+
*
146+
* @param level
147+
* @param oldPos 旧链接方块位置
148+
* @param newPos 新链接方块位置
149+
* @return 是否成功
150+
*/
151+
boolean setLink(Level level, BlockPos oldPos, BlockPos newPos);
152+
153+
/**
154+
* 设置链接列表
155+
*
156+
* @param linkList 链接列表
157+
*/
158+
void setLinkList(Collection<BlockPos> linkList);
159+
137160
/**
138161
* 获取世界
139162
*/

src/main/java/top/ctnstudio/futurefood/client/core/ModRenderType.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ public final class ModRenderType {
4242
1536, false, false, RenderType.CompositeState.builder()
4343
.setShaderState(RENDERTYPE_TEXT_SHADER)
4444
.setTextureState(new TextureStateShard(texture, false, false))
45-
.setTransparencyState(TRANSLUCENT_TRANSPARENCY)
46-
.setLightmapState(LIGHTMAP)
47-
.setOverlayState(OVERLAY)
45+
.setTransparencyState(TRANSLUCENT_TRANSPARENCY)
46+
.setLightmapState(LIGHTMAP)
47+
.setOverlayState(OVERLAY)
4848
.createCompositeState(false)));
4949

5050
public static RenderType getHighlighted(DepthTestStateShard depth) {

src/main/java/top/ctnstudio/futurefood/client/gui/screen/EnergyScreen.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
public class EnergyScreen<T extends BasicEnergyMenu> extends AbstractContainerScreen<T> {
1414
public static final ResourceLocation BG = FutureFood.modRL("textures/gui/container/energy.png");
1515
private final ResourceLocation textureBg;
16-
protected EnergyBar listener;
16+
protected EnergyBar energyBar;
1717

1818
public EnergyScreen(T menu, Inventory playerInventory, Component title) {
1919
this(menu, playerInventory, title, BG);
@@ -29,14 +29,14 @@ public EnergyScreen(T menu, Inventory playerInventory, Component title, Resource
2929
@Override
3030
protected void init() {
3131
super.init();
32-
this.listener = new EnergyBar(leftPos + 1, topPos + 14, menu.getEnergy(), menu.getMaxEnergy());
33-
addRenderableWidget(listener);
32+
this.energyBar = new EnergyBar(leftPos + 2, topPos + 15, menu.getEnergy(), menu.getMaxEnergy());
33+
addRenderableWidget(energyBar);
3434
}
3535

3636
@Override
3737
public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
38-
if (listener != null) {
39-
listener.setEnergy(menu.getEnergy(), menu.getMaxEnergy());
38+
if (energyBar != null) {
39+
energyBar.setEnergy(menu.getEnergy(), menu.getMaxEnergy());
4040
}
4141
super.render(guiGraphics, mouseX, mouseY, partialTick);
4242
renderTooltip(guiGraphics, mouseX, mouseY);
@@ -53,8 +53,8 @@ protected void renderBg(GuiGraphics guiGraphics, float partialTick, int mouseX,
5353
@Override
5454
protected void containerTick() {
5555
super.containerTick();
56-
if (listener != null) {
57-
listener.setEnergy(menu.getEnergy(), menu.getMaxEnergy());
56+
if (energyBar != null) {
57+
energyBar.setEnergy(menu.getEnergy(), menu.getMaxEnergy());
5858
}
5959
}
6060

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,100 @@
11
package top.ctnstudio.futurefood.client.gui.screen;
22

3+
import net.minecraft.client.Minecraft;
4+
import net.minecraft.client.gui.GuiGraphics;
5+
import net.minecraft.client.gui.components.ImageWidget;
36
import net.minecraft.network.chat.Component;
7+
import net.minecraft.network.chat.MutableComponent;
48
import net.minecraft.resources.ResourceLocation;
59
import net.minecraft.world.entity.player.Inventory;
610
import top.ctnstudio.futurefood.common.menu.ParticleColliderMenu;
711
import top.ctnstudio.futurefood.core.FutureFood;
812

13+
import static top.ctnstudio.futurefood.util.TextUtil.formatGameTime;
14+
915
public class ParticleColliderScreen extends EnergyScreen<ParticleColliderMenu> {
1016
public static final ResourceLocation BG = FutureFood.modRL("textures/gui/container/particle_collider.png");
11-
public static final ResourceLocation PROGRESSO = FutureFood.modRL("container/particle_collider/progresso.png");
17+
protected ProgressBar progressBar;
1218

1319
public ParticleColliderScreen(ParticleColliderMenu menu, Inventory playerInventory, Component title) {
1420
super(menu, playerInventory, title, BG);
1521
}
1622

17-
// TODO 完成粒子加速器界面
23+
@Override
24+
protected void init() {
25+
super.init();
26+
this.progressBar = new ProgressBar(leftPos + 65, topPos + 67, menu.getRemainingTick(), menu.getMaxWorkTick());
27+
addRenderableWidget(progressBar);
28+
}
29+
30+
@Override
31+
public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
32+
if (progressBar != null) {
33+
progressBar.setTick(menu.getEnergy(), menu.getMaxEnergy());
34+
}
35+
super.render(guiGraphics, mouseX, mouseY, partialTick);
36+
}
37+
38+
@Override
39+
protected void containerTick() {
40+
if (progressBar != null) {
41+
progressBar.setTick(menu.getEnergy(), menu.getMaxEnergy());
42+
}
43+
super.containerTick();
44+
}
45+
46+
public static class ProgressBar extends ImageWidget.Sprite {
47+
public static final String TOOLTIP = FutureFood.ID + ".gui.progress.tooltip";
48+
public static final ResourceLocation TEXTURE = FutureFood.modRL("container/particle_collider/progresso.png");
49+
private int remainingTick;
50+
private int maxWorkTick;
51+
52+
public ProgressBar(int x, int y, int remainingTick, int maxWorkTick) {
53+
super(x, y, 46, 12, TEXTURE);
54+
this.remainingTick = remainingTick;
55+
this.maxWorkTick = maxWorkTick;
56+
}
57+
58+
public void setMaxWorkTick(int maxWorkTick) {
59+
this.maxWorkTick = maxWorkTick;
60+
}
61+
62+
public void setTick(int energy, int maxTick) {
63+
this.remainingTick = energy;
64+
this.maxWorkTick = maxTick;
65+
}
66+
67+
public int getRemainingTick() {
68+
return this.remainingTick;
69+
}
70+
71+
public void setRemainingTick(int remainingTick) {
72+
this.remainingTick = remainingTick;
73+
}
74+
75+
@Override
76+
public void renderWidget(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
77+
if (maxWorkTick > 0 && remainingTick > 0 & isHovered()) {
78+
Minecraft minecraft = Minecraft.getInstance();
79+
MutableComponent translatable = Component.translatable(TOOLTIP, formatGameTime(remainingTick));
80+
guiGraphics.renderTooltip(minecraft.font, translatable, mouseX, mouseY);
81+
}
82+
if (this.maxWorkTick > 0) {
83+
int spriteWidth = this.getWidth();
84+
int spriteHeight = this.getHeight();
85+
86+
int remainingTick = Math.max(0, this.remainingTick);
87+
int uWidth = (int) ((Math.min(remainingTick, this.maxWorkTick) / (float) this.maxWorkTick) * spriteWidth);
88+
int xPosition = spriteWidth - uWidth;
89+
int x = this.getX() + xPosition;
90+
int y = this.getY();
91+
92+
guiGraphics.blitSprite(this.sprite, spriteWidth, spriteHeight, xPosition, 0, x, y, uWidth, spriteHeight);
93+
}
94+
}
95+
96+
public ResourceLocation getTexture() {
97+
return this.sprite;
98+
}
99+
}
18100
}

src/main/java/top/ctnstudio/futurefood/client/gui/widget/energy/EnergyBar.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class EnergyBar extends ImageWidget.Sprite {
1717
private int maxEnergy;
1818

1919
public EnergyBar(int x, int y, int energy, int maxEnergy) {
20-
this(x, y, 12, 39, TEXTURE, energy, maxEnergy);
20+
this(x, y, 10, 37, TEXTURE, energy, maxEnergy);
2121
}
2222

2323
public EnergyBar(int x, int y, int width, int height, ResourceLocation sprite, int energy, int maxEnergy) {
@@ -53,14 +53,16 @@ public void renderWidget(GuiGraphics guiGraphics, int mouseX, int mouseY, float
5353
guiGraphics.renderTooltip(minecraft.font, translatable, mouseX, mouseY);
5454
}
5555
if (this.maxEnergy > 0) {
56+
int spriteHeight = this.getHeight();
57+
int spriteWidth = this.getWidth();
58+
5659
int energy = Math.max(0, this.energy);
57-
int height = (int) ((Math.min(energy, this.maxEnergy) / (float) this.maxEnergy) * this.getHeight());
58-
int yPosition = this.getHeight() - height;
59-
guiGraphics.blitSprite(this.sprite,
60-
this.getWidth(), this.getHeight(),
61-
0, yPosition,
62-
this.getX(), this.getY() + yPosition,
63-
this.getWidth(), height);
60+
int uHeight = (int) ((Math.min(energy, this.maxEnergy) / (float) this.maxEnergy) * spriteHeight);
61+
int yPosition = spriteHeight - uHeight;
62+
int x = this.getX();
63+
int y = this.getY() + yPosition;
64+
65+
guiGraphics.blitSprite(this.sprite, spriteWidth, spriteHeight, 0, yPosition, x, y, spriteWidth, uHeight);
6466
}
6567
}
6668

src/main/java/top/ctnstudio/futurefood/client/gui/widget/energy/EnergySlot.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
public abstract class EnergySlot extends SlotItemHandler {
1414
public static final ResourceLocation ENERGY_ICON = FutureFood.modRL("container/energy_bar/energy_icon");
15+
1516
public EnergySlot(IItemHandler itemHandler, int slot, int x, int y) {
1617
super(itemHandler, slot, x, y);
1718
}

0 commit comments

Comments
 (0)