添加screen和menu

This commit is contained in:
17146 2024-12-06 23:32:36 +08:00
parent 80bdd7c6ac
commit f910e509bc
6 changed files with 221 additions and 2 deletions

View file

@ -1,5 +1,6 @@
package com.atsuishio.superbwarfare.block.entity; package com.atsuishio.superbwarfare.block.entity;
import com.atsuishio.superbwarfare.block.menu.ChargingStationMenu;
import com.atsuishio.superbwarfare.entity.IChargeEntity; import com.atsuishio.superbwarfare.entity.IChargeEntity;
import com.atsuishio.superbwarfare.init.ModBlockEntities; import com.atsuishio.superbwarfare.init.ModBlockEntities;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
@ -16,6 +17,7 @@ import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.ContainerData;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntity;
@ -41,7 +43,7 @@ public class ChargingStationBlockEntity extends BlockEntity implements WorldlyCo
private static final int[] SLOTS_FOR_DOWN = new int[]{0}; private static final int[] SLOTS_FOR_DOWN = new int[]{0};
public static final int MAX_ENERGY = 4000000; public static final int MAX_ENERGY = 4000000;
public static final int MAX_DATA_COUNT = 2; public static final int MAX_DATA_COUNT = 3;
public static final int DEFAULT_FUEL_TIME = 1600; public static final int DEFAULT_FUEL_TIME = 1600;
public static final int CHARGE_SPEED = 128; public static final int CHARGE_SPEED = 128;
public static final int CHARGE_RADIUS = 4; public static final int CHARGE_RADIUS = 4;
@ -53,6 +55,36 @@ public class ChargingStationBlockEntity extends BlockEntity implements WorldlyCo
public int fuelTick = 0; public int fuelTick = 0;
public int maxFuelTick = DEFAULT_FUEL_TIME; public int maxFuelTick = DEFAULT_FUEL_TIME;
public int energy;
protected final ContainerData dataAccess = new ContainerData() {
public int get(int pIndex) {
return switch (pIndex) {
case 0 -> ChargingStationBlockEntity.this.fuelTick;
case 1 -> ChargingStationBlockEntity.this.maxFuelTick;
case 2 -> ChargingStationBlockEntity.this.energy;
default -> 0;
};
}
public void set(int pIndex, int pValue) {
switch (pIndex) {
case 0:
ChargingStationBlockEntity.this.fuelTick = pValue;
break;
case 1:
ChargingStationBlockEntity.this.maxFuelTick = pValue;
break;
case 2:
ChargingStationBlockEntity.this.energy = pValue;
break;
}
}
public int getCount() {
return MAX_DATA_COUNT;
}
};
public ChargingStationBlockEntity(BlockPos pos, BlockState state) { public ChargingStationBlockEntity(BlockPos pos, BlockState state) {
super(ModBlockEntities.CHARGING_STATION.get(), pos, state); super(ModBlockEntities.CHARGING_STATION.get(), pos, state);
@ -109,6 +141,7 @@ public class ChargingStationBlockEntity extends BlockEntity implements WorldlyCo
blockEntity.energyHandler.ifPresent(handler -> { blockEntity.energyHandler.ifPresent(handler -> {
int energy = handler.getEnergyStored(); int energy = handler.getEnergyStored();
blockEntity.energy = energy;
if (energy > 0) { if (energy > 0) {
List<Entity> entities = pLevel.getEntitiesOfClass(Entity.class, new AABB(pPos).inflate(CHARGE_RADIUS)); List<Entity> entities = pLevel.getEntitiesOfClass(Entity.class, new AABB(pPos).inflate(CHARGE_RADIUS));
entities.forEach(entity -> { entities.forEach(entity -> {
@ -231,7 +264,7 @@ public class ChargingStationBlockEntity extends BlockEntity implements WorldlyCo
@Nullable @Nullable
@Override @Override
public AbstractContainerMenu createMenu(int pContainerId, Inventory pPlayerInventory, Player pPlayer) { public AbstractContainerMenu createMenu(int pContainerId, Inventory pPlayerInventory, Player pPlayer) {
return null; return new ChargingStationMenu(pContainerId, pPlayerInventory, this, this.dataAccess);
} }
@Override @Override

View file

@ -0,0 +1,129 @@
package com.atsuishio.superbwarfare.block.menu;
import com.atsuishio.superbwarfare.block.entity.ChargingStationBlockEntity;
import com.atsuishio.superbwarfare.init.ModMenuTypes;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.ContainerData;
import net.minecraft.world.inventory.SimpleContainerData;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.capabilities.ForgeCapabilities;
public class ChargingStationMenu extends AbstractContainerMenu {
private final Container container;
private final ContainerData containerData;
public static final int X_OFFSET = 0;
public static final int Y_OFFSET = 11;
public ChargingStationMenu(int id, Inventory inventory) {
this(id, inventory, new SimpleContainer(2), new SimpleContainerData(ChargingStationBlockEntity.MAX_DATA_COUNT));
}
public ChargingStationMenu(int id, Inventory inventory, Container container, ContainerData containerData) {
super(ModMenuTypes.CHARGING_STATION_MENU.get(), id);
checkContainerSize(container, 2);
checkContainerDataCount(containerData, ChargingStationBlockEntity.MAX_DATA_COUNT);
this.container = container;
this.containerData = containerData;
this.addSlot(new Slot(container, 0, 51, 43));
this.addSlot(new ChargingStationMenu.ChargingSlot(container, 1, 110, 43));
this.addDataSlots(containerData);
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 9; ++j) {
this.addSlot(new Slot(inventory, j + i * 9 + 9, 8 + j * 18 + X_OFFSET, 84 + i * 18 + Y_OFFSET));
}
}
for (int k = 0; k < 9; ++k) {
this.addSlot(new Slot(inventory, k, 8 + k * 18 + X_OFFSET, 142 + Y_OFFSET));
}
}
@Override
public ItemStack quickMoveStack(Player pPlayer, int pIndex) {
ItemStack itemstack = ItemStack.EMPTY;
Slot slot = this.slots.get(pIndex);
if (slot.hasItem()) {
ItemStack itemstack1 = slot.getItem();
itemstack = itemstack1.copy();
if (pIndex == 1) {
if (!this.moveItemStackTo(itemstack1, 2, 38, true)) {
return ItemStack.EMPTY;
}
} else if (pIndex != 0) {
if (itemstack1.getCapability(ForgeCapabilities.ENERGY).isPresent()) {
if (!this.moveItemStackTo(itemstack1, 1, 2, true)) {
return ItemStack.EMPTY;
}
} else if (itemstack1.getBurnTime(null) > 0 || itemstack1.getFoodProperties(null) != null) {
if (!this.moveItemStackTo(itemstack1, 0, 1, false)) {
return ItemStack.EMPTY;
}
} else if (pIndex >= 2 && pIndex < 29) {
if (!this.moveItemStackTo(itemstack1, 29, 38, false)) {
return ItemStack.EMPTY;
}
} else if (pIndex >= 29 && pIndex < 38 && !this.moveItemStackTo(itemstack1, 2, 29, false)) {
return ItemStack.EMPTY;
}
} else if (!this.moveItemStackTo(itemstack1, 2, 38, false)) {
return ItemStack.EMPTY;
}
if (itemstack1.isEmpty()) {
slot.setByPlayer(ItemStack.EMPTY);
} else {
slot.setChanged();
}
if (itemstack1.getCount() == itemstack.getCount()) {
return ItemStack.EMPTY;
}
slot.onTake(pPlayer, itemstack1);
}
return itemstack;
}
@Override
public boolean stillValid(Player pPlayer) {
return this.container.stillValid(pPlayer);
}
public int getFuelTick() {
return this.containerData.get(0);
}
public int getMaxFuelTick() {
return this.containerData.get(1);
}
public int getEnergy() {
return this.containerData.get(2);
}
class ChargingSlot extends Slot {
public ChargingSlot(Container pContainer, int pSlot, int pX, int pY) {
super(pContainer, pSlot, pX, pY);
}
@Override
public boolean mayPlace(ItemStack pStack) {
return super.mayPlace(pStack);
}
}
}

View file

@ -0,0 +1,49 @@
package com.atsuishio.superbwarfare.client.screens;
import com.atsuishio.superbwarfare.ModUtils;
import com.atsuishio.superbwarfare.block.menu.ChargingStationMenu;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
@OnlyIn(Dist.CLIENT)
public class ChargingStationScreen extends AbstractContainerScreen<ChargingStationMenu> {
private static final ResourceLocation TEXTURE = ModUtils.loc("textures/gui/charging_station.png");
public ChargingStationScreen(ChargingStationMenu pMenu, Inventory pPlayerInventory, Component pTitle) {
super(pMenu, pPlayerInventory, pTitle);
imageWidth = 176;
imageHeight = 166;
}
@Override
protected void renderBg(GuiGraphics pGuiGraphics, float pPartialTick, int pMouseX, int pMouseY) {
int i = (this.width - this.imageWidth) / 2;
int j = (this.height - this.imageHeight) / 2;
pGuiGraphics.blit(TEXTURE, i, j, 0, 0, this.imageWidth, this.imageHeight);
}
@Override
public void render(GuiGraphics pGuiGraphics, int pMouseX, int pMouseY, float pPartialTick) {
this.renderBackground(pGuiGraphics);
super.render(pGuiGraphics, pMouseX, pMouseY, pPartialTick);
this.renderTooltip(pGuiGraphics, pMouseX, pMouseY);
}
@Override
protected void init() {
super.init();
this.titleLabelX = 8;
this.titleLabelY = 5;
this.inventoryLabelX = 8;
this.inventoryLabelY = 74;
}
}

View file

@ -1,6 +1,7 @@
package com.atsuishio.superbwarfare.init; package com.atsuishio.superbwarfare.init;
import com.atsuishio.superbwarfare.ModUtils; import com.atsuishio.superbwarfare.ModUtils;
import com.atsuishio.superbwarfare.block.menu.ChargingStationMenu;
import com.atsuishio.superbwarfare.block.menu.ReforgingTableMenu; import com.atsuishio.superbwarfare.block.menu.ReforgingTableMenu;
import net.minecraft.world.inventory.MenuType; import net.minecraft.world.inventory.MenuType;
import net.minecraftforge.common.extensions.IForgeMenuType; import net.minecraftforge.common.extensions.IForgeMenuType;
@ -9,9 +10,13 @@ import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject; import net.minecraftforge.registries.RegistryObject;
public class ModMenuTypes { public class ModMenuTypes {
public static final DeferredRegister<MenuType<?>> REGISTRY = DeferredRegister.create(ForgeRegistries.MENU_TYPES, ModUtils.MODID); public static final DeferredRegister<MenuType<?>> REGISTRY = DeferredRegister.create(ForgeRegistries.MENU_TYPES, ModUtils.MODID);
public static final RegistryObject<MenuType<ReforgingTableMenu>> REFORGING_TABLE_MENU = public static final RegistryObject<MenuType<ReforgingTableMenu>> REFORGING_TABLE_MENU =
REGISTRY.register("reforging_table_menu", REGISTRY.register("reforging_table_menu",
() -> IForgeMenuType.create((windowId, inv, data) -> new ReforgingTableMenu(windowId, inv))); () -> IForgeMenuType.create((windowId, inv, data) -> new ReforgingTableMenu(windowId, inv)));
public static final RegistryObject<MenuType<ChargingStationMenu>> CHARGING_STATION_MENU =
REGISTRY.register("charging_station_menu",
() -> IForgeMenuType.create((windowId, inv, data) -> new ChargingStationMenu(windowId, inv)));
} }

View file

@ -1,5 +1,6 @@
package com.atsuishio.superbwarfare.init; package com.atsuishio.superbwarfare.init;
import com.atsuishio.superbwarfare.client.screens.ChargingStationScreen;
import com.atsuishio.superbwarfare.client.screens.ReforgingTableScreen; import com.atsuishio.superbwarfare.client.screens.ReforgingTableScreen;
import net.minecraft.client.gui.screens.MenuScreens; import net.minecraft.client.gui.screens.MenuScreens;
import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.Dist;
@ -9,10 +10,12 @@ import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD, value = Dist.CLIENT) @Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD, value = Dist.CLIENT)
public class ModScreens { public class ModScreens {
@SubscribeEvent @SubscribeEvent
public static void clientLoad(FMLClientSetupEvent event) { public static void clientLoad(FMLClientSetupEvent event) {
event.enqueueWork(() -> { event.enqueueWork(() -> {
MenuScreens.register(ModMenuTypes.REFORGING_TABLE_MENU.get(), ReforgingTableScreen::new); MenuScreens.register(ModMenuTypes.REFORGING_TABLE_MENU.get(), ReforgingTableScreen::new);
MenuScreens.register(ModMenuTypes.CHARGING_STATION_MENU.get(), ChargingStationScreen::new);
}); });
} }
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 2 KiB