Skip to content

Commit

Permalink
Add keybinding for off grid placement, this defaults to unset, but us…
Browse files Browse the repository at this point in the history
…es sneak when unset. (#377)
  • Loading branch information
AlgorithmX2 committed Jun 8, 2018
1 parent cf3603e commit d91f5ce
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 73 deletions.
9 changes: 5 additions & 4 deletions src/main/java/mod/chiselsandbits/api/ModKeyBinding.java
Expand Up @@ -6,16 +6,17 @@
@SideOnly( Side.CLIENT )
public enum ModKeyBinding
{
//Misc
// Misc
ROTATE_CCW,
ROTATE_CW,
UNDO,
REDO,
MODE_MENU,
ADD_TO_CLIPBOARD,
PICK_BIT,
OFFGRID_PLACEMENT,

//Chisel Modes
// Chisel Modes
SINGLE,
SNAP2,
SNAP4,
Expand All @@ -30,13 +31,13 @@ public enum ModKeyBinding
DRAWN_REGION,
CONNECTED_MATERIAL,

//Positive Pattern Modes
// Positive Pattern Modes
REPLACE,
ADDITIVE,
PLACEMENT,
IMPOSE,

//Tape Measure Modes
// Tape Measure Modes
BIT,
BLOCK,
DISTANCE;
Expand Down
Expand Up @@ -69,7 +69,7 @@ public void addInformation(
super.addInformation( stack, worldIn, tooltip, advanced );
ChiselsAndBits.getConfig().helpText( LocalStrings.HelpChiseledBlock, tooltip,
ClientSide.instance.getKeyName( Minecraft.getMinecraft().gameSettings.keyBindUseItem ),
ClientSide.instance.getKeyName( Minecraft.getMinecraft().gameSettings.keyBindSneak ) );
ClientSide.instance.getKeyName( ClientSide.getOffGridPlacementKey() ) );

if ( stack.hasTagCompound() )
{
Expand Down Expand Up @@ -99,7 +99,7 @@ public boolean canPlaceBlockOnSide(
final EntityPlayer player,
final ItemStack stack )
{
return canPlaceBlockHere( worldIn, pos, side, player, stack );
return canPlaceBlockHere( worldIn, pos, side, player, stack, false );
}

public boolean vanillaStylePlacementTest(
Expand Down Expand Up @@ -128,14 +128,15 @@ public boolean canPlaceBlockHere(
final @Nonnull BlockPos pos,
final @Nonnull EnumFacing side,
final EntityPlayer player,
final ItemStack stack )
final ItemStack stack,
boolean offgrid )
{
if ( vanillaStylePlacementTest( worldIn, pos, side, player, stack ) )
{
return true;
}

if ( player.isSneaking() )
if ( offgrid )
{
return true;
}
Expand All @@ -150,46 +151,41 @@ public boolean canPlaceBlockHere(

@Override
public EnumActionResult onItemUse(
final EntityPlayer playerIn,
final World worldIn,
final EntityPlayer player,
final World world,
final BlockPos pos,
final EnumHand hand,
final EnumFacing side,
final float hitX,
final float hitY,
final float hitZ )
{
final ItemStack stack = playerIn.getHeldItem( hand );
final ItemStack stack = player.getHeldItem( hand );

if ( playerIn.isSneaking() )
if ( !world.isRemote )
{
if ( !worldIn.isRemote )
{
// Say it "worked", Don't do anything we'll get a better packet.
return EnumActionResult.SUCCESS;
}
else
{
// send accurate packet.
final PacketAccurateSneakPlace pasp = new PacketAccurateSneakPlace();

pasp.hand = hand;
pasp.pos = pos;
pasp.side = side;
pasp.stack = stack;
pasp.hitX = hitX;
pasp.hitY = hitY;
pasp.hitZ = hitZ;

NetworkRouter.instance.sendToServer( pasp );
}
// Say it "worked", Don't do anything we'll get a better packet.
return EnumActionResult.SUCCESS;
}

return doItemUse( stack, playerIn, worldIn, pos, hand, side, hitX, hitY, hitZ );
// send accurate packet.
final PacketAccurateSneakPlace pasp = new PacketAccurateSneakPlace();

pasp.hand = hand;
pasp.pos = pos;
pasp.side = side;
pasp.stack = stack;
pasp.offgrid = ClientSide.offGridPlacement( player );
pasp.hitX = hitX;
pasp.hitY = hitY;
pasp.hitZ = hitZ;

NetworkRouter.instance.sendToServer( pasp );
return placeItem( stack, player, world, pos, hand, side, hitX, hitY, hitZ, ClientSide.offGridPlacement( player ) );
}

@Override
public EnumActionResult doItemUse(
public EnumActionResult placeItem(
final ItemStack stack,
final EntityPlayer playerIn,
final World worldIn,
Expand All @@ -198,7 +194,8 @@ public EnumActionResult doItemUse(
EnumFacing side,
final float hitX,
final float hitY,
final float hitZ )
final float hitZ,
boolean offgrid )
{
final IBlockState state = worldIn.getBlockState( pos );
final Block block = state.getBlock();
Expand All @@ -221,7 +218,7 @@ public EnumActionResult doItemUse(
}
}

if ( !canMerge && !playerIn.isSneaking() && !block.isReplaceable( worldIn, pos ) )
if ( !canMerge && !offgrid && !block.isReplaceable( worldIn, pos ) )
{
pos = pos.offset( side );
}
Expand All @@ -239,12 +236,12 @@ else if ( pos.getY() == 255 && DeprecationHelper.getStateFromItem( stack ).getMa
{
return EnumActionResult.FAIL;
}
else if ( canPlaceBlockHere( worldIn, pos, side, playerIn, stack ) )
else if ( canPlaceBlockHere( worldIn, pos, side, playerIn, stack, offgrid ) )
{
final int i = this.getMetadata( stack.getMetadata() );
final IBlockState iblockstate1 = this.block.getStateForPlacement( worldIn, pos, side, hitX, hitY, hitZ, i, playerIn, hand );

if ( placeBlockAt( stack, playerIn, worldIn, pos, side, hitX, hitY, hitZ, iblockstate1 ) )
if ( placeBitBlock( stack, playerIn, worldIn, pos, side, hitX, hitY, hitZ, iblockstate1, offgrid ) )
{
worldIn.playSound( pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, DeprecationHelper.getSoundType( this.block ).getPlaceSound(), SoundCategory.BLOCKS,
( DeprecationHelper.getSoundType( this.block ).getVolume() + 1.0F ) / 2.0F,
Expand Down Expand Up @@ -272,7 +269,22 @@ public boolean placeBlockAt(
final float hitZ,
final IBlockState newState )
{
if ( player.isSneaking() )
return placeBitBlock( stack, player, world, pos, side, hitX, hitY, hitZ, newState, false );
}

public boolean placeBitBlock(
final ItemStack stack,
final EntityPlayer player,
final World world,
final BlockPos pos,
final EnumFacing side,
final float hitX,
final float hitY,
final float hitZ,
final IBlockState newState,
boolean offgrid )
{
if ( offgrid )
{
final BitLocation bl = new BitLocation( new RayTraceResult( RayTraceResult.Type.BLOCK, new Vec3d( hitX, hitY, hitZ ), side, pos ), false, BitOperation.PLACE );
return tryPlaceBlockAt( block, stack, player, world, bl.blockPos, side, EnumHand.MAIN_HAND, new BlockPos( bl.bitX, bl.bitY, bl.bitZ ), true );
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/mod/chiselsandbits/client/ModConflictContext.java
Expand Up @@ -9,6 +9,7 @@
import mod.chiselsandbits.helpers.ChiselToolType;
import mod.chiselsandbits.helpers.ModUtil;
import mod.chiselsandbits.interfaces.IVoxelBlobItem;
import mod.chiselsandbits.network.packets.PacketAccurateSneakPlace.IItemBlockAccurate;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
Expand All @@ -18,6 +19,36 @@

public enum ModConflictContext implements IKeyConflictContext
{
HOLDING_OFFGRID
{
@Override
public boolean isActive()
{
if ( super.isActive() )
{
return true;
}

try
{
final ItemStack held = getPlayer().getHeldItemMainhand();
return !ModUtil.isEmpty( held ) && held.getItem() instanceof IItemBlockAccurate;
}
catch ( final NoPlayerException e )
{
// just fail.
}

return false;
}

@Override
public boolean conflicts(
final IKeyConflictContext other )
{
return this == other || other == KeyConflictContext.IN_GAME;
}
},

HOLDING_ROTATEABLE
{
Expand Down
25 changes: 23 additions & 2 deletions src/main/java/mod/chiselsandbits/core/ClientSide.java
Expand Up @@ -147,6 +147,7 @@ public class ClientSide
private KeyBinding modeMenu;
private KeyBinding addToClipboard;
private KeyBinding pickBit;
private KeyBinding offgridPlacement;
private Stopwatch rotateTimer;

final public TapeMeasures tapeMeasures = new TapeMeasures();
Expand All @@ -168,6 +169,8 @@ public KeyBinding getKeyBinding(
return addToClipboard;
case PICK_BIT:
return pickBit;
case OFFGRID_PLACEMENT:
return ClientSide.getOffGridPlacementKey();
default:
return modeMenu;
}
Expand Down Expand Up @@ -209,6 +212,7 @@ public void init(
rotateCCW = registerKeybind( "mod.chiselsandbits.other.rotate.ccw", 0, "itemGroup.chiselsandbits", ModConflictContext.HOLDING_ROTATEABLE );
rotateCW = registerKeybind( "mod.chiselsandbits.other.rotate.cw", 0, "itemGroup.chiselsandbits", ModConflictContext.HOLDING_ROTATEABLE );
pickBit = registerKeybind( "mod.chiselsandbits.other.pickbit", 0, "itemGroup.chiselsandbits", ModConflictContext.HOLDING_ROTATEABLE );
offgridPlacement = registerKeybind( "mod.chiselsandbits.other.offgrid", 0, "itemGroup.chiselsandbits", ModConflictContext.HOLDING_OFFGRID );
undo = registerKeybind( "mod.chiselsandbits.other.undo", 0, "itemGroup.chiselsandbits", KeyConflictContext.IN_GAME );
redo = registerKeybind( "mod.chiselsandbits.other.redo", 0, "itemGroup.chiselsandbits", KeyConflictContext.IN_GAME );
addToClipboard = registerKeybind( "mod.chiselsandbits.other.add_to_clipboard", 0, "itemGroup.chiselsandbits", KeyConflictContext.IN_GAME );
Expand Down Expand Up @@ -1242,7 +1246,7 @@ private void doGhostForChiseledBlock(
{
final BlockPos offset = mop.getBlockPos();

if ( player.isSneaking() )
if ( ClientSide.offGridPlacement( player ) )
{
final BitLocation bl = new BitLocation( mop, true, BitOperation.PLACE );
showGhost( currentItem, item, bl.blockPos, player, rotations, x, y, z, mop.sideHit, new BlockPos( bl.bitX, bl.bitY, bl.bitZ ), null );
Expand All @@ -1263,7 +1267,7 @@ private void doGhostForChiseledBlock(

BlockPos newOffset = offset;
final Block block = theWorld.getBlockState( newOffset ).getBlock();
if ( !canMerge && !player.isSneaking() && !block.isReplaceable( theWorld, newOffset ) )
if ( !canMerge && !ClientSide.offGridPlacement( player ) && !block.isReplaceable( theWorld, newOffset ) )
{
newOffset = offset.offset( mop.sideHit );
}
Expand Down Expand Up @@ -1673,4 +1677,21 @@ private String makeMoreFrendly(
.replace( "RSHIFT", LocalStrings.rightShift.getLocal() );
}

public static boolean offGridPlacement(
EntityPlayer player )
{
if ( player.getEntityWorld().isRemote )
return getOffGridPlacementKey().isKeyDown();

throw new RuntimeException( "checking keybinds on server." );
}

public static KeyBinding getOffGridPlacementKey()
{
if ( ClientSide.instance.offgridPlacement.isSetToDefaultValue() )
return Minecraft.getMinecraft().gameSettings.keyBindSneak;

return ClientSide.instance.offgridPlacement;
}

}

0 comments on commit d91f5ce

Please sign in to comment.