Skip to content
This repository has been archived by the owner on Aug 10, 2018. It is now read-only.

Commit

Permalink
Update AutoEatMod
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander01998 committed Apr 6, 2017
1 parent c878a2e commit 7e74fb5
Showing 1 changed file with 108 additions and 48 deletions.
156 changes: 108 additions & 48 deletions src/net/wurstclient/features/mods/AutoEatMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@
*/
package net.wurstclient.features.mods;

import net.minecraft.block.BlockContainer;
import net.minecraft.entity.Entity;
import net.minecraft.entity.passive.EntityTameable;
import net.minecraft.entity.passive.EntityVillager;
import net.minecraft.item.ItemFood;
import net.minecraft.item.ItemStack;
import net.wurstclient.compatibility.WBlock;
import net.wurstclient.compatibility.WMinecraft;
import net.wurstclient.compatibility.WPlayerController;
import net.wurstclient.events.listeners.UpdateListener;
import net.wurstclient.features.Feature;
import net.wurstclient.features.special_features.YesCheatSpf.BypassLevel;
import net.wurstclient.settings.CheckboxSetting;

@Mod.Info(description = "Automatically eats food when necessary.",
name = "AutoEat",
Expand All @@ -21,91 +28,144 @@
@Mod.Bypasses
public final class AutoEatMod extends Mod implements UpdateListener
{
private int oldSlot;
private int bestSlot;
public CheckboxSetting ignoreScreen =
new CheckboxSetting("Ignore screen", true);

private int oldSlot = -1;

@Override
public Feature[] getSeeAlso()
{
return new Feature[]{wurst.mods.autoSoupMod};
}

@Override
public void initSettings()
{
settings.add(ignoreScreen);
}

@Override
public void onEnable()
{
oldSlot = -1;
wurst.events.add(UpdateListener.class, this);
}

@Override
public void onDisable()
{
wurst.events.remove(UpdateListener.class, this);
stopIfEating();
}

@Override
public void onUpdate()
{
if(oldSlot != -1 || WMinecraft.getPlayer().capabilities.isCreativeMode
|| WMinecraft.getPlayer().getFoodStats().getFoodLevel() >= 20)
if(!shouldEat())
{
stopIfEating();
return;
float bestSaturation = 0F;
bestSlot = -1;
}

// search food in hotbar
int bestSlot = -1;
float bestSaturation = -1;
for(int i = 0; i < 9; i++)
{
ItemStack item = WMinecraft.getPlayer().inventory.getStackInSlot(i);
if(item == null)
// filter out non-food items
ItemStack stack =
WMinecraft.getPlayer().inventory.getStackInSlot(i);
if(stack == null || !(stack.getItem() instanceof ItemFood))
continue;
float saturation = 0;
if(item.getItem() instanceof ItemFood)
saturation =
((ItemFood)item.getItem()).getSaturationModifier(item);

// compare to previously found food
float saturation =
((ItemFood)stack.getItem()).getSaturationModifier(stack);
if(saturation > bestSaturation)
{
bestSaturation = saturation;
bestSlot = i;
}
}

// check if any food was found
if(bestSlot == -1)
{
stopIfEating();
return;
oldSlot = WMinecraft.getPlayer().inventory.currentItem;
wurst.events.add(UpdateListener.class, new UpdateListener()
}

// save old slot
if(!isEating())
oldSlot = WMinecraft.getPlayer().inventory.currentItem;

// set slot
WMinecraft.getPlayer().inventory.currentItem = bestSlot;

// eat food
mc.gameSettings.keyBindUseItem.pressed = true;
WPlayerController.processRightClick();
}

@Override
public void onYesCheatUpdate(BypassLevel bypassLevel)
{
switch(bypassLevel)
{
@Override
public void onUpdate()
{
if(!AutoEatMod.this.isActive()
|| WMinecraft.getPlayer().capabilities.isCreativeMode
|| WMinecraft.getPlayer().getFoodStats()
.getFoodLevel() >= 20)
{
stop();
return;
}
ItemStack item =
WMinecraft.getPlayer().inventory.getStackInSlot(bestSlot);
if(item == null || !(item.getItem() instanceof ItemFood))
{
stop();
return;
}
WMinecraft.getPlayer().inventory.currentItem = bestSlot;
WPlayerController.processRightClick();
mc.gameSettings.keyBindUseItem.pressed = true;
}
case GHOST_MODE:
ignoreScreen.lock(() -> false);
break;

private void stop()
{
mc.gameSettings.keyBindUseItem.pressed = false;
WMinecraft.getPlayer().inventory.currentItem = oldSlot;
oldSlot = -1;
wurst.events.remove(UpdateListener.class, this);
}
});
default:
ignoreScreen.unlock();
break;
}
}

@Override
public void onDisable()
private boolean shouldEat()
{
wurst.events.remove(UpdateListener.class, this);
// check hunger
if(!WMinecraft.getPlayer().canEat(false))
return false;

// check screen
if(!ignoreScreen.isChecked() && mc.currentScreen != null)
return false;

// check for clickable objects
if(mc.currentScreen == null && mc.objectMouseOver != null)
{
// clickable entities
Entity entity = mc.objectMouseOver.entityHit;
if(entity instanceof EntityVillager
|| entity instanceof EntityTameable)
return false;

// clickable blocks
if(mc.objectMouseOver.getBlockPos() != null && WBlock.getBlock(
mc.objectMouseOver.getBlockPos()) instanceof BlockContainer)
return false;
}

return true;
}

public boolean isEating()
{
return oldSlot != -1;
}

private void stopIfEating()
{
// check if eating
if(!isEating())
return;

// stop eating
mc.gameSettings.keyBindUseItem.pressed = false;

// reset slot
WMinecraft.getPlayer().inventory.currentItem = oldSlot;
oldSlot = -1;
}
}

0 comments on commit 7e74fb5

Please sign in to comment.