下面列出了net.minecraft.util.DefaultedList#net.minecraft.block.DispenserBlock 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected ItemStack dispenseSilently(BlockPointer source, ItemStack stack) {
if(!CarpetExtraSettings.dispensersToggleThings) {
return super.dispenseSilently(source, stack);
}
World world = source.getWorld();
Direction direction = (Direction) source.getBlockState().get(DispenserBlock.FACING);
BlockPos pos = source.getBlockPos().offset(direction);
BlockState state = world.getBlockState(pos);
if(toggleable.contains(state.getBlock())) {
ActionResult result = state.onUse(
world,
null,
Hand.MAIN_HAND,
new BlockHitResult(
new Vec3d(new Vec3i(pos.getX(), pos.getY(), pos.getZ())),
direction,
pos,
false
)
);
if(result.isAccepted()) return stack; // success or consume
}
return super.dispenseSilently(source, stack);
}
@SuppressWarnings("UnresolvedMixinReference")
@Inject(method = "dispenseSilently(Lnet/minecraft/util/math/BlockPointer;Lnet/minecraft/item/ItemStack;)Lnet/minecraft/item/ItemStack;", at = @At("HEAD"), cancellable = true)
private void convertNetherrack(BlockPointer pointer, ItemStack stack, CallbackInfoReturnable<ItemStack> cir)
{
if (!CarpetExtraSettings.fireChargeConvertsToNetherrack)
return;
World world = pointer.getWorld();
Direction direction = pointer.getBlockState().get(DispenserBlock.FACING);
BlockPos front = pointer.getBlockPos().offset(direction);
BlockState state = world.getBlockState(front);
if (state.getBlock() == Blocks.COBBLESTONE)
{
world.setBlockState(front, Blocks.NETHERRACK.getDefaultState());
stack.decrement(1);
cir.setReturnValue(stack);
cir.cancel();
}
}
@Override
protected ItemStack dispenseSilently(BlockPointer source, ItemStack stack)
{
if (!CarpetExtraSettings.dispensersPlayRecords)
return super.dispenseSilently(source, stack);
Direction direction = source.getBlockState().get(DispenserBlock.FACING);
BlockPos pos = source.getBlockPos().offset(direction);
World world = source.getWorld();
BlockState state = world.getBlockState(pos);
if (state.getBlock() == Blocks.JUKEBOX)
{
JukeboxBlockEntity jukebox = (JukeboxBlockEntity) world.getBlockEntity(pos);
if (jukebox != null)
{
ItemStack itemStack = jukebox.getRecord();
((JukeboxBlock) state.getBlock()).setRecord(world, pos, state, stack);
world.playLevelEvent(null, 1010, pos, Item.getRawId(stack.getItem()));
return itemStack;
}
}
return super.dispenseSilently(source, stack);
}
private ItemStack defaultBehaviour(BlockPointer source, ItemStack stack)
{
if (this.minecartType == AbstractMinecartEntity.Type.TNT)
{
World world = source.getWorld();
BlockPos pos = source.getBlockPos().offset((Direction) source.getBlockState().get(DispenserBlock.FACING));
TntEntity tntEntity = new TntEntity(world, (double)pos.getX() + 0.5D, (double)pos.getY(), (double)pos.getZ() + 0.5D, (LivingEntity)null);
world.spawnEntity(tntEntity);
world.playSound((PlayerEntity)null, tntEntity.getX(), tntEntity.getY(), tntEntity.getZ(), SoundEvents.ENTITY_TNT_PRIMED, SoundCategory.BLOCKS, 1.0F, 1.0F);
stack.decrement(1);
return stack;
}
else
{
return super.dispenseSilently(source, stack);
}
}
@Override
protected ItemStack dispenseSilently(BlockPointer source, ItemStack stack)
{
if (!CarpetExtraSettings.dragonsBreathConvertsCobbleToEndstone)
return super.dispenseSilently(source, stack);
World world = source.getWorld();
Direction direction = source.getBlockState().get(DispenserBlock.FACING);
BlockPos front = source.getBlockPos().offset(direction);
BlockState state = world.getBlockState(front);
if (state.getBlock() == Blocks.COBBLESTONE)
{
world.setBlockState(front, Blocks.END_STONE.getDefaultState());
stack.decrement(1);
return stack;
}
return super.dispenseSilently(source, stack);
}
@Override
public boolean isValidInvStack(int int_1, ItemStack itemStack_1)
{
if (CarpetExtraSettings.autoCraftingDropper && world != null)
{
BlockState state = world.getBlockState(pos);
if (state.getBlock() == Blocks.DROPPER)
{
if (world.getBlockState(pos.offset(state.get(DispenserBlock.FACING))).getBlock() == Blocks.CRAFTING_TABLE)
{
return inventory.get(int_1).isEmpty();
}
}
}
return super.isValidInvStack(int_1, itemStack_1);
}
@Override
public int getComparatorOutput(BlockState blockState_1, World world_1, BlockPos blockPos_1)
{
if (CarpetExtraSettings.autoCraftingDropper)
{
BlockPos front = blockPos_1.offset(world_1.getBlockState(blockPos_1).get(DispenserBlock.FACING));
if (world_1.getBlockState(front).getBlock() == Blocks.CRAFTING_TABLE)
{
DispenserBlockEntity dispenserBlockEntity_1 = (DispenserBlockEntity) world_1.getBlockEntity(blockPos_1);
if (dispenserBlockEntity_1 != null)
{
int filled = 0;
for (ItemStack stack : ((DispenserBlockEntityInterface) dispenserBlockEntity_1).getInventory())
{
if (!stack.isEmpty()) filled++;
}
return (filled * 15) / 9;
}
}
}
return super.getComparatorOutput(blockState_1, world_1, blockPos_1);
}
public void drawShulkerToolTip(Slot slot, int mX, int mY) {
if (!(slot.getStack().getItem() instanceof BlockItem)) return;
if (!(((BlockItem) slot.getStack().getItem()).getBlock() instanceof ShulkerBoxBlock)
&& !(((BlockItem) slot.getStack().getItem()).getBlock() instanceof ChestBlock)
&& !(((BlockItem) slot.getStack().getItem()).getBlock() instanceof DispenserBlock)
&& !(((BlockItem) slot.getStack().getItem()).getBlock() instanceof HopperBlock)) return;
List<ItemStack> items = ItemContentUtils.getItemsInContainer(slot.getStack());
Block block = ((BlockItem) slot.getStack().getItem()).getBlock();
int count = block instanceof HopperBlock || block instanceof DispenserBlock ? 18 : 0;
if (block instanceof HopperBlock) renderTooltipBox(mX, mY - 21, 13, 82, true);
else if (block instanceof DispenserBlock) renderTooltipBox(mX, mY - 21, 13, 150, true);
else renderTooltipBox(mX, mY - 55, 47, 150, true);
for (ItemStack i: items) {
if (count > 26) break;
int x = mX + 10 + (17 * (count % 9));
int y = mY - 69 + (17 * (count / 9));
mc.getItemRenderer().renderGuiItem(i, x, y);
mc.getItemRenderer().renderGuiItemOverlay(mc.textRenderer, i, x, y, i.getCount() > 1 ? i.getCount() + "" : "");
count++;
}
}
public void drawShulkerToolTip(Slot slot, int mX, int mY) {
if (!(slot.getStack().getItem() instanceof BlockItem)) return;
if (!(((BlockItem) slot.getStack().getItem()).getBlock() instanceof ShulkerBoxBlock)
&& !(((BlockItem) slot.getStack().getItem()).getBlock() instanceof ChestBlock)
&& !(((BlockItem) slot.getStack().getItem()).getBlock() instanceof DispenserBlock)
&& !(((BlockItem) slot.getStack().getItem()).getBlock() instanceof HopperBlock)) return;
List<ItemStack> items = ItemContentUtils.getItemsInContainer(slot.getStack());
Block block = ((BlockItem) slot.getStack().getItem()).getBlock();
int count = block instanceof HopperBlock || block instanceof DispenserBlock ? 18 : 0;
if (block instanceof HopperBlock) renderTooltipBox(mX, mY - 21, 13, 82, true);
else if (block instanceof DispenserBlock) renderTooltipBox(mX, mY - 21, 13, 150, true);
else renderTooltipBox(mX, mY - 55, 47, 150, true);
for (ItemStack i: items) {
if (count > 26) break;
int x = mX + 10 + (17 * (count % 9));
int y = mY - 69 + (17 * (count / 9));
mc.getItemRenderer().renderGuiItemIcon(i, x, y);
mc.getItemRenderer().renderGuiItemOverlay(mc.textRenderer, i, x, y, i.getCount() > 1 ? i.getCount() + "" : "");
count++;
}
}
public void drawShulkerToolTip(Slot slot, int mX, int mY) {
if (!(slot.getStack().getItem() instanceof BlockItem)) return;
if (!(((BlockItem) slot.getStack().getItem()).getBlock() instanceof ShulkerBoxBlock)
&& !(((BlockItem) slot.getStack().getItem()).getBlock() instanceof ChestBlock)
&& !(((BlockItem) slot.getStack().getItem()).getBlock() instanceof DispenserBlock)
&& !(((BlockItem) slot.getStack().getItem()).getBlock() instanceof HopperBlock)) return;
List<ItemStack> items = ItemContentUtils.getItemsInContainer(slot.getStack());
Block block = ((BlockItem) slot.getStack().getItem()).getBlock();
int count = block instanceof HopperBlock || block instanceof DispenserBlock ? 18 : 0;
if (block instanceof HopperBlock) renderTooltipBox(mX, mY - 21, 13, 82, true);
else if (block instanceof DispenserBlock) renderTooltipBox(mX, mY - 21, 13, 150, true);
else renderTooltipBox(mX, mY - 55, 47, 150, true);
for (ItemStack i: items) {
if (count > 26) break;
int x = mX + 10 + (17 * (count % 9));
int y = mY - 69 + (17 * (count / 9));
mc.getItemRenderer().renderGuiItem(i, x, y);
mc.getItemRenderer().renderGuiItemOverlay(mc.textRenderer, i, x, y, i.getCount() > 1 ? i.getCount() + "" : "");
count++;
}
}
@Override
protected ItemStack dispenseSilently(BlockPointer source, ItemStack stack)
{
if (!CarpetExtraSettings.dispensersFillMinecarts)
{
return defaultBehaviour(source, stack);
}
else
{
BlockPos pos = source.getBlockPos().offset((Direction) source.getBlockState().get(DispenserBlock.FACING));
List<MinecartEntity> list = source.getWorld().<MinecartEntity>getEntities(MinecartEntity.class, new Box(pos), null);
if (list.isEmpty())
{
return defaultBehaviour(source, stack);
}
else
{
MinecartEntity minecart = list.get(0);
minecart.remove();
AbstractMinecartEntity minecartEntity = AbstractMinecartEntity.create(minecart.world, minecart.getX(), minecart.getY(), minecart.getZ(), this.minecartType);
minecartEntity.setVelocity(minecart.getVelocity());
minecartEntity.pitch = minecart.pitch;
minecartEntity.yaw = minecart.yaw;
minecart.world.spawnEntity(minecartEntity);
stack.decrement(1);
return stack;
}
}
}
@Override
protected ItemStack dispenseSilently(BlockPointer source, ItemStack stack) {
BlockPos pos = source.getBlockPos().offset((Direction) source.getBlockState().get(DispenserBlock.FACING));
List<AnimalEntity> list = source.getWorld().getEntities(AnimalEntity.class, new Box(pos),null);
boolean failure = false;
for(AnimalEntity mob : list) {
if(!mob.isBreedingItem(stack)) continue;
if(mob.getBreedingAge() != 0 || mob.isInLove()) {
failure = true;
continue;
}
stack.decrement(1);
mob.lovePlayer(null);
return stack;
}
if(failure) return stack;
// fix here for now - if problem shows up next time, will need to fix it one level above.
if(
CarpetExtraSettings.dispenserPlacesBlocks &&
stack.getItem() instanceof BlockItem &&
PlaceBlockDispenserBehavior.canPlace(((BlockItem) stack.getItem()).getBlock())
)
{
return PlaceBlockDispenserBehavior.getInstance().dispenseSilently(source, stack);
}
else
{
return super.dispenseSilently(source, stack);
}
}
@Override
protected ItemStack dispenseSilently(BlockPointer blockPointer_1, ItemStack itemStack_1)
{
if (!CarpetExtraSettings.dispensersTillSoil)
return super.dispenseSilently(blockPointer_1, itemStack_1);
World world = blockPointer_1.getWorld();
Direction direction = blockPointer_1.getBlockState().get(DispenserBlock.FACING);
BlockPos front = blockPointer_1.getBlockPos().offset(direction);
BlockPos down = blockPointer_1.getBlockPos().down().offset(direction);
BlockState frontState = world.getBlockState(front);
BlockState downState = world.getBlockState(down);
if (isFarmland(frontState) || isFarmland(downState))
return itemStack_1;
if (canDirectlyTurnToFarmland(frontState))
world.setBlockState(front, Blocks.FARMLAND.getDefaultState());
else if (canDirectlyTurnToFarmland(downState))
world.setBlockState(down, Blocks.FARMLAND.getDefaultState());
else if (frontState.getBlock() == Blocks.COARSE_DIRT)
world.setBlockState(front, Blocks.DIRT.getDefaultState());
else if (downState.getBlock() == Blocks.COARSE_DIRT)
world.setBlockState(down, Blocks.DIRT.getDefaultState());
if (itemStack_1.damage(1, world.random, null))
itemStack_1.setCount(0);
return itemStack_1;
}
@Inject(
method = "dispenseSilently(Lnet/minecraft/util/math/BlockPointer;Lnet/minecraft/item/ItemStack;)Lnet/minecraft/item/ItemStack;",
at = @At("HEAD"), cancellable = true
)
private void milkCow(BlockPointer pointer, ItemStack stack, CallbackInfoReturnable<ItemStack> cir)
{
if (!CarpetExtraSettings.dispensersMilkCows)
return;
World world = pointer.getWorld();
if (!world.isClient)
{
BlockPos pos = pointer.getBlockPos().offset(pointer.getBlockState().get(DispenserBlock.FACING));
List<CowEntity> cows = world.getEntities(CowEntity.class, new Box(pos), e -> e.isAlive() && !e.isBaby());
if (!cows.isEmpty())
{
stack.decrement(1);
if (stack.isEmpty())
{
cir.setReturnValue(new ItemStack(Items.MILK_BUCKET));
}
else
{
if (((DispenserBlockEntity)pointer.getBlockEntity()).addToFirstFreeSlot(new ItemStack(Items.MILK_BUCKET)) < 0)
{
this.dispense(pointer, new ItemStack(Items.MILK_BUCKET));
}
cir.setReturnValue(stack);
}
}
}
}
@Override
public void onCommand(String command, String[] args) throws Exception {
ItemStack item = mc.player.inventory.getMainHandStack();
if (!(item.getItem() instanceof BlockItem)) {
BleachLogger.errorMessage("Must be holding a containter to peek.");
return;
}
if (!(((BlockItem) item.getItem()).getBlock() instanceof ShulkerBoxBlock)
&& !(((BlockItem) item.getItem()).getBlock() instanceof ChestBlock)
&& !(((BlockItem) item.getItem()).getBlock() instanceof DispenserBlock)
&& !(((BlockItem) item.getItem()).getBlock() instanceof HopperBlock)) {
BleachLogger.errorMessage("Must be holding a containter to peek.");
return;
}
List<ItemStack> items = ItemContentUtils.getItemsInContainer(item);
BasicInventory inv = new BasicInventory(items.toArray(new ItemStack[27]));
BleachQueue.queue.add(() -> {
mc.openScreen(new ShulkerBoxScreen(
new ShulkerBoxContainer(420, mc.player.inventory, inv),
mc.player.inventory,
item.getName()));
});
}
@SubscribeEvent
public void onGuiDraw(GuiScreenEvent.DrawScreenEvent.Post event) {
if (!(event.getGui() instanceof ContainerScreen<?>)) return;
ContainerScreen<?> screen = (ContainerScreen<?>) event.getGui();
if (screen.getSlotUnderMouse() == null) return;
if (!(screen.getSlotUnderMouse().getStack().getItem() instanceof BlockItem)) return;
if (!(((BlockItem) screen.getSlotUnderMouse().getStack().getItem()).getBlock() instanceof ContainerBlock)) return;
NonNullList<ItemStack> items = NonNullList.withSize(27, new ItemStack(Items.AIR));
CompoundNBT nbt = screen.getSlotUnderMouse().getStack().getTag();
if (nbt != null && nbt.contains("BlockEntityTag")) {
CompoundNBT itemnbt = nbt.getCompound("BlockEntityTag");
if (itemnbt.contains("Items")) ItemStackHelper.loadAllItems(itemnbt, items);
}
GlStateManager.translatef(0.0F, 0.0F, 500.0F);
Block block = ((BlockItem) screen.getSlotUnderMouse().getStack().getItem()).getBlock();
int count = block instanceof HopperBlock || block instanceof DispenserBlock ? 18 : 0;
for (ItemStack i: items) {
if (count > 26) break;
int x = event.getMouseX() + 8 + (17 * (count % 9));
int y = event.getMouseY() - 68 + (17 * (count / 9));
if (i.getItem() != Items.AIR) {
Screen.fill(x, y, x+17, y+17, 0x90000000);
Screen.fill(x, y, x+17, y+1, 0xff000000); Screen.fill(x, y+1, x+1, y+17, 0xff000000);
Screen.fill(x+16, y+1, x+17, y+17, 0xff000000); Screen.fill(x+1, y+16, x+17, y+17, 0xff000000);
}
mc.getItemRenderer().renderItemAndEffectIntoGUI(i, x, y);
mc.getItemRenderer().renderItemOverlayIntoGUI(mc.fontRenderer, i, x, y, i.getCount() > 1 ? i.getCount() + "" : "");
count++;
}
}
@Override
public void onCommand(String command, String[] args) throws Exception {
ItemStack item = mc.player.inventory.getMainHandStack();
if (!(item.getItem() instanceof BlockItem)) {
BleachLogger.errorMessage("Must be holding a containter to peek.");
return;
}
if (!(((BlockItem) item.getItem()).getBlock() instanceof ShulkerBoxBlock)
&& !(((BlockItem) item.getItem()).getBlock() instanceof ChestBlock)
&& !(((BlockItem) item.getItem()).getBlock() instanceof DispenserBlock)
&& !(((BlockItem) item.getItem()).getBlock() instanceof HopperBlock)) {
BleachLogger.errorMessage("Must be holding a containter to peek.");
return;
}
List<ItemStack> items = ItemContentUtils.getItemsInContainer(item);
SimpleInventory inv = new SimpleInventory(items.toArray(new ItemStack[27]));
BleachQueue.queue.add(() -> {
mc.openScreen(new ShulkerBoxScreen(
new ShulkerBoxScreenHandler(420, mc.player.inventory, inv),
mc.player.inventory,
item.getName()));
});
}
@Override
public void onCommand(String command, String[] args) throws Exception {
ItemStack item = mc.player.inventory.getMainHandStack();
if (!(item.getItem() instanceof BlockItem)) {
BleachLogger.errorMessage("Must be holding a containter to peek.");
return;
}
if (!(((BlockItem) item.getItem()).getBlock() instanceof ShulkerBoxBlock)
&& !(((BlockItem) item.getItem()).getBlock() instanceof ChestBlock)
&& !(((BlockItem) item.getItem()).getBlock() instanceof DispenserBlock)
&& !(((BlockItem) item.getItem()).getBlock() instanceof HopperBlock)) {
BleachLogger.errorMessage("Must be holding a containter to peek.");
return;
}
List<ItemStack> items = ItemContentUtils.getItemsInContainer(item);
BasicInventory inv = new BasicInventory(items.toArray(new ItemStack[27]));
BleachQueue.queue.add(() -> {
mc.openScreen(new ShulkerBoxScreen(
new ShulkerBoxContainer(420, mc.player.inventory, inv),
mc.player.inventory,
item.getName()));
});
}
public HallowCharmItem(Settings settings) {
super(settings);
DispenserBlock.registerBehavior(this, TRINKET_DISPENSER_BEHAVIOR);
}
public PaperBagItem(Settings settings) {
super(settings);
DispenserBlock.registerBehavior(this, TRINKET_DISPENSER_BEHAVIOR);
}
public PumpkinRingItem(Settings settings) {
super(settings);
DispenserBlock.registerBehavior(this, TRINKET_DISPENSER_BEHAVIOR);
}
public SkirtCostumeItem(Settings settings) {
super(settings);
DispenserBlock.registerBehavior(this, TRINKET_DISPENSER_BEHAVIOR);
}
public static BlockState alternativeBlockPlacement(Block block, ItemPlacementContext context)//World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
{
//actual alternative block placement code
Direction facing;
Vec3d vec3d = context.getHitPos();
BlockPos pos = context.getBlockPos();
float hitX = (float) vec3d.x - pos.getX();
if (hitX<2) // vanilla
return null;
int code = (int)(hitX-2)/2;
//
// now it would be great if hitX was adjusted in context to original range from 0.0 to 1.0
// since its actually using it. Its private - maybe with Reflections?
//
PlayerEntity placer = context.getPlayer();
World world = context.getWorld();
if (block instanceof GlazedTerracottaBlock)
{
facing = Direction.byId(code);
if(facing == Direction.UP || facing == Direction.DOWN)
{
facing = placer.getHorizontalFacing().getOpposite();
}
return block.getDefaultState().with(HorizontalFacingBlock.FACING, facing);
}
else if (block instanceof ObserverBlock)
{
return block.getDefaultState()
.with(FacingBlock.FACING, Direction.byId(code))
.with(ObserverBlock.POWERED, true);
}
else if (block instanceof RepeaterBlock)
{
facing = Direction.byId(code % 16);
if(facing == Direction.UP || facing == Direction.DOWN)
{
facing = placer.getHorizontalFacing().getOpposite();
}
return block.getDefaultState()
.with(HorizontalFacingBlock.FACING, facing)
.with(RepeaterBlock.DELAY, MathHelper.clamp(code / 16, 1, 4))
.with(RepeaterBlock.LOCKED, Boolean.FALSE);
}
else if (block instanceof TrapdoorBlock)
{
return block.getDefaultState()
.with(TrapdoorBlock.FACING, Direction.byId(code % 16))
.with(TrapdoorBlock.OPEN, Boolean.FALSE)
.with(TrapdoorBlock.HALF, (code >= 16) ? BlockHalf.TOP : BlockHalf.BOTTOM)
.with(TrapdoorBlock.OPEN, world.isReceivingRedstonePower(pos));
}
else if (block instanceof ComparatorBlock)
{
facing = Direction.byId(code % 16);
if((facing == Direction.UP) || (facing == Direction.DOWN))
{
facing = placer.getHorizontalFacing().getOpposite();
}
ComparatorMode m = (hitX >= 16)?ComparatorMode.SUBTRACT: ComparatorMode.COMPARE;
return block.getDefaultState()
.with(HorizontalFacingBlock.FACING, facing)
.with(ComparatorBlock.POWERED, Boolean.FALSE)
.with(ComparatorBlock.MODE, m);
}
else if (block instanceof DispenserBlock)
{
return block.getDefaultState()
.with(DispenserBlock.FACING, Direction.byId(code))
.with(DispenserBlock.TRIGGERED, Boolean.FALSE);
}
else if (block instanceof PistonBlock)
{
return block.getDefaultState()
.with(FacingBlock.FACING, Direction.byId(code))
.with(PistonBlock.EXTENDED, Boolean.FALSE);
}
else if (block instanceof StairsBlock)
{
return block.getPlacementState(context)//worldIn, pos, facing, hitX, hitY, hitZ, meta, placer)
.with(StairsBlock.FACING, Direction.byId(code % 16))
.with(StairsBlock.HALF, ( hitX >= 16)?BlockHalf.TOP : BlockHalf.BOTTOM);
}
return null;
}
@Inject(method = "dispense", at = @At("HEAD"), cancellable = true)
private void tryCraft(World world_1, BlockPos blockPos_1, CallbackInfo ci)
{
if (!CarpetExtraSettings.autoCraftingDropper) return;
BlockPos front = blockPos_1.offset(world_1.getBlockState(blockPos_1).get(DispenserBlock.FACING));
if (world_1.getBlockState(front).getBlock() != Blocks.CRAFTING_TABLE) return;
DispenserBlockEntity dispenserBlockEntity_1 = (DispenserBlockEntity) world_1.getBlockEntity(blockPos_1);
if (dispenserBlockEntity_1 == null) return;
CraftingInventory craftingInventory = new CraftingInventory(new VoidContainer(), 3, 3);
for (int i=0; i < 9; i++) craftingInventory.setInvStack(i, dispenserBlockEntity_1.getInvStack(i));
CraftingRecipe recipe = world_1.getRecipeManager().getFirstMatch(RecipeType.CRAFTING, craftingInventory, world_1).orElse(null);
if (recipe == null) return;
// crafting it
Vec3d target = new Vec3d(front).add(0.5, 0.2, 0.5);
ItemStack result = recipe.craft(craftingInventory);
spawn(world_1, target.x, target.y, target.z, result);
// copied from CraftingResultSlot.onTakeItem()
DefaultedList<ItemStack> defaultedList_1 = world_1.getRecipeManager().getRemainingStacks(RecipeType.CRAFTING, craftingInventory, world_1);
for(int int_1 = 0; int_1 < defaultedList_1.size(); ++int_1) {
ItemStack itemStack_2 = dispenserBlockEntity_1.getInvStack(int_1);
ItemStack itemStack_3 = defaultedList_1.get(int_1);
if (!itemStack_2.isEmpty()) {
dispenserBlockEntity_1.takeInvStack(int_1, 1);
itemStack_2 = dispenserBlockEntity_1.getInvStack(int_1);
}
if (!itemStack_3.isEmpty()) {
if (itemStack_2.isEmpty()) {
dispenserBlockEntity_1.setInvStack(int_1, itemStack_3);
} else if (ItemStack.areItemsEqualIgnoreDamage(itemStack_2, itemStack_3) && ItemStack.areTagsEqual(itemStack_2, itemStack_3)) {
itemStack_3.increment(itemStack_2.getCount());
dispenserBlockEntity_1.setInvStack(int_1, itemStack_3);
} else {
spawn(world_1, target.x, target.y, target.z, itemStack_3);
}
}
}
Vec3d vec = new Vec3d(blockPos_1).add(0.5, 0.5, 0.5);
ServerWorld world = (ServerWorld) world_1;
world.playSound(null, blockPos_1, SoundEvents.ENTITY_VILLAGER_WORK_MASON, SoundCategory.BLOCKS, 0.2f, 2.0f);
ci.cancel();
}
public static BlockState alternativeBlockPlacement(Block block, ItemPlacementContext context)//World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
{
//actual alternative block placement code
//
if (true) throw new UnsupportedOperationException("Alternative Block Placement / client controlled / is not implemnted");
Direction facing;
Vec3d vec3d = context.getHitPos();
float hitX = (float) vec3d.x;
if (hitX<2) // vanilla
return null;
int code = (int)(hitX-2)/2;
//
// now it would be great if hitX was adjusted in context to original range from 0.0 to 1.0
// since its actually using it. Its private - maybe with Reflections?
//
PlayerEntity placer = context.getPlayer();
BlockPos pos = context.getBlockPos();
World world = context.getWorld();
if (block instanceof GlazedTerracottaBlock)
{
facing = Direction.byId(code);
if(facing == Direction.UP || facing == Direction.DOWN)
{
facing = placer.getHorizontalFacing().getOpposite();
}
return block.getDefaultState().with(HorizontalFacingBlock.FACING, facing);
}
else if (block instanceof ObserverBlock)
{
return block.getDefaultState()
.with(FacingBlock.FACING, Direction.byId(code))
.with(ObserverBlock.POWERED, true);
}
else if (block instanceof RepeaterBlock)
{
facing = Direction.byId(code % 16);
if(facing == Direction.UP || facing == Direction.DOWN)
{
facing = placer.getHorizontalFacing().getOpposite();
}
return block.getDefaultState()
.with(HorizontalFacingBlock.FACING, facing)
.with(RepeaterBlock.DELAY, MathHelper.clamp(code / 16, 1, 4))
.with(RepeaterBlock.LOCKED, Boolean.FALSE);
}
else if (block instanceof TrapdoorBlock)
{
return block.getDefaultState()
.with(TrapdoorBlock.FACING, Direction.byId(code % 16))
.with(TrapdoorBlock.OPEN, Boolean.FALSE)
.with(TrapdoorBlock.HALF, (code >= 16) ? BlockHalf.TOP : BlockHalf.BOTTOM)
.with(TrapdoorBlock.OPEN, world.isReceivingRedstonePower(pos));
}
else if (block instanceof ComparatorBlock)
{
facing = Direction.byId(code % 16);
if((facing == Direction.UP) || (facing == Direction.DOWN))
{
facing = placer.getHorizontalFacing().getOpposite();
}
ComparatorMode m = (hitX >= 16)?ComparatorMode.SUBTRACT: ComparatorMode.COMPARE;
return block.getDefaultState()
.with(HorizontalFacingBlock.FACING, facing)
.with(ComparatorBlock.POWERED, Boolean.FALSE)
.with(ComparatorBlock.MODE, m);
}
else if (block instanceof DispenserBlock)
{
return block.getDefaultState()
.with(DispenserBlock.FACING, Direction.byId(code))
.with(DispenserBlock.TRIGGERED, Boolean.FALSE);
}
else if (block instanceof PistonBlock)
{
return block.getDefaultState()
.with(FacingBlock.FACING, Direction.byId(code))
.with(PistonBlock.EXTENDED, Boolean.FALSE);
}
else if (block instanceof StairsBlock)
{
return block.getPlacementState(context)//worldIn, pos, facing, hitX, hitY, hitZ, meta, placer)
.with(StairsBlock.FACING, Direction.byId(code % 16))
.with(StairsBlock.HALF, ( hitX >= 16)?BlockHalf.TOP : BlockHalf.BOTTOM);
}
return null;
}
public static boolean flip_block(BlockState state, World world, PlayerEntity player, Hand hand, BlockHitResult hit)
{
Block block = state.getBlock();
BlockPos pos = hit.getBlockPos();
Vec3d hitVec = hit.getPos().subtract(pos.getX(), pos.getY(), pos.getZ());
Direction facing = hit.getSide();
BlockState newState = null;
if ( (block instanceof GlazedTerracottaBlock) || (block instanceof AbstractRedstoneGateBlock) || (block instanceof RailBlock) ||
(block instanceof TrapdoorBlock) || (block instanceof LeverBlock) || (block instanceof FenceGateBlock))
{
newState = state.rotate(BlockRotation.CLOCKWISE_90);
}
else if ((block instanceof ObserverBlock) || (block instanceof EndRodBlock))
{
newState = state.with(FacingBlock.FACING, (Direction) state.get(FacingBlock.FACING).getOpposite());
}
else if (block instanceof DispenserBlock)
{
newState = state.with(DispenserBlock.FACING, state.get(DispenserBlock.FACING).getOpposite());
}
else if (block instanceof PistonBlock)
{
if (!(state.get(PistonBlock.EXTENDED)))
newState = state.with(FacingBlock.FACING, state.get(FacingBlock.FACING).getOpposite());
}
else if (block instanceof SlabBlock)
{
if (((SlabBlock) block).hasSidedTransparency(state))
{
newState = state.with(SlabBlock.TYPE, state.get(SlabBlock.TYPE) == SlabType.TOP ? SlabType.BOTTOM : SlabType.TOP);
}
}
else if (block instanceof HopperBlock)
{
if ((Direction)state.get(HopperBlock.FACING) != Direction.DOWN)
{
newState = state.with(HopperBlock.FACING, state.get(HopperBlock.FACING).rotateYClockwise());
}
}
else if (block instanceof StairsBlock)
{
//LOG.error(String.format("hit with facing: %s, at side %.1fX, X %.1fY, Y %.1fZ",facing, hitX, hitY, hitZ));
if ((facing == Direction.UP && hitVec.y == 1.0f) || (facing == Direction.DOWN && hitVec.y == 0.0f))
{
newState = state.with(StairsBlock.HALF, state.get(StairsBlock.HALF) == BlockHalf.TOP ? BlockHalf.BOTTOM : BlockHalf.TOP );
}
else
{
boolean turn_right;
if (facing == Direction.NORTH)
{
turn_right = (hitVec.x <= 0.5);
}
else if (facing == Direction.SOUTH)
{
turn_right = !(hitVec.x <= 0.5);
}
else if (facing == Direction.EAST)
{
turn_right = (hitVec.z <= 0.5);
}
else if (facing == Direction.WEST)
{
turn_right = !(hitVec.z <= 0.5);
}
else
{
return false;
}
if (turn_right)
{
newState = state.rotate(BlockRotation.COUNTERCLOCKWISE_90);
}
else
{
newState = state.rotate(BlockRotation.CLOCKWISE_90);
}
}
}
else
{
return false;
}
if (newState != null)
{
world.setBlockState(pos, newState, 2 | 1024);
world.checkBlockRerender(pos, state, newState);
return true;
}
return false;
}