Skip to content

Commit

Permalink
feat: 讓格子能夠根據魔杖的容量變化
Browse files Browse the repository at this point in the history
  • Loading branch information
northgreen committed Apr 22, 2024
1 parent efba8f1 commit 35c2fb5
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@
- name: upload snapshot
uses: actions/upload-artifact@v3
with:
name: snapshot-1.19.2
name: snapshot-1.19.2-${{github.sha}}
path: build/devlibs/*
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// 1.19.2 2024-04-21T20:00:12.9330783 Language
// 1.19.2 2024-04-22T20:37:07.5325428 Language
186c3f9c9fe92b38b1958905153cee3079c2fa03 assets\the_origin_of_magic\lang\en_us.json
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// 1.19.2 2024-04-21T20:00:12.9355771 Block Loot Tables
// 1.19.2 2024-04-22T20:37:07.5345493 Block Loot Tables
fe57f13449a11436bbede8c37e01bd0d1cd87191 data\the_origin_of_magic\loot_tables\blocks\magic_workbench.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 1.19.2 2024-04-21T20:00:12.9375772 Models
// 1.19.2 2024-04-22T20:37:07.5366576 Models
02d3ff4716d16f7c5a4dd4432d1b98f744a13f02 assets\the_origin_of_magic\models\item\blood_essence.json
781092579f171cf0db8317aa6267ad8e5cfbbd1f assets\the_origin_of_magic\models\item\fire_magic_element.json
f54d22f024ca8dbc5f81e759c90de28d15c18b17 assets\the_origin_of_magic\models\item\kind_magic_element.json
Expand Down
Original file line number Diff line number Diff line change
@@ -1,63 +1,66 @@
package com.ictye.the_origin_of_magic.infrastructure.GUI.MagicWorkbench;

import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.gui.Element;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.client.render.GameRenderer;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.slot.Slot;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

import java.util.Optional;

public class MagicWorkbenchScreen extends HandledScreen<ScreenHandler> {

private static Text STAFFNAME = Text.empty();
private static final Identifier TEXTURE = new Identifier("the_origin_of_magic","textures/gui/magic_workbench.png");

public MagicWorkbenchScreen(ScreenHandler handler, PlayerInventory inventory, Text title) {
super(handler, inventory, title);
backgroundHeight = 204;
backgroundWidth = 176;
this.playerInventoryTitleY = this.backgroundHeight - 94;
}

public static void setSTAFFNAME(Text STAFFNAME) {
MagicWorkbenchScreen.STAFFNAME = STAFFNAME;
}



@Override
protected void drawBackground(MatrixStack matrices, float delta, int mouseX, int mouseY) {
RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.setShaderColor(1.0F,1.0F,1.0F,1.0F);
RenderSystem.setShaderTexture(0,TEXTURE);
int x = (width - backgroundWidth) / 2;
int y = (height - backgroundHeight) / 2;
drawTexture(matrices,x,y - 2 ,0,0,backgroundWidth,backgroundHeight);
}
drawTexture(matrices,this.x, this.y,354,0,backgroundWidth,backgroundHeight,550,550);
for (int k = 0; k < this.handler.slots.size(); ++k){
if (!this.handler.slots.get(k).isEnabled()){
Slot slot = this.handler.slots.get(k);
int slotX = this.x + slot.x - 1;
int slotY = this.y + slot.y - 1;
drawTexture(matrices,slotX,slotY,0,170,18,18,550,550);
}
}

@Override
public Optional<Element> hoveredElement(double mouseX, double mouseY) {
return super.hoveredElement(mouseX, mouseY);
}

@Override
public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
renderBackground(matrices);
super.render(matrices, mouseX, mouseY, delta);
drawMouseoverTooltip(matrices, mouseX, mouseY);
textRenderer.draw(matrices,Text.translatable("text.the_origin_of_magic.magic_work_station.staff_state"),x + 45,y - 2 + 20, 0x3F3F3F);
textRenderer. draw(matrices,STAFFNAME,x + 45,y - 2 + 29, 0x3F3F3F);
}

@Override
protected void handledScreenTick() {
super.handledScreenTick();
textRenderer.draw(matrices,Text.translatable("text.the_origin_of_magic.magic_work_station.staff_state"),x + 51,y + 18, 0x3F3F3F);
textRenderer. draw(matrices,STAFFNAME,x + 51 + 2,y + 18 + 9, 0x3F3F3F);
}

@Override
protected void init() {
super.init();
// 将标题居中
titleX = (backgroundWidth - textRenderer.getWidth(title)) / 2;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
import net.minecraft.screen.slot.Slot;
import net.minecraft.text.Text;
import org.jetbrains.annotations.Nullable;

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

public class MagicWorkbenchScreenHandler extends ScreenHandler {

private final MagicInventory magicSlotInventory = new MagicInventory(9); // 魔法物品欄
private final MagicInventory magicSlotInventory = new MagicInventory(27); // 魔法物品欄
private final SimpleInventory staffInventory = new SimpleInventory(1); // 魔杖格子
private final Slot staffSlot; // 魔杖格子
private final List<magicSlot> magicSlots = new ArrayList<>();
Expand All @@ -39,7 +40,7 @@ public MagicWorkbenchScreenHandler(int syncId, PlayerInventory playerInventory,
this.context = context;
this.playerInvnetory = playerInventory;
// 魔杖格子
Slot StaffSlot = new Slot(staffInventory, 0, 19, 21){
Slot StaffSlot = new Slot(staffInventory, 0, 19, 23){
////////////////////////////////////////////////
// 魔杖格子 //
///////////////////////////////////////////////
Expand Down Expand Up @@ -93,13 +94,13 @@ public void onTakeItem(PlayerEntity player, ItemStack stack) {
staffSlot = addSlot(StaffSlot);

// 創建魔法格子
int slotsCount = 9;
int slotsCount = 27;
magicSlotInventory.onOpen(playerInventory.player);
int slotIndex = 0;
for (int m = 0; m < slotsCount / 9 + 1; m++) {
for (int n = 0; n < 9; n++) {
if (slotIndex < slotsCount) {
magicSlots.add((magicSlot) addSlot(new magicSlot(magicSlotInventory, slotIndex, 8 + n * 18, 54 + m * 18)));
magicSlots.add((magicSlot) addSlot(new magicSlot(magicSlotInventory, slotIndex, 8 + n * 18, 55 + m * 18)));
slotIndex++;
} else {
break;
Expand All @@ -110,13 +111,13 @@ public void onTakeItem(PlayerEntity player, ItemStack stack) {
// 主物品栏的格子
for (int m = 0; m < 3; ++m) {
for (int n = 0; n < 9; ++n) {
this.addSlot(new Slot(playerInventory, n + m * 9 + 9, 8 + n * 18, 85 + m * 18));
this.addSlot(new Slot(playerInventory, n + m * 9 + 9, 8 + n * 18, 122 + m * 18));
}
}

// 快捷栏的格子
for (int m = 0; m < 9; ++m) {
this.addSlot(new Slot(playerInventory, m, 8 + m * 18, 143));
this.addSlot(new Slot(playerInventory, m, 8 + m * 18, 180));
}
}

Expand All @@ -128,7 +129,15 @@ public boolean canUse(PlayerEntity player) {
@Override
public void close(PlayerEntity player){
if(staffInventory.getStack(0).getItem() instanceof StdStaff staff){
setStaffNBT(new MagicInventory(staff.getInventory().size()).setStackFromInventory(magicSlotInventory));
// 同步NBT
MagicInventory inventory1 = staff.getInventory();
staff.getInventory().clear();
for(int i = 0; i < magicSlotInventory.size(); i++){
if (i < staff.getSize()){
inventory1.setStack(i,magicSlotInventory.getStack(i));
}
}
staffInventory.getStack(0).setSubNbt("items",new MagicInventory(inventory1.size()).setStackFromInventory(inventory1).toNbtList());
}
magicSlotInventory.clear();
this.context.run((world, pos) -> this.dropInventory(player, this.staffInventory));
Expand Down Expand Up @@ -192,5 +201,13 @@ public boolean canInsert(ItemStack stack) {
}
return false;
}

@Override
public boolean isEnabled() {
if (getSlot(0).getStack().getItem() instanceof StdStaff staff){
return this.getIndex() < staff.getSize();
}
return false;
}
}
}
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 35c2fb5

Please sign in to comment.