Skip to content

Commit

Permalink
Followup on previous PR (#372)
Browse files Browse the repository at this point in the history
* requireBagSpace now allows inventory space

* Added nl_NL translation. (updated en_US to fit previous commit)

* Fully tested and fixed some bugs

* Forgot about Bittank

* Fixed previous commit

* Simplified space calculation methods

* Method-ified certain identical pieces of code
  • Loading branch information
MineDaveXD authored and AlgorithmX2 committed Feb 18, 2018
1 parent 3f22985 commit 720f2cc
Show file tree
Hide file tree
Showing 12 changed files with 469 additions and 193 deletions.
Expand Up @@ -25,6 +25,7 @@
import mod.chiselsandbits.helpers.BitOperation;
import mod.chiselsandbits.helpers.DeprecationHelper;
import mod.chiselsandbits.helpers.ModUtil;
import mod.chiselsandbits.helpers.BitInventoryFeeder;
import mod.chiselsandbits.integration.mcmultipart.MCMultipartProxy;
import mod.chiselsandbits.items.ItemBitBag;
import mod.chiselsandbits.items.ItemChisel;
Expand Down Expand Up @@ -295,7 +296,8 @@ public void giveBitToPlayer(
return;
}

ModUtil.feedPlayer( player.getEntityWorld(), player, ei );
BitInventoryFeeder feeder = new BitInventoryFeeder( player, player.getEntityWorld() );
feeder.addItem(ei);
return;
}
else if ( !player.inventory.addItemStackToInventory( stack ) )
Expand Down
132 changes: 132 additions & 0 deletions src/main/java/mod/chiselsandbits/helpers/BitInventoryFeeder.java
@@ -0,0 +1,132 @@
package mod.chiselsandbits.helpers;

import java.util.*;

import mod.chiselsandbits.core.ChiselsAndBits;
import mod.chiselsandbits.items.ItemBitBag;
import mod.chiselsandbits.items.ItemChiseledBit;
import mod.chiselsandbits.items.ItemBitBag.BagPos;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.ItemStack;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.EntityItemPickupEvent;
import net.minecraftforge.fml.common.eventhandler.Event.Result;

public class BitInventoryFeeder
{
private final static Random itemRand = new Random();
ArrayList<Integer> seenBits = new ArrayList<>();
boolean hasSentMessage = false;
final EntityPlayer player;
final World world;

public BitInventoryFeeder(
final EntityPlayer p,
final World w)
{
player = p;
world = w;
}

public void addItem(
final EntityItem ei)
{
ItemStack is = ModUtil.nonNull( ei.getEntityItem() );

final List<BagPos> bags = ItemBitBag.getBags( player.inventory );

if ( !ModUtil.containsAtLeastOneOf( player.inventory, is ) )
{
final ItemStack minSize = is.copy();

if ( ModUtil.getStackSize( minSize ) > minSize.getMaxStackSize() )
{
ModUtil.setStackSize( minSize, minSize.getMaxStackSize() );
}

ModUtil.adjustStackSize( is, -ModUtil.getStackSize( minSize ) );
player.inventory.addItemStackToInventory( minSize );
ModUtil.adjustStackSize( is, ModUtil.getStackSize( minSize ) );
}

for ( final BagPos bp : bags )
{
is = bp.inv.insertItem( is );
}

if ( ModUtil.isEmpty( is ) )
return;

ei.setEntityItemStack( is );
EntityItemPickupEvent event = new EntityItemPickupEvent( player, ei );

if ( MinecraftForge.EVENT_BUS.post( event ) )
{
// cancelled...
spawnItem( world, ei );
}
else
{
if ( event.getResult() != Result.DENY )
{
is = ei.getEntityItem();

if ( is != null && !player.inventory.addItemStackToInventory( is ) )
{
ei.setEntityItemStack( is );
//Never spawn the items for dropped excess items if setting is enabled.
if ( !ChiselsAndBits.getConfig().voidExcessBits )
{
spawnItem( world, ei );
}
}
else
{
if ( !ei.isSilent() )
{
ei.worldObj.playSound( (EntityPlayer) null, ei.posX, ei.posY, ei.posZ, SoundEvents.ENTITY_ITEM_PICKUP, SoundCategory.PLAYERS, 0.2F, ( ( itemRand.nextFloat() - itemRand.nextFloat() ) * 0.7F + 1.0F ) * 2.0F );
}
}

player.inventory.markDirty();

if ( player.inventoryContainer != null )
{
player.inventoryContainer.detectAndSendChanges();
}

}
else
spawnItem( world, ei );
}

final int blk = ItemChiseledBit.getStackState( is );
if ( ChiselsAndBits.getConfig().voidExcessBits && !seenBits.contains(blk) && !hasSentMessage )
{
if ( !ItemChiseledBit.hasBitSpace( player, blk ) )
{
player.addChatMessage( new TextComponentTranslation( "mod.chiselsandbits.result.void_excess" ) );
hasSentMessage = true;
}
if ( !seenBits.contains( blk ))
{
seenBits.add( blk );
}
}
}

private static void spawnItem(
World world,
EntityItem ei )
{
if ( world.isRemote ) // no spawning items on the client.
return;

world.spawnEntityInWorld( ei );
}
}
95 changes: 0 additions & 95 deletions src/main/java/mod/chiselsandbits/helpers/ModUtil.java
Expand Up @@ -19,18 +19,15 @@
import mod.chiselsandbits.integration.mcmultipart.MCMultipartProxy;
import mod.chiselsandbits.integration.mods.LittleTiles;
import mod.chiselsandbits.items.ItemBitBag;
import mod.chiselsandbits.items.ItemBitBag.BagPos;
import mod.chiselsandbits.items.ItemChiseledBit;
import mod.chiselsandbits.items.ItemNegativePrint;
import mod.chiselsandbits.items.ItemPositivePrint;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Blocks;
import net.minecraft.init.SoundEvents;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
Expand All @@ -39,17 +36,13 @@
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumFacing.Axis;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.ChunkCache;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.EntityItemPickupEvent;
import net.minecraftforge.fml.common.eventhandler.Event.Result;

public class ModUtil
{
Expand Down Expand Up @@ -361,94 +354,6 @@ public static void removeChisledBlock(
world.markBlockRangeForRenderUpdate( pos, pos );
}

private final static Random itemRand = new Random();

public static void feedPlayer(
final World world,
final EntityPlayer player,
final EntityItem ei )
{
ItemStack is = ModUtil.nonNull( ei.getEntityItem() );

final List<BagPos> bags = ItemBitBag.getBags( player.inventory );

if ( !containsAtLeastOneOf( player.inventory, is ) )
{
final ItemStack minSize = is.copy();

if ( getStackSize( minSize ) > minSize.getMaxStackSize() )
{
setStackSize( minSize, minSize.getMaxStackSize() );
}

adjustStackSize( is, -getStackSize( minSize ) );
player.inventory.addItemStackToInventory( minSize );
adjustStackSize( is, getStackSize( minSize ) );
}

for ( final BagPos bp : bags )
{
is = bp.inv.insertItem( is );
}

if ( ModUtil.isEmpty( is ) )
return;

ei.setEntityItemStack( is );
EntityItemPickupEvent event = new EntityItemPickupEvent( player, ei );

if ( MinecraftForge.EVENT_BUS.post( event ) )
{
// canceled...
spawnItem( world, ei );
}
else
{
if ( event.getResult() != Result.DENY )
{
is = ei.getEntityItem();

if ( is != null && !player.inventory.addItemStackToInventory( is ) )
{
ei.setEntityItemStack( is );
//Never spawn the items for dropped excess items if setting is enabled.
if ( !ChiselsAndBits.getConfig().voidExcessBits )
{
spawnItem( world, ei );
}
}
else
{
if ( !ei.isSilent() )
{
ei.worldObj.playSound( (EntityPlayer) null, ei.posX, ei.posY, ei.posZ, SoundEvents.ENTITY_ITEM_PICKUP, SoundCategory.PLAYERS, 0.2F, ( ( itemRand.nextFloat() - itemRand.nextFloat() ) * 0.7F + 1.0F ) * 2.0F );
}
}

player.inventory.markDirty();

if ( player.inventoryContainer != null )
{
player.inventoryContainer.detectAndSendChanges();
}

}
else
spawnItem( world, ei );
}

}

private static void spawnItem(
World world,
EntityItem ei )
{
if ( world.isRemote ) // no spawning items on the client.
return;

world.spawnEntityInWorld( ei );
}

public static boolean containsAtLeastOneOf(
final IInventory inv,
final ItemStack is )
Expand Down
20 changes: 0 additions & 20 deletions src/main/java/mod/chiselsandbits/items/ItemBitBag.java
Expand Up @@ -318,24 +318,4 @@ public double getDurabilityForDisplay(

return 0;
}

public static boolean hasBagSpace(
final EntityPlayer player,
final int blk )
{
final List<BagPos> bags = getBags( player.inventory );
for ( final BagPos bp : bags )
{
for ( int x = 0; x < bp.inv.getSizeInventory(); x++ )
{
final ItemStack is = bp.inv.getStackInSlot( x );
if( ( ItemChiseledBit.sameBit( is, blk ) && ModUtil.getStackSize( is ) < bp.inv.getInventoryStackLimit() ) || ModUtil.isEmpty( is ) )
{
return true;
}
}
}
return false;
}

}
20 changes: 2 additions & 18 deletions src/main/java/mod/chiselsandbits/items/ItemChisel.java
Expand Up @@ -128,9 +128,6 @@ public boolean onBlockStartBreak(
return ItemChisel.fromBreakToChisel( ChiselMode.castMode( ChiselModeManager.getChiselMode( player, ChiselToolType.CHISEL, EnumHand.MAIN_HAND ) ), itemstack, pos, player, EnumHand.MAIN_HAND );
}

//The previous stateId, avoids spamming the require_bag message.
private static BlockPos lastPos = new BlockPos(0, -1, 0);

static public boolean fromBreakToChisel(
final ChiselMode mode,
final ItemStack itemstack,
Expand All @@ -139,21 +136,8 @@ static public boolean fromBreakToChisel(
final EnumHand hand )
{
final IBlockState state = player.getEntityWorld().getBlockState( pos );
if ( ChiselsAndBits.getConfig().requireBagSpace && !player.isCreative() )
{
//Cycle every item in any bag, if the player can't store the clicked block then
//send them a message.
final int stateId = ModUtil.getStateId( state );
if ( !ItemBitBag.hasBagSpace( player, stateId ) )
{
if( player.worldObj.isRemote && !pos.equals( lastPos ) )
{
//Only client should handle messaging.
player.addChatMessage( new TextComponentTranslation( "mod.chiselsandbits.result.require_bag" ) );
lastPos = pos;
}
return false;
}
if ( ItemChiseledBit.checkRequiredSpace( player, state ) ) {
return false;
}
if ( BlockBitInfo.canChisel( state ) || MCMultipartProxy.proxyMCMultiPart.isMultiPartTileEntity( player.getEntityWorld(), pos ) || LittleTiles.isLittleTilesBlock( player.getEntityWorld().getTileEntity( pos ) ) )
{
Expand Down

0 comments on commit 720f2cc

Please sign in to comment.