Skip to content

Instantly share code, notes, and snippets.

@Lanse505
Last active January 10, 2021 21:48
Show Gist options
  • Save Lanse505/389ae67ed766d1380a2676b638c3e76c to your computer and use it in GitHub Desktop.
Save Lanse505/389ae67ed766d1380a2676b638c3e76c to your computer and use it in GitHub Desktop.
@Override
public ActionResultType onItemUseFirst (ItemStack stack, ItemUseContext context) {
World world = context.getWorld();
BlockPos pos = context.getPos();
BlockState state = world.getBlockState(pos);
PlayerEntity player = context.getPlayer();
TileEntity te = world.getTileEntity(pos);
BlockSerializationEnum config = EssenceGeneralConfig.getInstance().getSerializeBlock().get();
if (state.getBlock().isAir(state, world, pos) || !state.getFluidState().equals(Fluids.EMPTY.getDefaultState()) || player != null && !player.abilities.allowEdit && !stack.canPlaceOn(world.getTags(), new CachedBlockInfo(world, pos, false))) {
return ActionResultType.PASS;
}
if (player != null) {
if (mode == WrenchModeEnum.SERIALIZE && (state.getProperties().size() > 0 || state.hasTileEntity()) && ((((config == BlockSerializationEnum.BLACKLIST ? !state.isIn(EssenceBlockTags.FORGE_MOVEABLE_BLACKLIST) : state.isIn(EssenceBlockTags.FORGE_MOVEABLE_WHITELIST)) && !state.isIn(EssenceBlockTags.RELOCATION_NOT_SUPPORTED)) || (te != null && (!te.getType().isIn(EssenceTags.EssenceTileEntityTypeTags.IMMOVABLE) && !te.getType().isIn(EssenceTags.EssenceTileEntityTypeTags.RELOCATION_NOT_SUPPORTED)))) && !state.getPushReaction().equals(PushReaction.BLOCK))) {
ItemStack drop = new ItemStack(state.getBlock());
CompoundNBT stateNBT = new CompoundNBT();
state.getProperties().forEach(iProperty -> stateNBT.putString(iProperty.getName(), getStatePropertyValue(state, iProperty)));
// Serializes the BlockState
drop.setTagInfo("BlockStateTag", stateNBT);
// Serializes the TE
if (te != null) {
drop.setTagInfo("BlockEntityTag", te.serializeNBT());
}
// Adds the Stats
player.addStat(EssenceStats.INSTANCE.SERIALIZED);
player.addStat(Stats.ITEM_USED.get(this));
// Cleans up the World and Drops the Item
world.setBlockState(pos, Blocks.AIR.getDefaultState(), Constants.BlockFlags.DEFAULT_AND_RERENDER);
world.addEntity(Util.make(new ItemEntity(world, pos.getX(), pos.getY(), pos.getZ(), drop), ItemEntity::setDefaultPickupDelay));
stack.damageItem(1, player, playerEntity -> playerEntity.sendBreakAnimation(EquipmentSlotType.MAINHAND));
return ActionResultType.SUCCESS;
}
if (mode == WrenchModeEnum.ROTATE) {
if (EssenceInformationHelper.isSneakKeyDown()) {
state.rotate(world, pos, Rotation.CLOCKWISE_180);
}
state.rotate(world, pos, Rotation.CLOCKWISE_90);
player.addStat(Stats.ITEM_USED.get(this));
return ActionResultType.SUCCESS;
}
}
return ActionResultType.PASS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment