Skip to content

Commit

Permalink
进度条,但是无法同步
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuqi154 committed Sep 22, 2024
1 parent 2b21588 commit e4e0254
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ public class SpringingMenu extends AbstractContainerMenu implements Supplier<Map
public final Level world;
public final Player entity;
public int x, y, z;
public int p;
public BlockPos pos;
private final ContainerLevelAccess access;
private final ContainerData containerData;
public final ContainerData containerData;
private final Map<Integer, Slot> customSlots = new HashMap<>();

@SuppressWarnings("unused")
Expand All @@ -50,14 +51,18 @@ public SpringingMenu(int containerId, Inventory inventory, ContainerLevelAccess
this.x = pos.getX();
this.y = pos.getY();
this.z = pos.getZ();
this.p = containerData.get(1);
this.pos = pos;
for (int ni = 0 ; ni < 3; ++ni) {
for (int nj = 0; nj < 3; ++nj) {
this.customSlots.put(ni * 3 + nj, this.addSlot(new SlotItemHandler(itemHandler, ni * 3 + nj, 97 + nj * 18, 24 + ni * 18)));
}
}

this.customSlots.put(9, this.addSlot(new SlotItemHandler(itemHandler, 9, 209, 42)));
this.customSlots.put(9, this.addSlot(new SlotItemHandler(itemHandler, 9, 209, 42){
@Override
public boolean mayPlace(ItemStack stack) {return false;}
}));
for (int si = 0; si < 3; ++si) {
for (int sj = 0; sj < 9; ++sj) {
this.addSlot(new Slot(inventory, sj + (si + 1) * 9, 84 + sj * 18, 100 + si * 18));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.ContainerData;
import net.minecraft.world.level.Level;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;
import net.neoforged.neoforge.network.PacketDistributor;
import org.hiedacamellia.whispergrove.WhisperGrove;
import org.hiedacamellia.whispergrove.content.client.menu.SpringingMenu;
import org.hiedacamellia.whispergrove.core.debug.Debug;
import org.hiedacamellia.whispergrove.core.entry.WGTickableBlockEntity;
import org.hiedacamellia.whispergrove.core.entry.gui.WGImageButton;
import org.hiedacamellia.whispergrove.core.network.PlayerMenuC2SPacket;

Expand All @@ -30,6 +33,7 @@ public class SpringingScreen extends AbstractContainerScreen<SpringingMenu> {
private final int x, y, z;
private final BlockPos pos;
private final Player entity;
public final ContainerData containerData;
private Button confirm;

public SpringingScreen(SpringingMenu container, Inventory inventory, Component title) {
Expand All @@ -38,6 +42,7 @@ public SpringingScreen(SpringingMenu container, Inventory inventory, Component t
this.x = container.x;
this.y = container.y;
this.z = container.z;
this.containerData = container.containerData;
this.pos = container.pos;
this.entity = container.entity;
this.imageWidth = 252;
Expand All @@ -47,11 +52,11 @@ public SpringingScreen(SpringingMenu container, Inventory inventory, Component t
@Override
public void init() {
super.init();

confirm = new WGImageButton(leftPos+220,topPos+23,14,14,
confirm = new WGImageButton(leftPos + 220, topPos + 23, 14, 14,
new WidgetSprites(WhisperGrove.prefix("textures/screens/springing_conform_button.png"),
WhisperGrove.prefix("textures/screens/springing_conform_button_pressed.png")), e->{
PacketDistributor.sendToServer(new PlayerMenuC2SPacket(pos,0));
WhisperGrove.prefix("textures/screens/springing_conform_button_pressed.png")), e -> {
PacketDistributor.sendToServer(new PlayerMenuC2SPacket(pos, 0));
containerData.set(1, 400);
});

this.addRenderableWidget(confirm);
Expand All @@ -62,6 +67,12 @@ public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partia
this.renderBackground(guiGraphics, mouseX, mouseY, partialTicks);
super.render(guiGraphics, mouseX, mouseY, partialTicks);
this.renderTooltip(guiGraphics, mouseX, mouseY);
//Debug.getLogger().debug("tickCount: " + p);
if (containerData.get(1) != 0) {
float width = 38 * (1 - ((float) containerData.get(1) / 400));
guiGraphics.blit(WhisperGrove.prefix("textures/screens/springing_arrow_progress.png"), this.leftPos + 163, this.topPos + 41, 0, 0, (int)width, 14, 38, 14);
}

}

@Override
Expand All @@ -85,6 +96,8 @@ public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
@Override
public void containerTick() {
super.containerTick();
if (containerData.get(1) > 0)
containerData.set(1, containerData.get(1) - 1);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
import net.minecraft.world.inventory.ContainerData;
import net.minecraft.world.inventory.ContainerLevelAccess;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.neoforge.items.ItemStackHandler;
import org.hiedacamellia.whispergrove.content.client.menu.SpringingMenu;
import org.hiedacamellia.whispergrove.core.debug.Debug;
import org.hiedacamellia.whispergrove.core.entry.WGTickableBlockEntity;
import org.hiedacamellia.whispergrove.core.recipe.generalprescriptprocess.GeneralPrescriptProcessApplier;
import org.hiedacamellia.whispergrove.registers.WGBlockEntity;
Expand Down Expand Up @@ -48,15 +50,24 @@ protected void saveAdditional(CompoundTag tag, HolderLookup.Provider registries)
}

@Override
public void assemble(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) {
public void assemble(BlockState state, Level level, BlockPos pos, RandomSource random) {
NonNullList<ItemStack> stacks = this.handler.getStacks();
Debug.getLogger().debug("Assembling");
Debug.getLogger().debug("stacks: "+stacks);
stacks.set(9,GeneralPrescriptProcessApplier.result(state, stacks.subList(0,8), level));
Debug.getLogger().debug("set: "+stacks.get(9));
stacks.forEach(itemStack -> {
int index = stacks.indexOf(itemStack);
if(index!=9)
stacks.set(index,ItemStack.EMPTY);
});
this.handler.setStacks(stacks);
}

@Override
public void tryAssemble(BlockState state, ServerLevel level) {
public void tryAssemble(BlockState state, Level level) {
int tick = GeneralPrescriptProcessApplier.getProcesstime(state, this.handler.getStacks().subList(0,8), level);
//Debug.getLogger().debug("tick: "+tick);
if(tick>0){
this.setTickCount(tick);
}
Expand Down Expand Up @@ -124,7 +135,7 @@ public void setStacks(NonNullList<ItemStack> items) {

}

private class Data implements ContainerData {
private static class Data implements ContainerData {

@Override
public int get(int index) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
Expand All @@ -18,6 +19,8 @@ public void tick(BlockState state, ServerLevel level, BlockPos pos, RandomSource
BlockEntity blockEntity = level.getBlockEntity(pos);
if(blockEntity instanceof WGTickableBlockEntity tickableBlockEntity){
tickableBlockEntity.tick(state, level, pos, random);
if(tickableBlockEntity.getTickCount()>0)
level.scheduleTick(pos, state.getBlock(), 1);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import net.minecraft.core.BlockPos;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.RandomSource;
import net.minecraft.world.WorldlyContainer;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.entity.RandomizableContainerBlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import org.hiedacamellia.whispergrove.core.debug.Debug;

public abstract class WGTickableBlockEntity extends RandomizableContainerBlockEntity implements WorldlyContainer {

Expand All @@ -31,21 +32,28 @@ protected void saveAdditional(CompoundTag tag, HolderLookup.Provider registries)
tag.putInt("tickCount", tickCount);
}

public void tick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random){
public void tick(BlockState state,Level level, BlockPos pos, RandomSource random){
if(tickCount==0)
return;
if(tickCount==1){
Debug.getLogger().debug("Assemble");
assemble(state, level, pos, random);
}
if(tickCount>=1)
if(tickCount>=1) {
//Debug.getLogger().debug("tick: "+tickCount);
tickCount--;
}
}

public void setTickCount(int tickCount) {
this.tickCount = tickCount;
}

public abstract void assemble(BlockState state, ServerLevel level, BlockPos pos, RandomSource random);
public int getTickCount() {
return tickCount;
}

public abstract void assemble(BlockState state,Level level, BlockPos pos, RandomSource random);

public abstract void tryAssemble(BlockState state, ServerLevel level);
public abstract void tryAssemble(BlockState state, Level level);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,18 @@ public Type<? extends CustomPacketPayload> type() {
return TYPE;
}

@SuppressWarnings("unchecked")
public static void handleData(PlayerMenuC2SPacket packet, IPayloadContext context) {
context.enqueueWork(() -> {
Level level = context.player().level();
if(level instanceof ServerLevel serverLevel){
if (packet.rtype() == 0) {
BlockState blockState = serverLevel.getBlockState(packet.pos());
BlockEntity blockEntity = serverLevel.getBlockEntity(packet.pos());
if(blockEntity instanceof WGTickableBlockEntity wgTickableBlockEntity){
wgTickableBlockEntity.tryAssemble(blockState, serverLevel);
}
if (packet.rtype() == 0) {
BlockState blockState = level.getBlockState(packet.pos());
BlockEntity blockEntity = level.getBlockEntity(packet.pos());
if (blockEntity instanceof WGTickableBlockEntity wgTickableBlockEntity) {
wgTickableBlockEntity.tryAssemble(blockState, level);
level.scheduleTick(packet.pos(), blockState.getBlock(), 1);
}

}

});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import net.minecraft.world.item.crafting.RecipeManager;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import org.hiedacamellia.whispergrove.core.debug.Debug;
import org.hiedacamellia.whispergrove.registers.WGItem;
import org.hiedacamellia.whispergrove.registers.WGRicipe;

import java.util.List;
Expand Down Expand Up @@ -42,7 +44,7 @@ public static ItemStack result(BlockState blockState, List<ItemStack> itemStacks
return optional
.map(RecipeHolder::value)
.map(e -> e.assemble(input, level.registryAccess()))
.orElse(ItemStack.EMPTY);
.orElse(WGItem.SOUP.toStack());

}

Expand All @@ -59,11 +61,10 @@ public static int getProcesstime(BlockState blockState, List<ItemStack> itemStac
}

if(isAllEmpty){
Debug.getLogger().debug("All empty");
return 0;
}



GeneralPrescriptProcessInput input = new GeneralPrescriptProcessInput(blockState, itemStacks);

var optional = recipes.getRecipeFor(
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e4e0254

Please sign in to comment.