Skip to content

Commit

Permalink
Update for 1.11 and Fix #5
Browse files Browse the repository at this point in the history
  • Loading branch information
GotoLink committed Dec 23, 2016
1 parent 70587b1 commit 344bae5
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 75 deletions.
24 changes: 16 additions & 8 deletions assets/recipehandler/ChangePacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
Expand All @@ -12,16 +13,18 @@
import net.minecraftforge.fml.common.network.internal.FMLProxyPacket;
import net.minecraftforge.fml.relauncher.Side;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;

public final class ChangePacket {
public final static String CHANNEL = "recipemod:key";
private ItemStack itemstack;
private ItemStack itemstack = ItemStack.EMPTY;
private int slot;
private int index;
public ChangePacket(){}
public ChangePacket(int slot, ItemStack stack, int recipeIndex) {
public ChangePacket(int slot, @Nonnull ItemStack stack, int recipeIndex) {
this.slot = slot;
this.itemstack = stack;
this.index = recipeIndex;
Expand All @@ -40,17 +43,18 @@ public void toBytes(ByteBuf buf) {
buf.writeInt(index);
}

@Nullable
ChangePacket handle(final EntityPlayerMP player) {
if(itemstack != null && slot >= 0 && index >= 0) {
if(!itemstack.isEmpty() && slot >= 0 && index >= 0) {
ListenableFuture<ChangePacket> future = player.mcServer.callFromMainThread(new Callable<ChangePacket>() {
@Override
public ChangePacket call() {
InventoryCrafting crafting = CraftingHandler.getCraftingMatrix(player.openContainer);
if(crafting!=null) {
CraftingHandler.setRecipeIndex(index);
ItemStack itr = CraftingHandler.findMatchingRecipe(crafting, player.worldObj);
ItemStack itr = CraftingHandler.findMatchingRecipe(crafting, player.getEntityWorld());
if(ItemStack.areItemStacksEqual(itr, itemstack)) {
Slot result = CraftingHandler.getResultSlot(player.openContainer, slot);
Slot result = CraftingHandler.getResultSlot(player.openContainer, crafting, slot);
if (result != null) {
result.putStack(itemstack.copy());
return new ChangePacket(slot, itemstack, index);
Expand Down Expand Up @@ -83,9 +87,13 @@ public Runnable getRun(){
return new Runnable() {
@Override
public void run() {
Slot result = CraftingHandler.getResultSlot(RecipeMod.registry.getPlayer().openContainer, slot);
if (result != null) {
result.putStack(itemstack);
Container container = RecipeMod.registry.getPlayer().openContainer;
InventoryCrafting crafting = CraftingHandler.getCraftingMatrix(container);
if(crafting!=null) {
Slot result = CraftingHandler.getResultSlot(container, crafting, slot);
if (result != null) {
result.putStack(itemstack);
}
}
}
};
Expand Down
38 changes: 19 additions & 19 deletions assets/recipehandler/ClientEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

public final class ClientEventHandler implements RecipeMod.IRegister{
private KeyBinding key;
private ItemStack oldItem = null;
private ItemStack oldItem = ItemStack.EMPTY;
private boolean pressed = false;

@Override
Expand Down Expand Up @@ -77,23 +77,23 @@ public void keyDown(TickEvent.ClientTickEvent event) {
Slot result = null;
if(FMLClientHandler.instance().getClient().currentScreen instanceof GuiContainer){
Slot slot = ((GuiContainer) FMLClientHandler.instance().getClient().currentScreen).getSlotUnderMouse();
if(slot == null) {
if(!(slot instanceof SlotCrafting))
return;
}else if(slot instanceof SlotCrafting){
result = slot;
}
}
if(result == null) {
result = CraftingHandler.getResultSlot(getPlayer().openContainer, 0);
result = slot;
}
if(result != null){
InventoryCrafting craft = CraftingHandler.getCraftingMatrix(getPlayer().openContainer);
if(craft != null){
ItemStack res = CraftingHandler.findMatchingRecipe(craft, getWorld());
if(res != null && !ItemStack.areItemStacksEqual(res, result.getStack())){
RecipeMod.networkWrapper.sendToServer(new ChangePacket(result.slotNumber, res, CraftingHandler.getRecipeIndex()).toProxy(Side.SERVER));
InventoryCrafting craft = CraftingHandler.getCraftingMatrix(getPlayer().openContainer);
if(craft != null){
if(result == null) {
result = CraftingHandler.getResultSlot(getPlayer().openContainer, craft, 0);
}
if(result != null){
ItemStack res = CraftingHandler.findMatchingRecipe(craft, getWorld());
if (res.isEmpty()){
oldItem = ItemStack.EMPTY;
} else if(!ItemStack.areItemStacksEqual(res, result.getStack())){
RecipeMod.NETWORK.sendToServer(new ChangePacket(result.slotNumber, res, CraftingHandler.getRecipeIndex()).toProxy(Side.SERVER));
oldItem = res;
}
}
}
}
}
Expand All @@ -104,14 +104,14 @@ public void pressed() {
InventoryCrafting craft = CraftingHandler.getCraftingMatrix(getPlayer().openContainer);
if (craft != null) {
ItemStack res = CraftingHandler.findNextMatchingRecipe(craft, getWorld());
if (res == null){
oldItem = null;
if (res.isEmpty()){
oldItem = ItemStack.EMPTY;
} else if (!ItemStack.areItemStacksEqual(res, oldItem)) {
int index = 0;
Slot slot = CraftingHandler.getResultSlot(getPlayer().openContainer, index);
Slot slot = CraftingHandler.getResultSlot(getPlayer().openContainer, craft, index);
if(slot!= null)
index = slot.slotNumber;
RecipeMod.networkWrapper.sendToServer(new ChangePacket(index, res, CraftingHandler.getRecipeIndex()).toProxy(Side.SERVER));
RecipeMod.NETWORK.sendToServer(new ChangePacket(index, res, CraftingHandler.getRecipeIndex()).toProxy(Side.SERVER));
oldItem = res;
}
}
Expand Down
46 changes: 27 additions & 19 deletions assets/recipehandler/CraftingHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,25 @@
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.ReflectionHelper;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.lang.reflect.Field;
import java.util.*;

public final class CraftingHandler {
private static HashMap<String, Field> knownCraftingContainer;
private static HashSet<String> notCraftingContainer;
private static Field slotCraftInv;
private static int previousNumberOfCraft;
private static int delayTimer = 10;
private static int recipeIndex;

public static void enableGuessing(){
knownCraftingContainer = new HashMap<String, Field>();
notCraftingContainer = new HashSet<String>();
slotCraftInv = ReflectionHelper.findField(SlotCrafting.class, "field_75239_a", "craftMatrix");
}

public static int getRecipeIndex(){
Expand All @@ -31,6 +36,7 @@ public static void setRecipeIndex(int id){
}
}

@Nullable
public static InventoryCrafting getCraftingMatrix(Container container){
if(container == null)
return null;
Expand All @@ -39,6 +45,11 @@ else if (container instanceof ContainerPlayer)
else if (container instanceof ContainerWorkbench)
return ((ContainerWorkbench) container).craftMatrix;
else if(notCraftingContainer!=null){
for (Slot slot : container.inventorySlots) {
if (slot!=null && slot.inventory instanceof InventoryCrafting){
return (InventoryCrafting) slot.inventory;
}
}
String name = container.getClass().getName();
if (!notCraftingContainer.contains(name)) {
Field f = knownCraftingContainer.get(name);
Expand Down Expand Up @@ -69,6 +80,7 @@ else if(notCraftingContainer!=null){
return null;
}

@Nonnull
public static ItemStack findNextMatchingRecipe(InventoryCrafting craft, World world) {
if (recipeIndex == Integer.MAX_VALUE) {
recipeIndex = 0;
Expand All @@ -78,11 +90,12 @@ public static ItemStack findNextMatchingRecipe(InventoryCrafting craft, World wo
return findMatchingRecipe(craft, world);
}

@Nonnull
public static ItemStack findMatchingRecipe(InventoryCrafting craft, World world) {
if (CraftingManager.getInstance().findMatchingRecipe(craft, world) != null) {
if (!CraftingManager.getInstance().findMatchingRecipe(craft, world).isEmpty()) {
List<ItemStack> result = getCraftResult(craft, world);
if (result.size() == 0) {
return null;
return ItemStack.EMPTY;
}
if (recipeIndex < 0) {
int j1 = -recipeIndex;
Expand All @@ -96,7 +109,7 @@ public static ItemStack findMatchingRecipe(InventoryCrafting craft, World world)
return result.get(recipeIndex % result.size());
}
}
return null;
return ItemStack.EMPTY;
}

public static List<ItemStack> getCraftResult(InventoryCrafting craft, World world) {
Expand All @@ -111,29 +124,24 @@ public static List<ItemStack> getCraftResult(InventoryCrafting craft, World worl
return arraylist;
}

public static Slot getResultSlot(Container container, int index){
@Nullable
public static Slot getResultSlot(Container container, InventoryCrafting inventory, int index){
if(container == null)
return null;
else {
else if(index < container.inventorySlots.size()){
Slot slot = container.getSlot(index);
if(slot instanceof SlotCrafting)
return slot;
}
if(notCraftingContainer!=null){
for(Field field:container.getClass().getDeclaredFields()){
if(field != null){
try {
field.setAccessible(true);
Object result = field.get(container);
if (result instanceof IInventory && ((IInventory) result).getSizeInventory() > 0) {
for(Slot slot: container.inventorySlots){
if(slot instanceof SlotCrafting && slot.inventory == result)
return slot;
}
}
}catch (Exception ignored){}
if(slotCraftInv != null){
try {
for (Slot slot : container.inventorySlots) {
if (slot instanceof SlotCrafting) {
if (inventory == slotCraftInv.get(slot))
return slot;
}
}
}
}catch (Exception ignored){}
}
return null;
}
Expand Down
7 changes: 4 additions & 3 deletions assets/recipehandler/GuiEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void onPostInitGui(GuiScreenEvent.InitGuiEvent.Post event){
if(event.getGui() instanceof GuiContainer){
InventoryCrafting craft = CraftingHandler.getCraftingMatrix(((GuiContainer) event.getGui()).inventorySlots);
if (craft != null){
int guiLeft = (event.getGui().width + 176) / 2 + deltaX;
int guiLeft = (event.getGui().width + ((GuiContainer) event.getGui()).getXSize()) / 2 + deltaX;
int guiTop = (event.getGui().height) / 2;
event.getButtonList().add(new CreativeButton(event.getButtonList().size() + 2, guiLeft + RecipeMod.xOffset, guiTop + RecipeMod.yOffset));
}
Expand All @@ -46,8 +46,9 @@ public CreativeButton(int id, int posX, int posY){
@Override
public void drawButton(Minecraft mc, int mouseX, int mouseY){
if (this.visible) {
displayString = String.valueOf(CraftingHandler.getNumberOfCraft(mc.thePlayer.openContainer, mc.theWorld));
enabled = !("0".equals(displayString));
int crafts = CraftingHandler.getNumberOfCraft(mc.player.openContainer, mc.world);
displayString = String.valueOf(crafts);
enabled = crafts > 1;
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
mc.renderEngine.bindTexture(this.texture);
int k = 176;
Expand Down
23 changes: 14 additions & 9 deletions assets/recipehandler/RecipeMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.ModContainer;
import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.network.FMLEventChannel;
import net.minecraftforge.fml.common.network.NetworkRegistry;
Expand All @@ -21,16 +22,12 @@ public final class RecipeMod {
@SidedProxy(clientSide = "assets.recipehandler.ClientEventHandler", serverSide = "assets.recipehandler.PacketHandler")
public static IRegister registry;
private static final boolean debug = false;
public static FMLEventChannel networkWrapper;
public static FMLEventChannel NETWORK;
public static boolean switchKey = false, cycleButton = true, cornerText = false;
public static int xOffset = 0, yOffset = 0;

@EventHandler
public void loading(FMLPreInitializationEvent event) {
if (debug) {
GameRegistry.addShapelessRecipe(new ItemStack(Items.GOLDEN_APPLE), Blocks.PLANKS, Items.STICK);
GameRegistry.addShapelessRecipe(new ItemStack(Items.APPLE), Blocks.PLANKS, Items.STICK);
}
public void preloading(FMLPreInitializationEvent event) {
if (event.getSide().isClient()) {
if(event.getSourceFile().getName().endsWith(".jar")){
try {
Expand Down Expand Up @@ -61,11 +58,19 @@ public void loading(FMLPreInitializationEvent event) {
if(config.hasChanged())
config.save();
}catch (Throwable ignored){}
registry.register();
networkWrapper = NetworkRegistry.INSTANCE.newEventDrivenChannel(ChangePacket.CHANNEL);
networkWrapper.register(new PacketHandler());
NETWORK = NetworkRegistry.INSTANCE.newEventDrivenChannel(ChangePacket.CHANNEL);
NETWORK.register(new PacketHandler());
}

@EventHandler
public void loading(FMLInitializationEvent event) {
registry.register();
if (debug) {
GameRegistry.addShapelessRecipe(new ItemStack(Items.GOLDEN_APPLE), Blocks.PLANKS, Items.STICK);
GameRegistry.addShapelessRecipe(new ItemStack(Items.APPLE), Blocks.PLANKS, Items.STICK);
}
}

interface IRegister{
void register();
EntityPlayer getPlayer();
Expand Down
24 changes: 10 additions & 14 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
buildscript {
repositories {
jcenter()
maven {
name = "forge"
url = "http://files.minecraftforge.net/maven"
}
maven { url = "http://files.minecraftforge.net/maven" }
}
dependencies {
classpath 'net.minecraftforge.gradle:ForgeGradle:2.2-SNAPSHOT'
}
}
apply plugin: 'net.minecraftforge.gradle.forge'

/*plugins {
id "net.minecraftforge.gradle.forge" version "2.0.2"
}*/
sourceCompatibility = 1.6
targetCompatibility = 1.6
archivesBaseName = "NoMoreRecipeConflict"
version = "0.6"
sourceCompatibility = targetCompatibility = "1.6"
compileJava {
sourceCompatibility = targetCompatibility = "1.6"
}
minecraft {
version = "1.10.2-12.18.1.2073"
version = "1.11-13.19.1.2198"
runDir = "eclipse"
mappings = "snapshot_20160901"
mappings = "snapshot_20161221"
replace '$version', project.version
replace '$mcversion', "[1.9,)"
replace '$mcversion', "[1.11,)"
makeObfSourceJar = false
}
sourceSets.main.java{srcDirs '.' include('assets/')}
Expand All @@ -35,7 +31,7 @@ processResources {
// replace stuff in mcmod.info
from(['mcmod.info', 'pack.mcmeta']) {
// replace version and mcversion
expand([version:project.version, mcversion: "[1.9,)"])
expand([version:project.version, mcversion: "[1.11,)"])
}
from(fileTree('.').matching{ include('assets/**/*.lang')})
}
Expand All @@ -45,4 +41,4 @@ jar{
//Append with minecraft version
classifier = "("+minecraft.version+")"
archiveName = archivesBaseName + "-" + version + classifier+"."+extension
}
}
3 changes: 3 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Sets default memory used for gradle commands. Can be overridden by user or command line properties.
# This is required to provide enough memory for the Minecraft decompilation process.
org.gradle.jvmargs=-Xmx3G
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sun Oct 16 22:07:40 CEST 2016
#Mon Sep 14 12:28:28 PDT 2015
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.7-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14-bin.zip
Loading

0 comments on commit 344bae5

Please sign in to comment.