Skip to content

Commit 8ddda63

Browse files
committed
Add Overload Station
1 parent 43635f1 commit 8ddda63

8 files changed

Lines changed: 366 additions & 1 deletion

File tree

src/main/java/com/redcrafter07/ultrautilities/UltraUtilities.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.redcrafter07.ultrautilities.data.recipes.ModRecipeTypes;
66
import com.redcrafter07.ultrautilities.item.ModItems;
77
import com.redcrafter07.ultrautilities.screen.CraftingStationScreen;
8+
import com.redcrafter07.ultrautilities.screen.OverloadStationScreen;
89
import com.redcrafter07.ultrautilities.tileentity.ModTileEntities;
910
import net.minecraft.block.Block;
1011
import net.minecraft.block.Blocks;
@@ -66,6 +67,7 @@ private void doClientStuff(final FMLClientSetupEvent event) {
6667
//LOGGER.info("Got game settings {}", event.getMinecraftSupplier().get().options);
6768

6869
ScreenManager.registerFactory(ModContainers.CRAFTING_STATION_CONTAINER.get(), CraftingStationScreen::new);
70+
ScreenManager.registerFactory(ModContainers.OVERLOAD_STATION_CONTAINER.get(), OverloadStationScreen::new);
6971

7072
}
7173

src/main/java/com/redcrafter07/ultrautilities/blocks/ModBlocks.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public class ModBlocks {
2323
public static final RegistryObject<Block> OVERLOAD_ORE = registerBlock("overload_ore", () -> new Block(AbstractBlock.Properties.create(Material.ROCK).harvestLevel(3).harvestTool(ToolType.PICKAXE).setRequiresTool()));
2424

2525
// TILE ENTITIES
26-
public static final RegistryObject<Block> CRAFTING_STATION = registerBlock("crafting_station", () -> new CraftingStationBlock(AbstractBlock.Properties.create(Material.WOOD).harvestLevel(0).harvestTool(ToolType.AXE).setRequiresTool()));
26+
public static final RegistryObject<Block> CRAFTING_STATION = registerBlock("crafting_station", () -> new CraftingStationBlock(AbstractBlock.Properties.create(Material.ROCK).harvestLevel(0).harvestTool(ToolType.PICKAXE).setRequiresTool()));
27+
public static final RegistryObject<Block> OVERLOAD_STATION = registerBlock("overload_station", () -> new OverloadStationBlock(AbstractBlock.Properties.create(Material.ROCK).harvestLevel(0).harvestTool(ToolType.PICKAXE).setRequiresTool()));
2728

2829
public static <T extends Block>RegistryObject<T> registerBlock(String name, Supplier<T> block) {
2930
RegistryObject<T> toReturn = BLOCKS.register(name, block);
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.redcrafter07.ultrautilities.blocks;
2+
3+
import com.redcrafter07.ultrautilities.container.CraftingStationContainer;
4+
import com.redcrafter07.ultrautilities.container.OverloadStationContainer;
5+
import com.redcrafter07.ultrautilities.tileentity.CraftingStationTile;
6+
import com.redcrafter07.ultrautilities.tileentity.ModTileEntities;
7+
import com.redcrafter07.ultrautilities.tileentity.OverloadStationTile;
8+
import net.minecraft.block.Block;
9+
import net.minecraft.block.BlockState;
10+
import net.minecraft.entity.player.PlayerEntity;
11+
import net.minecraft.entity.player.PlayerInventory;
12+
import net.minecraft.entity.player.ServerPlayerEntity;
13+
import net.minecraft.inventory.container.Container;
14+
import net.minecraft.inventory.container.INamedContainerProvider;
15+
import net.minecraft.tileentity.TileEntity;
16+
import net.minecraft.util.ActionResultType;
17+
import net.minecraft.util.Hand;
18+
import net.minecraft.util.math.BlockPos;
19+
import net.minecraft.util.math.BlockRayTraceResult;
20+
import net.minecraft.util.text.ITextComponent;
21+
import net.minecraft.util.text.TranslationTextComponent;
22+
import net.minecraft.world.IBlockReader;
23+
import net.minecraft.world.World;
24+
import net.minecraftforge.fml.network.NetworkHooks;
25+
26+
import javax.annotation.Nullable;
27+
28+
public class OverloadStationBlock extends Block {
29+
public OverloadStationBlock(Properties properties) {
30+
super(properties);
31+
}
32+
33+
@Override
34+
public ActionResultType onBlockActivated(BlockState blockState, World world, BlockPos blockPos, PlayerEntity playerEntity, Hand hand, BlockRayTraceResult blockRayTraceResult) {
35+
if(!world.isRemote()) {
36+
TileEntity tileEntity = world.getTileEntity(blockPos);
37+
if(tileEntity instanceof OverloadStationTile) {
38+
INamedContainerProvider containerProvider = createContainerProvider(world, blockPos);
39+
40+
NetworkHooks.openGui(((ServerPlayerEntity) playerEntity), containerProvider, tileEntity.getPos());
41+
} else {
42+
throw new IllegalStateException("Container Provider missing!");
43+
}
44+
}
45+
46+
47+
return ActionResultType.SUCCESS;
48+
}
49+
50+
private INamedContainerProvider createContainerProvider(World world, BlockPos blockPos) {
51+
return new INamedContainerProvider() {
52+
@Override
53+
public ITextComponent getDisplayName() {
54+
return new TranslationTextComponent("screen.ultrautilities.overload_station");
55+
}
56+
57+
@Nullable
58+
@Override
59+
public Container createMenu(int p_createMenu_1_, PlayerInventory inv, PlayerEntity player) {
60+
return new OverloadStationContainer(p_createMenu_1_, world, blockPos, inv, player);
61+
}
62+
};
63+
};
64+
65+
@Nullable
66+
@Override
67+
public TileEntity createTileEntity(BlockState state, IBlockReader world) {
68+
return ModTileEntities.OVERLOAD_STATION_TILE.get().create();
69+
}
70+
71+
@Override
72+
public boolean hasTileEntity(BlockState state) {
73+
return true;
74+
}
75+
}

src/main/java/com/redcrafter07/ultrautilities/container/ModContainers.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ public class ModContainers {
2121
return new CraftingStationContainer(windowId, playerWorld, blockPosition, inv, inv.player);
2222
})));
2323

24+
public static final RegistryObject<ContainerType<OverloadStationContainer>> OVERLOAD_STATION_CONTAINER = CONTAINERS.register("overload_station_container",
25+
() -> IForgeContainerType.create(((windowId, inv, data) -> {
26+
BlockPos blockPosition = data.readBlockPos();
27+
World playerWorld = inv.player.getEntityWorld();
28+
return new OverloadStationContainer(windowId, playerWorld, blockPosition, inv, inv.player);
29+
})));
30+
2431
public static void register(IEventBus eventBus) {
2532
CONTAINERS.register(eventBus);
2633
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.redcrafter07.ultrautilities.container;
2+
3+
import com.redcrafter07.ultrautilities.blocks.ModBlocks;
4+
import net.minecraft.entity.player.PlayerEntity;
5+
import net.minecraft.entity.player.PlayerInventory;
6+
import net.minecraft.inventory.container.Container;
7+
import net.minecraft.inventory.container.Slot;
8+
import net.minecraft.item.ItemStack;
9+
import net.minecraft.tileentity.TileEntity;
10+
import net.minecraft.util.IWorldPosCallable;
11+
import net.minecraft.util.math.BlockPos;
12+
import net.minecraft.world.World;
13+
import net.minecraftforge.items.CapabilityItemHandler;
14+
import net.minecraftforge.items.IItemHandler;
15+
import net.minecraftforge.items.SlotItemHandler;
16+
import net.minecraftforge.items.wrapper.InvWrapper;
17+
18+
public class OverloadStationContainer extends Container {
19+
private final TileEntity tileEntity;
20+
private final PlayerEntity playerEntity;
21+
private final IItemHandler playerInventory;
22+
23+
public OverloadStationContainer(int containerId, World world, BlockPos blockPos, PlayerInventory inv, PlayerEntity playerEntity) {
24+
super(ModContainers.OVERLOAD_STATION_CONTAINER.get(), containerId);
25+
this.tileEntity = world.getTileEntity(blockPos);
26+
this.playerEntity = playerEntity;
27+
this.playerInventory = new InvWrapper(inv);
28+
29+
layoutPlayerInventorySlots(8, 86);
30+
31+
if(tileEntity != null) {
32+
tileEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).ifPresent(handler -> {
33+
addSlot(new SlotItemHandler(handler, 0, 10, 13));
34+
addSlot(new SlotItemHandler(handler, 1, 54, 13));
35+
addSlot(new SlotItemHandler(handler, 2, 152, 55));
36+
});
37+
}
38+
}
39+
40+
@Override
41+
public boolean canInteractWith(PlayerEntity playerIn) {
42+
return isWithinUsableDistance(IWorldPosCallable.of(tileEntity.getWorld(), tileEntity.getPos()), playerIn, ModBlocks.OVERLOAD_STATION.get());
43+
}
44+
45+
// Credit: Kaupenjoe (https://gist.github.com/Kaupenjoe/cbb03a4db3f94a2bfd5b2ad2fa78f539)
46+
private int addSlotRange(IItemHandler handler, int index, int x, int y, int amount, int dx) {
47+
for (int i = 0; i < amount; i++) {
48+
addSlot(new SlotItemHandler(handler, index, x, y));
49+
x += dx;
50+
index++;
51+
}
52+
53+
return index;
54+
}
55+
56+
private int addSlotBox(IItemHandler handler, int index, int x, int y, int horAmount, int dx, int verAmount, int dy) {
57+
for (int j = 0; j < verAmount; j++) {
58+
index = addSlotRange(handler, index, x, y, horAmount, dx);
59+
y += dy;
60+
}
61+
62+
return index;
63+
}
64+
65+
private void layoutPlayerInventorySlots(int leftCol, int topRow) {
66+
addSlotBox(playerInventory, 9, leftCol, topRow, 9, 18, 3, 18);
67+
68+
topRow += 58;
69+
addSlotRange(playerInventory, 0, leftCol, topRow, 9, 18);
70+
}
71+
72+
// Credit: diesieben07 (https://github.com/diesieben07/SevenCommons)
73+
private static final int HOTBAR_SLOT_COUNT = 9;
74+
private static final int PLAYER_INVENTORY_ROW_COUNT = 3;
75+
private static final int PLAYER_INVENTORY_COLUMN_COUNT = 9;
76+
private static final int PLAYER_INVENTORY_SLOT_COUNT = PLAYER_INVENTORY_COLUMN_COUNT * PLAYER_INVENTORY_ROW_COUNT;
77+
private static final int VANILLA_SLOT_COUNT = HOTBAR_SLOT_COUNT + PLAYER_INVENTORY_SLOT_COUNT;
78+
private static final int VANILLA_FIRST_SLOT_INDEX = 0;
79+
private static final int TE_INVENTORY_FIRST_SLOT_INDEX = VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT;
80+
81+
// THIS YOU HAVE TO DEFINE!
82+
private static final int TE_INVENTORY_SLOT_COUNT = 3; // must match TileEntityInventoryBasic.NUMBER_OF_SLOTS
83+
84+
@Override
85+
public ItemStack transferStackInSlot(PlayerEntity playerIn, int index) {
86+
Slot sourceSlot = inventorySlots.get(index);
87+
if (sourceSlot == null || !sourceSlot.getHasStack()) return ItemStack.EMPTY; //EMPTY_ITEM
88+
ItemStack sourceStack = sourceSlot.getStack();
89+
ItemStack copyOfSourceStack = sourceStack.copy();
90+
91+
// Check if the slot clicked is one of the vanilla container slots
92+
if (index < VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT) {
93+
// This is a vanilla container slot so merge the stack into the tile inventory
94+
if (!mergeItemStack(sourceStack, TE_INVENTORY_FIRST_SLOT_INDEX, TE_INVENTORY_FIRST_SLOT_INDEX
95+
+ TE_INVENTORY_SLOT_COUNT, false)) {
96+
return ItemStack.EMPTY; // EMPTY_ITEM
97+
}
98+
} else if (index < TE_INVENTORY_FIRST_SLOT_INDEX + TE_INVENTORY_SLOT_COUNT) {
99+
// This is a TE slot so merge the stack into the players inventory
100+
if (!mergeItemStack(sourceStack, VANILLA_FIRST_SLOT_INDEX, VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT, false)) {
101+
return ItemStack.EMPTY;
102+
}
103+
} else {
104+
System.out.println("Invalid slotIndex:" + index);
105+
return ItemStack.EMPTY;
106+
}
107+
// If stack size == 0 (the entire stack was moved) set slot contents to null
108+
if (sourceStack.getCount() == 0) {
109+
sourceSlot.putStack(ItemStack.EMPTY);
110+
} else {
111+
sourceSlot.onSlotChanged();
112+
}
113+
sourceSlot.onTake(playerEntity, sourceStack);
114+
return copyOfSourceStack;
115+
}
116+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.redcrafter07.ultrautilities.screen;
2+
3+
import com.mojang.blaze3d.matrix.MatrixStack;
4+
import com.mojang.blaze3d.systems.RenderSystem;
5+
import com.redcrafter07.ultrautilities.UltraUtilities;
6+
import com.redcrafter07.ultrautilities.container.CraftingStationContainer;
7+
import com.redcrafter07.ultrautilities.container.OverloadStationContainer;
8+
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
9+
import net.minecraft.entity.player.PlayerInventory;
10+
import net.minecraft.util.ResourceLocation;
11+
import net.minecraft.util.text.ITextComponent;
12+
13+
public class OverloadStationScreen extends ContainerScreen<OverloadStationContainer> {
14+
private final ResourceLocation guiLocation = new ResourceLocation(UltraUtilities.MOD_ID, "textures/gui/crafting_station.png");
15+
16+
public OverloadStationScreen(OverloadStationContainer container, PlayerInventory inv, ITextComponent textComponent) {
17+
super(container, inv, textComponent);
18+
}
19+
20+
@Override
21+
public void render(MatrixStack matrix, int mouseX, int mouseY, float partialTicks) {
22+
this.renderBackground(matrix);
23+
super.render(matrix, mouseX, mouseY, partialTicks);
24+
this.renderHoveredTooltip(matrix, mouseX, mouseY);
25+
}
26+
27+
@Override
28+
protected void drawGuiContainerBackgroundLayer(MatrixStack matrixStack, float partialTicks, int x, int y) {
29+
RenderSystem.color4f(1f, 1f ,1f, 1f);
30+
this.minecraft.getTextureManager().bindTexture(guiLocation);
31+
int guiLeft = this.guiLeft;
32+
int guiTop = this.guiTop;
33+
this.blit(matrixStack, guiLeft, guiTop, 0, 0, this.xSize, this.ySize);
34+
}
35+
}

src/main/java/com/redcrafter07/ultrautilities/tileentity/ModTileEntities.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ public class ModTileEntities {
1717
TILE_ENTITIES.register("crafting_station_tile", () -> TileEntityType.Builder.create(
1818
CraftingStationTile::new, ModBlocks.CRAFTING_STATION.get()).build(null));
1919

20+
public static RegistryObject<TileEntityType<OverloadStationTile>> OVERLOAD_STATION_TILE =
21+
TILE_ENTITIES.register("overload_station_tile", () -> TileEntityType.Builder.create(
22+
OverloadStationTile::new, ModBlocks.OVERLOAD_STATION.get()).build(null));
23+
2024

2125
public static void register(IEventBus eventBus) {
2226
TILE_ENTITIES.register(eventBus);

0 commit comments

Comments
 (0)