Skip to content

Commit

Permalink
Sort/Combine/Condense Button for Bit Bag. Implements #125
Browse files Browse the repository at this point in the history
  • Loading branch information
AlgorithmX2 committed Nov 10, 2018
1 parent 506c988 commit 128606a
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/main/java/mod/chiselsandbits/bitbag/BagContainer.java
Expand Up @@ -369,4 +369,10 @@ public void clear(
( (EntityPlayerMP) thePlayer ).sendContainerToPlayer( this );
}

public void sort()
{
bagInv.sort();
( (EntityPlayerMP) thePlayer ).sendContainerToPlayer( this );
}

}
15 changes: 15 additions & 0 deletions src/main/java/mod/chiselsandbits/bitbag/BagGui.java
Expand Up @@ -11,6 +11,7 @@
import mod.chiselsandbits.network.NetworkRouter;
import mod.chiselsandbits.network.packets.PacketBagGui;
import mod.chiselsandbits.network.packets.PacketClearBagGui;
import mod.chiselsandbits.network.packets.PacketSortBagGui;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.inventory.GuiContainer;
Expand All @@ -32,6 +33,7 @@ public class BagGui extends GuiContainer

private static GuiBagFontRenderer specialFontRenderer = null;
private GuiIconButton trashBtn;
private GuiIconButton sortBtn;

public BagGui(
final EntityPlayer player,
Expand All @@ -52,6 +54,7 @@ public void initGui()
super.initGui();

buttonList.add( trashBtn = new GuiIconButton( 1, guiLeft - 18, guiTop + 0, "help.trash", ClientSide.trashIcon ) );
buttonList.add( sortBtn = new GuiIconButton( 1, guiLeft - 18, guiTop + 18, "help.sort", ClientSide.sortIcon ) );
}

BagContainer getBagContainer()
Expand Down Expand Up @@ -193,6 +196,13 @@ protected void drawGuiContainerForegroundLayer(
}
}

if ( sortBtn.isMouseOver() )
{
final List<String> text = Arrays
.asList( new String[] { LocalStrings.Sort.getLocal() } );
drawHoveringText( text, mouseX - guiLeft, mouseY - guiTop, fontRendererObj );
}

if ( trashBtn.isMouseOver() )
{
if ( isValidBitItem() )
Expand Down Expand Up @@ -229,6 +239,11 @@ private ItemStack getInHandItem()
protected void actionPerformed(
final GuiButton button ) throws IOException
{
if ( button == sortBtn )
{
NetworkRouter.instance.sendToServer( new PacketSortBagGui() );
}

if ( button == trashBtn )
{
if ( requireConfirm )
Expand Down
96 changes: 96 additions & 0 deletions src/main/java/mod/chiselsandbits/bitbag/BagInventory.java
Expand Up @@ -245,6 +245,101 @@ public int getFieldCount()
return 0;
}

private static class StateQtyPair
{
public StateQtyPair(
int state,
int qty )
{
this.qty = qty;
this.state = state;
}

int qty;
int state;
};

public void sort()
{
List<StateQtyPair> stacks = new ArrayList<StateQtyPair>();

for ( int x = 0; x < stackSlots.length; ++x )
{
int state = inv.contents[x * ItemBitBag.INTS_PER_BIT_TYPE + ItemBitBag.OFFSET_STATE_ID];
int qty = inv.contents[x * ItemBitBag.INTS_PER_BIT_TYPE + ItemBitBag.OFFSET_QUANTITY];

if ( state > 0 && qty > 0 )
{
stacks.add( new StateQtyPair( state, qty ) );
}
}

stacks.sort( new Comparator<StateQtyPair>() {

@Override
public int compare(
StateQtyPair o1,
StateQtyPair o2 )
{
if ( o1.state < o2.state )
return 1;

if ( o1.state > o2.state )
return -1;

if ( o1.qty < o2.qty )
return 1;

if ( o1.qty > o2.qty )
return -1;

return 0;
}
} );

for ( int x = 0; x < stacks.size() - 1; x++ )
{
StateQtyPair a = stacks.get( x );
StateQtyPair b = stacks.get( x + 1 );

if ( a.state == b.state )
{
if ( a.qty < getInventoryStackLimit() )
{
int shiftSize = getInventoryStackLimit() - a.qty;
shiftSize = Math.min( shiftSize, b.qty );

a.qty += shiftSize;
b.qty -= shiftSize;

if ( b.qty <= 0 )
stacks.remove( x + 1 );

--x;
}
}
}

for ( int x = 0; x < stackSlots.length; ++x )
{
int state = 0;
int qty = 0;

if ( stacks.size() > x )
{
state = stacks.get( x ).state;
qty = stacks.get( x ).qty;
}

inv.contents[x * ItemBitBag.INTS_PER_BIT_TYPE + ItemBitBag.OFFSET_STATE_ID] = state;
inv.contents[x * ItemBitBag.INTS_PER_BIT_TYPE + ItemBitBag.OFFSET_QUANTITY] = qty;

stackSlots[x] = ModUtil.getEmptyStack();
}

inv.onChange();
}

public void clear(
final ItemStack stack )
{
Expand Down Expand Up @@ -472,4 +567,5 @@ public boolean func_191420_l()

return true;
}

}
3 changes: 3 additions & 0 deletions src/main/java/mod/chiselsandbits/core/ClientSide.java
Expand Up @@ -399,6 +399,8 @@ public Map<IBlockState, ModelResourceLocation> putStateModelLocations(
public static TextureAtlasSprite redoIcon;
public static TextureAtlasSprite trashIcon;

public static TextureAtlasSprite sortIcon;

public static TextureAtlasSprite swapIcon;
public static TextureAtlasSprite placeIcon;

Expand All @@ -415,6 +417,7 @@ void registerIconTextures(
undoIcon = map.registerSprite( new ResourceLocation( "chiselsandbits", "icons/undo" ) );
redoIcon = map.registerSprite( new ResourceLocation( "chiselsandbits", "icons/redo" ) );
trashIcon = map.registerSprite( new ResourceLocation( "chiselsandbits", "icons/trash" ) );
sortIcon = map.registerSprite( new ResourceLocation( "chiselsandbits", "icons/sort" ) );
roll_x = map.registerSprite( new ResourceLocation( "chiselsandbits", "icons/roll_x" ) );
roll_z = map.registerSprite( new ResourceLocation( "chiselsandbits", "icons/roll_z" ) );

Expand Down
1 change: 1 addition & 0 deletions src/main/java/mod/chiselsandbits/helpers/LocalStrings.java
Expand Up @@ -59,6 +59,7 @@ public enum LocalStrings
leftAlt( "help.leftalt" ),
rightAlt( "help.rightalt" ),

Sort( "help.sort" ),
Trash( "help.trash" ),
TrashItem( "help.trashitem" ),
ReallyTrash( "help.reallytrash" ),
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/mod/chiselsandbits/network/ModPacketTypes.java
Expand Up @@ -11,6 +11,7 @@
import mod.chiselsandbits.network.packets.PacketRotateVoxelBlob;
import mod.chiselsandbits.network.packets.PacketSetChiselMode;
import mod.chiselsandbits.network.packets.PacketSetColor;
import mod.chiselsandbits.network.packets.PacketSortBagGui;
import mod.chiselsandbits.network.packets.PacketSuppressInteraction;
import mod.chiselsandbits.network.packets.PacketUndo;

Expand All @@ -26,7 +27,8 @@ public enum ModPacketTypes
CLEAR_BAG( PacketClearBagGui.class ),
SUPRESS_INTERACTION( PacketSuppressInteraction.class ),
SET_COLOR( PacketSetColor.class ),
ACCURATE_PLACEMENT( PacketAccurateSneakPlace.class );
ACCURATE_PLACEMENT( PacketAccurateSneakPlace.class ),
SORT_BAG_GUI( PacketSortBagGui.class );

private final Class<? extends ModPacket> packetClass;

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/chiselsandbits/lang/en_us.lang
Expand Up @@ -119,6 +119,7 @@ mod.chiselsandbits.help.rightshift=Right Shift
mod.chiselsandbits.help.leftalt=Left Alt
mod.chiselsandbits.help.rightalt=Right Alt
mod.chiselsandbits.help.trash=Delete Bag Contents, Hold Item to filter.
mod.chiselsandbits.help.sort=Condense and Combine
mod.chiselsandbits.help.trashitem=Delete only %s
mod.chiselsandbits.help.reallytrash=Click again to confirm deletion of all
mod.chiselsandbits.help.reallytrash_blank=Click again to confirm deletion of %s
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 128606a

Please sign in to comment.