Skip to content

Commit

Permalink
1.1.1 Update
Browse files Browse the repository at this point in the history
Fixed Inventory Cable Connector not showing up in the creative inventory
and JEI
Added JEI compatibility to crafting terminal
Fixed Storage Terminal recipe not using tags
Terminals now emit light
  • Loading branch information
tom5454 committed May 26, 2020
1 parent a06a43d commit 68e969e
Show file tree
Hide file tree
Showing 15 changed files with 325 additions and 13 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ apply plugin: 'net.minecraftforge.gradle'
apply plugin: 'eclipse'
apply plugin: 'maven-publish'

version = '1.1.0'
version = '1.1.1'
group = 'com.yourname.modid' // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = 'toms_storage'

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/tom/storagemod/ItemBlockConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public class ItemBlockConnector extends BlockItem {

public ItemBlockConnector() {
super(StorageMod.invCableConnector, new Properties());
super(StorageMod.invCableConnector, new Properties().group(StorageMod.STORAGE_MOD_TAB));
setRegistryName(StorageMod.invCableConnector.getRegistryName());
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/tom/storagemod/StorageMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.tom.storagemod.item.ItemBlockPainted;
import com.tom.storagemod.item.ItemPaintKit;
import com.tom.storagemod.item.ItemWirelessTerminal;
import com.tom.storagemod.network.NetworkHandler;
import com.tom.storagemod.proxy.ClientProxy;
import com.tom.storagemod.proxy.IProxy;
import com.tom.storagemod.proxy.ServerProxy;
Expand All @@ -52,8 +53,7 @@

// The value here should match an entry in the META-INF/mods.toml file
@Mod(StorageMod.modid)
public class StorageMod
{
public class StorageMod {
public static final String modid = "toms_storage";
public static IProxy proxy = DistExecutor.runForDist(() -> ClientProxy::new, () -> ServerProxy::new);
public static InventoryConnector connector;
Expand Down Expand Up @@ -104,6 +104,7 @@ public StorageMod() {
private void setup(final FMLCommonSetupEvent event) {
LOGGER.info("Tom's Storage Setup starting");
proxy.setup();
NetworkHandler.init();
}

private void doClientStuff(final FMLClientSetupEvent event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public abstract class StorageTerminalBase extends ContainerBlock implements IWat
private static final VoxelShape SHAPE_U = Block.makeCuboidShape(0.0D, 10.0D, 0.0D, 16.0D, 16.0D, 16.0D);
private static final VoxelShape SHAPE_D = Block.makeCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 6.0D, 16.0D);
public StorageTerminalBase() {
super(Block.Properties.create(Material.WOOD).hardnessAndResistance(3).harvestTool(ToolType.AXE));
super(Block.Properties.create(Material.WOOD).hardnessAndResistance(3).harvestTool(ToolType.AXE).lightValue(6));
setDefaultState(getDefaultState().with(TERMINAL_POS, TerminalPos.CENTER));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.RecipeItemHelper;
import net.minecraft.item.crafting.ServerRecipePlacer;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT;
import net.minecraft.network.play.server.SSetSlotPacket;

import net.minecraftforge.api.distmarker.Dist;
Expand All @@ -27,9 +29,12 @@

import com.tom.storagemod.StorageMod;
import com.tom.storagemod.StoredItemStack;
import com.tom.storagemod.jei.IJEIAutoFillTerminal;
import com.tom.storagemod.network.IDataReceiver;
import com.tom.storagemod.network.NetworkHandler;
import com.tom.storagemod.tile.TileEntityCraftingTerminal;

public class ContainerCraftingTerminal extends ContainerStorageTerminal {
public class ContainerCraftingTerminal extends ContainerStorageTerminal implements IJEIAutoFillTerminal, IDataReceiver {
public static class SlotCrafting extends Slot {
public SlotCrafting(IInventory inventoryIn, int index, int xPosition, int yPosition) {
super(inventoryIn, index, xPosition, yPosition);
Expand Down Expand Up @@ -299,4 +304,26 @@ protected void clear() {
}
}).place(p_217056_3_, p_217056_2_, p_217056_1_);
}

@Override
public void sendMessage(CompoundNBT compound) {
NetworkHandler.sendDataToServer(compound);
}

@Override
public void receive(CompoundNBT message) {
ItemStack[][] stacks = new ItemStack[9][];
ListNBT list = message.getList("i", 10);
for (int i = 0;i < list.size();i++) {
CompoundNBT nbttagcompound = list.getCompound(i);
byte slot = nbttagcompound.getByte("s");
byte l = nbttagcompound.getByte("l");
stacks[slot] = new ItemStack[l];
for (int j = 0;j < l;j++) {
CompoundNBT tag = nbttagcompound.getCompound("i" + j);
stacks[slot][j] = ItemStack.read(tag);
}
}
((TileEntityCraftingTerminal) te).handlerItemTransfer(player, stacks);
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/tom/storagemod/gui/GuiStorageTerminalBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,17 @@ public boolean charTyped(char p_charTyped_1_, int p_charTyped_2_) {
if(searchField.charTyped(p_charTyped_1_, p_charTyped_2_))return true;
return super.charTyped(p_charTyped_1_, p_charTyped_2_);
}

@Override
public boolean mouseScrolled(double p_mouseScrolled_1_, double p_mouseScrolled_3_, double p_mouseScrolled_5_) {
if (!this.needsScrollBars()) {
return false;
} else {
int i = ((this.container).itemList.size() + 9 - 1) / 9 - 5;
this.currentScroll = (float)(this.currentScroll - p_mouseScrolled_5_ / i);
this.currentScroll = MathHelper.clamp(this.currentScroll, 0.0F, 1.0F);
this.container.scrollTo(this.currentScroll);
return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.tom.storagemod.jei;

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

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.container.Container;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT;

import com.google.common.base.Function;

import com.tom.storagemod.gui.ContainerCraftingTerminal;

import mezz.jei.api.constants.VanillaRecipeCategoryUid;
import mezz.jei.api.gui.IRecipeLayout;
import mezz.jei.api.gui.ingredient.IGuiIngredient;
import mezz.jei.api.gui.ingredient.IGuiItemStackGroup;
import mezz.jei.api.recipe.transfer.IRecipeTransferError;
import mezz.jei.api.recipe.transfer.IRecipeTransferHandler;
import mezz.jei.api.registration.IRecipeTransferRegistration;

@SuppressWarnings("rawtypes")
public class CraftingTerminalTransferHandler implements IRecipeTransferHandler {
private final Class<? extends Container> containerClass;
private static final List<Class<? extends Container>> containerClasses = new ArrayList<>();
private static final Function<IRecipeLayout, ItemStack[][]> transferFunc = new Function<IRecipeLayout, ItemStack[][]>() {
@Override
public ItemStack[][] apply(IRecipeLayout t) {
List<ItemStack[]> inputs = new ArrayList<>();
IGuiItemStackGroup itemStackGroup = t.getItemStacks();
for (IGuiIngredient<ItemStack> ingredient : itemStackGroup.getGuiIngredients().values()) {
if (ingredient.isInput()) {
if (!ingredient.getAllIngredients().isEmpty() && ingredient.getAllIngredients().get(0) != null) {
inputs.add(ingredient.getAllIngredients().toArray(new ItemStack[]{}));
} else {
inputs.add(null);
}
}
}
return inputs.toArray(new ItemStack[][]{});
}
};
private static final IRecipeTransferError ERROR_INSTANCE = new IRecipeTransferError() {
@Override public IRecipeTransferError.Type getType() { return IRecipeTransferError.Type.INTERNAL; }
@Override public void showError(int mouseX, int mouseY, IRecipeLayout recipeLayout, int recipeX, int recipeY) {}
};
static {
containerClasses.add(ContainerCraftingTerminal.class);
}

public CraftingTerminalTransferHandler(Class<? extends Container> containerClass) {
this.containerClass = containerClass;
}

@Override
public Class<? extends Container> getContainerClass() {
return containerClass;
}

@Override
public IRecipeTransferError transferRecipe(Container container, IRecipeLayout recipeLayout, PlayerEntity player, boolean maxTransfer, boolean doTransfer) {
if (container instanceof IJEIAutoFillTerminal) {
if (doTransfer) {
ItemStack[][] stacks = transferFunc.apply(recipeLayout);
CompoundNBT compound = new CompoundNBT();
ListNBT list = new ListNBT();
for (int i = 0;i < stacks.length;++i) {
if (stacks[i] != null) {
CompoundNBT CompoundNBT = new CompoundNBT();
CompoundNBT.putByte("s", (byte) i);
for (int j = 0;j < stacks[i].length && j < 3;j++) {
if (stacks[i][j] != null && !stacks[i][j].isEmpty()) {
CompoundNBT tag = new CompoundNBT();
stacks[i][j].write(tag);
CompoundNBT.put("i" + j, tag);
}
}
CompoundNBT.putByte("l", (byte) Math.min(3, stacks[i].length));
list.add(CompoundNBT);
}
}
compound.put("i", list);
((IJEIAutoFillTerminal) container).sendMessage(compound);
}
} else {
return ERROR_INSTANCE;
}
return null;
}

public static void registerTransferHandlers(IRecipeTransferRegistration recipeTransferRegistry) {
for (int i = 0;i < containerClasses.size();i++)
recipeTransferRegistry.addRecipeTransferHandler(new CraftingTerminalTransferHandler(containerClasses.get(i)), VanillaRecipeCategoryUid.CRAFTING);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.tom.storagemod.jei;

import net.minecraft.nbt.CompoundNBT;

public interface IJEIAutoFillTerminal {
void sendMessage(CompoundNBT compound);

}
38 changes: 38 additions & 0 deletions src/main/java/com/tom/storagemod/jei/JEIHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.tom.storagemod.jei;

import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;

import com.tom.storagemod.StorageMod;
import com.tom.storagemod.gui.GuiCraftingTerminal;

import mezz.jei.api.IModPlugin;
import mezz.jei.api.JeiPlugin;
import mezz.jei.api.constants.VanillaRecipeCategoryUid;
import mezz.jei.api.registration.IGuiHandlerRegistration;
import mezz.jei.api.registration.IRecipeCatalystRegistration;
import mezz.jei.api.registration.IRecipeTransferRegistration;

@JeiPlugin
public class JEIHandler implements IModPlugin {

@Override
public ResourceLocation getPluginUid() {
return new ResourceLocation(StorageMod.modid, "jei");
}

@Override
public void registerGuiHandlers(IGuiHandlerRegistration registration) {
registration.addRecipeClickArea(GuiCraftingTerminal.class, 83, 125, 28, 23, new ResourceLocation[] { VanillaRecipeCategoryUid.CRAFTING });
}

@Override
public void registerRecipeTransferHandlers(IRecipeTransferRegistration registration) {
CraftingTerminalTransferHandler.registerTransferHandlers(registration);
}

@Override
public void registerRecipeCatalysts(IRecipeCatalystRegistration registration) {
registration.addRecipeCatalyst(new ItemStack(StorageMod.craftingTerminal), new ResourceLocation[] { VanillaRecipeCategoryUid.CRAFTING });
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/tom/storagemod/network/DataPacket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.tom.storagemod.network;

import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.PacketBuffer;

public class DataPacket {
public CompoundNBT tag;

public DataPacket(CompoundNBT tag) {
this.tag = tag;
}

public DataPacket(PacketBuffer pb) {
tag = pb.readCompoundTag();
}

public void toBytes(PacketBuffer pb) {
pb.writeCompoundTag(tag);
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/tom/storagemod/network/IDataReceiver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.tom.storagemod.network;

import net.minecraft.nbt.CompoundNBT;

public interface IDataReceiver {
void receive(CompoundNBT tag);
}
56 changes: 56 additions & 0 deletions src/main/java/com/tom/storagemod/network/NetworkHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.tom.storagemod.network;

import java.util.function.Supplier;

import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.ResourceLocation;

import net.minecraftforge.fml.network.NetworkDirection;
import net.minecraftforge.fml.network.NetworkEvent;
import net.minecraftforge.fml.network.NetworkRegistry;
import net.minecraftforge.fml.network.PacketDistributor;
import net.minecraftforge.fml.network.simple.SimpleChannel;

import com.tom.storagemod.StorageMod;

public class NetworkHandler {
private static final String PROTOCOL_VERSION = "1";
public static final SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel(
new ResourceLocation(StorageMod.modid, "main"),
() -> PROTOCOL_VERSION,
PROTOCOL_VERSION::equals,
PROTOCOL_VERSION::equals
);
public static void init() {
INSTANCE.registerMessage(0, DataPacket.class, DataPacket::toBytes, DataPacket::new, NetworkHandler::handleData);
StorageMod.LOGGER.info("Initilaized Network Handler");
}

public static void handleData(DataPacket packet, Supplier<NetworkEvent.Context> ctx) {
if(ctx.get().getDirection() == NetworkDirection.PLAY_TO_SERVER) {
ctx.get().enqueueWork(() -> {
ServerPlayerEntity sender = ctx.get().getSender();
if(sender.openContainer instanceof IDataReceiver) {
((IDataReceiver)sender.openContainer).receive(packet.tag);
}
});
} else if(ctx.get().getDirection() == NetworkDirection.PLAY_TO_CLIENT) {
ctx.get().enqueueWork(() -> {
if(Minecraft.getInstance().currentScreen instanceof IDataReceiver) {
((IDataReceiver)Minecraft.getInstance().currentScreen).receive(packet.tag);
}
});
}
ctx.get().setPacketHandled(true);
}

public static void sendDataToServer(CompoundNBT tag) {
INSTANCE.sendToServer(new DataPacket(tag));
}

public static void sendTo(ServerPlayerEntity pl, CompoundNBT tag) {
INSTANCE.send(PacketDistributor.PLAYER.with(() -> pl), new DataPacket(tag));
}
}
Loading

0 comments on commit 68e969e

Please sign in to comment.