Skip to content

Commit

Permalink
Added Magic Press
Browse files Browse the repository at this point in the history
  • Loading branch information
PiTheGuy committed Apr 8, 2022
1 parent 81ef97d commit d52c4a6
Show file tree
Hide file tree
Showing 21 changed files with 516 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/pitheguy/magicmod/MagicMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.pitheguy.magicmod.client.gui.MagicCrateScreen;
import com.pitheguy.magicmod.client.gui.MagicInfuserScreen;
import com.pitheguy.magicmod.client.gui.MagicPressScreen;
import com.pitheguy.magicmod.init.ModContainerTypes;
import com.pitheguy.magicmod.init.ModTileEntityTypes;
import com.pitheguy.magicmod.util.RegistryHandler;
Expand Down Expand Up @@ -51,6 +52,7 @@ public MagicMod() {
private void doClientStuff(final FMLClientSetupEvent event) {
ScreenManager.registerFactory(ModContainerTypes.MAGIC_INFUSER.get(), MagicInfuserScreen::new);
ScreenManager.registerFactory(ModContainerTypes.MAGIC_CRATE.get(), MagicCrateScreen::new);
ScreenManager.registerFactory(ModContainerTypes.MAGIC_PRESS.get(), MagicPressScreen::new);
}

@SubscribeEvent
Expand Down
80 changes: 80 additions & 0 deletions src/main/java/com/pitheguy/magicmod/blocks/MagicPress.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.pitheguy.magicmod.blocks;

import com.pitheguy.magicmod.init.ModTileEntityTypes;
import com.pitheguy.magicmod.tileentity.MagicPressTileEntity;
import com.pitheguy.magicmod.util.ModItemHandler;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.item.ItemEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.inventory.container.INamedContainerProvider;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;
import net.minecraftforge.common.ToolType;
import net.minecraftforge.fml.network.NetworkHooks;

import javax.annotation.Nullable;

public class MagicPress extends Block {

public MagicPress() {
super(Properties.create(Material.IRON)
.hardnessAndResistance(6.5f, 8.0f)
.sound(SoundType.METAL)
.harvestLevel(4)
.harvestTool(ToolType.PICKAXE)
);
}

@Override
public boolean hasTileEntity(BlockState state) {
return true;
}

@Override
public TileEntity createTileEntity(BlockState state, IBlockReader world) {
return ModTileEntityTypes.MAGIC_PRESS.get().create();
}

@Override
public void onBlockPlacedBy(World worldIn, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack stack) {
super.onBlockPlacedBy(worldIn, pos, state, placer, stack);
}

@Override
public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit) {
if (worldIn != null && !worldIn.isRemote()) {
TileEntity tile = worldIn.getTileEntity(pos);
if(tile instanceof MagicPressTileEntity) {
NetworkHooks.openGui((ServerPlayerEntity) player,(INamedContainerProvider) tile,pos);
return ActionResultType.SUCCESS;
}
}
return ActionResultType.SUCCESS;
}

@Override
public void onReplaced(BlockState state, World worldIn, BlockPos pos, BlockState newState, boolean isMoving) {
TileEntity tile = worldIn.getTileEntity(pos);
if(tile instanceof MagicPressTileEntity) {
MagicPressTileEntity infuser = (MagicPressTileEntity) tile;
((ModItemHandler)infuser.getInventory()).toNonNullList().forEach(item -> {
ItemEntity itemEntity = new ItemEntity(worldIn, pos.getX(), pos.getY(), pos.getZ(), item);
worldIn.addEntity(itemEntity);
});
}
if (state.hasTileEntity() && state.getBlock() != newState.getBlock()) {
worldIn.removeTileEntity(pos);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.pitheguy.magicmod.client.gui;

import com.mojang.blaze3d.systems.RenderSystem;
import com.pitheguy.magicmod.container.MagicPressContainer;
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.ITextComponent;

public class MagicPressScreen extends ContainerScreen<MagicPressContainer> {
private static final ResourceLocation TEXTURE = new ResourceLocation("magicmod", "textures/gui/magic_press.png");
public MagicPressScreen(MagicPressContainer screenContainer, PlayerInventory inv, ITextComponent titleIn) {
super(screenContainer, inv, titleIn);

this.guiLeft = 0;
this.guiTop = 0;
this.xSize = 176;
this.ySize = 162;
}

@Override
protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
this.minecraft.getTextureManager().bindTexture(TEXTURE);
this.blit(this.guiLeft, this.guiTop, 0, 0, this.xSize, this.ySize);
this.blit(this.guiLeft + 49, this.guiTop + 53, 0, 165, this.container.getFuelScaled(), 16);
}

@Override
protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {
super.drawGuiContainerForegroundLayer(mouseX, mouseY);
this.font.drawString(this.title.getFormattedText(), 8, 8, 0x404040);
this.font.drawString(this.playerInventory.getDisplayName().getFormattedText(), 8, 70, 0x404040);
}

@Override
public void render(int mouseX, int mouseY, float partialTicks) {
this.renderBackground();
super.render(mouseX, mouseY, partialTicks);
this.renderHoveredToolTip(mouseX, mouseY);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.pitheguy.magicmod.container;

import com.pitheguy.magicmod.container.itemhandlers.magicinfuser.SingleItemSlotItemHandler;
import com.pitheguy.magicmod.container.itemhandlers.SingleItemSlotItemHandler;
import com.pitheguy.magicmod.init.ModContainerTypes;
import com.pitheguy.magicmod.tileentity.MagicInfuserTileEntity;
import com.pitheguy.magicmod.util.RegistryHandler;
Expand Down
115 changes: 115 additions & 0 deletions src/main/java/com/pitheguy/magicmod/container/MagicPressContainer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package com.pitheguy.magicmod.container;

import com.pitheguy.magicmod.container.itemhandlers.MultiItemSlotItemHandler;
import com.pitheguy.magicmod.container.itemhandlers.SingleItemSlotItemHandler;
import com.pitheguy.magicmod.init.ModContainerTypes;
import com.pitheguy.magicmod.tileentity.MagicPressTileEntity;
import com.pitheguy.magicmod.util.FunctionalIntReferenceHolder;
import com.pitheguy.magicmod.util.RegistryHandler;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IWorldPosCallable;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

import javax.annotation.Nonnull;
import java.util.Arrays;
import java.util.Objects;

import static com.pitheguy.magicmod.util.RegistryHandler.*;

public class MagicPressContainer extends Container {

public MagicPressTileEntity tileEntity;
private final IWorldPosCallable canInteractWithCallable;
public FunctionalIntReferenceHolder fuel;

//Server constructor
public MagicPressContainer(final int windowID, final PlayerInventory playerInv, final MagicPressTileEntity tile) {
super(ModContainerTypes.MAGIC_PRESS.get(), windowID);
this.canInteractWithCallable = IWorldPosCallable.of(tile.getWorld(), tile.getPos());
this.tileEntity = tile;
final int slotSizePlus2 = 18;
final int startX = 8;
//Hotbar
int hotbarY = 139;
for (int col = 0; col < 9; col++) {
this.addSlot(new Slot(playerInv, col, (startX + (col * slotSizePlus2)), hotbarY));
}
//Main Player Inventory
int startY = 81;
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 9; col++) {
this.addSlot(new Slot(playerInv, 9+(row*9)+col, startX + (col * slotSizePlus2), startY + (row * slotSizePlus2)));
}
}
//Magic Press Inventory
this.addSlot(new MultiItemSlotItemHandler(tile.getInventory(), 0, 27, 19, Arrays.asList(MAGIC_HELMET.get(),MAGIC_CHESTPLATE.get(),MAGIC_LEGGINGS.get(),MAGIC_BOOTS.get(),MAGIC_PICKAXE.get(),MAGIC_AXE.get(),MAGIC_SHOVEL.get(),MAGIC_SWORD.get(),MAGIC_HOE.get())));
this.addSlot(new SingleItemSlotItemHandler(tile.getInventory(), 1, 76, 19, MAGIC_PLATE.get()));
this.addSlot(new MultiItemSlotItemHandler(tile.getInventory(), 2, 134,19, Arrays.asList(REINFORCED_MAGIC_HELMET.get(), REINFORCED_MAGIC_CHESTPLATE.get(), REINFORCED_MAGIC_LEGGINGS.get(), REINFORCED_MAGIC_BOOTS.get(), REINFORCED_MAGIC_PICKAXE.get(), REINFORCED_MAGIC_AXE.get(), REINFORCED_MAGIC_SHOVEL.get(), REINFORCED_MAGIC_SWORD.get(), REINFORCED_MAGIC_HOE.get())));
this.addSlot(new MultiItemSlotItemHandler(tile.getInventory(), 3,15,53, Arrays.asList(MAGIC_GEM.get(), MAGIC_BLOCK_ITEM.get())));

this.trackInt(fuel = new FunctionalIntReferenceHolder(() -> this.tileEntity.fuel,
value -> this.tileEntity.fuel = value));

}
//Client constructor
public MagicPressContainer(final int windowID, final PlayerInventory playerInv, final PacketBuffer data) {
this(windowID, playerInv, getTileEntity(playerInv, data));
}

private static MagicPressTileEntity getTileEntity(final PlayerInventory playerInv, final PacketBuffer data) {
Objects.requireNonNull(playerInv, "playerInv cannot be null");
Objects.requireNonNull(data, "data cannot be null");
final TileEntity tileAtPos = playerInv.player.world.getTileEntity(data.readBlockPos());
if (tileAtPos instanceof MagicPressTileEntity) {
return (MagicPressTileEntity) tileAtPos;
}
throw new IllegalStateException("TileEntity is not correct " + tileAtPos);
}

@Override
public boolean canInteractWith(PlayerEntity playerIn) {
return isWithinUsableDistance(canInteractWithCallable, playerIn, RegistryHandler.MAGIC_PRESS.get());
}

@Nonnull
@Override
public ItemStack transferStackInSlot(final PlayerEntity player, final int index) {
ItemStack returnStack = ItemStack.EMPTY;
final Slot slot = this.inventorySlots.get(index);
if (slot != null && slot.getHasStack()) {
final ItemStack slotStack = slot.getStack();
returnStack = slotStack.copy();

final int containerSlots = this.inventorySlots.size() - player.inventory.mainInventory.size();
if (index < containerSlots) {
if (!mergeItemStack(slotStack, containerSlots, this.inventorySlots.size(), true)) {
return ItemStack.EMPTY;
}
} else if (!mergeItemStack(slotStack, 0, containerSlots, false)) {
return ItemStack.EMPTY;
}
if (slotStack.getCount() == 0) {
slot.putStack(ItemStack.EMPTY);
} else {
slot.onSlotChanged();
}
if (slotStack.getCount() == returnStack.getCount()) {
return ItemStack.EMPTY;
}
slot.onTake(player, slotStack);
}
return returnStack;
}

@OnlyIn(Dist.CLIENT)
public int getFuelScaled() {
return this.fuel.get() != 0 && this.tileEntity.fuel != 0 ? this.fuel.get() * 112 / this.tileEntity.maxFuel : 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.pitheguy.magicmod.container.itemhandlers;

import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.SlotItemHandler;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class MultiItemSlotItemHandler extends SlotItemHandler {
final List<Item> validItem;
public MultiItemSlotItemHandler(IItemHandler itemHandler, int index, int xPosition, int yPosition, List<Item> validItem) {
super(itemHandler, index, xPosition, yPosition);
this.validItem = validItem;
}

@Override
public boolean isItemValid(@NotNull ItemStack stack) {
return validItem.contains(stack.getItem()) && super.isItemValid(stack);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.pitheguy.magicmod.container.itemhandlers.magicinfuser;
package com.pitheguy.magicmod.container.itemhandlers;

import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.pitheguy.magicmod.container.MagicCrateContainer;
import com.pitheguy.magicmod.container.MagicInfuserContainer;
import com.pitheguy.magicmod.container.MagicPressContainer;
import net.minecraft.inventory.container.ContainerType;
import net.minecraftforge.common.extensions.IForgeContainerType;
import net.minecraftforge.fml.RegistryObject;
Expand All @@ -15,4 +16,6 @@ public class ModContainerTypes {
.register("magic_infuser", () -> IForgeContainerType.create(MagicInfuserContainer::new));
public static final RegistryObject<ContainerType<MagicCrateContainer>> MAGIC_CRATE = CONTAINER_TYPES
.register("magic_crate", () -> IForgeContainerType.create(MagicCrateContainer::new));
public static final RegistryObject<ContainerType<MagicPressContainer>> MAGIC_PRESS = CONTAINER_TYPES
.register("magic_press", () -> IForgeContainerType.create(MagicPressContainer::new));
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.pitheguy.magicmod.tileentity.MagicCrateTileEntity;
import com.pitheguy.magicmod.tileentity.MagicInfuserTileEntity;
import com.pitheguy.magicmod.tileentity.MagicPressTileEntity;
import com.pitheguy.magicmod.util.RegistryHandler;
import net.minecraft.tileentity.TileEntityType;
import net.minecraftforge.fml.RegistryObject;
Expand All @@ -13,4 +14,5 @@ public class ModTileEntityTypes {
ForgeRegistries.TILE_ENTITIES, "magicmod");
public static final RegistryObject<TileEntityType<MagicInfuserTileEntity>> MAGIC_INFUSER = TILE_ENTITY_TYPES.register("magic_infuser",() -> TileEntityType.Builder.create(MagicInfuserTileEntity::new, RegistryHandler.MAGIC_INFUSER.get()).build(null));
public static final RegistryObject<TileEntityType<MagicCrateTileEntity>> MAGIC_CRATE = TILE_ENTITY_TYPES.register("magic_crate",() -> TileEntityType.Builder.create(MagicCrateTileEntity::new, RegistryHandler.MAGIC_CRATE.get()).build(null));
public static final RegistryObject<TileEntityType<MagicPressTileEntity>> MAGIC_PRESS = TILE_ENTITY_TYPES.register("magic_press",() -> TileEntityType.Builder.create(MagicPressTileEntity::new, RegistryHandler.MAGIC_PRESS.get()).build(null));
}
Loading

0 comments on commit d52c4a6

Please sign in to comment.